Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

msynth

Use case

Code deobfuscation framework to simplify Mixed Boolean-Arithmetic (MBA) expressions.

Explanation

Python framework built on top of Miasm.

Supported simplification methods:

Usage

$ git clone https://github.com/mrphrazer/msynth.git
$ cd msynth
$ python -m venv msynth-env
$ source msynth-env/bin/activate
$ pip install -r requirements.txt
$ pip install .
$ unzip -d database -q database/3_variables_constants_7_nodes.txt.zip

Example 0

The test binary is generated with obfuscator-llvm:

undefined8 main(int param_1,long param_2)

{
  uint uVar1;
  uint uVar2;
  long lVar3;
  
  if (param_1 < 2) {
    uVar1 = 7;
  }
  else {
    lVar3 = strtol(*(char **)(param_2 + 8),(char **)0x0,10);
    uVar1 = (uint)lVar3;
  }
  if (param_1 < 3) {
    uVar2 = 3;
  }
  else {
    lVar3 = strtol(*(char **)(param_2 + 0x10),(char **)0x0,10);
    uVar2 = (uint)lVar3;
  }
  printf("or=%d",((uVar2 ^ 0xffffffff) & 0xef27f53c | uVar2 & 0x10d80ac3) ^
                 ((uVar1 ^ 0xffffffff) & 0xef27f53c | uVar1 & 0x10d80ac3) |
                 (~uVar2 | uVar1 ^ 0xffffffff) ^ 0xffffffff);
  return 0;
}
from miasm.expression.expression import ExprId, ExprInt
from msynth import Synthesizer

uVar1 = ExprId("uVar1", 32)
uVar2 = ExprId("uVar2", 32)
k = lambda v: ExprInt(v, 32)

e = ((uVar2 ^ k(0xffffffff)) & k(0xef27f53c) | uVar2 & k(0x10d80ac3)) ^ \
    ((uVar1 ^ k(0xffffffff)) & k(0xef27f53c) | uVar1 & k(0x10d80ac3)) | \
    (~uVar2 | uVar1 ^ k(0xffffffff)) ^ k(0xffffffff)

s = Synthesizer()
left, right = e.args
print(s.simplify(s.simplify(left) | s.simplify(right)))
$ python simplify.py           
uVar1 | uVar2

Example 1

Simplify the following code:

...
  *(undefined8 *)(unaff_x19 + 0x840) = 0;
...
  (*(code *)((*(ulong *)(unaff_x19 + 0x840) ^ 0x32a6ad) +
            (*(ulong *)(unaff_x19 + 0x840) & 0x32a6ad) * 2))();
...
# https://github.com/cea-sec/miasm/blob/master/doc/expression/expression.ipynb

"""
...
  *(undefined8 *)(unaff_x19 + 0x840) = 0;
...
  (*(code *)((*(ulong *)(unaff_x19 + 0x840) ^ 0x32a6ad) +
            (*(ulong *)(unaff_x19 + 0x840) & 0x32a6ad) * 2))();
...
"""

"""
  (x ^ 0x32a6ad) + (x & 0x32a6ad) * 2
"""

from miasm.expression.expression import ExprId, ExprInt
from msynth import Synthesizer

k = lambda v: ExprInt(v, 64)

x = ExprId("x", 64)
e = (x ^ k(0x32a6ad)) + (x & k(0x32a6ad)) * k(2)
e.replace_expr({x: k(0)})
s = Synthesizer()

print(s.simplify(e))
$ python simplify2.py
x + 0x32A6AD

Example 2

# https://github.com/mrphrazer/msynth/blob/dfd0f9e2cd0890658fe752aa207a2cfda2aef8d7/tests/synthesis/test_synthesizer_more.py#L84

from miasm.expression.expression import ExprId, ExprInt
from msynth import Synthesizer
from msynth.synthesis.oracle import SynthesisOracle
from msynth.synthesis.grammar import Grammar
from msynth.synthesis.mutations import Mutator

def obf(i0):
    i1 = 1
    while i1 != 0:
        i2 = i0 ^ i1
        i1 = ((i0 & i1) * 2)
        i0 = i2
    return i0

xs = [0, 1, 2, 3]

samples = {}
for x in xs:
    inputs = (ExprInt(x, 32),) # tuple
    output = ExprInt(obf(x), 32)
    samples[inputs] = output

oracle = SynthesisOracle(samples)

x = ExprId("x", 32)
grammar = Grammar(32, [x])
mutator = Mutator(grammar)

synthesizer = Synthesizer()
state, _ = synthesizer.iterated_local_search(mutator, oracle)

print(state.get_expr_simplified())
$ python simplify-loop.py
x + 0x1

References:

  • https://github.com/mrphrazer/msynth/blob/main/scripts/synthesize_expression.py