./c0de/c/elfchange.txt

0.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
/* elf_fsck.c -- by mort -- http://mortix.net
 *
 * A program to change the ELF identification (magic)
 * field in the program header.
 *
 * mad gr33tz to dlab
 */
 
#include <stdio.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <elf.h>
#include <string.h>
 
int main(int argc, char *argv[])  {
 
  int fd;
  Elf32_Ehdr *keke;
 
  if(argc!=3)  {
    printf("Usage: %s <file> <string>\n", argv[0]);
    return 0;
  }
 
  /* Open file */
  fd = open(argv[1], O_RDWR);
 
  if(fd < 0)  {
    printf("Error opening file...\nExiting...\n");
    return 1;
  }
 
  /* mmap file into memory */
  keke = (Elf32_Ehdr *) mmap(0,sizeof(Elf32_Ehdr), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
 
  if(keke == MAP_FAILED)  {
    printf("Unable to map file...\nExiting...\n");
    return 1;
  }
 
  /* Copy over new string */
  memcpy(keke->e_ident, argv[2], sizeof(keke->e_ident));
 
  /* Write changes to file */
  msync(keke, sizeof(Elf32_Ehdr), MS_SYNC);
 
  /* Un-map file */
  munmap(keke, sizeof(Elf32_Ehdr));
 
  /* Close file */
  close(fd);
 
  printf("Done.\n");
 
  return 0;
 
}

Back