Update switch finding code to find the correct one

This commit is contained in:
Flawed
2024-11-12 00:42:56 -08:00
parent ce8e2f5c4c
commit 4039ca24a0
3 changed files with 18 additions and 7 deletions
+3 -2
View File
@@ -4,7 +4,7 @@ import pathlib
from minor_patch_diff import (
get_opcode_offset,
get_longest_switch,
get_correct_switch,
get_zone_proto_down_sig,
)
@@ -163,7 +163,8 @@ def extract_opcode_data(exe_file):
fn_graph = r2.cmdj(f"pdrj")
## STEP 4: Process data
packet_handler_switch = get_longest_switch(switch_cases)
switch_ea, packet_handler_switch = get_correct_switch(packet_handler_ea, switch_cases)
eprint(f" Found switch at {switch_ea}")
opcodes_db, blocks = generate_opcodes_db(
r2, packet_handler_switch, opcode_offset, fn_graph
+10 -3
View File
@@ -42,7 +42,7 @@ def get_opcode_offset(r2):
return opcode_offset
def get_longest_switch(switch_cases):
def get_correct_switch(approx_ea, switch_cases):
switches = dict()
pattern = re.compile("case\.(0x[0-9a-fA-F]+)\.(\d+)")
@@ -61,12 +61,17 @@ def get_longest_switch(switch_cases):
}
switches[switch_ea][case_ea]["opcodes"].append(match[2])
found_switch = None
longest_switch = dict()
for switch_ea in switches:
int_switch_ea = int(switch_ea, 16)
if int_switch_ea < approx_ea or int_switch_ea > approx_ea+0x100:
continue
if len(switches[switch_ea].keys()) > len(longest_switch):
found_switch = switch_ea
longest_switch = switches[switch_ea]
return longest_switch
return found_switch, longest_switch
def get_block_sizes(blocks):
@@ -126,7 +131,9 @@ def extract_opcode_data(exe_file):
eprint(f" Grabbed blocks from packet handler")
## STEP 4: Process data
packet_handler_switch = get_longest_switch(switch_cases)
switch_ea, packet_handler_switch = get_correct_switch(packet_handler_ea, switch_cases)
eprint(f" Found switch at {switch_ea}")
block_sizes = get_block_sizes(blocks)
opcodes_db = generate_opcodes_db(
packet_handler_ea, packet_handler_switch, opcode_offset, block_sizes
+5 -2
View File
@@ -1,7 +1,7 @@
import click
import json
from minor_patch_diff import get_longest_switch
from minor_patch_diff import get_correct_switch
from utils import eprint, create_r2_byte_pattern, sync_r2_output
import r2pipe
@@ -35,6 +35,7 @@ def extract_opcode_data(exe_file):
p = create_r2_byte_pattern(ON_RECEIVE_PACKET_SIG)
target = r2.cmd(f"/x {p}").split()[0] # Find byte pattern
packet_handler_ea = int(target, 16)
r2.cmd(f"s {target}") # Seek to target
@@ -56,7 +57,9 @@ def extract_opcode_data(exe_file):
## STEP 4: Process data
opcodes_db = dict()
packet_handler_switch = get_longest_switch(switch_cases)
switch_ea, packet_handler_switch = get_correct_switch(packet_handler_ea, switch_cases)
eprint(f" Found switch at {switch_ea}")
vtable_offset = 0x10
for data in packet_handler_switch.values():
if len(data["opcodes"]) > 10: