PDA

View Full Version : pseudo-c / IDA



Inprise
چهارشنبه 14 اردیبهشت 1384, 16:01 عصر
سلام؛

<span dir=ltr>Creates pseudo-c code to aid you in the progress of decompiling a target. Of course, the script doesn't give you accurate results. It doesn't have any dataflow analysis nor doese it handle every mnemonic/code structure</span>

بسته به نسخهء مورد استفاده :


// pseudo_c.idc v1.02 by trapflag
//
// Creates pseudo-c code to aid you in the progress
// of decompiling a target. Of course, the script doesn't
// give you accurate results. It doesn't have any dataflow analysis
// nor doese it handle every mnemonic/code structure.
// My intention was to play a bit with the IDC scripting
// language. The messy code is due to the limited instruction set of
// the idc language. Go, code a plugin or help extending desquirr &#58;&#41;
//
// What's new&#58;
//
// Jan,9th,2004&#58;
//
// - script can track back stdcall function parameters.
// assumes stdcall for every function tho ;-\
//
// by default, the script adds comments to the database.
// This can be turned off by commenting the '#define OVERRIDE' line.


#define OVERRIDE


#include "idc.idc"



static NextMnem&#40;ea&#41;
&#123;
auto mnem, nexthead;
nexthead = NextHead&#40;ea, FindFuncEnd&#40; ea &#41;&#41;;
mnem = GetMnem&#40;nexthead&#41;;
return mnem;
&#125;

static GetParamCount&#40;ea&#41;
&#123;
auto nextea;

//spdif = GetSpDiff&#40;ea&#41; - GetSpd&#40;ea&#41;;
nextea = NextHead&#40;ea, FindFuncEnd&#40; ea &#41;&#41;;
// Message&#40;"%x params\n",GetSpDiff&#40;nextea&#41;/4&#41;;
return &#40;GetSpDiff&#40;nextea&#41;/4&#41;;
&#125;


static GetParamEa&#40;ea,n&#41;
&#123;
auto i,tempea,actualparam;

i=0;
tempea=ea;
actualparam = n;

while&#40;i!=actualparam&#41;
&#123;
tempea=PrevHead&#40;tempea,0&#41;;
if&#40;GetMnem&#40;tempea&#41;=="call"&#41;
actualparam = actualparam + GetParamCount&#40;tempea&#41;;
else if&#40;GetMnem&#40;tempea&#41;=="push"&#41;
i++;
&#125;
return tempea;
&#125;

static NextOpnd0&#40;ea&#41;
&#123;
auto mnem, nexthead;
nexthead = NextHead&#40;ea, FindFuncEnd&#40; ea &#41;&#41;;
return GetOpnd&#40;nexthead,0&#41;;
&#125;

static NextOpnd1&#40;ea&#41;
&#123;
auto mnem, nexthead;
nexthead = NextHead&#40;ea, FindFuncEnd&#40; ea &#41;&#41;;
return GetOpnd&#40;nexthead,1&#41;;
&#125;

static main&#40;&#41;
&#123;
auto ea, nextea, screenea,funcend,mnem,opnd0,opnd1,output,spdif,i;
screenea = ScreenEA&#40;&#41;;
Message&#40;"funcend %08X\n"FindFuncEnd&#40;screenea&#41;&#41;;
funcend=FindFuncEnd&#40;screenea&#41;;

for &#40;ea=screenea;ea&lt;=funcend;ea=NextHead&#40;ea, funcend&#41;&#41;
&#123;
//Message&#40;GetCurrentLine&#40;&#41;&#41;;
//Message&#40;GetMnem&#40;ea&#41;&#41;;
mnem = GetMnem&#40;ea&#41;;
opnd0 = GetOpnd&#40;ea,0&#41;;
opnd1 = GetOpnd&#40;ea,1&#41;;
if &#40;ea==BADADDR&#41; break;
Message&#40;"%08X&#58; ",ea&#41;;

if&#40; strstr&#40; mnem,"jmp" &#41; != -1&#41;
&#123;
output = "goto "+opnd0;
&#125;
else if&#40; strstr&#40; mnem,"mov" &#41; != -1&#41;
&#123;
output= opnd0 + " = " + opnd1;
&#125;
else if&#40; strstr&#40; mnem,"call" &#41; != -1&#41;
&#123;
output = opnd0 + "&#40;";
if&#40;GetParamCount&#40;ea&#41;>0&#41;
&#123;
for&#40;i=1;i&lt;=GetParamCount&#40;ea&#41;;i++&#41;
&#123;
output = output+GetOpnd&#40;GetParamEa&#40;ea,i&#41;,0&#41;;
if&#40;i>=1 &amp;&amp; i!=GetParamCount&#40;ea&#41;&#41; output=output+", ";
&#125;
&#125;
output = output + "&#41;";
// output= opnd0 + "&#40;"+ltoa&#40;GetParamCount&#40;ea&#41;,10&#41;+"&#41;";
&#125;
else if&#40; strstr&#40; mnem,"lea" &#41; != -1 &amp; strstr&#40; mnem,"leave" &#41; == -1&#41;
&#123;
output= opnd0 + " = &amp;" + opnd1;
&#125;
else if&#40; strstr&#40; mnem,"cmp" &#41; != -1&#41;
&#123;
if &#40; NextMnem&#40;ea&#41; == "jz" &#41;
&#123;
output= "if &#40;"+opnd0+" == "+opnd1+"&#41; ";
ea = NextHead&#40;ea,funcend&#41;;
output=output+"goto "+GetOpnd&#40;ea,0&#41;;
&#125;
else if &#40; NextMnem&#40;ea&#41; == "jnz" &#41;
&#123;
output= "if &#40;"+opnd0+" != "+opnd1+"&#41; ";
ea = NextHead&#40;ea,funcend&#41;;
output=output+"goto "+GetOpnd&#40;ea,0&#41;;
&#125;
//Message&#40;opnd0 + " = &amp;" + opnd1&#41;;
&#125;
else if&#40; strstr&#40; mnem,"test" &#41; != -1&#41;
&#123;
if &#40; GetOpnd&#40;ea,0&#41; == GetOpnd&#40;ea,1&#41; &#41;
if &#40; NextMnem&#40;ea&#41; == "jnz" &#41;
&#123;
output="if &#40;"+opnd0+" != 0&#41; ";
ea = NextHead&#40;ea,funcend&#41;;
output=output+"goto "+GetOpnd&#40;ea,0&#41;;
&#125;
else if &#40; NextMnem&#40;ea&#41; == "jz" &#41;
&#123;
output="if &#40;"+opnd0+" == 0&#41; ";
ea = NextHead&#40;ea,funcend&#41;;
output=output+"goto "+GetOpnd&#40;ea,0&#41;;
&#125;

&#125;
else if&#40; strstr&#40; mnem,"dec" &#41; != -1&#41;
&#123;
output=opnd0 + "--";
&#125;
else if&#40; strstr&#40; mnem,"inc" &#41; != -1&#41;
&#123;
output=opnd0 + "++";
&#125;
else if&#40; strstr&#40; mnem,"xor" &#41; != -1&#41;
&#123;
if&#40; opnd0 == opnd1 &#41;
&#123;
if &#40; NextMnem&#40;ea&#41; == "inc" &amp;&amp; NextOpnd0&#40;ea&#41; == GetOpnd&#40;ea,0&#41; &#41;
&#123;
output=GetOpnd&#40;ea,0&#41;+" = 1";
ea = NextHead&#40;ea,funcend&#41;;
&#125;
else output= opnd0 + " = 0";
&#125;
else output=opnd0 + " ^= " + opnd1;
&#125;
else if&#40; strstr&#40; mnem,"add" &#41; != -1&#41;
&#123;
output=opnd0+ " += "+opnd1;
&#125;
else if&#40; strstr&#40; mnem,"sub" &#41; != -1&#41;
&#123;
output=opnd0+ " -= "+opnd1;
&#125;
else if&#40; strstr&#40; mnem,"ret" &#41; != -1&#41;
&#123;
output="return";
&#125;
else output = "";//="???";//Message&#40;"???"&#41;;
if&#40;output!=""&#41; output = output + ";";
#ifdef OVERRIDE
//SetManualInsn&#40;ea,output&#41;;
MakeComm&#40;ea,output&#41;;
#endif
Message&#40;output+"\n"&#41;;
output="";
&#125;
&#125;

به من که خیلی کمک میکنه ، حتما" ازش استفاده کنید .

http://img94.echo.cx/img94/8554/psc6me.jpg

:)