Spaces:
				
			
			
	
			
			
		Running
		
			on 
			
			Zero
	
	
	
			
			
	
	
	
	
		
		
		Running
		
			on 
			
			Zero
	Commit 
							
							·
						
						d0f659d
	
1
								Parent(s):
							
							7ce9459
								
try again
Browse files- prep_decompiled.py +5 -68
    	
        prep_decompiled.py
    CHANGED
    
    | @@ -1,10 +1,13 @@ | |
| 1 | 
             
            import argparse
         | 
| 2 | 
             
            import os
         | 
| 3 | 
            -
            from utils import *
         | 
| 4 | 
             
            from typing import Dict, List
         | 
| 5 | 
             
            from tqdm import tqdm
         | 
| 6 | 
             
            import re
         | 
| 7 | 
            -
             | 
|  | |
|  | |
|  | |
| 8 |  | 
| 9 | 
             
            HEADER = '#include "/home/ReSym/clang-parser/defs.hh"\n'
         | 
| 10 |  | 
| @@ -18,8 +21,6 @@ def process_funname(raw_addr:str) -> str: | |
| 18 | 
             
                else:
         | 
| 19 | 
             
                    return None
         | 
| 20 |  | 
| 21 | 
            -
             | 
| 22 | 
            -
             | 
| 23 |  | 
| 24 | 
             
            def hex_to_decimal(hex_str : str) -> int:
         | 
| 25 | 
             
                # Check if the input hex string is valid
         | 
| @@ -141,67 +142,3 @@ def parse_signature(file_content:List[str], funname:str=None) -> List[Dict]: | |
| 141 |  | 
| 142 | 
             
                return arg_info
         | 
| 143 |  | 
| 144 | 
            -
             | 
| 145 | 
            -
             | 
| 146 | 
            -
            def prep_decompiled(src_dir_or_file, file_save_dir, parsed_save_dir):
         | 
| 147 | 
            -
                if os.path.isdir(src_dir_or_file):
         | 
| 148 | 
            -
                    files = [os.path.join(src_dir_or_file, f) for f in get_file_list(src_dir_or_file)]
         | 
| 149 | 
            -
                else:
         | 
| 150 | 
            -
                    files = [src_dir_or_file]
         | 
| 151 | 
            -
             | 
| 152 | 
            -
             | 
| 153 | 
            -
             | 
| 154 | 
            -
                for f in tqdm(files, disable=len(files)==1):
         | 
| 155 | 
            -
                    if not f.endswith(".decompiled"):
         | 
| 156 | 
            -
                        continue
         | 
| 157 | 
            -
                    fname = os.path.basename(f)
         | 
| 158 | 
            -
             | 
| 159 | 
            -
                    decompiled = read_json(f)
         | 
| 160 | 
            -
             | 
| 161 | 
            -
                    for fun in decompiled:
         | 
| 162 | 
            -
                        dex_addr, funname, code = fun['addr'], fun['funname'], fun['code']
         | 
| 163 | 
            -
                        if funname.startswith('sub_'):
         | 
| 164 | 
            -
                            addr = process_funname(funname).upper()
         | 
| 165 | 
            -
                        else:
         | 
| 166 | 
            -
                            addr = str(hex(dex_addr))[2:].upper()
         | 
| 167 | 
            -
                            
         | 
| 168 | 
            -
             | 
| 169 | 
            -
                        code_with_header = HEADER + code
         | 
| 170 | 
            -
             | 
| 171 | 
            -
                        new_fname = fname.replace('.decompiled', '-' + str(addr))+'.c'
         | 
| 172 | 
            -
                        write_file(os.path.join(file_save_dir,new_fname), code_with_header)
         | 
| 173 | 
            -
             | 
| 174 | 
            -
                        # parse decompiled
         | 
| 175 | 
            -
                        code_lines = code.split('\n')
         | 
| 176 | 
            -
                        try:
         | 
| 177 | 
            -
                            if funname.startswith('sub_'):
         | 
| 178 | 
            -
                                arg_info: List[Dict] = parse_signature(code_lines)
         | 
| 179 | 
            -
                            else:
         | 
| 180 | 
            -
                                if funname.startswith('.'):
         | 
| 181 | 
            -
                                    funname = funname[1:]
         | 
| 182 | 
            -
             | 
| 183 | 
            -
                                arg_info: List[Dict] = parse_signature(code_lines, funname=funname)
         | 
| 184 | 
            -
                            var_info: List[Dict] = extract_comments(code_lines)
         | 
| 185 | 
            -
                        except ParseError as e:
         | 
| 186 | 
            -
                            print(f'{fname} - {funname}: {e.msg}')
         | 
| 187 | 
            -
                            continue
         | 
| 188 | 
            -
                        except Exception as e:
         | 
| 189 | 
            -
                            print(f'[ERROR] (parse_decomplied) Other error {fname} - {funname}: {e}')
         | 
| 190 | 
            -
                            continue
         | 
| 191 | 
            -
                        save_data = {'argument': arg_info, 'variable': var_info}
         | 
| 192 | 
            -
             | 
| 193 | 
            -
                        var_fname = fname.replace('.decompiled', '-' + str(addr) + '_var.json')
         | 
| 194 | 
            -
                        dump_json(os.path.join(parsed_save_dir, var_fname), save_data)
         | 
| 195 | 
            -
             | 
| 196 | 
            -
             | 
| 197 | 
            -
             | 
| 198 | 
            -
             | 
| 199 | 
            -
            if __name__=='__main__':
         | 
| 200 | 
            -
                parser = argparse.ArgumentParser()
         | 
| 201 | 
            -
                parser.add_argument('src_dir_or_file')
         | 
| 202 | 
            -
                parser.add_argument('file_save_dir')
         | 
| 203 | 
            -
                parser.add_argument('parsed_save_dir')
         | 
| 204 | 
            -
                args = parser.parse_args()
         | 
| 205 | 
            -
                prep_decompiled(args.src_dir_or_file, args.file_save_dir, args.parsed_save_dir)
         | 
| 206 | 
            -
             | 
| 207 | 
            -
                
         | 
|  | |
| 1 | 
             
            import argparse
         | 
| 2 | 
             
            import os
         | 
| 3 | 
            +
            #from utils import *
         | 
| 4 | 
             
            from typing import Dict, List
         | 
| 5 | 
             
            from tqdm import tqdm
         | 
| 6 | 
             
            import re
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            class ParseError(Exception):
         | 
| 9 | 
            +
                def __init__(self, msg):
         | 
| 10 | 
            +
                    self.msg = msg
         | 
| 11 |  | 
| 12 | 
             
            HEADER = '#include "/home/ReSym/clang-parser/defs.hh"\n'
         | 
| 13 |  | 
|  | |
| 21 | 
             
                else:
         | 
| 22 | 
             
                    return None
         | 
| 23 |  | 
|  | |
|  | |
| 24 |  | 
| 25 | 
             
            def hex_to_decimal(hex_str : str) -> int:
         | 
| 26 | 
             
                # Check if the input hex string is valid
         | 
|  | |
| 142 |  | 
| 143 | 
             
                return arg_info
         | 
| 144 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  |