NtGlobalFlag.c
上传用户:haohao_zhu
上传日期:2014-08-15
资源大小:2446k
文件大小:10k
源码类别:

PlugIns编程

开发平台:

Visual C++

  1. #include <windows.h>
  2. #include <winbase.h>
  3. #include <shlwapi.h>
  4. #include <winternl.h>
  5. #include <stdlib.h>
  6. #include "plugin.h"
  7. NTSTATUS (WINAPI *ntdll_NtQueryInformationProcess)(HANDLE,PROCESSINFOCLASS,PVOID,ULONG,PULONG);
  8.  
  9. HINSTANCE         hinst;
  10. HINSTANCE   Dll_Handle;
  11. FARPROC ntdll_DbgBreakPoint;
  12. FARPROC ntdll_RtlRaiseException;
  13. void * status;
  14. void * glob;
  15. void * data;
  16. void * debesp;
  17. void * debespplus;
  18. void * hProcess;
  19. char * mySecPointer;
  20. LPVOID  dlah;
  21. ulong myPointer;
  22. long elah;
  23. long  isSet;
  24. long  isUnset;
  25. int  type;
  26. int  pid;
  27. int  answer;
  28. int noofitems;
  29. int  breakname;
  30. int lenofbreakname;
  31. int debstrlen;
  32. int  breakontlscallback;
  33. int  breakondllinitrout;
  34. PROCESS_BASIC_INFORMATION  pbi;
  35. MEMORY_BASIC_INFORMATION mbi;
  36. DEBUG_EVENT debev;
  37. t_table  valref;
  38. t_reg currentreg;
  39. DWORD lpOld;
  40. DWORD Curr_Pid;
  41. HANDLE Curr_Proc_Pseudo;
  42. HANDLE debproc;
  43. HWND              hwmain; 
  44. HWND seqwindow;
  45. HWND editwindow;
  46. HWND buttwindow;
  47. HWND asswindow;
  48. HWND asseditwindow;
  49. HWND assbuttwindow;
  50. HWND asscombo;
  51. HWND asscancel;
  52. char  clah[4] = {0x00,0x00,0x00,0x00};
  53. char              NtGlobalFlagwinclass[32];
  54. char  spattern[] = {"MOV DWORD PTR DS:[RA+CONST],40010006"};
  55. char  mystring[200]; 
  56. char  asstext[200];
  57. char  trim[200];
  58. char debugstring[400];
  59. char tlscallback[10];
  60. int myfunc (int flag);
  61. DWORD WINAPI ThreadProcFirst( LPVOID lpParam );
  62. DWORD WINAPI ThreadProcSecond( LPVOID lpParam );
  63. #pragma argsused
  64. BOOL WINAPI DllEntryPoint(HINSTANCE hi,DWORD reason,LPVOID reserved)
  65. {
  66.    if (reason==DLL_PROCESS_ATTACH)
  67.      hinst=hi;                            // Mark plugin instance
  68.    return 1;                            // Report success
  69. };
  70. #pragma argsused
  71. extc int _export cdecl ODBG_Plugindata(char shortname[32])
  72. {
  73. strcpy(shortname,"NtGlobalFlag");       // Name of plugin
  74. return PLUGIN_VERSION;
  75. };
  76. #pragma argsused
  77. extc int _export cdecl ODBG_Plugininit(int ollydbgversion,HWND hw,ulong *features)
  78. {
  79.    // Check that version of OllyDbg is correct.
  80.    if (ollydbgversion<PLUGIN_VERSION)
  81.      return -1;
  82.   // Keep handle of main OllyDbg window. This handle is necessary, for example,
  83.    // to display message box.
  84.    hwmain=hw;
  85. Addtolist(0,0,"NtGlobalFlag Plugin v1.10");
  86.    Addtolist(0, 1,"  Dedicated to oleh yuschuk author of this Debugger");
  87. Addtolist(0,-1,"    authored by stingduk");
  88. Addtolist(0, 1,"      and thanks to Detten for cing my asm ");
  89. return 0;
  90. };
  91. #pragma argsused
  92. int ODBG_Pausedex(int reason, int extdata, t_reg *reg, DEBUG_EVENT *debugevent)
  93. {
  94. if (reg != 0)
  95. {
  96. if(debugevent !=0)
  97. {
  98. debev = *debugevent;
  99. currentreg = *reg;
  100. if(debev.u.Exception.ExceptionRecord.ExceptionCode != EXCEPTION_BREAKPOINT)
  101. {
  102. return 0;
  103. }
  104. Dll_Handle = GetModuleHandle("ntdll.dll");
  105. if(Dll_Handle ==0)
  106. {
  107. MessageBox(hwmain,"error","LoadLibFailed",MB_OK);
  108. return 0;
  109. }
  110. ntdll_DbgBreakPoint = GetProcAddress(Dll_Handle,"DbgBreakPoint");
  111. if(ntdll_DbgBreakPoint == 0)
  112. {
  113. return 0;
  114. }
  115. ntdll_RtlRaiseException = GetProcAddress(Dll_Handle,"RtlRaiseException");
  116. if(ntdll_RtlRaiseException == 0)
  117. {
  118. return 0;
  119. }
  120. if (debev.u.Exception.ExceptionRecord.ExceptionAddress == ntdll_DbgBreakPoint)
  121. *(FARPROC *)&ntdll_NtQueryInformationProcess = GetProcAddress(Dll_Handle,"NtQueryInformationProcess");
  122. if(ntdll_NtQueryInformationProcess == 0)
  123. {
  124. MessageBox(hwmain,"error","GetProcFailed",MB_OK);
  125. return 0;
  126. }
  127. pid = Plugingetvalue(VAL_PROCESSID);
  128. debproc = OpenProcess(PROCESS_ALL_ACCESS,FALSE,pid);
  129. ntdll_NtQueryInformationProcess(debproc,ProcessBasicInformation,&pbi,sizeof(pbi),NULL);
  130. status = pbi.PebBaseAddress;
  131. (byte *)status += 0x68;
  132. VirtualQueryEx(debproc,status,&mbi,sizeof(mbi));
  133. VirtualProtectEx(debproc,mbi.BaseAddress,mbi.RegionSize,PAGE_EXECUTE_READWRITE,&lpOld);
  134. ReadProcessMemory(debproc,status,&dlah,4,NULL);
  135. elah = (long)dlah;
  136. elah |= 0x02;
  137. WriteProcessMemory(debproc,status,&elah,4,NULL);
  138. noofitems = 0;
  139. while (noofitems < 2)
  140. {
  141. CreateThread(NULL,NULL,ThreadProcFirst,NULL,NULL,NULL);
  142. Sendshortcut(PM_DISASM,0,WM_KEYDOWN,1,0,'S');
  143. Sendshortcut(PM_DISASM,0,WM_KEYDOWN,1,0,'R');
  144. valref = *(t_table *)Plugingetvalue(VAL_REFERENCES);
  145. noofitems = valref.data.n;
  146. }
  147. valref = *(t_table *)Plugingetvalue(VAL_REFERENCES);
  148. data = valref.data.data;
  149. Setdisasm(*(ulong *)data,1,CPU_ASMCENTER|CPU_ASMFOCUS);
  150. memset(mystring,0,200);
  151. wsprintf(mystring,"Assemble at %x",*(ulong *)data);
  152. CreateThread(NULL,NULL,ThreadProcSecond,NULL,NULL,NULL);
  153. Sendshortcut(PM_DISASM,*(ulong *)data,WM_CHAR,0,0,' ');
  154. Insertname((ulong)ntdll_RtlRaiseException,NM_BREAK,"STRING [[[esp+4]+18h]] == "LDR: Calling Tls Callback Imagebase" | STRING [[[esp+4]+18h]] == " - Calling"");
  155. Setbreakpointext((ulong)ntdll_RtlRaiseException,TY_ACTIVE|TY_KEEPCOND,0,0);
  156. Sendshortcut(PM_MAIN,0,WM_KEYDOWN,0,0,VK_F9);
  157. VirtualProtectEx(debproc,mbi.BaseAddress,mbi.RegionSize,lpOld,&lpOld);
  158. CloseHandle(debproc);
  159. return 0;
  160. }
  161. else if (debev.u.Exception.ExceptionRecord.ExceptionAddress == ntdll_RtlRaiseException)
  162. {
  163. myPointer = currentreg.r[4];
  164. myPointer += 0x4;
  165. hProcess = (void *)Plugingetvalue(VAL_HPROCESS);
  166. ReadProcessMemory(hProcess,(const void *)myPointer,&debesp,4,NULL);
  167. (byte *)debesp += 0x18;
  168. ReadProcessMemory(hProcess,(const void *)debesp,&debespplus,4,NULL);
  169. ReadProcessMemory(hProcess,(const void *)debespplus,&debugstring,0x400,NULL);
  170. debstrlen = lstrlen(debugstring);
  171. mySecPointer = strrchr(debugstring,0x20);
  172. if(StrCmpN(" - Calling",debugstring,10) == 0)
  173. {
  174. if(breakondllinitrout == 1)
  175. {
  176. Setbreakpointext(strtol((unsigned char *)mySecPointer,NULL,16),TY_ONESHOT,0,0);
  177. Sendshortcut(PM_MAIN,0,WM_KEYDOWN,0,0,VK_F9);
  178. return 0;
  179. }
  180. Sendshortcut(PM_MAIN,0,WM_KEYDOWN,0,0,VK_F9);
  181. return 0;
  182. }
  183. if(breakontlscallback == 1)
  184. {
  185. Setbreakpointext(strtol((unsigned char *)mySecPointer,NULL,16),TY_ONESHOT,0,0);
  186. Sendshortcut(PM_MAIN,0,WM_KEYDOWN,0,0,VK_F9);
  187. return 0;
  188. }
  189. Sendshortcut(PM_MAIN,0,WM_KEYDOWN,0,0,VK_F9);
  190. return 0;
  191. }
  192. else 
  193. {
  194. return 0;
  195. }
  196. }
  197. }
  198. return 0;
  199. };
  200. #pragma argsused
  201. extc int _export cdecl ODBG_Pluginmenu(int origin,char data[4096],void *item)
  202. {
  203. switch (origin)
  204. {
  205. case PM_MAIN:                      // Plugin menu in main window
  206.        strcpy(data,"0 &SET LDR_SHOW_SNAPS|1 &UNSET LDR_SHOW_SNAPS|3 &SetTempBreak on Tls cllabacks |4 &SetTempbreak on all DllInit Routines|2 &About");
  207. return 1;
  208. default: break;                    // Any other window
  209. };
  210.    return 0;                           // Window not supported by plugin
  211. };
  212. #pragma argsused
  213. extc void _export cdecl ODBG_Pluginaction(int origin,int action,void *item)
  214. {
  215. if (origin==PM_MAIN)
  216.  {
  217.     switch (action)
  218.     {
  219.    case 2:
  220.   MessageBox(hwmain,
  221.            "NtGlobalFlag v1.10n"
  222.    "thanks to oleh yuschuk for making this wonderful Debuggern"
  223.    "thanks to Detten for converting my crazy asm code and ideasn"
  224.    " into c and compiling this plugin",
  225.    "NtGLobalFlag",
  226.             MB_OK|MB_ICONINFORMATION);
  227.          break;
  228. case 0:
  229. isSet = myfunc (2);
  230. if(isSet == 1)
  231. MessageBox(hwmain,"LDR_SHOW_SNAPS was successfully SETn"
  232.   "RESTART DEBUGEE nn"
  233.   "YES DEBUGGEE NOT DEBUGGER :)n",
  234.   "         Success",
  235.    MB_OK|MB_ICONINFORMATION);
  236. else
  237. MessageBox(hwmain,"         ERROR","LDR_SHOW_SNAPS could not be SET",MB_OK|MB_ICONEXCLAMATION);
  238. break;
  239. case 1:
  240. isUnset = myfunc (0);
  241. if(isUnset == 1)
  242. MessageBox(hwmain,"         Success","LDR_SHOW_SNAPS was successfully UNSET",MB_OK|MB_ICONINFORMATION);
  243. else
  244. MessageBox(hwmain,"         ERROR","LDR_SHOW_SNAPS could not be UNSET",MB_OK|MB_ICONEXCLAMATION);
  245. break;
  246. case 3:
  247. breakontlscallback = 1;
  248. break;
  249. case 4:
  250. breakondllinitrout = 1;
  251. break;
  252.       default: break;
  253. };
  254.  }
  255. };
  256. int myfunc(int flag)
  257. {
  258. Curr_Pid  = GetCurrentProcessId();
  259. Curr_Proc_Pseudo = OpenProcess(PROCESS_ALL_ACCESS,FALSE,Curr_Pid); 
  260. type = Plugingetvalue(VAL_FONTHEIGHTS); 
  261. (int)glob = type+0x2a2C;
  262. if (flag == 2)
  263. {
  264. clah[0] = 0;
  265. }
  266. else
  267. {
  268. clah[0] = 2;
  269. }
  270. if(WriteProcessMemory(Curr_Proc_Pseudo,glob,&clah,1,NULL))
  271. {
  272. CloseHandle(Curr_Proc_Pseudo);
  273.                         return 1;
  274. }
  275. CloseHandle(Curr_Proc_Pseudo);
  276. return -1;
  277. }
  278. #pragma argsused
  279. DWORD WINAPI ThreadProcFirst( LPVOID lpParam )
  280. {
  281. seqwindow =0;
  282. editwindow = 0;
  283. buttwindow =0;
  284. while (seqwindow == 0)
  285. {
  286. seqwindow = FindWindow("#32770","Find sequence of commands");
  287. }
  288. while (editwindow == 0)
  289. {
  290. editwindow = FindWindowEx(seqwindow,NULL,"EDIT",NULL);
  291. }
  292. SendMessage(editwindow,WM_SETTEXT,NULL,(long)&spattern);
  293. while (buttwindow == 0)
  294. {
  295. buttwindow = FindWindowEx(seqwindow,NULL,"BUTTON","Find");
  296. }
  297. SendMessage(buttwindow,BM_CLICK,NULL,NULL);
  298. return 0;
  299. }
  300. #pragma argsused
  301. DWORD WINAPI ThreadProcSecond( LPVOID lpParam )
  302. {
  303. memset(asstext,0,200);
  304. memset(trim,0,200);
  305. memcpy(trim,"JMP ",4);
  306. asswindow = 0;
  307. asseditwindow =0;
  308. assbuttwindow =0;
  309. asscombo = 0;
  310. asscancel = 0;
  311. while (asswindow == 0)
  312. {
  313. asswindow = FindWindow("#32770",mystring);
  314. }
  315. while (asscombo ==0)
  316. {
  317. asscombo = FindWindowEx(asswindow,NULL,"ComboBox",NULL);
  318. }
  319. while (asseditwindow == 0)
  320. {
  321. asseditwindow = FindWindowEx(asscombo,NULL,"EDIT",NULL);
  322. }
  323. SendMessage(asseditwindow,WM_GETTEXT,100,(long)&asstext);
  324. if(StrCmpN("JNZ ",asstext,4) != 0)
  325. {
  326. MessageBox(hwmain,"strings are not equal","NtGlobalFlag",MB_OK);
  327. return 0;
  328. }
  329. StrCat(trim,asstext+4);
  330. SendMessage(asseditwindow,WM_SETTEXT,NULL,(long)&trim);
  331. while (assbuttwindow == 0)
  332. {
  333. assbuttwindow =FindWindowEx(asswindow,NULL,"BUTTON","Assemble");
  334. }
  335. SendMessage(assbuttwindow,BM_CLICK,NULL,NULL);
  336. while (asscancel ==0)
  337. {
  338. asscancel = FindWindowEx(asswindow,NULL,"BUTTON","Cancel");
  339. }
  340. SendMessage(asscancel,BM_CLICK,NULL,NULL);
  341. return 0;
  342. }