exploit the possibilities
Home Files News &[SERVICES_TAB]About Contact Add New

segment.c

segment.c
Posted Jul 11, 2000
Authored by Sectorx | Site xorteam.cjb.net

ELF binary segment probe will search an exectuable or core dump for a string, giving you its exact location in the memory, its segment location, offset in segment and segment type. Very useful for finding offsets for exploits.

systems | unix
SHA-256 | 6a4b0b41129db20b4294574f736da5ddb56b3543e39a8cdd256949259d6d94f1

segment.c

Change Mirror Download
/* Segment probe by sectorx of xor (http://xorteam.cjb.net)
*
* This code will search an exectuable or core dump for a string, giving you
* its exact location in the memory, its segment location, offset in segment
* and segment type.
* note: the struct 'types' may require some changing on other systems since
* I built this using the information on Linux's elf.h
*
* --sectorx (sectorx@themarines.com)
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <elf.h>
#include <fcntl.h>

#define MAX 4096
#define SEGMENT_TYPES 12
#define VERSION "1.0"

struct Description {
char name[100];
Elf32_Word num;
};

struct Description types[SEGMENT_TYPES] =
{
{"Program header table entry", PT_NULL},
{"Loadable program segment", PT_LOAD},
{"Dynamic linking information",PT_DYNAMIC},
{"Program interpreter", PT_INTERP},
{"Auxiliary information", PT_NOTE},
{"Reserved", PT_SHLIB},
{"Entry for header table itself", PT_PHDR},
{"Number of defined types", PT_NUM},
{"Start of OS-specific", PT_LOOS},
{"End of OS-specific", PT_HIOS},
{"Start of processor-specific", PT_LOPROC},
{"End of processor-specific", PT_HIPROC}
};

int Define(Elf32_Word p_type)
{
int i;
for (i=0;i<SEGMENT_TYPES;i++) if (types[i].num == p_type) return i;
return -1;
}

void Header(int fd)
{
Elf32_Ehdr hdr;

lseek(fd,0,SEEK_SET);
read(fd,&hdr,sizeof(Elf32_Ehdr));
printf("ELF Binary header:\n");
printf("ident = \"%s\"\n",hdr.e_ident);
printf("type = ");
switch (hdr.e_type) {
case 2:
printf("Exec\n");
break;
case 4:
printf("Core\n");
break;
default:
printf("Invalid\n");
exit(1);
break;
}
printf("\n");
}

int Where(int fd) { return(lseek(fd,0,SEEK_CUR)); }

int Segment(int fd, char *str)
{
Elf32_Phdr seg;
long int pos;
int type;

read(fd,&seg,sizeof(Elf32_Phdr));
type = Define(seg.p_type);
if (type<0) return 0;
if (seg.p_filesz == 0) goto Eof;

pos = Where(fd);
{
const int size = seg.p_filesz;
int i;
char buf[size];

memset(&buf,0,size);
if (lseek(fd,seg.p_offset,SEEK_SET)!=seg.p_offset) { printf("Error.\n "); goto Eof; }
if (read(fd,&buf,size)!=size) { printf("Error.\n "); goto Eof; }
for (i=0;i<size;i++) if (buf[i]==0) buf[i]=1;
buf[size-1]='\0';
if (strstr(buf, str)) {
char *ptr = strstr(buf,str);
ptr-=buf;
printf("*** Query found:\n");
printf("Segment type : %s\n", types[type].name);
printf("Segment start : 0x%x\n",seg.p_vaddr);
printf("Location : 0x%x\n",ptr+seg.p_vaddr);
printf("Offset : %d\n",ptr);
}
}
if (lseek(fd,pos,SEEK_SET)!=pos) {
perror("Unable to return to initial position ");
exit(1);
}

Eof: ;
return 1;
}

int main(int argc, char *argv[])
{
int fd;

printf("Segment probe v%s by sectorx of xor (http://xorteam.cjb.net)\n",VERSION);
printf("- thanks to duke for helping me start coding this\n\n");
if (argc < 3) {
printf("usage: %s <elf> <string to search>\n",argv[0]);
exit(1);
}

fd = open(argv[1],O_RDONLY);
if (fd<0) {
perror("open() ");
return;
}
Header(fd);
printf("Searching segments for requested string ...\n",argv[1]);
while (Segment(fd,argv[2])>0);
close(fd);
}
Login or Register to add favorites

File Archive:

April 2024

  • Su
  • Mo
  • Tu
  • We
  • Th
  • Fr
  • Sa
  • 1
    Apr 1st
    10 Files
  • 2
    Apr 2nd
    26 Files
  • 3
    Apr 3rd
    40 Files
  • 4
    Apr 4th
    6 Files
  • 5
    Apr 5th
    26 Files
  • 6
    Apr 6th
    0 Files
  • 7
    Apr 7th
    0 Files
  • 8
    Apr 8th
    22 Files
  • 9
    Apr 9th
    14 Files
  • 10
    Apr 10th
    10 Files
  • 11
    Apr 11th
    13 Files
  • 12
    Apr 12th
    14 Files
  • 13
    Apr 13th
    0 Files
  • 14
    Apr 14th
    0 Files
  • 15
    Apr 15th
    30 Files
  • 16
    Apr 16th
    10 Files
  • 17
    Apr 17th
    22 Files
  • 18
    Apr 18th
    45 Files
  • 19
    Apr 19th
    8 Files
  • 20
    Apr 20th
    0 Files
  • 21
    Apr 21st
    0 Files
  • 22
    Apr 22nd
    11 Files
  • 23
    Apr 23rd
    68 Files
  • 24
    Apr 24th
    23 Files
  • 25
    Apr 25th
    0 Files
  • 26
    Apr 26th
    0 Files
  • 27
    Apr 27th
    0 Files
  • 28
    Apr 28th
    0 Files
  • 29
    Apr 29th
    0 Files
  • 30
    Apr 30th
    0 Files

Top Authors In Last 30 Days

File Tags

Systems

packet storm

© 2022 Packet Storm. All rights reserved.

Services
Security Services
Hosting By
Rokasec
close