Advertisement
Guest User

Untitled

a guest
Apr 25th, 2011
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <windowsx.h>
  4.  
  5. typedef FILE *PFILE;
  6.  
  7. #define APPLICATIONNAME "Get Shellcode\0"
  8. #define CLASSNAME "GetShellcode\0"
  9.  
  10. #define IDC_FILENAME_EDIT 101
  11. #define IDC_LOAD_BUTTON 102
  12. #define IDC_SHELLCODE_EDIT 103
  13.  
  14. HINSTANCE hInst;
  15.  
  16. ATOM MyRegisterClass(HINSTANCE hInstance);
  17. BOOL InitInstance(HINSTANCE, int);
  18. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  19. void ResizeControls(HWND, HWND, HWND, HWND);
  20. void OnButtonClick(HWND, HWND, HWND);
  21. void GetShellcode(HWND, HWND, LPSTR);
  22. void DumpTextSegment(PFILE, IMAGE_SECTION_HEADER, HWND, HWND);
  23.  
  24. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26. MSG msg;
  27.  
  28. MyRegisterClass(hInstance);
  29.  
  30. if(!InitInstance(hInstance, nCmdShow))
  31. {
  32. return FALSE;
  33. }
  34.  
  35. while(GetMessage(&msg, NULL, 0, 0))
  36. {
  37. TranslateMessage(&msg);
  38. DispatchMessage(&msg);
  39. }
  40.  
  41. return (int)msg.wParam;
  42. }
  43.  
  44. ATOM MyRegisterClass(HINSTANCE hInstance)
  45. {
  46. WNDCLASSEX wcex;
  47.  
  48. wcex.cbSize = sizeof(WNDCLASSEX);
  49.  
  50. wcex.style = CS_HREDRAW | CS_VREDRAW;
  51. wcex.lpfnWndProc = WndProc;
  52. wcex.cbClsExtra = 0;
  53. wcex.cbWndExtra = 0;
  54. wcex.hInstance = hInstance;
  55. wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  56. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  57. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  58. wcex.lpszMenuName = NULL;
  59. wcex.lpszClassName = CLASSNAME;
  60. wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  61.  
  62. return RegisterClassEx(&wcex);
  63. }
  64.  
  65. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  66. {
  67. HWND hWnd;
  68.  
  69. hInst = hInstance; // Stocke le handle d'instance dans la variable globale
  70.  
  71. hWnd = CreateWindow(CLASSNAME, APPLICATIONNAME, WS_OVERLAPPEDWINDOW,
  72. CW_USEDEFAULT, 0, 800, 500, NULL, NULL, hInstance, NULL);
  73.  
  74. if(!hWnd)
  75. {
  76. return FALSE;
  77. }
  78.  
  79. ShowWindow(hWnd, nCmdShow);
  80. UpdateWindow(hWnd);
  81.  
  82. return TRUE;
  83. }
  84.  
  85. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  86. {
  87. int wmId, wmEvent;
  88. PAINTSTRUCT ps;
  89. HDC hdc;
  90.  
  91. static HWND hwndFilenameEdit;
  92. static HWND hwndLoadButton;
  93. static HWND hwndShellcodeEdit;
  94.  
  95. switch(message)
  96. {
  97. case WM_CREATE:
  98. hwndFilenameEdit = CreateWindow("EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_READONLY | ES_LEFT,
  99. 0, 0, 0, 0, hWnd, (HMENU)IDC_FILENAME_EDIT, hInst, NULL);
  100. hwndLoadButton = CreateWindow("BUTTON", "Load...", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
  101. 0, 0, 0, 0, hWnd, (HMENU)IDC_LOAD_BUTTON, hInst, NULL);
  102. hwndShellcodeEdit = CreateWindow("EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | ES_READONLY | ES_LEFT | ES_MULTILINE,
  103. 0, 0, 0, 0, hWnd, (HMENU)IDC_SHELLCODE_EDIT, hInst, NULL);
  104. ResizeControls(hwndFilenameEdit, hwndLoadButton, hwndShellcodeEdit, hWnd);
  105. break;
  106. case WM_SIZE:
  107. ResizeControls(hwndFilenameEdit, hwndLoadButton, hwndShellcodeEdit, hWnd);
  108. break;
  109. case WM_COMMAND:
  110. wmId = LOWORD(wParam);
  111. wmEvent = HIWORD(wParam);
  112. // Analyse les sélections de menu :
  113. switch(wmId)
  114. {
  115. case IDC_LOAD_BUTTON:
  116. OnButtonClick(hwndFilenameEdit, hwndShellcodeEdit, hWnd);
  117. break;
  118. default:
  119. return DefWindowProc(hWnd, message, wParam, lParam);
  120. }
  121. break;
  122. case WM_PAINT:
  123. hdc = BeginPaint(hWnd, &ps);
  124. // TODO: ajoutez ici le code de dessin...
  125. EndPaint(hWnd, &ps);
  126. break;
  127. case WM_DESTROY:
  128. PostQuitMessage(0);
  129. break;
  130. default:
  131. return DefWindowProc(hWnd, message, wParam, lParam);
  132. }
  133.  
  134. return 0;
  135. }
  136.  
  137. void ResizeControls(HWND hwndFilenameEdit, HWND hwndLoadButton, HWND hwndShellcodeEdit, HWND hWnd)
  138. {
  139. RECT rcClient;
  140.  
  141. GetClientRect(hWnd, &rcClient);
  142.  
  143. MoveWindow(hwndFilenameEdit, 15, 15, rcClient.right - rcClient.left - (15 + 80 + 15 + 15), 25, TRUE);
  144.  
  145. MoveWindow(hwndLoadButton, rcClient.right - (15 + 80), 15, 80, 25, TRUE);
  146.  
  147. MoveWindow(hwndShellcodeEdit, 15, 15 + 25 + 15, rcClient.right - rcClient.left - (15 + 15),
  148. rcClient.bottom - rcClient.top - (15 + 25 + 15 + 15), TRUE);
  149. }
  150.  
  151. void OnButtonClick(HWND hwndFilenameEdit, HWND hwndShellcodeEdit, HWND hWnd)
  152. {
  153. OPENFILENAME ofn;
  154. TCHAR szFile[1024];
  155.  
  156. ZeroMemory(&ofn, sizeof(ofn));
  157. ofn.lStructSize = sizeof(ofn);
  158. ofn.hwndOwner = hWnd;
  159. ofn.lpstrFile = szFile;
  160. ofn.lpstrFile[0] = '\0';
  161. ofn.nMaxFile = sizeof(szFile);
  162. ofn.lpstrFilter = TEXT("Executable Files\0*.exe\0\0");
  163. ofn.nFilterIndex = 1;
  164. ofn.lpstrFileTitle = NULL;
  165. ofn.nMaxFileTitle = 0;
  166. ofn.lpstrInitialDir = NULL;
  167. ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  168.  
  169. if(GetOpenFileName(&ofn))
  170. {
  171. Edit_SetText(hwndFilenameEdit, ofn.lpstrFile);
  172. return GetShellcode(hWnd, hwndShellcodeEdit, ofn.lpstrFile);
  173. }
  174. }
  175.  
  176. void GetShellcode(HWND hWnd, HWND hwndShellcodeEdit, LPSTR lpstrFile)
  177. {
  178. PFILE pfile = NULL;
  179. IMAGE_DOS_HEADER iDosHeader;
  180. IMAGE_NT_HEADERS iNtHeaders;
  181. IMAGE_SECTION_HEADER iSectionHeader;
  182.  
  183. pfile = fopen(lpstrFile, "rb");
  184.  
  185. if(pfile == NULL)
  186. {
  187. MessageBox(hWnd, "Impossible d'ouvrir le fichier.", "Erreur", MB_OK | MB_ICONERROR);
  188. return;
  189. }
  190.  
  191. fread(&iDosHeader, sizeof(IMAGE_DOS_HEADER), 1, pfile);
  192.  
  193. fseek(pfile, iDosHeader.e_lfanew, SEEK_SET);
  194.  
  195. fread(&iNtHeaders, sizeof(IMAGE_NT_HEADERS), 1, pfile);
  196.  
  197. for(WORD w = 0; w < iNtHeaders.FileHeader.NumberOfSections; w++)
  198. {
  199. fread(&iSectionHeader, sizeof(IMAGE_SECTION_HEADER), 1, pfile);
  200.  
  201. if(!strcmp((char*)iSectionHeader.Name, ".text"))
  202. {
  203. return DumpTextSegment(pfile, iSectionHeader, hwndShellcodeEdit, hWnd);
  204. }
  205. }
  206.  
  207. MessageBox(hWnd, "Impossible de trouver le segment text.", "Erreur", MB_OK | MB_ICONERROR);
  208.  
  209. return;
  210. }
  211.  
  212. void DumpTextSegment(PFILE pfile, IMAGE_SECTION_HEADER iSectionHeader, HWND hwndShellcodeEdit, HWND hWnd)
  213. {
  214. BYTE by = 0;
  215. int nLength = 0;
  216. char szText[5];
  217.  
  218. fseek(pfile, iSectionHeader.PointerToRawData, SEEK_SET);
  219.  
  220. for(DWORD dw = 0; dw < iSectionHeader.Misc.VirtualSize; dw++)
  221. {
  222. fread(&by, sizeof(BYTE), 1, pfile);
  223. sprintf(szText, "\\x%.2X", by);
  224.  
  225. nLength = Edit_GetTextLength(hwndShellcodeEdit);
  226. Edit_SetSel(hwndShellcodeEdit, nLength, nLength);
  227. Edit_ReplaceSel(hwndShellcodeEdit, szText);
  228. }
  229.  
  230. fclose(pfile);
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement