PnUIC
January 4th, 2011, 13:16
Hi people! I'm trying to fix the elf header of tiny-crackme ("http://crackmes.de/users/yanisto/tiny_crackme/") (http://crackmes.de/users/yanisto/tiny_crackme), I also coded a bit of c Code ad hoc for this one, but when I try to run the file the process is killed, can anyone help me? I'm a newbie on elf file format, and I'm reading this http://www.codeproject.com/KB/cpp/shared_object_injection_1.aspx
This is the code:
This is the code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <elf.h>
int main(void) {
FILE *pFile, *pFile2;
char *buffer;
unsigned int fSize, phSize;
Elf32_Ehdr elfHeader;
Elf32_Phdr progHeader;
Elf32_Off phOff;
pFile = fopen("tiny-crackme", "rb";
if(pFile == NULL)
return -1;
/* read header */
fread(&elfHeader, sizeof(Elf32_Ehdr), 1, pFile);
/* read prog header */
fseek(pFile, elfHeader.e_phoff, SEEK_SET);
fread(&progHeader, sizeof(Elf32_Phdr), 1, pFile);
/* get segment infos */
phSize = progHeader.p_filesz;
phOff = progHeader.p_offset;
/* read segment */
fseek(pFile, phOff, SEEK_SET);
buffer = (char*)malloc(phSize);
fread(buffer, phSize, 1, pFile);
fclose(pFile);
/* fix Program Header Offset*/
elfHeader.e_phoff = (Elf32_Off) sizeof(Elf32_Ehdr);
/* fix Elf header's size*/
elfHeader.e_ehsize = (Elf32_Half) sizeof(Elf32_Ehdr);
/* fix section header's number */
elfHeader.e_shoff = 0;
elfHeader.e_shnum = 0;
/* fix file offset segment */
progHeader.p_offset = (Elf32_Off)(sizeof(Elf32_Ehdr)+sizeof(Elf32_Phdr));
/* write the new elf file */
pFile2 = fopen("tiny-crackme-fix", "wb";
if(pFile2 == NULL) {
free(buffer);
return -1;
}
fwrite(&elfHeader, sizeof(Elf32_Ehdr), 1, pFile2);
fwrite(&progHeader, sizeof(Elf32_Phdr), 1, pFile2);
fwrite(buffer, phSize, 1, pFile2);
free(buffer);
fclose(pFile2);
printf("\nWork done!!\n";
return 0;
}