Skip to content

Commit 24988f9

Browse files
alessandrogariopgoodman
authored andcommitted
The disassembler script (get_cfg.py) can now be run manually. (lifting-bits#227)
* The disassembler script (get_cfg.py) can now be run manually. The disassembler script can now be run manually from IDA Pro (File, Script file...). The 'ida_kernwin.cvar.batch' variable is used to determine whether IDA Pro is being run in batch mode or not. * Use the function under the cursor as starting entry point
1 parent bfb0084 commit 24988f9

File tree

1 file changed

+69
-12
lines changed

1 file changed

+69
-12
lines changed

tools/mcsema_disass/ida/get_cfg.py

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,29 +2256,84 @@ def try_mark_as_function(address):
22562256

22572257
if __name__ == "__main__":
22582258

2259+
#
2260+
# interactive mode support
2261+
#
2262+
2263+
architecture = None
2264+
operating_system = None
2265+
output_file_path = None
2266+
log_file_path = None
2267+
entry_point_list = []
2268+
2269+
if ida_kernwin.cvar.batch == 0:
2270+
print "Manual run detected; setting default parameters..."
2271+
2272+
# attempt to guess the architecture
2273+
# todo: use idaapi.get_inf_structure().procName to determine which arch we are dealing with.
2274+
# the 'bits' field is enough for the time being, since we only support x86 and amd64
2275+
2276+
if idaapi.get_inf_structure().procName != 'metapc':
2277+
print "Unsupported architecture"
2278+
exit(1)
2279+
2280+
if idaapi.get_inf_structure().is_64bit():
2281+
architecture="amd64"
2282+
elif idaapi.get_inf_structure().is_32bit():
2283+
architecture="x86"
2284+
else:
2285+
print "Only 32-bits and 64-bits targets are supported!"
2286+
exit(1)
2287+
2288+
# attempt to guess the file format
2289+
loader_module_name = idaapi.get_file_type_name()
2290+
if "Portable executable" in loader_module_name:
2291+
operating_system = "windows"
2292+
elif "ELF" in loader_module_name:
2293+
operating_system = "linux"
2294+
else:
2295+
print "Unsupported image type! Only PE and ELF executables are supported!"
2296+
exit(1)
2297+
2298+
# generate a default output path for both the cfg and the log file
2299+
output_file_path = idc.GetIdbPath() + '-mcsema.cfg'
2300+
log_file_path = idc.GetIdbPath() + '-mcsema.log'
2301+
2302+
# get the function name under the cursor and set it as the starting entry point
2303+
entry_point_name = idc.GetFunctionName(idc.ScreenEA())
2304+
entry_point_list.append(entry_point_name)
2305+
2306+
print "Summary:"
2307+
print 'Log file: ' + log_file_path
2308+
print 'Architecture: ' + architecture
2309+
print 'Operating system: ' + operating_system
2310+
print 'Output file: ' + output_file_path
2311+
print 'Entry point: ' + entry_point_name
2312+
2313+
#
2314+
# parse the command line argument
2315+
#
2316+
22592317
parser = argparse.ArgumentParser()
22602318

22612319
parser.add_argument("--log_file", type=argparse.FileType('w'),
2262-
default=sys.stderr,
2320+
default=log_file_path,
22632321
help="Log to a specific file. Default is stderr.")
22642322

22652323
parser.add_argument(
2266-
'--arch',
2267-
help='Name of the architecture. Valid names are x86, amd64.',
2268-
required=True)
2324+
'--arch', default=architecture,
2325+
help='Name of the architecture. Valid names are x86, amd64.')
22692326

22702327
parser.add_argument(
2271-
'--os',
2272-
help='Name of the operating system. Valid names are linux, windows.',
2273-
required=True)
2328+
'--os', default=operating_system,
2329+
help='Name of the operating system. Valid names are linux, windows.')
22742330

22752331
parser.add_argument(
2276-
"--output", type=argparse.FileType('wb'), default=None,
2277-
help="The output control flow graph recovered from this file",
2278-
required=True)
2332+
"--output", type=argparse.FileType('wb'), default=output_file_path,
2333+
help="The output control flow graph recovered from this file")
22792334

22802335
parser.add_argument(
2281-
"--entrypoint", nargs='*',
2336+
"--entrypoint", nargs='*', default=entry_point_list,
22822337
help="Symbol(s) to start disassembling from")
22832338

22842339
parser.add_argument("--std-defs", action='append', type=str,
@@ -2406,4 +2461,6 @@ def try_mark_as_function(address):
24062461
DEBUG(str(e))
24072462
DEBUG(traceback.format_exc())
24082463

2409-
idc.Exit(0)
2464+
# do not close IDA if we are not being run in batch mode
2465+
if ida_kernwin.cvar.batch != 0:
2466+
idc.Exit(0)

0 commit comments

Comments
 (0)