Skip to content

Add Complex Functions heuristic#1212

Open
kunalsz wants to merge 2 commits intomandiant:masterfrom
kunalsz:heuristic-complex-functions
Open

Add Complex Functions heuristic#1212
kunalsz wants to merge 2 commits intomandiant:masterfrom
kunalsz:heuristic-complex-functions

Conversation

@kunalsz
Copy link

@kunalsz kunalsz commented Feb 7, 2026

Added complex functions heuristic from mrphrazer/obfuscation_detection

Complex Function Heuristic

The code uses this formula for Cyclomatic Complexity M = E - N + 2P. Where

  • E = number of edges (num_edges)
  • N = number of nodes (num_nodes)
  • P = number of connected components (here assumed to be 1 for a single function)

Fixes #696

Signed-off-by: kunalsz <kunalavengers@gmail.com>
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @kunalsz, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the feature extraction capabilities by introducing a new heuristic to identify and quantify the complexity of functions using Cyclomatic Complexity. This addition provides a standardized metric for function complexity, which can be leveraged for more nuanced code analysis and detection tasks.

Highlights

  • New Heuristic: Complex Functions: Introduced a new heuristic named 'ComplexFunctions' to measure the cyclomatic complexity of functions within the codebase.
  • Cyclomatic Complexity Calculation: Implemented a function extract_function_complex_functions that calculates the Cyclomatic Complexity (M = E - N + 2P) for a given function, where E is the number of edges, N is the number of nodes (basic blocks), and P is the number of connected components (assumed to be 1).
  • Integration into Feature Extraction: The newly created extract_function_complex_functions has been integrated into the FUNCTION_HANDLERS tuple, ensuring it is part of the standard feature extraction pipeline.
  • Complexity-Based Scoring: A scoring mechanism has been added to the ComplexFunctions class, assigning a weighted score from 0.0 to 1.0 based on predefined ranges of the calculated cyclomatic complexity, allowing for quantitative assessment of function complexity.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • floss/features/extract.py
    • Added ComplexFunctions to the import list from floss.features.features.
    • Implemented the extract_function_complex_functions function to compute cyclomatic complexity for a given function's control flow graph.
    • Registered extract_function_complex_functions in the FUNCTION_HANDLERS tuple to include it in the feature extraction process.
  • floss/features/features.py
    • Defined a new class ComplexFunctions that inherits from Feature.
    • Implemented an __init__ method for ComplexFunctions to store the calculated complexity.
    • Added a score method within ComplexFunctions to return a weighted score based on the function's cyclomatic complexity, categorizing functions from simple to extremely complex.
Activity
  • No specific activity (comments, reviews, etc.) has been recorded for this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new heuristic for identifying complex functions based on their cyclomatic complexity, which is a valuable addition. The implementation is sound, but I've identified a couple of areas for improvement. There's a bug in the new ComplexFunctions feature class where it's not initialized correctly, which would affect logging and string representation. Additionally, the error handling in the complexity calculation function could be improved by logging exceptions instead of silently ignoring them. My review includes specific suggestions to address these points.

Copy link
Collaborator

@mr-tz mr-tz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a few first comments below

Signed-off-by: kunalsz <kunalavengers@gmail.com>
@kunalsz kunalsz requested a review from mr-tz February 8, 2026 07:39
@mr-tz
Copy link
Collaborator

mr-tz commented Feb 9, 2026

Can you gather some stats on the runtime of this calculation? Do you think it would help to identify many new functions vs. the existing heuristics considering it's potential slow down of the overall program runtime?

@kunalsz
Copy link
Author

kunalsz commented Feb 10, 2026

@mr-tz Can you recommend some complex binaries against which I can test these heuristics and get a good comparison?

@mr-tz
Copy link
Collaborator

mr-tz commented Feb 10, 2026

You can initially try with some of the go/rust samples in https://github.com/mandiant/flare-floss-testfiles/tree/master/language or check out mimikatz or kernel32 or others in https://github.com/mandiant/capa-testfiles

@kunalsz
Copy link
Author

kunalsz commented Feb 10, 2026

@mr-tz I ran floss with and without this new heuristic on tests/data/malware/stackstrings/a294620543334a721a2ae8eaaf9680a0786f4b9a216d75b55cfd28f39e9430ea.exe_ and here are the results

Without extract_function_complex_functions:

image
"functions": {
    "analyzed_decoded_strings": 10,
    "analyzed_stack_strings": 8,
    "analyzed_tight_strings": 2,
    "decoding_function_scores": {
        "4198400": {
            "score": 0.543,
            "xrefs_to": 0
        },
        "4198864": {
            "score": 0.554,
            "xrefs_to": 1
        },
        "4199600": {
            "score": 0.764,
            "xrefs_to": 1
        },
        "4199696": {
            "score": 0.456,
            "xrefs_to": 1
        },
        "4199728": {
            "score": 0.95,
            "xrefs_to": 25
        },
        "4200272": {
            "score": 0.472,
            "xrefs_to": 2
        },
        "4200976": {
            "score": 0.472,
            "xrefs_to": 2
        },
        "4203168": {
            "score": 0.634,
            "xrefs_to": 3
        },
        "4205184": {
            "score": 0.751,
            "xrefs_to": 6
        },
        "4205328": {
            "score": 0.804,
            "xrefs_to": 3
        }
    },
    "discovered": 10,
    "library": 0
}

With extract_function_complex_functions:

image
"functions": {
    "analyzed_decoded_strings": 10,
    "analyzed_stack_strings": 8,
    "analyzed_tight_strings": 2,
    "decoding_function_scores": {
        "4198400": {
            "score": 0.467,
            "xrefs_to": 0
        },
        "4198864": {
            "score": 0.476,
            "xrefs_to": 1
        },
        "4199600": {
            "score": 0.662,
            "xrefs_to": 1
        },
        "4199696": {
            "score": 0.326,
            "xrefs_to": 1
        },
        "4199728": {
            "score": 0.889,
            "xrefs_to": 25
        },
        "4200272": {
            "score": 0.394,
            "xrefs_to": 2
        },
        "4200976": {
            "score": 0.451,
            "xrefs_to": 2
        },
        "4203168": {
            "score": 0.582,
            "xrefs_to": 3
        },
        "4205184": {
            "score": 0.707,
            "xrefs_to": 6
        },
        "4205328": {
            "score": 0.703,
            "xrefs_to": 3
        }
    },
    "discovered": 10,
    "library": 0
 }

The number of functions discovered remains the same but the score drops(maybe because of incorrect scoring implementation). I tried this again with a new heuristic extract_function_flattening_score based on Flattened Functions
but the results were same like this one.
Does this mean that the existing heuristics are good enough, no need of new ones ?
I'm open for discussing other heuristics which you recommend.

@kunalsz
Copy link
Author

kunalsz commented Feb 10, 2026

Also FLOSS’s heuristics are not “find obfuscation.” They are “find likely string-decoding routines" , in the /mrphrazer/obfuscation_detection repo it is written that for "string decryption routines" these techniques are used :

  • Most Called Functions
  • XOR Decryption Loops
  • RC4

Out of which most are already implemented

@mr-tz
Copy link
Collaborator

mr-tz commented Feb 11, 2026

Yes, good points so maybe we table this idea and document what we learned in the issue.

@kunalsz
Copy link
Author

kunalsz commented Feb 12, 2026

@mr-tz Are there any additional string deobfuscation routines or heuristics you’ve considered that aren’t yet implemented in FLOSS? I’d be happy to work on implementing them

@mr-tz
Copy link
Collaborator

mr-tz commented Feb 12, 2026

immediately to mind come only the ones listed in the code comments:

# extract_function_order,  # TODO decoding functions are often one of the first in a program
# extract_num_api_calls,  # TODO decoding functions don't normally contain many (API) calls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Import Detection Heuristics from mrphrazer/obfuscation_detection Repository

2 participants