From 81033d8e6b264738b67f250a31dc8b60b6a79276 Mon Sep 17 00:00:00 2001 From: Sarapulov Vasilii Date: Thu, 11 Jul 2024 17:23:12 +0300 Subject: [PATCH 1/2] feat: addtion of function write program data types --- resources/in/test.out | Bin 0 -> 15960 bytes src/code_receiving.py | 32 ++++++++++++++++++++++++++++++++ src/write_program_data_types | 17 +++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 resources/in/test.out create mode 100644 src/code_receiving.py create mode 100644 src/write_program_data_types diff --git a/resources/in/test.out b/resources/in/test.out new file mode 100644 index 0000000000000000000000000000000000000000..68211d92a68d4d85172085d5867536c6aeed533b GIT binary patch literal 15960 zcmeHOYit}>6~4R9!=X-Wrw(--NVd?F7VyT79V-QbH-2S|oCl6As6-jBy<_i+eYm@` zja{K)l!9tCkpdDlD*aI;M3HDwe*iKP60QQJAVAFzq@b!IL@1(D&Ld(Sr+ z59?H_5K^VN($06!s7>f?%~i6)~-iQ#OIR0vXPzuHX4 zU23P=K=L}RGxn4LvYK)sz8dRAUq{HiR<6{*_ZYPTVuXy$TdUm11EK;{avscE&jyjh zSd|Wdu>1Wbm`o+1k3L=#5g6q0P?p+_!_tA(_-TpDgdlh5;e~O)&=ipS;Bx%3n|V9^9TQ$lLN}wr*lpAbi2goFJh`;uaeKgJ z9+m#qx(=4aA1L8fd-A!-p6)&QOnW|8tj@L1?eA&t=}uNk$-TN`9#?`sJf{vF8CTTI zGV3IaD!yQ3w8ypS;rU=WJ zAM9h|@kLfkU8(cVZwgwm#(_CoYuGH(C2HuopIZKzHT)}U=vztuKKlGunXS4NW!n_) zDZApP%dTBW=Zd76noHZ$xneq>d(=^E=HU0JLnFg|{r2AE-sC>N-qo$_;jzPZ#wj~9 zxr*zQ#}4=BOGRfaJ(+i?t(iipD6QMZwWcEtE>6*}qYods#7Of2Ld>UXTW%9yl}^!p z|0`dokj9nVrV95t?ypocx1!XtI6$vNU!T%u1wG^oy++U1i?0F7#ehCIFRldiJdaRT z0($-Y6T^*w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{s`1{qh(&&fsG`l_MR0)q}qv0|a6nNG`f?8^Q803B1MvAL}X~#Tny= zAMi1<^O_AZ)@{HJ;fKdBYQ0Y7A%l-~Byd?A(SP{-pCUW+`@|3HQXt~Qe6EN2-y|K! z1nv9Z5Ar>M^*XueBH@Loo}pThpAeppzNe|+>j(H;h@TPz5PoO|GVsL^|Cktn@LP-j zqY!^W2*8b@2@`JbCm}x8A3*#bLjU3T{Y?0{{wg7!g0dVCrMcF=K$Tz9hldD$OM(O{ zz-}Y@$8i6zktN8t62=wyyw5Zh%pa&PQ-#t{{XJaxorispVSmWLtHf;#@bNovxt)|$ z;Af;;GW7o@1J7Mq?)&Rk>}$pTRlfhYfQRe&UDEOSgOBy>(jNAv{8|J45q#hVA_w^@ zC9WL?(GMB`4{`&MS%?0y&TA5W*q{3fmS*W++ Date: Thu, 11 Jul 2024 23:42:24 +0300 Subject: [PATCH 2/2] fix: file closing Signed-off-by: Sarapulov Vasilii --- src/code_receiving.py | 32 -------------------------------- src/write_program_data_types | 17 ----------------- src/write_program_data_types.py | 14 ++++++++++++++ 3 files changed, 14 insertions(+), 49 deletions(-) delete mode 100644 src/code_receiving.py delete mode 100644 src/write_program_data_types create mode 100644 src/write_program_data_types.py diff --git a/src/code_receiving.py b/src/code_receiving.py deleted file mode 100644 index 9951878..0000000 --- a/src/code_receiving.py +++ /dev/null @@ -1,32 +0,0 @@ -'''Receiving decompiled code from Ghidra API via pyhidra''' -#pylint: disable=wrong-import-position -import pyhidra # pylint: disable=import-error -pyhidra.start() -import ghidra # pylint: disable=import-error -from java.io import File # pylint: disable=import-error - -def receive_code(binary_file_path, output_file_path): - '''Receiving decompiled code''' - with pyhidra.open_program(binary_file_path) as flat_api: - program = flat_api.getCurrentProgram() - dtm = program.getDataTypeManager() - ifc = ghidra.app.decompiler.DecompInterface() - ifc.openProgram(program) - print(ghidra.app.util.exporter.getFakeCTypeDefinitions(dtm.getDataOrganization())) - # for i in data_type.getDataOrganization(): - # print(i) - # for f in program.getFunctionManager().getFunctions(True): - # print(f, f.getBody()) - # print() - # results = ifc.decompileFunction(f, 0, flat_api.monitor) - # print(results.getDecompiledFunction().getC()) - - # listing = program.getListing() - # for i in listing.getCodeUnits(True): - # print(i) - # exporter = ghidra.app.util.exporter.CppExporter() - - # f = File(output_file_path) - # exporter.export(f, program, None, flat_api.monitor) - -receive_code("resources/in/test.out", "resources/out/test.c") diff --git a/src/write_program_data_types b/src/write_program_data_types deleted file mode 100644 index 03c78f5..0000000 --- a/src/write_program_data_types +++ /dev/null @@ -1,17 +0,0 @@ -'''Dumping program data types''' -#pylint: disable=wrong-import-position -import pyhidra # pylint: disable=import-error -pyhidra.start() -import ghidra # pylint: disable=import-error -from java.io import File, PrintWriter # pylint: disable=import-error - -def write_program_data_types(file, monitor): - """Dumping program data types""" - dtm = program.getDataTypeManager() - cfile_writer = PrintWriter(file) - data_type_writer = ghidra.program.model.data.DataTypeWriter(dtm, cfile_writer, True) - data_type_writer.write(dtm, monitor) -with pyhidra.open_program("resources/in/test.out") as flat_api: - program = flat_api.getCurrentProgram() - f = File("resources/out/test1.c") - write_program_data_types(f, flat_api.monitor) diff --git a/src/write_program_data_types.py b/src/write_program_data_types.py new file mode 100644 index 0000000..a5edce7 --- /dev/null +++ b/src/write_program_data_types.py @@ -0,0 +1,14 @@ +'''Dumping program data types''' +#pylint: disable=wrong-import-position +import pyhidra # pylint: disable=import-error +pyhidra.start() +import ghidra # pylint: disable=import-error +from java.io import PrintWriter # pylint: disable=import-error + +def write_program_data_types(program, file, monitor): + """Dumping program data types""" + dtm = program.getDataTypeManager() + c_file_writer = PrintWriter(file) + data_type_writer = ghidra.program.model.data.DataTypeWriter(dtm, c_file_writer, False) + data_type_writer.write(dtm, monitor) + c_file_writer.close()