def apply_mba_on_operands(expr, depth=0, max_depth=2):
"""
Applies the MBA obfuscation on the operands of the expression recursively up to a specified depth.
:param expr: The expression on which to apply the MBA obfuscation.
:param depth: The current depth of the recursion.
:param max_depth: The maximum depth for the recursion.
:return: The obfuscated expression.
"""
if depth >= max_depth:
return expr
# Apply MBA obfuscation on the left operand and recurse if necessary.
if isinstance(expr.left, ast.BinOp):
expr.left = apply_mba_on_operands(expr.left, depth + 1, max_depth)
elif isinstance(expr.left, ast.Constant) or isinstance(expr.left, ast.Name):
expr.left = generate_linear_mba(expr.left)
# Apply MBA obfuscation on the right operand and recurse if necessary.
if isinstance(expr.right, ast.BinOp):
expr.right = apply_mba_on_operands(expr.right, depth + 1, max_depth)
elif isinstance(expr.right, ast.Constant) or isinstance(expr.right, ast.Name):
expr.right = generate_linear_mba(expr.right)
return expr
def custom_mba_function1(x, y):
return (x ^ y) + (~(x | y))
def custom_mba_function2(x, y):
return (x & y) | (~(x & ~y))
def custom_mba_function3(x, y):
return (~x & y) ^ (x | ~y)
def custom_mba_function4(x, y):
return (~(x & y)) ^ (x | y)
def custom_mba_function5(x, y):
return (x & ~y) + (~x & y)
def custom_mba_function6(x, y):
return (x ^ ~y) | (~x ^ y)
def custom_mba_function7(x, y):
return (x ^ y) & (~(x & y))
def custom_mba_function8(x, y):
return (x | y) + (~x & ~y)
def custom_mba_function9(x, y):
return (~x | ~y) ^ (x & y)
def custom_mba_function10(x, y):
return (x & y) + (x ^ ~y)
def custom_mba_function11(x, y):
return ~(x | y) & (x ^ y)
def custom_mba_function12(x, y):
return (x & ~y) ^ (~x & y)
def custom_mba_function13(x, y):
return (~(x ^ y)) | (x & y)
def custom_mba_function14(x, y):
return (x & ~y) + (~x | y)
def custom_mba_function15(x, y):
return (~(x & y)) & (x | ~y)
def custom_mba_function16(x, y):
return (x ^ y) | (~x & ~y)
func_list = [
# ... (previous functions)
custom_mba_function1,
custom_mba_function2,
custom_mba_function3,
custom_mba_function4,
custom_mba_function5,
custom_mba_function6
custom_mba_function7,
custom_mba_function8,
custom_mba_function9,
custom_mba_function10,
custom_mba_function11,
custom_mba_function12,
custom_mba_function13,
custom_mba_function14,
custom_mba_function15,
custom_mba_function16
]
truth_table = np.array([
# ... (previous rows)
[0, 1, 1, 1], # custom_mba_function1
[1, 1, 0, 1], # custom_mba_function2
[1, 0, 0, 1], # custom_mba_function3
[1, 1, 1, 0], # custom_mba_function4
[0, 1, 1, 0], # custom_mba_function5
[1, 0, 1, 0], # custom_mba_function6
[0, 1, 1, 0], # custom_mba_function7
[1, 1, 0, 0], # custom_mba_function8
[1, 0, 0, 0], # custom_mba_function9
[0, 0, 0, 0], # custom_mba_function10
[1, 1, 1, 1], # custom_mba_function11
[1, 0, 1, 1], # custom_mba_function12
[0, 1, 0, 0], # custom_mba_function13
[1, 1, 0, 1], # custom_mba_function14
[1, 0, 1, 0], # custom_mba_function15
[0, 0, 1, 1], # custom_mba_function16
])
@generate_linear_mba.register
def _(expr: ast.BinOp):
"""
Generates linear MBA for BinOp nodes:
class ast.Add
class ast.Sub
class ast.Mult
class ast.Div
class ast.FloorDiv
class ast.Mod
class ast.Pow
class ast.LShift
class ast.RShift
class ast.BitOr
class ast.BitXor
class ast.BitAnd
class ast.MatMult
"""
final_expr = []
primitive_expr = None
terms_to_edit = None
if isinstance(expr.op, ast.Add):
terms_to_edit = [(2, 1), (4, 1)]
elif isinstance(expr.op, ast.Sub):
terms_to_edit = [(2, 1), (4, -1)]
elif isinstance(expr.op, ast.BitXor):
terms_to_edit = [(5, 1)]
elif isinstance(expr.op, ast.BitAnd):
terms_to_edit = [(0, 1)]
elif isinstance(expr.op, ast.BitOr):
terms_to_edit = [(6, 1)]
if (
isinstance(expr.left, ast.Name)
and isinstance(expr.right, ast.Name)
):
terms = generate_terms(NUM_TERMS + 10) # Update the number of terms to reflect added custom functions
if terms_to_edit is None:
return expr
for idx, val in terms_to_edit:
terms[idx] += val
x = expr.left.id
y = expr.right.id
for i in range(25): # Update the range to reflect added custom functions
primitive_expr = func_list[i](x=x, y=y)
if terms[i] != 0:
final_expr.append(
f"({terms[i]})*({ast.unparse(primitive_expr)})"
)
final_expr = '+'.join(final_expr)
return ast.parse(final_expr).body[0].value
else:
return expr