-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
64 lines (51 loc) · 2.52 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
try:
from binaryninja.log import log_info, log_error
from binaryninja.binaryview import BinaryViewType
from binaryninja.plugin import PluginCommand
from binaryninja.plugin import BackgroundTaskThread
from binaryninja.enums import MediumLevelILOperation
from mbased.parser.lex import Lexer
from mbased.parser.parse import Parser
from mbased.parser.ast import Expr
from mbased.solver import Solver
from mbased.utils.coding import DictionaryEncoder, DictionaryDecoder
class MBADeobfuscationInBackground(BackgroundTaskThread):
"""Assigns a thread to MBA deobfuscation"""
def __init__(self, bv: BinaryViewType, msg: str):
"""Initiates the MBADeobfuscationInBackground object and defines bv attribute"""
BackgroundTaskThread.__init__(self, msg, True)
self.bv = bv
def run(self):
"""Logs all program if statements to BinaryNinja log"""
for instr in self.bv.mlil_instructions:
if instr.operation == MediumLevelILOperation.MLIL_IF:
try:
encoder: DictionaryEncoder = DictionaryEncoder()
encoded_instr: str = encoder.encode(str(instr))
l: Lexer = Lexer()
l.lex(encoded_instr)
p: Parser = Parser()
ast: Expr = p.parse(l.getTokens())
passes: list[str] = ["sympy_pass"]
s: Solver = Solver(passes)
simplified_ast: Expr = s.run(ast)
decoded_instr: str = DictionaryDecoder(
encoder.get_encoded_dictionary()
).decode(str(simplified_ast))
log_info(f"{hex(instr.address)}: {decoded_instr}", "MBASED")
except Exception as e:
log_error(f"{hex(instr.address)}: {e}", "MBASED")
def mba_deobfuscation_in_background(bv: BinaryViewType):
"""Creates a background task and starts MBA deobfuscation"""
background_task: MBADeobfuscationInBackground = MBADeobfuscationInBackground(
bv, "Starting MBASED..."
)
background_task.start()
PluginCommand.register(
"MBASED: Simplify all MBA expressions.",
"Simplifying booleans...",
mba_deobfuscation_in_background,
)
except:
import sys
print("Not in Binary Ninja.", file=sys.stderr)