-
Notifications
You must be signed in to change notification settings - Fork 527
feat: add snprintf/sprintf hook #1210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
codesensei-tushar
wants to merge
4
commits into
mandiant:master
Choose a base branch
from
codesensei-tushar:feat/hook-snprintf
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2644a21
feat: add snprintf/sprintf hook
codesensei-tushar e6cef28
test: add unit tests for SnprintfHook
codesensei-tushar 7cb125b
fix: resolve isort lint errors in tests
codesensei-tushar 70375f6
fix: resolve black formatting and mypy type errors
codesensei-tushar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import pytest | ||
|
|
||
| from floss.api_hooks import MAX_STR_SIZE, SnprintfHook | ||
|
|
||
|
|
||
| class MockEmulator: | ||
| """Mock emulator for testing hooks without vivisect dependency.""" | ||
|
|
||
| def __init__(self): | ||
| self.memory = {} | ||
| self.return_value = None | ||
|
|
||
| def writeMemory(self, addr, data): | ||
| self.memory[addr] = data | ||
|
|
||
| def readMemory(self, addr, size): | ||
| return self.memory.get(addr, b"\x00" * size)[:size] | ||
|
|
||
|
|
||
| class TestSnprintfHook: | ||
| """Tests for SnprintfHook._prepare_args and __call__""" | ||
|
|
||
| @pytest.fixture | ||
| def hook(self): | ||
| return SnprintfHook() | ||
|
|
||
| @pytest.fixture | ||
| def emu(self): | ||
| return MockEmulator() | ||
|
|
||
| def test_prepare_args_integer(self, hook, emu): | ||
| """Test %d format specifier""" | ||
| fmt = "Value: %d" | ||
| argv = [0, 64, 0, 42] # buf, size, fmt, value | ||
| args = hook._prepare_args(emu, fmt, argv, arg_start_idx=3) | ||
| assert args == (42,) | ||
|
|
||
| def test_prepare_args_negative_integer(self, hook, emu): | ||
| """Test %d with negative value (unsigned to signed conversion)""" | ||
| fmt = "Value: %d" | ||
| # 0xFFFFFF85 = 4294967173 (unsigned representation of -123) | ||
| argv = [0, 64, 0, 0xFFFFFF85] | ||
| args = hook._prepare_args(emu, fmt, argv, arg_start_idx=3) | ||
| assert args == (-123,) | ||
|
|
||
| def test_prepare_args_hex(self, hook, emu): | ||
| """Test %x format specifier""" | ||
| fmt = "Hex: %x" | ||
| argv = [0, 64, 0, 255] | ||
| args = hook._prepare_args(emu, fmt, argv, arg_start_idx=3) | ||
| assert args == (255,) | ||
|
|
||
| def test_prepare_args_unsigned(self, hook, emu): | ||
| """Test %u format specifier with large unsigned value""" | ||
| fmt = "Unsigned: %u" | ||
| argv = [0, 64, 0, 0xFFFFFFFF] | ||
| args = hook._prepare_args(emu, fmt, argv, arg_start_idx=3) | ||
| assert args == (0xFFFFFFFF,) | ||
|
|
||
| def test_prepare_args_string(self, hook, emu): | ||
| """Test %s format specifier (reads from memory)""" | ||
| fmt = "Name: %s" | ||
| string_ptr = 0x1000 | ||
| emu.memory[string_ptr] = b"TestString\x00" | ||
|
|
||
| argv = [0, 64, 0, string_ptr] | ||
|
|
||
| with patch("floss.api_hooks.fu.readStringAtRva") as mock_read: | ||
| mock_read.return_value = b"TestString" | ||
| args = hook._prepare_args(emu, fmt, argv, arg_start_idx=3) | ||
| assert args == ("TestString",) | ||
|
|
||
| def test_prepare_args_null_string(self, hook, emu): | ||
| """Test %s with NULL pointer""" | ||
| fmt = "Name: %s" | ||
| argv = [0, 64, 0, 0] # NULL pointer | ||
| args = hook._prepare_args(emu, fmt, argv, arg_start_idx=3) | ||
| assert args == ("(null)",) | ||
|
|
||
| def test_prepare_args_multiple(self, hook, emu): | ||
| """Test multiple format specifiers""" | ||
| fmt = "User: %s, Port: %d" | ||
| string_ptr = 0x1000 | ||
|
|
||
| argv = [0, 64, 0, string_ptr, 8080] | ||
|
|
||
| with patch("floss.api_hooks.fu.readStringAtRva") as mock_read: | ||
| mock_read.return_value = b"admin" | ||
| args = hook._prepare_args(emu, fmt, argv, arg_start_idx=3) | ||
| assert args == ("admin", 8080) | ||
|
|
||
| def test_prepare_args_percent_escape(self, hook, emu): | ||
| """Test %% escape sequence (should not consume argument)""" | ||
| fmt = "100%% complete: %d" | ||
| argv = [0, 64, 0, 42] | ||
| args = hook._prepare_args(emu, fmt, argv, arg_start_idx=3) | ||
| assert args == (42,) | ||
|
|
||
| def test_prepare_args_width_modifier(self, hook, emu): | ||
| """Test format specifier with width modifier""" | ||
| fmt = "Padded: %08x" | ||
| argv = [0, 64, 0, 255] | ||
| args = hook._prepare_args(emu, fmt, argv, arg_start_idx=3) | ||
| assert args == (255,) | ||
|
|
||
|
|
||
| class TestSnprintfHookCall: | ||
| """Tests for the full __call__ method""" | ||
|
|
||
| @pytest.fixture | ||
| def hook(self): | ||
| return SnprintfHook() | ||
|
|
||
| @pytest.fixture | ||
| def emu(self): | ||
| return MockEmulator() | ||
|
|
||
| def test_snprintf_basic(self, hook, emu): | ||
| """Test snprintf call writes formatted string to memory""" | ||
| buf_ptr = 0x2000 | ||
| fmt_ptr = 0x1000 | ||
|
|
||
| api: tuple = (None, None, None, "snprintf", []) | ||
| argv = [buf_ptr, 64, fmt_ptr, 42] | ||
|
|
||
| with ( | ||
| patch("floss.api_hooks.fu.contains_funcname") as mock_contains, | ||
| patch("floss.api_hooks.fu.readStringAtRva") as mock_read, | ||
| patch("floss.api_hooks.fu.call_return") as mock_return, | ||
| ): | ||
|
|
||
| mock_contains.side_effect = lambda api, names: "snprintf" in names | ||
| mock_read.return_value = b"Value: %d" | ||
|
|
||
| result = hook(emu, api, argv) | ||
|
|
||
| assert result == True | ||
| assert buf_ptr in emu.memory | ||
| assert emu.memory[buf_ptr] == b"Value: 42\x00" | ||
|
|
||
| def test_sprintf_basic(self, hook, emu): | ||
| """Test sprintf call (no size parameter)""" | ||
| buf_ptr = 0x2000 | ||
| fmt_ptr = 0x1000 | ||
|
|
||
| api: tuple = (None, None, None, "sprintf", []) | ||
| argv = [buf_ptr, fmt_ptr, 1337] | ||
|
|
||
| with ( | ||
| patch("floss.api_hooks.fu.contains_funcname") as mock_contains, | ||
| patch("floss.api_hooks.fu.readStringAtRva") as mock_read, | ||
| patch("floss.api_hooks.fu.call_return") as mock_return, | ||
| ): | ||
|
|
||
| # Mock: first call checks if any of snprintf/sprintf | ||
| # Second call checks if "sprintf" is in names | ||
| # Third call checks if "snprintf" is in names (should return False for sprintf) | ||
| def contains_check(api, names): | ||
| # Check if "sprintf" is in the tuple (matches sprintf, snprintf,) | ||
| if "sprintf" in names: | ||
| return True | ||
| if "snprintf" in names: | ||
| return False # sprintf is NOT snprintf | ||
| return False | ||
|
|
||
| mock_contains.side_effect = contains_check | ||
| mock_read.return_value = b"Code: %d" | ||
|
|
||
| result = hook(emu, api, argv) | ||
|
|
||
| assert result == True | ||
| assert buf_ptr in emu.memory | ||
| assert emu.memory[buf_ptr] == b"Code: 1337\x00" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add some tests that demonstrate this routine works the way we think it does
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review! I'd like to clarify what kind of tests you're looking for:
Unit tests - pytest tests that mock the emulator and verify SnprintfHook._prepare_args() and .call() work correctly with various format specifiers.
Integration tests - Tests that run FLOSS on an actual binary (like the C test file I shared) and verify decoded strings are extracted.