diff --git "a/prep_decompiled.py" "b/prep_decompiled.py" --- "a/prep_decompiled.py" +++ "b/prep_decompiled.py" @@ -1,2031 +1,207 @@ +import argparse +import os +from utils import * +from typing import Dict, List +from tqdm import tqdm +import re +from error import ParseError +HEADER = '#include "/home/ReSym/clang-parser/defs.hh"\n' +def process_funname(raw_addr:str) -> str: + # sub_401220 -> 401220 + if raw_addr == 'main': + return raw_addr + match = re.search(r'^sub_([\w\d]+)$', raw_addr) + if match: + return match.group(1) + else: + return None - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - resym/process_data/prep_decompiled.py at main · lt-asset/resym · GitHub - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - -
- Skip to content - - - - - - - - - - - - -
-
- - - - - - - - - - - - - - -
- -
- - - - - - - - -
- - - - - -
- - - - - - - - - -
-
-
- - - - - - - - - - - - -
- -
- -
+def hex_to_decimal(hex_str : str) -> int: + # Check if the input hex string is valid + if not re.match(r'^-?[0-9a-fA-F]+$', hex_str): + return None + + # Convert the hex string to decimal + decimal_num = int(hex_str, 16) + return decimal_num + + + +def extract_comments(fun_content:List[str]) -> List[Dict]: + var_decl_pattern = r'^(.+?\s+\**)(\S+);\s+\/\/(.*)$' # ; // + rbp_offset_pattern = r'\[rbp(-[\d\w]+?)h\]' # [rbp-h] + array_name_pattern = r'^(.*?)\[(\d+)\]$' # [] + + + var_decl_info = [] + for line in fun_content: + match = re.match(var_decl_pattern, line.strip()) + if match: + var_type = match.group(1).strip() + var_name = match.group(2).strip() + comment = match.group(3).strip() + + # parse var_name (handle array) + array_name_match = re.match(array_name_pattern, var_name) + if array_name_match: + var_name = array_name_match.group(1) + array_size = int(array_name_match.group(2)) + else: + array_size = None + + # parse comment, get rbp offset + rbp_offset = None + rbp_offset_match = re.search(rbp_offset_pattern, comment) + if rbp_offset_match: + rbp_offset = rbp_offset_match.group(1) -
- - - - / - - resym - - - Public -
- - -
- -
- - -
-
- -
-
- - - + new_fname = fname.replace('.decompiled', '-' + str(addr))+'.c' + write_file(os.path.join(file_save_dir,new_fname), code_with_header) -
+ # parse decompiled + code_lines = code.split('\n') + try: + if funname.startswith('sub_'): + arg_info: List[Dict] = parse_signature(code_lines) + else: + if funname.startswith('.'): + funname = funname[1:] - + arg_info: List[Dict] = parse_signature(code_lines, funname=funname) + var_info: List[Dict] = extract_comments(code_lines) + except ParseError as e: + print(f'{fname} - {funname}: {e.msg}') + continue + except Exception as e: + print(f'[ERROR] (parse_decomplied) Other error {fname} - {funname}: {e}') + continue + save_data = {'argument': arg_info, 'variable': var_info} + var_fname = fname.replace('.decompiled', '-' + str(addr) + '_var.json') + dump_json(os.path.join(parsed_save_dir, var_fname), save_data) - -
- +if __name__=='__main__': + parser = argparse.ArgumentParser() + parser.add_argument('src_dir_or_file') + parser.add_argument('file_save_dir') + parser.add_argument('parsed_save_dir') + args = parser.parse_args() + prep_decompiled(args.src_dir_or_file, args.file_save_dir, args.parsed_save_dir) - - - - - - - - - - - - - -

Latest commit

 

History

History
207 lines (155 loc) · 6.48 KB

prep_decompiled.py

File metadata and controls

207 lines (155 loc) · 6.48 KB
-
- - - - -
- -
- -
-
- -
- - - - - - - - - - - - - - - - - - - - - -
- -
-
- - -