-
Notifications
You must be signed in to change notification settings - Fork 744
feat: R2Qiling with refactored memory and de-flatten plugin #1244
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
chinggg
wants to merge
15
commits into
qilingframework:dev
Choose a base branch
from
chinggg:r2qiling
base: dev
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
15 commits
Select commit
Hold shift + click to select a range
0510096
feat(r2): CallStack and fuzzy backtrace hook
chinggg 287e5f6
feat(r2): interactive shell
chinggg 4f81f62
fix(r2): skip ill instruction in disassembler
chinggg c336675
test(mem): mmap2 syscall
chinggg d6b88b9
feat(r2): `oba` to load bininfo and update flags
chinggg e9c8631
feat(r2): new APIs enhancing fine-grained analysis
chinggg e310cb8
feat(r2): PoC of de-flatten plugin
chinggg b16e2c7
feat(r2): wrapper class R2Qiling and R2Mem
chinggg 5e33859
test(mem): remove assert_mem_equal, add option to use R2Qiling
chinggg 2de22d1
chore: add example source code for deflat
chinggg 7a1beb1
feat(r2): load symbols from file if possible
chinggg dd679e9
refactor(r2): add addr wrap and move wrap to utils
chinggg 580a758
refactor(r2): move R2Qiling and utils out of __init__.py
chinggg ab41d49
refactor(r2): improve shell and examples
chinggg a53790e
refactor(r2): assume compatibility with ql.mem
chinggg 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/usr/bin/env python3 | ||
# | ||
# Cross Platform and Multi Architecture Advanced Binary Emulation Framework | ||
# | ||
|
||
import sys | ||
|
||
sys.path.append('..') | ||
|
||
from qiling.const import QL_VERBOSE | ||
from qiling.extensions.r2 import R2Qiling as Qiling | ||
|
||
|
||
|
||
if __name__ == "__main__": | ||
# a program obfuscated by OLLVM control flow graph flatten, which should print 4 when argv[1] is 1 | ||
# see source code at examples/src/linux/fla_test.c | ||
ql = Qiling(['rootfs/x86_linux/bin/test_fla_argv', '1'], 'rootfs/x86_linux', verbose=QL_VERBOSE.DEFAULT) | ||
ctx = ql.save() | ||
r2 = ql.r2 | ||
# now we can use r2 parsed symbol name instead of address to get function | ||
fcn = r2.get_fcn('target_function') | ||
# de-flatten the target function, ql code will be patched | ||
r2.deflat(fcn) | ||
# run the de-flattened program, it should print 4 as expected | ||
ql.run() | ||
# get a r2-like interactive shell to reverse engineering target_function | ||
r2.shell('target_function') | ||
# run `pdf` in r2 shell to print disassembly of target_function | ||
# we should see many patched NOP instructions | ||
|
||
print('restore the original program') | ||
ql.restore(ctx) | ||
r2 = ql.r2 | ||
# the program is still obfuscated | ||
r2.shell('target_function') |
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,37 @@ | ||
/* Build Instructions: | ||
git clone [email protected]:heroims/obfuscator.git -b llvm-9.0 | ||
mkdir build-ollvm && cd build-ollvm | ||
cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_INCLUDE_TESTS=OFF -G Ninja ../obfuscator/ | ||
ninja | ||
./bin/clang -m32 -mllvm -fla fla_test.c -o test_fla_argv | ||
*/ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
unsigned int target_function(unsigned int n) | ||
{ | ||
unsigned int mod = n % 4; | ||
unsigned int result = 0; | ||
|
||
if (mod == 0) result = (n | 0xBAAAD0BF) * (2 ^ n); | ||
|
||
else if (mod == 1) result = (n & 0xBAAAD0BF) * (3 + n); | ||
|
||
else if (mod == 2) result = (n ^ 0xBAAAD0BF) * (4 | n); | ||
|
||
else result = (n + 0xBAAAD0BF) * (5 & n); | ||
|
||
return result; | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
int n; | ||
if (argc < 2) { | ||
n = 0; | ||
} else { | ||
n = atoi(argv[1]); | ||
} | ||
int val = target_function(n); | ||
printf("%d\n", val); | ||
return 0; | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
from .r2 import R2 | ||
from .r2q import R2Qiling |
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,75 @@ | ||
from dataclasses import dataclass | ||
from typing import Iterator, Optional | ||
|
||
|
||
@dataclass | ||
class CallStack: | ||
"""Linked Frames | ||
See https://github.com/angr/angr/blob/master/angr/state_plugins/callstack.py | ||
""" | ||
addr: int | ||
sp: int | ||
bp: int | ||
name: str = None # 'name + offset' | ||
next: Optional['CallStack'] = None | ||
|
||
def __iter__(self) -> Iterator['CallStack']: | ||
""" | ||
Iterate through the callstack, from top to bottom | ||
(most recent first). | ||
""" | ||
i = self | ||
while i is not None: | ||
yield i | ||
i = i.next | ||
|
||
def __getitem__(self, k): | ||
""" | ||
Returns the CallStack at index k, indexing from the top of the stack. | ||
""" | ||
orig_k = k | ||
for i in self: | ||
if k == 0: | ||
return i | ||
k -= 1 | ||
raise IndexError(orig_k) | ||
|
||
def __len__(self): | ||
""" | ||
Get how many frames there are in the current call stack. | ||
|
||
:return: Number of frames | ||
:rtype: int | ||
""" | ||
|
||
o = 0 | ||
for _ in self: | ||
o += 1 | ||
return o | ||
|
||
def __repr__(self): | ||
""" | ||
Get a string representation. | ||
|
||
:return: A printable representation of the CallStack object | ||
:rtype: str | ||
""" | ||
return "<CallStack (depth %d)>" % len(self) | ||
|
||
def __str__(self): | ||
return "Backtrace:\n" + "\n".join(f"Frame {i}: [{f.name}] {f.addr:#x} sp={f.sp:#x}, bp={f.bp:#x}" for i, f in enumerate(self)) | ||
|
||
def __eq__(self, other): | ||
if not isinstance(other, CallStack): | ||
return False | ||
|
||
if self.addr != other.addr or self.sp != other.sp or self.bp != other.bp: | ||
return False | ||
|
||
return self.next == other.next | ||
|
||
def __ne__(self, other): | ||
return not (self == other) | ||
|
||
def __hash__(self): | ||
return hash(tuple((c.addr, c.sp, c.bp) for c in self)) |
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.
Uh oh!
There was an error while loading. Please reload this page.