-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.c
318 lines (300 loc) · 5.9 KB
/
init.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/user.h>
#include <sys/ptrace.h>
#include <asm/ptrace.h>
#include <signal.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "global.h"
#include "injector.h"
#include "elf.h"
int OpenLibInchild(char *libPath,pid_t pid);
int LoadLibInChild(int fd,pid_t pid,char *libPath);
int ChildEntry(unsigned int start,pid_t pid);
/*load control program,modify the bin program entry to control program
*/
int ChildStartFromLib(char *libPath ,pid_t pid,int fd[2])
{
int r;
int libFd;
r=MyPtrace(PTRACE_ATTACH,pid,NULL,NULL);
if(r<0)
{
#ifdef PRINTF
printf("ptrace attach error\n");
#endif
return -1;
}
r=WaitSingal(pid,SIGSTOP);
if(r<0)
{
#ifdef LOGMESSAGE
LogMessage("no singal stop come error\n");
#endif
return -1;
}
close(fd[0]);
r=WritePipe(fd[1],"start_child");
if(r<0)
{
#ifdef LOGMESSAGE
LogMessage("write pipe error\n");
#endif
return -1;
}
close(fd[1]);
r=ptrace(PTRACE_SETOPTIONS,pid,NULL,PTRACE_O_TRACEEXEC);
if(r<0)
{
#ifdef LOGMESSAGE
LogMessage("ptrace trace exec error\n");
#endif
return -1;
}
r=MyPtrace(PTRACE_CONT,pid,NULL,NULL);
if(r<0)
{
#ifdef LOGMESSAGE
LogMessage("ptrace_cont to exec error\n");
#endif
return -1;
}
r=WaitSingal(pid,SIGTRAP);
if(r<0)
{
#ifdef LOGMESSAGE
LogMessage("wait singal sigtrap error\n");
#endif
return -1;
}
libFd=OpenLibInChild(libPath,pid);
if(libFd<0)
{
#ifdef LOGMESSAGE
LogMessage("open lib in child error\n");
#endif
return -1;
}
r=LoadLibInChild(libFd,pid,libPath);
if(r<0)
{
#ifdef LOGMESSAGE
LogMessage("load lib in child error\n");
#endif
return -1;
}
r=MyPtrace(PTRACE_CONT,pid,NULL,NULL);
// ptrace(PTRACE_SINGLESTEP,pid,NULL,0);
if(r<0)
{
#ifdef LOGMESSAGE
LogMessage("ptrace_cont start child error\n");
#endif
return -1;
}
struct pt_regs regs;
r=MyPtrace(PTRACE_GETREGS,pid,NULL,®s);
if(r<0)
printf("here\n");
printf("end:%x\n",regs.ARM_pc);
return 0;
}
int LoadLibInChild(int fd,pid_t pid,char *libPath)
{
int fdLib;
int r;
unsigned char *libStart;
unsigned char *libBase;
unsigned char *libEnd;
unsigned int delta,start;
unsigned int imageSize;
Elf32_Off phOff;
Elf32_Half phSize;
Elf32_Half phNum;
unsigned int size;
fdLib=open(libPath,O_RDONLY);
if(fdLib<0)
return -1;
ELFHEADER elfHeader;
memset(&elfHeader,0,sizeof(ELFHEADER));
if(r=GetElfEhdr(&elfHeader,fdLib)<0)
return -1;
phOff=elfHeader.e_phoff;
phSize=elfHeader.e_phentsize;
phNum=elfHeader.e_phnum;
ELFPHDR elfPhdr[phNum];
memset(elfPhdr,0,sizeof(ELFPHDR)*phNum);
size=phSize*phNum;
GetElfPhdr(elfPhdr,fdLib,size,phOff);
GetMapAddr(elfPhdr,phNum,&libStart,&libEnd);
imageSize=libEnd-libStart;
delta=MapSegment(elfPhdr,&elfHeader,phNum,imageSize,libStart,fd,pid,fdLib);
if(delta<0)
{
printf("run delta\n");
return -1;
}
start=elfHeader.e_entry+delta;
r=ChildEntry(start,pid);
if(r<0)
return -1;
return 0;
}
int ChildEntry(unsigned int start,pid_t pid)
{
/*int r;
unsigned char *pc=(unsigned char*)start;
unsigned int regPCOffset;
regPCOffset=offsetof(struct pt_regs,ARM_pc);
printf("%u\n",pc);
r=MyPtrace(PTRACE_POKEUSER,pid,(void*)regPCOffset,pc);
if(r<0)
{
return -1;
}
*/
long r;
struct pt_regs regs;
r=MyPtrace(PTRACE_GETREGS,pid,NULL,®s);
if(r<0){
return -1;
}
regs.ARM_pc=start;
r=ptrace(PTRACE_SETREGS,pid,NULL,®s);
if(r<0)
{
return -1;
}
printf("start:%x\n",regs.ARM_pc);
return 0;
}
/*open the control program which is made as a lib.so in the child
* program space,return the fd in child.
*/
int OpenLibInChild(char *libPath,pid_t pid)
{
int r;
unsigned char *pc;
unsigned int *openCode=(unsigned int*)malloc(MAX_CODE_SIZE*sizeof(int));
unsigned int codeSize;
GenerateOpenCode(openCode,&codeSize);/*open() code */
r=RunCode(openCode,codeSize,pid);
free(openCode);
if(r<0)
{
#ifdef LOGMESSAGE
LogMessage("run open code error\n");
#endif
return -1;
}
return r;
}
int WritePipe(int fd,char *buf)
{
int nwrite=strlen(buf);
int wirtten=0;
int n=0;
/*wite pipe,which is read by pipe in child*/
while(nwrite>0)
{
n=write(fd,buf+wirtten,nwrite);
if(n<0)
break;
wirtten+=n;
nwrite-=n;
}
return 0;
}
int ForkSuspendedChild(char *exe,char **argv,int fd[2])
{
int r;
pid_t pid;
if((pid=fork())==0)
{
/*child*/
char buf[BUF_MAX_SIZE];
memset(buf,0,BUF_MAX_SIZE);
close(fd[1]);
int nread=0;
int n;
/*read pipe,if pipe is empty,suspend the child until
* the pipe is writen by parent
*/
while((n=read(fd[0],buf+nread,BUF_MAX_SIZE))>0)
{
if(n<0)
break;
nread+=n;
}
buf[nread]='\0';
close(fd[0]);
if(strcmp(buf,"start_child")==0)
{
execv("/data/work/test.out",argv);
}
/*if run here,the execv run error*/
#ifdef LOGMESSAGE
LogMessage("execv error\n");
#endif
exit(-1);
}
return pid;
}
int CreatePipe(int fd[2])
{
int r;
r=pipe(fd);
return r;
}
int main(int argc,char *argv[])
{
//add haddle argv code
//
int fd[2];/*pipe*/
int r;
char libPath[MAX_PATH_NAME];
char binPath[MAX_PATH_NAME];
pid_t pid;
char str[BUF_MAX_SIZE];
strcpy(libPath,"/data/work/libtest.so");
strcpy(binPath,"/data/work/test.out");
/*create pipe,parent thread use write pipe,child use read pipe*/
r = CreatePipe(fd);
if (r!=0)
return -1;
/*fork suspend child which is suspended by pipe*/
pid=ForkSuspendedChild(binPath,NULL,fd);
if(pid<0)
{
#ifdef LOGMESSAGE
LogMessage("fork child error\n");
#endif
return -1;
}
/*load lib in child,and then start the program from lib,
*the lib can control the bin program ,the aim of the lib is to
*get the hot path frim bin program
* */
r=ChildStartFromLib(libPath,pid,fd);
if(r!=0)
{
#ifdef LOGMESSAGE
LogMessage("change the entry from bin file program to control program error\n");
#endif
return -1;
}
/*parent wait child stop*/
if(waitpid(pid,NULL,0)!=pid)
{
#ifdef LogMessage
LogMessage("waitpid error\n");
#endif
return -1;
}
return 0;
}