Advertisement
Guest User

Untitled

a guest
May 14th, 2016
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <windows.h>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.     if (argc != 2)
  8.     {
  9.         printf ("Usage: filename\n");
  10.         return 0;
  11.     }
  12.  
  13.     TCHAR *filename = argv[1];
  14.     HANDLE hFile = CreateFile (filename, FILE_ALL_ACCESS, 0, 0,
  15.                                OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  16.     if (hFile == INVALID_HANDLE_VALUE)
  17.     {
  18.         perror ("Cant open file given\n");
  19.         return 0;
  20.     }
  21.  
  22.     DWORD dwFileSize = GetFileSize(hFile, NULL);
  23.     if (dwFileSize == INVALID_FILE_SIZE)
  24.     {
  25.         perror ("GetFileSize failed\n");
  26.         CloseHandle(hFile);
  27.         return 0;
  28.     }
  29.  
  30.     HANDLE hMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
  31.     if (hMapping == NULL)
  32.     {
  33.         perror ("CreateFileMapping failed\n");
  34.         CloseHandle(hFile);
  35.         return 0;
  36.     }
  37.  
  38.     unsigned char *dataPtr = (unsigned char*) MapViewOfFile(hMapping,
  39.                                                            FILE_MAP_READ,
  40.                                                            0,
  41.                                                            0,
  42.                                                            dwFileSize);
  43.     if (dataPtr == NULL)
  44.     {
  45.         perror ("MapViewOfFile failed\n");
  46.         CloseHandle(hMapping);
  47.         CloseHandle(hFile);
  48.         return 0;
  49.     }
  50.  
  51.     IMAGE_DOS_HEADER dos_header;
  52.     memcpy (&dos_header, dataPtr, sizeof (IMAGE_DOS_HEADER));
  53.  
  54.     IMAGE_NT_HEADERS nt_headers;
  55.     memcpy (&nt_headers, dataPtr + dos_header.e_lfanew, sizeof (IMAGE_NT_HEADERS));
  56.  
  57.     PIMAGE_SECTION_HEADER section_header = IMAGE_FIRST_SECTION (&nt_headers);
  58.     int i;
  59.     for (i = 0; i < nt_headers.FileHeader.NumberOfSections; i++, section_header++)
  60.     {
  61.         char name[9] = {0};
  62.         memcpy (name, section_header->Name, 8);
  63.         printf ("Section name: %s\n=======================\n", name);
  64.         printf ("Virtual size : %x\n", section_header->Misc.VirtualSize);
  65.         printf ("Raw size: %x\n", section_header->SizeOfRawData);
  66.         printf ("Virtual address: %x\n", section_header->VirtualAddress);
  67.         printf ("Raw address: %x\n", section_header->PointerToRawData);
  68.        
  69.         printf ("Characteristics: ");
  70.         if (section_header->Characteristics & IMAGE_SCN_MEM_READ)
  71.             printf ("R ");
  72.         if (section_header->Characteristics & IMAGE_SCN_MEM_WRITE)
  73.             printf ("W ");
  74.         if (section_header->Characteristics & IMAGE_SCN_MEM_EXECUTE)
  75.             printf ("X ");
  76.         if (section_header->Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
  77.             printf ("discardable ");
  78.         if (section_header->Characteristics & IMAGE_SCN_MEM_SHARED)
  79.             printf ("shared");
  80.         printf ("\n\n");
  81.     }
  82.  
  83.     UnmapViewOfFile (dataPtr);
  84.     CloseHandle (hMapping);
  85.     CloseHandle (hFile);
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement