Skip to content

Add num_api_calls heuristic#1214

Open
kunalsz wants to merge 1 commit intomandiant:masterfrom
kunalsz:heuristic-num-api-calls
Open

Add num_api_calls heuristic#1214
kunalsz wants to merge 1 commit intomandiant:masterfrom
kunalsz:heuristic-num-api-calls

Conversation

@kunalsz
Copy link

@kunalsz kunalsz commented Feb 13, 2026

In reference to the discussion here #1212

What it is?

extract_num_api_calls is a function-level heuristic that counts how many API calls a function makes

  • Decoding/decryption routines usually do a lot of tight arithmetic / bitwise operations over buffers(like xor)
  • They often operate mostly on memory and registers and may call very few external/library APIs (sometimes none)
  • In contrast, “normal” higher level functions (UI, file I/O, networking) often make many API/library calls

Low API call count : more consistent with a decoding rpoutine
High API call count : less consistent with decoding

How is it implemented ?

  • count call instructions where vivisect resolved the target AND the target is not within the current function and not within the same binary’s code (it’s an import / external)
  • also count calls “to” imported symbols by checking for a name like kernel32.CreateFileW or similar

Test results

With the heuristic in place the score has improved(same num of strings found)
Tests were executed on tests/data/malware/stackstrings/a294620543334a721a2ae8eaaf9680a0786f4b9a216d75b55cfd28f39e9430ea.exe_

  • Without this heuristic:
"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
                }
            }

"runtime": {
            "decoded_strings": 3.8492,
            "find_features": 0.1299,
            "language_strings": 0,
            "stack_strings": 0.2986,
            "start_date": "2026-02-13T13:20:57.460886Z",
            "static_strings": 0.0004,
            "tight_strings": 0.2559,
            "total": 7.9169,
            "vivisect": 3.3805
        }

With this heuristic :

"functions": {
            "analyzed_decoded_strings": 10,
            "analyzed_stack_strings": 8,
            "analyzed_tight_strings": 2,
            "decoding_function_scores": {
                "4198400": {
                    "score": 0.6,
                    "xrefs_to": 0
                },
                "4198864": {
                    "score": 0.61,
                    "xrefs_to": 1
                },
                "4199600": {
                    "score": 0.788,
                    "xrefs_to": 1
                },
                "4199696": {
                    "score": 0.547,
                    "xrefs_to": 1
                },
                "4199728": {
                    "score": 0.953,
                    "xrefs_to": 25
                },
                "4200272": {
                    "score": 0.56,
                    "xrefs_to": 2
                },
                "4200976": {
                    "score": 0.56,
                    "xrefs_to": 2
                },
                "4203168": {
                    "score": 0.68,
                    "xrefs_to": 3
                },
                "4205184": {
                    "score": 0.762,
                    "xrefs_to": 6
                },
                "4205328": {
                    "score": 0.822,
                    "xrefs_to": 3
                }
            },
            "discovered": 10,
            "library": 0
        }
    }

"runtime": {
            "decoded_strings": 3.9446,
            "find_features": 0.0661,
            "language_strings": 0,
            "stack_strings": 0.3221,
            "start_date": "2026-02-13T13:36:02.831242Z",
            "static_strings": 0.0004,
            "tight_strings": 0.3008,
            "total": 8.1679,
            "vivisect": 3.5308
        }

@mr-tz Looking forward to your insights !

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 FLOSS tool's capability to detect string decoding functions by incorporating a novel heuristic based on the number of external API calls made by a function. This addition aims to refine the scoring of functions, leveraging the observation that decoding routines typically perform low-level operations with minimal external API interaction, thereby improving the accuracy of identifying such functions.

Highlights

  • New Heuristic: NumAPICalls: Introduced a new function-level heuristic, NumAPICalls, to improve the identification of string decoding routines by counting external API calls.
  • API Call Counting Logic: Implemented helper functions (_iter_call_targets, _looks_like_import_name, _is_external_api_target) to accurately identify and count external API calls, distinguishing them from internal function calls.
  • Scoring Mechanism: Defined a scoring mechanism for NumAPICalls where functions with fewer API calls receive a higher score, indicating a stronger likelihood of being a decoding routine.
  • Integration: Integrated the extract_num_api_calls function into the FUNCTION_HANDLERS list, enabling its automatic application during feature extraction.

🧠 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 NumAPICalls to the imported features.
    • Implemented helper functions _iter_call_targets, _looks_like_import_name, and _is_external_api_target to identify external API call targets.
    • Developed extract_num_api_calls to count these external API calls within a function.
    • Included extract_num_api_calls in the FUNCTION_HANDLERS tuple.
  • floss/features/features.py
    • Defined the NumAPICalls class, inheriting from Feature.
    • Assigned a LOW weight to the NumAPICalls feature.
    • Implemented the score method for NumAPICalls, providing a tiered scoring logic based on the count of API calls.
Activity
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, num_api_calls, to help identify string decoding functions based on the number of API calls they make. The implementation is sound, but I've identified a high-severity bug that would limit its effectiveness on non-x86 architectures, and a medium-severity issue related to error handling that could make debugging difficult. I have provided specific suggestions to address these points.

Comment on lines +391 to +395
# only proceed on call mnemonics when available
mnem = insn.mnem.lower()
if mnem and mnem != "call":
continue

Copy link
Contributor

Choose a reason for hiding this comment

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

high

The check for mnem == "call" is specific to x86/x64 architectures and will not work for other architectures like ARM, where call instructions are BL. This makes the heuristic less portable. The _iter_call_targets function already correctly identifies call-like instructions using the architecture-agnostic envi.BR_PROC flag, so this check is both redundant and buggy.

Comment on lines +332 to +333
except Exception:
return
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Swallowing exceptions with a broad except Exception: and a return or pass can hide bugs and makes debugging difficult. It is better to log the exception to provide visibility into potential issues during analysis. This pattern is also present in _is_external_api_target.

Suggested change
except Exception:
return
except Exception as e:
logger.warning("Failed to get branches for instruction at 0x%x: %s", insn.va, e)
return

@mr-tz
Copy link
Collaborator

mr-tz commented Feb 16, 2026

Thanks for the proposal. On closer look, and seeing the already noticeable slowdown, let's hold off with additional heuristics unless these really provide a benefit in detection more detection functions. What do you think?

@kunalsz
Copy link
Author

kunalsz commented Feb 16, 2026

Thanks for the feedback! I understand the concern and just wanted to highlight that the time difference added is quite small and more like applying a filter , it improves the confidence in detection without significantly altering existing logic. I still believe it adds value, but I'm happy to adapt based on your suggestions!

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.

2 participants