Je vous présente ici un petit tool avec interface graphique qui liste les fonctions exportées d’une dll (comme par exemple kernel32.dll) avec possibilité d’obtenir l’adresse effective d’une fonction grâce à un menu par un clic droit sur un nom de fonction… Pratique pour les shellcodes static. 🙂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | #include <windows.h> #include <stdio.h> #include <commctrl.h> #include <windowsx.h> typedef FILE *PFILE; #define APPLICATIONNAME "List exported functions\0" #define CLASSNAME "ListExportedFunctions\0" #define IDC_FILENAME_EDIT 101 #define IDC_BUTTON 102 #define IDC_LISTVIEW 103 #define IDC_ADDRESS_EDIT 104 #define IDM_GET_NAME 201 #define IDM_GET_ADDRESS 202 HINSTANCE hInst; ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); void ResizeControls(HWND, HWND, HWND, HWND, HWND); void CreateColumn(HWND); void OnButtonClick(HWND, HWND, HWND); void InsertItems(HWND, HWND, LPSTR); void ReadCString(PFILE, LPSTR); DWORD RvaToOffset(PIMAGE_SECTION_HEADER, WORD, DWORD); void OnRightClick(HWND); void OnGetAddressClick(HWND, HWND, HWND, HWND, int); void OnGetNameClick(HWND, HWND, HWND, HWND, int); int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { MSG msg; INITCOMMONCONTROLSEX icex; icex.dwSize = sizeof(INITCOMMONCONTROLSEX); icex.dwICC = ICC_LISTVIEW_CLASSES; InitCommonControlsEx(&icex); MyRegisterClass(hInstance); if(!InitInstance(hInstance, nCmdShow)) { return FALSE; } while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL; wcex.lpszClassName = CLASSNAME; wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION); return RegisterClassEx(&wcex); } BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; hInst = hInstance; // Stocke le handle d'instance dans la variable globale. hWnd = CreateWindow(CLASSNAME, APPLICATIONNAME, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 900, 600, NULL, NULL, hInstance, NULL); if(!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; static HWND hwndFilenameEdit; static HWND hwndButton; static HWND hwndListView; static HWND hwndAddressEdit; static int iItem; switch(message) { case WM_CREATE: hwndFilenameEdit = CreateWindow("EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL | ES_READONLY | ES_LEFT, 0, 0, 0, 0, hWnd, (HMENU)IDC_FILENAME_EDIT, hInst, NULL); hwndButton = CreateWindow("BUTTON", "Load...", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 0, 0, 0, 0, hWnd, (HMENU)IDC_BUTTON, hInst, NULL); hwndListView = CreateWindow(WC_LISTVIEW, NULL, WS_CHILD | WS_VISIBLE | LVS_LIST, 0, 0, 0, 0, hWnd, (HMENU)IDC_LISTVIEW, hInst, NULL); hwndAddressEdit = CreateWindow("EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL | ES_READONLY | ES_LEFT, 0, 0, 0, 0, hWnd, (HMENU)IDC_ADDRESS_EDIT, hInst, NULL); CreateColumn(hwndListView); ResizeControls(hwndFilenameEdit, hwndButton, hwndListView, hwndAddressEdit, hWnd); break; case WM_SIZE: ResizeControls(hwndFilenameEdit, hwndButton, hwndListView, hwndAddressEdit, hWnd); break; case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Analyse les sélections de menu : switch(wmId) { case IDC_BUTTON: OnButtonClick(hWnd, hwndFilenameEdit, hwndListView); break; case IDM_GET_NAME: OnGetNameClick(hWnd, hwndFilenameEdit, hwndListView, hwndAddressEdit, iItem); break; case IDM_GET_ADDRESS: OnGetAddressClick(hWnd, hwndFilenameEdit, hwndListView, hwndAddressEdit, iItem); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_NOTIFY: switch(((LPNMHDR)lParam)->code) { case NM_RCLICK: LVHITTESTINFO lvhti; LPNMITEMACTIVATE lpnmia; lpnmia = (LPNMITEMACTIVATE)lParam; lvhti.pt = lpnmia->ptAction; if(ListView_SubItemHitTest(hwndListView, &lvhti) != -1) { iItem = lpnmia->iItem; OnRightClick(hWnd); } break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO : ajoutez ici le code de dessin... EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } void ResizeControls(HWND hwndFilenameEdit, HWND hwndButton, HWND hwndListView, HWND hwndAddressEdit, HWND hWnd) { RECT rc; GetClientRect(hWnd, &rc); MoveWindow(hwndFilenameEdit, 15, 15, rc.right - rc.left - (15 + 80 + 15 + 15), 25, TRUE); MoveWindow(hwndButton, rc.right - (15 + 80), 15, 80, 25, TRUE); MoveWindow(hwndListView, 15, 15 + 25 + 15, rc.right - rc.left - (15 + 15), rc.bottom - rc.top - (15 + 25 + 15 + 25 + 15 + 15), TRUE); MoveWindow(hwndAddressEdit, 15, rc.bottom - (15 + 25), rc.right - rc.left - (15 + 15), 25, TRUE); } void CreateColumn(HWND hwndListView) { LVCOLUMN lvc; ListView_InsertColumn(hwndListView, 0, &lvc); } void OnButtonClick(HWND hWnd, HWND hwndFilenameEdit, HWND hwndListView) { OPENFILENAME ofn; TCHAR szFile[1024]; ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = hWnd; ofn.lpstrFile = szFile; ofn.lpstrFile[0] = '\0'; ofn.nMaxFile = sizeof(szFile); ofn.lpstrFilter = TEXT("Dynamic Link Library\0*.dll\0\0"); ofn.nFilterIndex = 1; ofn.lpstrFileTitle = NULL; ofn.nMaxFileTitle = 0; ofn.lpstrInitialDir = NULL; ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; if(GetOpenFileName(&ofn)) { Edit_SetText(hwndFilenameEdit, ofn.lpstrFile); return InsertItems(hWnd, hwndListView, ofn.lpstrFile); } } void InsertItems(HWND hWnd, HWND hwndListView, LPSTR lpstrFile) { PFILE pfile = NULL; char name[1024]; DWORD namePos = -1; DWORD offsetOfEntryExport = -1; DWORD offsetOfNames = -1; DWORD offsetOfNamePos = -1; IMAGE_DOS_HEADER iDosHeader; IMAGE_NT_HEADERS iNtHeaders; PIMAGE_SECTION_HEADER piSectionHeader; IMAGE_EXPORT_DIRECTORY iExportDir; LVITEM lvI; lvI.mask = LVIF_TEXT; ListView_DeleteAllItems(hwndListView); pfile = fopen(lpstrFile, "rb"); if(pfile == NULL) { MessageBox(hWnd, "Impossible d'ouvrir le fichier.", "Erreur", MB_OK | MB_ICONERROR); return; } fread(&iDosHeader, sizeof(IMAGE_DOS_HEADER), 1, pfile); if(iDosHeader.e_magic != IMAGE_DOS_SIGNATURE) { MessageBox(hWnd, "Le fichier n'est pas valide.", "Erreur", MB_OK | MB_ICONERROR); return; } fseek(pfile, iDosHeader.e_lfanew, SEEK_SET); fread(&iNtHeaders, sizeof(IMAGE_NT_HEADERS), 1, pfile); if(iNtHeaders.Signature != IMAGE_NT_SIGNATURE) { MessageBox(hWnd, "Le fichier n'est pas valide.", "Erreur", MB_OK | MB_ICONERROR); return; } piSectionHeader = (PIMAGE_SECTION_HEADER)malloc(sizeof(IMAGE_SECTION_HEADER) * iNtHeaders.FileHeader.NumberOfSections); for(unsigned i = 0; i < iNtHeaders.FileHeader.NumberOfSections; i++) { fread(&piSectionHeader[i], sizeof(IMAGE_SECTION_HEADER), 1, pfile); } offsetOfEntryExport = RvaToOffset(piSectionHeader, iNtHeaders.FileHeader.NumberOfSections, iNtHeaders.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress); if(offsetOfEntryExport == -1) { MessageBox(hWnd, "Impossible de trouver la table des fonctions exportées.", "Erreur", MB_OK | MB_ICONERROR); return; } fseek(pfile, offsetOfEntryExport, SEEK_SET); fread(&iExportDir, sizeof(IMAGE_EXPORT_DIRECTORY), 1, pfile); offsetOfNames = RvaToOffset(piSectionHeader, iNtHeaders.FileHeader.NumberOfSections, iExportDir.AddressOfNames); if(offsetOfNames == -1) { MessageBox(hWnd, "Impossible d'aller à l'adresse des noms.", "Erreur", MB_OK | MB_ICONERROR); return; } for(DWORD i = 0; i < iExportDir.NumberOfNames; i++) { fseek(pfile, offsetOfNames + i * sizeof(DWORD), SEEK_SET); fread(&namePos, sizeof(DWORD), 1, pfile); offsetOfNamePos = RvaToOffset(piSectionHeader, iNtHeaders.FileHeader.NumberOfSections, namePos); if(offsetOfNamePos == -1) { MessageBox(hWnd, "Impossible d'aller à l'adresse du nom.", "Erreur", MB_OK | MB_ICONERROR); return; } fseek(pfile, offsetOfNamePos, SEEK_SET); ReadCString(pfile, name); lvI.iItem = i; lvI.iSubItem = 0; lvI.pszText = name; ListView_InsertItem(hwndListView, &lvI); } SetFocus(hwndListView); } DWORD RvaToOffset(PIMAGE_SECTION_HEADER piSectionHeader, WORD numberOfSections, DWORD rva) { for(WORD i = 0; i < numberOfSections; i++) { // La RVA est-elle dans cette section? if((rva >= piSectionHeader[i].VirtualAddress) && (rva < piSectionHeader[i].VirtualAddress + piSectionHeader[i].SizeOfRawData)) { rva -= piSectionHeader[i].VirtualAddress; rva += piSectionHeader[i].PointerToRawData; return rva; } } return -1; } void ReadCString(PFILE pfile, LPSTR name) { DWORD n = 0; do { fread(name+n, sizeof(char), 1, pfile); n++; }while(name[n-1] != 0 && n < 1023); name[n] = 0; } void OnRightClick(HWND hWnd) { HMENU hMenu; POINT pt; hMenu = CreatePopupMenu(); GetCursorPos(&pt); AppendMenu(hMenu, MF_STRING, IDM_GET_NAME, TEXT("Get name")); AppendMenu(hMenu, MF_STRING, IDM_GET_ADDRESS, TEXT("Get address")); TrackPopupMenu(hMenu, 0, pt.x, pt.y, 0, hWnd, NULL); } void OnGetAddressClick(HWND hWnd, HWND hwndFilenameEdit, HWND hwndListView, HWND hwndAddressEdit, int iItem) { CHAR szFile[1024]; HMODULE hMod; DWORD dwAddress; CHAR szFunc[1024]; CHAR szWorkBuff[12]; Edit_GetText(hwndFilenameEdit, szFile, 1024); hMod = LoadLibrary(szFile); if(!hMod) { MessageBox(hWnd, "Impossible de charger la dll.", "Erreur", MB_OK | MB_ICONERROR); return; } ListView_GetItemText(hwndListView, iItem, 0, szFunc, 1024); dwAddress = (DWORD)GetProcAddress(hMod, szFunc); if(!dwAddress) { MessageBox(hWnd, "Impossible d'obtenir l'adresse.", "Erreur", MB_OK | MB_ICONERROR); return; } sprintf(szWorkBuff, "0x%X", dwAddress); Edit_SetText(hwndAddressEdit, szWorkBuff); } void OnGetNameClick(HWND hWnd, HWND hwndFilenameEdit, HWND hwndListView, HWND hwndAddressEdit, int iItem) { CHAR szFunc[1024]; ListView_GetItemText(hwndListView, iItem, 0, szFunc, 1024); Edit_SetText(hwndAddressEdit, szFunc); } |