elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Recopilación Tutoriales y Manuales Hacking, Seguridad, Privacidad, Hardware, etc


+  Foro de elhacker.net
|-+  Seguridad Informática
| |-+  Análisis y Diseño de Malware (Moderador: fary)
| | |-+  Intento de agregar sección a ejecutable
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Intento de agregar sección a ejecutable  (Leído 4,359 veces)
fary
Moderador
***
Desconectado Desconectado

Mensajes: 1.062



Ver Perfil WWW
Intento de agregar sección a ejecutable
« en: 16 Septiembre 2011, 23:22 pm »

Ya estoi aqui de nuevo, esta vez e intentado agregar una nueva sección a un ejecutable, sin exito  :xD

este es el código que tengo:

Código
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int main()
  6. {
  7. IMAGE_DOS_HEADER dh;
  8. IMAGE_NT_HEADERS nth;
  9.  
  10. IMAGE_SECTION_HEADER * sección;
  11. IMAGE_SECTION_HEADER nSeccion;
  12.    // char * stub_dos -> Datos del STUB_DOS
  13.    // char * dSecciones -> Datos de las secciones
  14.  
  15. FILE * archivo = fopen("c:\\windows\\system32\\calc.exe","r+b");
  16.    if (archivo == NULL)
  17.    {
  18.        printf("Error al leer el archivo\n");
  19.        system("PAUSE");
  20.  
  21.        return 1;
  22.    }
  23.  
  24. fread(&dh,sizeof(dh),1,archivo); // Rellenamos IMAGE_DOS_HEADER
  25.  
  26.    char * stub_dos = (char*)malloc(dh.e_lfanew-0x40);
  27.  
  28.    fread(stub_dos,1,dh.e_lfanew-0x40,archivo); // Leemos el Stub DOS
  29.    fread(&nth,sizeof(nth),1,archivo); // leemos nt headers
  30.  
  31.    sección = (IMAGE_SECTION_HEADER*)malloc(sizeof(IMAGE_SECTION_HEADER)*nth.FileHeader.NumberOfSections);
  32.  
  33.    fread(sección,sizeof(IMAGE_SECTION_HEADER),nth.FileHeader.NumberOfSections,archivo);
  34.  
  35.    char * dSecciones = (char*)malloc(nth.OptionalHeader.SizeOfImage);
  36.    fread(dSecciones,nth.OptionalHeader.SizeOfImage,1,archivo); //leo todos los datos de las secciones.
  37.  
  38.    fclose(archivo); // terminamos de leer
  39.  
  40.    ZeroMemory(&nSeccion,sizeof(IMAGE_SECTION_HEADER));
  41.  
  42.    int A = sección[nth.FileHeader.NumberOfSections-1].VirtualAddress;
  43.    int B = sección[nth.FileHeader.NumberOfSections-1].Misc.VirtualSize;
  44.    int C = nth.OptionalHeader.SectionAlignment;
  45.  
  46.    strcpy((char*)nSeccion.Name,".fary"); // nombre de la nueva sección: .fary
  47.    nSeccion.VirtualAddress = ((A+B)/C)*C+C;// direccion Virtual
  48.    nSeccion.SizeOfRawData = 0x64; // tamaño de la sección: 100
  49.    nSeccion.PointerToRawData = sección[nth.FileHeader.NumberOfSections-1].PointerToRawData + 0x64;  // direccion fisica
  50.    nSeccion.Characteristics = 0x10000020;
  51.    nSeccion.Misc.VirtualSize = 0x64;
  52.    //nSeccion.VirtulSize
  53.  
  54.    nth.FileHeader.NumberOfSections += 1; // sumamos la nueva sección
  55.    nth.OptionalHeader.SizeOfImage += 0x64;
  56.  
  57.    FILE * nuevo = fopen("NuevaCalc.exe","wb+");
  58.  
  59.    fwrite(&dh,sizeof(dh),1,nuevo);
  60.    fwrite(stub_dos,dh.e_lfanew-0x40,1,nuevo);
  61.    fwrite(&nth,sizeof(nth),1,nuevo);
  62.    fwrite(sección,sizeof(IMAGE_SECTION_HEADER)*nth.FileHeader.NumberOfSections,1,nuevo);
  63.    fwrite(&nSeccion,sizeof(IMAGE_SECTION_HEADER),1,nuevo);
  64.    fwrite(dSecciones,nth.OptionalHeader.SizeOfImage,1,nuevo);
  65.  
  66.    char * DatosSeccion = (char*)malloc(0x64);
  67.    ZeroMemory(DatosSeccion,0x64);
  68.  
  69.    fwrite(DatosSeccion,0x64,1,nuevo);
  70.  
  71.    fclose(nuevo);
  72. system("PAUSE");
  73. return 0;
  74. }

Genera bien el nuevo archivo pero no pone bien los datos de la sección y ni que decir de ejecutarse (peta) XD. Así es como salen los datos de mi sección desde el lordPE:




haber si aguien sabe que ago mal...

saludos.
En línea

Un byte a la izquierda.
lucasluks1004

Desconectado Desconectado

Mensajes: 22


Ver Perfil
Re: Intento de agregar sección a ejecutable
« Respuesta #1 en: 18 Septiembre 2011, 18:54 pm »

Yo tengo este codigo el cual no lo he podido revisar aun pero si lo probe y es funcional ,,ve este codigo capaz puedes sacar en donde te equivocaste :
Código:
Option Explicit


Const IMAGE_DOS_SIGNATURE As Integer = &H5A4D
Const IMAGE_NT_SIGNATURE As Long = &H4550


Private Type IMAGE_DOS_HEADER
    e_magic                 As Integer
    e_cblp                  As Integer
    e_cp                    As Integer
    e_crlc                  As Integer
    e_cparhdr               As Integer
    e_minalloc              As Integer
    e_maxalloc              As Integer
    e_ss                    As Integer
    e_sp                    As Integer
    e_csum                  As Integer
    e_ip                    As Integer
    e_cs                    As Integer
    e_lfarlc                As Integer
    e_onvo                  As Integer
    e_res(0 To 3)           As Integer
    e_oemid                 As Integer
    e_oeminfo               As Integer
    e_res2(0 To 9)          As Integer
    e_lfanew                As Long
End Type


Private Type IMAGE_FILE_HEADER
    Machine                 As Integer
    NumberOfSections        As Integer
    TimeDataStamp           As Long
    PointerToSymbolTable    As Long
    NumberOfSymbols         As Long
    SizeOfOptionalHeader    As Integer
    Characteristics         As Integer
End Type


Private Type IMAGE_DATA_DIRECTORY
  VirtualAddress As Long
  isize As Long
End Type


Private Type IMAGE_OPTIONAL_HEADER32
    Magic                   As Integer
    MajorLinkerVersion      As Byte
    MinorLinkerVersion      As Byte
    SizeOfCode              As Long
    SizeOfInitalizedData    As Long
    SizeOfUninitalizedData  As Long
    AddressOfEntryPoint     As Long
    BaseOfCode              As Long
    BaseOfData              As Long
    ImageBase               As Long
    SectionAlignment        As Long
    FileAlignment           As Long
    MajorOperatingSystemVer As Integer
    MinorOperatingSystemVer As Integer
    MajorImageVersion       As Integer
    MinorImageVersion       As Integer
    MajorSubsystemVersion   As Integer
    MinorSubsystemVersion   As Integer
    Reserved1               As Long
    SizeOfImage             As Long
    SizeOfHeaders           As Long
    CheckSum                As Long
    Subsystem               As Integer
    DllCharacteristics      As Integer
    SizeOfStackReserve      As Long
    SizeOfStackCommit       As Long
    SizeOfHeapReserve       As Long
    SizeOfHeapCommit        As Long
    LoaerFlags              As Long
    NumberOfRvaAndSizes     As Long
    DataDirectory(1 To 16) As IMAGE_DATA_DIRECTORY
End Type


Private Type IMAGE_SECTION_HEADER
    Name As String * 8
    VirtualSize As Long
    VirtualAddress As Long
    SizeOfRawData As Long
    PointerToRawData As Long
    PointerToRelocations As Long
    PointerToLinenumbers As Long
    NumberOfRelocations As Integer
    NumberOfLinenumbers As Integer
    Characteristics As Long
End Type


Private Type IMAGE_NT_HEADERS
    Signature As Long
    FileHeader As IMAGE_FILE_HEADER
    OptionalHeader As IMAGE_OPTIONAL_HEADER32
End Type

Private Function Align(ByVal dwValue As Long, ByVal dwAlign As Long) As Long

If dwAlign <> 0 Then
    If dwValue Mod dwAlign <> 0 Then
        Align = (dwValue + dwAlign) - (dwValue Mod dwAlign)
        Exit Function
    End If
End If

Align = dwValue
End Function

Private Function LastSectionRaw(Sections() As IMAGE_SECTION_HEADER) As Long
Dim i As Integer
Dim ret As Long

For i = LBound(Sections) To UBound(Sections)

    If Sections(i).SizeOfRawData + Sections(i).PointerToRawData > ret Then
        ret = Sections(i).SizeOfRawData + Sections(i).PointerToRawData
    End If

Next i

LastSectionRaw = ret
End Function

Private Function LastSectionVirtual(Sections() As IMAGE_SECTION_HEADER) As Long
Dim i As Integer
Dim ret As Long

For i = LBound(Sections) To UBound(Sections)

    If Sections(i).VirtualSize + Sections(i).VirtualAddress > ret Then
        ret = Sections(i).VirtualSize + Sections(i).VirtualAddress
    End If

Next i

LastSectionVirtual = ret
End Function

Public Function AddSection(ByVal szFile As String, ByVal NewSectionName As String, _
        ByVal NewSectionSize As Long, ByVal NewSectionCharacteristics As Long, _
        ByRef dwNewSectionRVA As Long, ByVal FailIfExists As Boolean) As Long
Dim hFile As Long, hMap As Long, lpMap As Long, x As Long
Dim i As Integer, k As Integer, FF As Integer

Dim DOSHeader As IMAGE_DOS_HEADER
Dim NTHeader As IMAGE_NT_HEADERS
Dim SectionHeader() As IMAGE_SECTION_HEADER

If Len(NewSectionName) < 1 Then Exit Function
If Len(NewSectionName) > 8 Then
    NewSectionName = Left$(NewSectionName, 8)
Else
    NewSectionName = NewSectionName & String(8 - Len(NewSectionName), Chr$(0))
End If

FF = FreeFile
Open szFile For Binary As #FF

Get #FF, , DOSHeader
If DOSHeader.e_magic = IMAGE_DOS_SIGNATURE Then

    Get #FF, 1 + DOSHeader.e_lfanew, NTHeader

    If NTHeader.Signature = IMAGE_NT_SIGNATURE Then

        ReDim SectionHeader(0 To NTHeader.FileHeader.NumberOfSections - 1) As IMAGE_SECTION_HEADER

        k = NTHeader.FileHeader.NumberOfSections - 1
        x = DOSHeader.e_lfanew + 24 + NTHeader.FileHeader.SizeOfOptionalHeader

        For i = LBound(SectionHeader) To UBound(SectionHeader)
            Get #FF, 1 + x, SectionHeader(i)

            If SectionHeader(i).Name = NewSectionName And FailIfExists = True Then Exit Function

            x = x + Len(SectionHeader(i))
        Next i

        If NTHeader.OptionalHeader.SizeOfHeaders >= x + Len(SectionHeader(0)) Then

            NTHeader.FileHeader.NumberOfSections = NTHeader.FileHeader.NumberOfSections + 1
            ReDim Preserve SectionHeader(0 To NTHeader.FileHeader.NumberOfSections - 1) As IMAGE_SECTION_HEADER

            With SectionHeader(NTHeader.FileHeader.NumberOfSections - 1)
                .Name = NewSectionName

                .Characteristics = NewSectionCharacteristics

                .PointerToRawData = Align(LastSectionRaw(SectionHeader), NTHeader.OptionalHeader.FileAlignment)
                .SizeOfRawData = Align(NewSectionSize, NTHeader.OptionalHeader.FileAlignment)

                .VirtualAddress = Align(LastSectionVirtual(SectionHeader), NTHeader.OptionalHeader.SectionAlignment)
                .VirtualSize = NewSectionSize
            End With

            'write new section
            NTHeader.OptionalHeader.DataDirectory(12).VirtualAddress = 0
            NTHeader.OptionalHeader.DataDirectory(12).isize = 0

            NTHeader.OptionalHeader.SizeOfImage = NTHeader.OptionalHeader.SizeOfImage + SectionHeader(k + 1).VirtualSize

            Put #FF, 1 + DOSHeader.e_lfanew, NTHeader
            Put #FF, 1 + x, SectionHeader(k + 1)

            Put #FF, SectionHeader(k + 1).PointerToRawData + SectionHeader(k + 1).SizeOfRawData, Chr$(0)
            AddSection = SectionHeader(k + 1).PointerToRawData
        End If
    End If
End If

Close #FF

End Function


Código:
Function AddFile2File(File2BeAdded As String, File2Add2 As String, SectionName As String)
Dim dwSettingsRVA As Long, dwSettingsRaw As Long
Dim Settings As String
Settings = (ReadFile(File2BeAdded))
dwSettingsRaw = AddSection(File2Add2, SectionName, Len(Settings), &HC0000040, dwSettingsRVA, True)
Open File2Add2 For Binary Access Write As #1
Put #1, dwSettingsRaw + 1, Settings
Close #1
End Function



Para llamarlo usa
Código:
Call AddFile2File(Text1.Text, App.Path & "\salida.exe", ".comoquieras")


Suerte...
En línea

fary
Moderador
***
Desconectado Desconectado

Mensajes: 1.062



Ver Perfil WWW
Re: Intento de agregar sección a ejecutable
« Respuesta #2 en: 18 Septiembre 2011, 20:15 pm »

...

pffff... es qeu mi problema creo que esta en cosa de punteros o algo así  :-\ y no lo logro encontrar.

saludos.
En línea

Un byte a la izquierda.
fary
Moderador
***
Desconectado Desconectado

Mensajes: 1.062



Ver Perfil WWW
Re: Intento de agregar sección a ejecutable
« Respuesta #3 en: 6 Octubre 2011, 00:37 am »

Tras seguir intentando y intentando no logro arreglarlo del todo (aunque lo otro ya lo arregle), aora parece que todo esta bien pero ejecuto el exe y no me funciona :S de seguro es un error tonto y minúsculo pero no logro dar con él.

Código
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. DWORD align(DWORD number, DWORD alignment)
  6. {
  7.  if(number % alignment == 0)
  8.    return number;
  9.  else
  10.    return (number / alignment) * alignment + alignment;
  11. }
  12.  
  13. int main()
  14. {
  15. IMAGE_DOS_HEADER dh;
  16. IMAGE_NT_HEADERS nth;
  17.  
  18. IMAGE_SECTION_HEADER * sección;
  19. IMAGE_SECTION_HEADER nSeccion;
  20.    // char * stub_dos -> Datos del STUB_DOS
  21.    // char * dSecciones -> Datos de las secciones
  22.    long TamanoSecciones = 0;
  23.    int i;
  24.  
  25. FILE * archivo = fopen("c:\\Mad.exe","r+b");
  26.    if (archivo == NULL)
  27.    {
  28.        printf("Error al leer el archivo\n");
  29.        system("PAUSE");
  30.  
  31.        return 1;
  32.    }
  33.  
  34. fread(&dh,sizeof(dh),1,archivo); // Rellenamos IMAGE_DOS_HEADER
  35.  
  36.    char * stub_dos = (char*)malloc(dh.e_lfanew-0x40);
  37.  
  38.    fread(stub_dos,1,dh.e_lfanew-0x40,archivo); // Leemos el Stub DOS
  39.    fread(&nth,sizeof(nth),1,archivo); // leemos nt headers
  40.  
  41.    sección = (IMAGE_SECTION_HEADER*)malloc(sizeof(IMAGE_SECTION_HEADER)*nth.FileHeader.NumberOfSections);
  42.  
  43.    fread(sección,sizeof(IMAGE_SECTION_HEADER),nth.FileHeader.NumberOfSections,archivo);
  44.  
  45.    for (i=0;i<=nth.FileHeader.NumberOfSections-1;i++)
  46.    {
  47.        printf("%i\n",sección[i].SizeOfRawData);
  48.        TamanoSecciones += sección[i].SizeOfRawData;
  49.    }
  50.  
  51.    printf("%i\n",TamanoSecciones);
  52.  
  53.    char * dSecciones = (char*)malloc(TamanoSecciones);
  54.    fread(dSecciones,TamanoSecciones,1,archivo); //leo todos los datos de las secciones.
  55.  
  56.    fclose(archivo); // terminamos de leer
  57.  
  58.    strcpy((char*)nSeccion.Name,".fary"); // nombre de la nueva sección: .fary
  59.    nSeccion.VirtualAddress = align(sección[nth.FileHeader.NumberOfSections-1].VirtualAddress + sección[nth.FileHeader.NumberOfSections-1].Misc.VirtualSize, nth.OptionalHeader.SectionAlignment);
  60.    nSeccion.SizeOfRawData = align(0x50, nth.OptionalHeader.FileAlignment);
  61.    nSeccion.PointerToRawData = sección[nth.FileHeader.NumberOfSections-1].PointerToRawData + sección[nth.FileHeader.NumberOfSections-1].SizeOfRawData;//align(0x50,nth.OptionalHeader.FileAlignment);
  62.    nSeccion.Characteristics = 0x60000020;
  63.    nSeccion.Misc.VirtualSize = 0x50;
  64.  
  65.    nth.FileHeader.NumberOfSections += 1; // sumamos la nueva sección
  66.    nth.OptionalHeader.SizeOfImage = align(0x50+nSeccion.VirtualAddress,nth.OptionalHeader.SectionAlignment);
  67.    nth.OptionalHeader.SizeOfHeaders += 0x28;
  68.  
  69.  
  70.    FILE * nuevo = fopen("Nuevo.exe","wb+");
  71.  
  72.    fwrite(&dh,sizeof(dh),1,nuevo);
  73.    fwrite(stub_dos,dh.e_lfanew-0x40,1,nuevo);
  74.    fwrite(&nth,sizeof(nth),1,nuevo);
  75.    nth.FileHeader.NumberOfSections -= 1;
  76.    fwrite(sección,sizeof(IMAGE_SECTION_HEADER)*nth.FileHeader.NumberOfSections,1,nuevo);
  77.    fwrite(&nSeccion,sizeof(IMAGE_SECTION_HEADER),1,nuevo);
  78.    fwrite(dSecciones,TamanoSecciones,1,nuevo);
  79.    if (fseek(nuevo,0,SEEK_END) != 0)
  80.    {
  81.        printf("error\n");
  82.    }
  83.  
  84.    char DatosSeccion[] = "Hola yo soy la sección de prueba, y ocupo exactamente la cantidad de 0x50 bytes.";
  85.    fwrite(DatosSeccion,0x50,1,nuevo);
  86.  
  87.    fclose(nuevo);
  88. system("PAUSE");
  89. return 0;
  90. }
  91.  

Si alguien es tan amable de hecharle un ojo y explicarme porque no anda le estaría muy agradecido ;)

saludos.
« Última modificación: 6 Octubre 2011, 17:45 pm por Drinky94 » En línea

Un byte a la izquierda.
karmany
Colaborador
***
Desconectado Desconectado

Mensajes: 1.614


Sueñas que sueñas


Ver Perfil WWW
Re: Intento de agregar sección a ejecutable
« Respuesta #4 en: 7 Octubre 2011, 00:24 am »

qué error te sale ahora?

Cuando añades una nueva sección, en la misma no añades ningún byte, entonces no entiendo por qué pones:
 nSeccion.SizeOfRawData = align(0x50, nth.OptionalHeader.FileAlignment);
alinearlo a filealignement se hace si realmente tienes bytes. Ponlo a cero directamente. (no he revisado completamente tu código, pero creo que no añades bytes, si me equivoco dímelo)

Además cuando añades una sección, realmente lo que haces es modificar los bytes en el encabezado pero no añades ninguno a no ser que lo hagas de verdad, como hago con Add PE bytes.

Mi código es muy diferente al tuyo y me lo tengo que leer entero para saber dónde has cometido el error.

Haz pruebas con tu programa y con LordPE y examinando los datos de la sección añadida verifica dónde está el error.
En línea

fary
Moderador
***
Desconectado Desconectado

Mensajes: 1.062



Ver Perfil WWW
Re: Intento de agregar sección a ejecutable
« Respuesta #5 en: 7 Octubre 2011, 16:42 pm »

Si añado datos... añado una pequeña cadena  :P. el problema está en que genera el ejecutable con menos peso y cambia datos de las secciones (osea el contenido) pero nose porque lo hace  :-\

saludos.
En línea

Un byte a la izquierda.
The Swash

Desconectado Desconectado

Mensajes: 194


Programmer


Ver Perfil WWW
Re: Intento de agregar sección a ejecutable
« Respuesta #6 en: 7 Octubre 2011, 17:59 pm »

Código
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. DWORD align(DWORD number, DWORD alignment)
  6. {
  7.    if(number % alignment == 0)
  8.       return number;
  9.    else
  10.       return (number / alignment) * alignment + alignment;
  11. }
  12.  
  13. int main()
  14. {
  15.   IMAGE_DOS_HEADER dh;
  16.   IMAGE_NT_HEADERS nth;
  17.  
  18.   IMAGE_SECTION_HEADER * sección;
  19.   IMAGE_SECTION_HEADER nSeccion;
  20.    // char * stub_dos -> Datos del STUB_DOS
  21.    // char * dSecciones -> Datos de las secciones
  22.   long TamanoSecciones = 0;
  23.   int i;
  24.  
  25.   FILE * archivo = fopen("c:\\Mad.exe","r+b");
  26.   if (archivo == NULL)
  27.   {
  28.       printf("Error al leer el archivo\n");
  29.       getchar();
  30.  
  31.       return 1;
  32.   }
  33.  
  34.   fread(&dh,sizeof(dh),1,archivo); // Rellenamos IMAGE_DOS_HEADER
  35.  
  36.   char * stub_dos = (char*)malloc(dh.e_lfanew-0x40);
  37.  
  38.   fread(stub_dos,1,dh.e_lfanew-0x40,archivo); // Leemos el Stub DOS
  39.   fread(&nth,sizeof(nth),1,archivo); // leemos nt headers
  40.  
  41.   sección = (IMAGE_SECTION_HEADER*)malloc(sizeof(IMAGE_SECTION_HEADER)*nth.FileHeader.NumberOfSections);
  42.  
  43.   fread(sección,sizeof(IMAGE_SECTION_HEADER),nth.FileHeader.NumberOfSections,archivo);
  44.  
  45.   /*for (i=0;i<=nth.FileHeader.NumberOfSections-1;i++)
  46.    {
  47.        printf("%i\n",sección[i].SizeOfRawData);
  48.        TamanoSecciones += sección[i].SizeOfRawData;
  49.    }*/
  50.   TamanoSecciones =  (sección[nth.FileHeader.NumberOfSections-1].PointerToRawData + sección[nth.FileHeader.NumberOfSections-1].SizeOfRawData) - sección[0].PointerToRawData;
  51.  
  52.   fseek(archivo, dh.e_lfanew + 248 + (nth.FileHeader.NumberOfSections * 0x28)+0x28, SEEK_SET);
  53.  
  54.   char * dSecciones = (char*)malloc(TamanoSecciones + (nth.OptionalHeader.SizeOfHeaders - ftell(archivo)));
  55.   int HeaderSize = nth.OptionalHeader.SizeOfHeaders - ftell(archivo);
  56.   char * Header = (char*) malloc(HeaderSize);
  57.   memset(Header,0, HeaderSize);
  58.   memset(dSecciones, 0, TamanoSecciones);
  59.   fread(Header, HeaderSize,1, archivo);
  60.   fread(dSecciones,TamanoSecciones,1,archivo); //leo todos los datos de las secciones.
  61.  
  62.   fclose(archivo); // terminamos de leer
  63.  
  64.   memset(&nSeccion,0, 0x28);
  65.   strncpy((char*)nSeccion.Name,".fary",5); // nombre de la nueva sección: .fary
  66.   nSeccion.VirtualAddress = align(sección[nth.FileHeader.NumberOfSections-1].VirtualAddress + sección[nth.FileHeader.NumberOfSections-1].Misc.VirtualSize, nth.OptionalHeader.SectionAlignment);
  67.   nSeccion.SizeOfRawData = align(0x50, nth.OptionalHeader.FileAlignment);
  68.   nSeccion.PointerToRawData = sección[nth.FileHeader.NumberOfSections-1].PointerToRawData + sección[nth.FileHeader.NumberOfSections-1].SizeOfRawData;//align(0x50,nth.OptionalHeader.FileAlignment);
  69.   nSeccion.Characteristics = 0x60000020;
  70.   nSeccion.Misc.VirtualSize = 0x50;
  71.  
  72.   nth.FileHeader.NumberOfSections += 1; // sumamos la nueva sección
  73.   nth.OptionalHeader.SizeOfImage = align(0x50+nSeccion.VirtualAddress,nth.OptionalHeader.SectionAlignment);
  74.   //nth.OptionalHeader.SizeOfHeaders += 0x28;
  75.  
  76.  
  77.   FILE * nuevo = fopen("Nuevo.exe","wb+");
  78.  
  79.   fwrite(&dh,sizeof(dh),1,nuevo);
  80.   fwrite(stub_dos,dh.e_lfanew-0x40,1,nuevo);
  81.   fwrite(&nth,sizeof(nth),1,nuevo);
  82.   nth.FileHeader.NumberOfSections -= 1;
  83.   fwrite(sección,sizeof(IMAGE_SECTION_HEADER)*nth.FileHeader.NumberOfSections,1,nuevo);
  84.   fwrite(&nSeccion,sizeof(IMAGE_SECTION_HEADER),1,nuevo);
  85.   fwrite(Header, HeaderSize,1, nuevo);
  86.   fwrite(dSecciones,TamanoSecciones,1,nuevo);
  87.   if (fseek(nuevo,0,SEEK_END) != 0)
  88.   {
  89.       printf("error\n");
  90.   }
  91.   int Size = nSeccion.SizeOfRawData;
  92.   char DatosSeccion[0x1000] = "Hola yo soy la sección de prueba, y ocupo exactamente la cantidad de 0x50 bytes.\0"; //Debe tener la cantidad de bytes del SizeOfRawData
  93.   fwrite(DatosSeccion,nSeccion.SizeOfRawData,1,nuevo);
  94.  
  95.  
  96.   fclose(nuevo);
  97.   system("PAUSE");
  98.   return 0;
  99. }

Tu primer error estaba a la hora de leer el resto de la cabecera, posterior no agregabas el tamaño del SizeOfRawData completo, de lo contrario se corrompe el exe.

Si es un ejecutable VB solo bastaría dejar BoundImport a 0 o moverlo más abajo.

« Última modificación: 7 Octubre 2011, 18:01 pm por The Swash » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Una sección de DB
Sugerencias y dudas sobre el Foro
V18s 2 1,965 Último mensaje 17 Junio 2005, 17:38 pm
por alcatel
Agregar sección a .exe
Programación Visual Basic
locot3 1 1,941 Último mensaje 8 Junio 2010, 18:12 pm
por Horricreu
Agregar bites a un ejecutable
Ingeniería Inversa
my_sistemas 3 2,690 Último mensaje 3 Febrero 2011, 20:53 pm
por apuromafo CLS
[C]-[AYUDA] Encontrar una sección en ejecutable
Programación C/C++
Miky Gonzalez 5 2,363 Último mensaje 21 Febrero 2013, 20:51 pm
por 0xDani
Agregar ejecutable a lista de permitidos del AV « 1 2 3 »
Análisis y Diseño de Malware
Vaagish 22 13,410 Último mensaje 2 Diciembre 2013, 16:44 pm
por Vaagish
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines