Advertisement
waliedassar

Legacy Dos Header Check

May 25th, 2012
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.81 KB | None | 0 0
  1. //http://waleedassar.blogspot.com
  2. //http://www.twitter.com/waleedassar
  3.  
  4.  
  5.     HANDLE h=CreateFile(buffer,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
  6.     if(h==INVALID_HANDLE_VALUE) return false;
  7.     unsigned long FileSize=GetFileSize(h,0);
  8.     if(FileSize<0x40)
  9.     {
  10.                   CloseHandle(h);
  11.           return false;
  12.     }
  13.         unsigned long DOSFileSize=0;
  14.         IMAGE_DOS_HEADER DOS={0};
  15.     unsigned long read;
  16.     ReadFile(h,&DOS,0x40,&read,0);
  17.         IMAGE_DOS_HEADER* pDos=&DOS;
  18.     if(pDos->e_cblp==0)        DOSFileSize=(pDos->e_cp)*512;
  19.         else                       DOSFileSize= ((pDos->e_cp-1)*512)+pDos->e_cblp;
  20.     if(FileSize<DOSFileSize)
  21.     {
  22.         CloseHandle(h);
  23.         return false;
  24.     }
  25.     if(pDos->e_lfarlc>DOSFileSize)
  26.     {
  27.         CloseHandle(h);
  28.         return false;
  29.     }
  30.     CloseHandle(h);
  31.     return true;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement