-
Notifications
You must be signed in to change notification settings - Fork 0
/
elf.c
178 lines (166 loc) · 4.3 KB
/
elf.c
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <unistd.h>
#include <string.h>
#include <malloc.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ptrace.h>
#include "elf.h"
#include "global.h"
#include "injector.h"
int GetElfEhdr(ELFHEADER *pElfHeader,int fd)
{
char *buf=(char*)pElfHeader;
if(fd<0)
return -1;
int nread;
int toread=sizeof(ELFHEADER);
while(toread>0)
{
nread=read(fd,buf,toread);
if(nread<0)
break;
toread-=nread;
buf=buf+nread;
}
if(toread==0)
{
return toread;
}
return -1;
}
int GetElfPhdr(ELFPHDR *pElfPhdr,int fd,unsigned size,int start)
{
char *buf=(char*)pElfPhdr;
if(fd<0)
return -1;
int nread;
unsigned int toread=size;
lseek(fd,start,SEEK_SET);
while(toread>0)
{
nread=read(fd,buf,toread);
if(nread<0)
break;
toread-=nread;
buf=buf+nread;
}
if(toread==0)
{
return toread;
}
return -1;
}
void GetMapAddr(ELFPHDR *pElfPhdr,int numSegment,char **pStart,char **pEnd)
{
char *minStart,*maxEnd;
minStart=(char*)ADDR_MAX;
maxEnd=(char*)ADDR_MIN;
int i;
for(i=0;i<numSegment;i++)
{
if((pElfPhdr+i)->p_type==LOAD)
{
minStart=MIN(minStart,PAGESTART((pElfPhdr+i)->p_vaddr,PAGE_SIZE));
maxEnd=MAX(maxEnd,PAGEEND((pElfPhdr+i)->p_vaddr+(pElfPhdr+i)->p_memsz,PAGE_SIZE));
}
}
*pStart=minStart;
*pEnd=maxEnd;
}
unsigned char *MapFunc(unsigned char *addr,unsigned int length,int prot,
int flags,int fd,unsigned int offset,pid_t pid)
{
unsigned char *libBase;
unsigned int *mapCode=(unsigned int*)malloc(MAX_CODE_SIZE*sizeof(int));
unsigned int codeSize;
GenerateMapCode(mapCode,&codeSize,addr,length,prot,flags,fd,offset);
libBase=(unsigned char *)RunCode(mapCode,codeSize,pid);
free(mapCode);
return libBase;
}
unsigned char *ProtFunc(unsigned char *addr,unsigned int length,int prot,pid_t pid)
{
unsigned char *ret;
unsigned int *protCode=(unsigned int*)malloc(MAX_CODE_SIZE*sizeof(int));
unsigned int codeSize;
GenerateMprotCode(protCode,&codeSize,addr,length,prot);
ret=RunCode(protCode,codeSize,pid);
free(protCode);
return ret;
}
unsigned char *UnmapFunc(unsigned char *addr,unsigned int length,pid_t pid)
{
unsigned char *ret;
unsigned int *unMapCode=(unsigned int*)malloc(MAX_CODE_SIZE*sizeof(int));
unsigned int codeSize;
GenerateUnmapCode(unMapCode,&codeSize,addr,length);
ret=RunCode(unMapCode,codeSize,pid);
free(unMapCode);
return ret;
}
unsigned char *ReadFunc(unsigned char *addr,unsigned int length,pid_t pid,int fd)
{
unsigned char *ret;
unsigned int *unMapCode=(unsigned int*)malloc(MAX_CODE_SIZE*sizeof(int));
unsigned int codeSize;
GenerateReadCode(unMapCode,&codeSize,fd,addr);
ret=RunCode(unMapCode,codeSize,pid);
free(unMapCode);
return ret;
}
unsigned int MapSegment(ELFPHDR *pElfPhdr,ELFHEADER *pElfHeader,int numSegment,
unsigned int imageSize,unsigned char *start,int fd,pid_t pid,int libFd)
{
int r,i;
unsigned int delta;
unsigned char *libBase,*lastEnd,*libEnd;
libBase=MapFunc(NULL,imageSize,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS,-1,0,pid);
libEnd=libBase+imageSize;
delta=libBase-start;
lastEnd=libBase;
for(i=0;i<numSegment;i++)
{
unsigned char *segBase,*segEnd;
unsigned int segSize;
unsigned int pgOffs;
ELFPHDR *tempHdr=pElfPhdr+i;
if(tempHdr->p_type==LOAD)
{
segBase=PAGESTART(tempHdr->p_vaddr,PAGE_SIZE)+delta;
segEnd=PAGEEND(tempHdr->p_vaddr+tempHdr->p_filesz,PAGE_SIZE)+delta;
segSize=segEnd-segBase;
if(segBase!=lastEnd)
{
unsigned int holeSize=segBase-lastEnd;
ProtFunc(lastEnd,holeSize,PROT_NONE,pid);
}
pgOffs=PAGESTART(tempHdr->p_offset,PAGE_SIZE);
//UnmapFunc(segBase,segSize,pid);
MapFunc(segBase,segSize,tempHdr->p_flags|PROT_WRITE|PROT_READ,
MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS,-1,0,pid);
unsigned char *data=(unsigned char*)malloc(segSize*sizeof(char));
memset(data,0,segSize);
lseek(libFd,pgOffs,SEEK_SET);
unsigned int toRead=segSize;
unsigned int n,nRead=0;
while(toRead!=0)
{
n=read(libFd,data+nRead,toRead);
toRead=toRead-n;
nRead=nRead+n;
}
PtraceWriteMemory(pid,(void *)segBase,(void *)data,segSize);
/* int i;
unsigned int *src=(unsigned int *)(data+pElfHeader->e_entry);
for(i=0;i<10;i++)
printf("%x\n",src[i]);
*/
free(data);
segEnd=PAGEEND(tempHdr->p_vaddr+tempHdr->p_memsz,PAGE_SIZE)+delta;
segSize=segEnd-segBase;
ProtFunc(segBase,segSize,tempHdr->p_flags,pid);
lastEnd=segEnd;
}
}
return delta;
}