Código: Seleccionar todo

/* 
   [Get size of file to base of PE] 
   - Programmer: The Swash
   - Dedicated to: Zero, Thor, Psymera, Steve10120
*/

#include <windows.h>
#include <stdio.h>
#define RB "rb"

int PEFileSize(char * fPath);


int main(void)
{
    printf("File size: %i", PEFileSize("C:\\test.exe"));
    getchar();
} 

int PEFileSize(char * fPath)
{
    IMAGE_DOS_HEADER IDH;
    IMAGE_NT_HEADERS INH;
    IMAGE_SECTION_HEADER ISH;
    FILE * lpFile;
    int sTemp = 0, i;
    
    lpFile = fopen(fPath,RB);
    if (lpFile != NULL)
    {
               fseek(lpFile, 0, SEEK_SET); // Seek to begin of file
               fread(&IDH, sizeof(IDH), 1, lpFile); // Read 64 bytes to IDH struct
               if (IDH.e_magic == IMAGE_DOS_SIGNATURE) // If IDH.e_magic = (MZ) 
               {
                               fseek(lpFile, IDH.e_lfanew, SEEK_SET); // Seek in file in begin of NT Headers (PE)
                               fread(&INH, sizeof(INH), 1, lpFile); // Read in structure 248 bytes 
                               if (INH.Signature == IMAGE_NT_SIGNATURE) // If INH.Signature = (PE)
                               {
                                                 for (i = 0; i < INH.FileHeader.NumberOfSections; i++) // go for all sections
                                                 {
                                                     fseek(lpFile, IDH.e_lfanew + sizeof(INH) + sizeof(ISH)*i, SEEK_SET); // Seek in actual section
                                                     fread(&ISH, sizeof(ISH), 1, lpFile); // Read section
                                                     sTemp += ISH.SizeOfRawData; // Save sizeofrawdata of section
                                                 }
                                                 sTemp += INH.OptionalHeader.SizeOfHeaders;
                                                 fclose(lpFile);
                                                 return sTemp;
                               }
               }
               else
               {
                   return -1;
               }
    }
    else
    {
        return -1;
    }
}
Get file size based in PE data.
Un saludo a mi amigo Linkgl, que uso algo parecido en una función suya, pero códigos provienen de distintas ideas =)
En tu ventana
Y en tu ventana, gritas al cielo pero lo dices callada..
Buena función podría adaptar la de detectar_eof que hize para que devuelva el peso porque también lo saca, xD solo te recomiendo para que no hagas dos if's lee el IMAGE_DOS_HEADER e IMAGE_NT_HEADERS y despues de leer ambos haces la comparación para ver si existe el PE en una sola linea:

Código: Seleccionar todo

...
               fseek(lpFile, 0, SEEK_SET); // Seek to begin of file
               fread(&IDH, sizeof(IDH), 1, lpFile); // Read 64 bytes to IDH struct
               fseek(lpFile, IDH.e_lfanew, SEEK_SET); // Seek in file in begin of NT Headers (PE)
               fread(&INH, sizeof(INH), 1, lpFile); // Read in structure 248 bytes
               if (IDH.e_magic==IMAGE_DOS_SIGNATURE && INH.Signature == IMAGE_NT_SIGNATURE)
               {
...
y bueno yo no haría un for mas bien leería la IMAGE_SECTION_HEADER posicionandome con fseek como haz estado haciendo y despues al final sumar el PointerToRawData + SizeOfRawData así me evito yo el bucle pero igual así como haces también funciona :P
//mHmm..
.
@The Swash:
Exelente control de las secciones Aton del formato PE
* No quites los if() como dice linkgl.
* Piensale un poco por que estas dejando abierto en algunos casos el archivo.

Dulces Lunas!¡.
Web: http://infrangelux.sytes.net/
ScanX: http://ScanX.sytes.net/
FileX: http://FileX.sytes.net/
Blog: http://BlogX.sytes.net/

Imagen


The Dark Shadow is my passion.
El infierno es mi Hogar, mi novia es Lilith y el metal mi religion
.
Rearme la función a mi gusto.

viewtopic.php?f=36&t=29716&p=265026#p265026

Código: Seleccionar todo

http://www.indetectables.net/foro/viewtopic.php?f=36&t=29716&p=265026#p265026
@The Swash
P.D.: Estas usando mal fread().

Ducles Lunas!¡.
Web: http://infrangelux.sytes.net/
ScanX: http://ScanX.sytes.net/
FileX: http://FileX.sytes.net/
Blog: http://BlogX.sytes.net/

Imagen


The Dark Shadow is my passion.
El infierno es mi Hogar, mi novia es Lilith y el metal mi religion
Responder

Volver a “Fuentes”