-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinjector.c
209 lines (196 loc) · 4.64 KB
/
injector.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
#include <unistd.h>
#include <sys/ptrace.h>
#include <asm/ptrace.h>
#include <sys/wait.h>
#include <signal.h>
#include <stddef.h>
#include "injector.h"
#include "global.h"
int MyPtrace(int requst,pid_t pid,void *addr,void *data)
{
return(ptrace(requst,pid,addr,data));
}
/*wait singal from pid*/
int WaitSingal(pid_t pid,int sig)
{
int r;
int status;
r=waitpid(pid,&status,0);
if(r<0)
{
return -1;
}
if(WIFSTOPPED(status))
{
// printf("number:%d",WSTOPSIG(status));
// if(WSTOPSIG(status)==sig)
return 0;
}
return -1;
}
int PtraceReadMemory(pid_t pid,void *dst,void *src,size_t len)
{
int i;
unsigned int r;
unsigned int *dstReg=dst;
unsigned int *srcReg=src;
for(i=0;i<len/sizeof(int);i++)
{
r=ptrace(PTRACE_PEEKDATA,pid,&srcReg[i],NULL);
*(dstReg+i)=r;
}
return 0;
}
int PtraceWriteMemory(pid_t pid,void *dst,void *src,size_t len)
{
int i;
int r;
int *dstReg=dst;
int *srcReg=src;
for(i=0;i<len/sizeof(int);i++)
{
r=ptrace(PTRACE_POKEDATA,pid,&dstReg[i],(void*)srcReg[i]);
if(r<0)
return -1;
}
return 0;
}
void GenerateOpenCode(unsigned int *code,unsigned int *codeSize)
{
*codeSize=200;
unsigned int codeTmp[]={0xe24dd01c,0xe3a0102f,0xe58d1000,0xe3a01064,0xe58d1001,
0xe3a01061,0xe58d1002,0xe3a01074,0xe58d1003,0xe3a01061,0xe58d1004,0xe3a0102f,
0xe58d1005,0xe3a01077,0xe58d1006,0xe3a0106f,0xe58d1007,0xe3a01072,0xe58d1008,
0xe3a0106b,0xe58d1009,0xe3a0102f,0xe58d100a,0xe3a0106c,0xe58d100b,0xe3a01069,
0xe58d100c,0xe3a01062,0xe58d100d,0xe3a01074,0xe58d100e,0xe3a01065,0xe58d100f,
0xe3a01073,0xe58d1010,0xe3a01074,0xe58d1011,0xe3a0102e,0xe58d1012,0xe3a01073,
0xe58d1013,0xe3a0106f,0xe58d1014,0xe3a01000,0xe58d1015,0xe1a0000d,0xe3a01000,
0xef900005,0xe28dd01c,0xe1200071};
int i;
for(i=0;i<(*codeSize)/4;i++)
code[i]=codeTmp[i];
}
void GenerateReadCode(unsigned int *code,unsigned int *codeSize,
int fd,unsigned char *addr)
{
int i;
*codeSize=28;
unsigned int codeTmp[]={0xe59f000c,0xe59f100c,0xe3a03064,0xef900003,
0x0,0x0,0xe1200071};
codeTmp[4]=fd;
codeTmp[5]=addr;
for(i=0;i<(*codeSize)/4;i++)
{
printf("code:%x\n",codeTmp[i]);
code[i]=codeTmp[i];
}
}
void GenerateMapCode(unsigned int *code,unsigned int *codeSize,unsigned char *addr,unsigned int length,int prot,
int flags,int fd,unsigned int offset)
{
int i;
if(addr==NULL&&fd==-1)
{
*codeSize=48;
unsigned int codeTmp1[]={0xe24dd008,0xe3e03000,0xe58d3000,0xe3a03000,0xe58d3004,
0xe59f1014,0xe3a00000,0x0,0x0,0xef9000c0,0xe1200071,0x0};
codeTmp1[7]=0xe3a02000|prot;
codeTmp1[8]=0xe3a03000|flags;
codeTmp1[11]=length;
for(i=0;i<(*codeSize)/4;i++)
{
code[i]=codeTmp1[i];
}
}
else
{
*codeSize=60;
unsigned int codeTmp2[]={0xe24dd008,0x0,0xe58d3000,0xe59f3024,
0xe58d3004,0xe59f1014,0xe59f0014,0x0,0x0,0xef9000c0,0xe28dd008,0xe1200071,
0x0,0x0,0x0};
//codeTmp2[1]=0xe3a03000|fd;
codeTmp2[1]=0xe3e03000;
codeTmp2[7]=0xe3a02000|prot;
codeTmp2[8]=0xe3a03000|flags;
codeTmp2[12]=length;
codeTmp2[13]=addr;
codeTmp2[14]=offset;
for(i=0;i<(*codeSize)/4;i++)
{
code[i]=codeTmp2[i];
}
}
}
void GenerateUnmapCode(unsigned int *code,unsigned int *codeSize,unsigned char *addr,unsigned int length)
{
int i;
*codeSize=24;
unsigned int codeTmp[]={0xe59f0004,0xe59f1004,0xef90005b,0x0,0x0,0xe1200071};
codeTmp[3]=addr;
codeTmp[4]=length;
for(i=0;i<(*codeSize)/4;i++)
{
code[i]=codeTmp[i];
}
}
void GenerateMprotCode(unsigned int *code,unsigned int *codeSize,unsigned char *addr,unsigned int length, int prot)
{
int i;
*codeSize=28;
unsigned int codeTmp[]={0xe59f0008,0xe59f1008,0x0,0xef90007d,0x0,0x0,0xe1200071};
codeTmp[2]=0xe3a02000|prot;
codeTmp[4]=addr;
codeTmp[5]=length;
for(i=0;i<(*codeSize)/4;i++)
{
code[i]=codeTmp[i];
}
}
unsigned int RunCode(unsigned int *code,unsigned int codeSize,pid_t pid)
{
int r;
unsigned int regPCOffset;
unsigned char *pc;
struct pt_regs regs;
struct pt_regs retRegs;
unsigned char orginCode[MAX_CODE_SIZE];
r=MyPtrace(PTRACE_GETREGS,pid,NULL,®s);
if(r<0)
{
return -1;
}
pc=(unsigned char*)PAGESTART(regs.ARM_pc,PAGE_SIZE);
codeSize=PAGEEND(codeSize,4);
r=PtraceReadMemory(pid,orginCode,pc,codeSize);
if(r<0)
return -1;
r=PtraceWriteMemory(pid,pc,code,codeSize);
if(r<0)
return -1;
regPCOffset=offsetof(struct pt_regs,ARM_pc);
r=MyPtrace(PTRACE_POKEUSER,pid,(void*)regPCOffset,pc);
if(r<0)
{
return -1;
}
r=MyPtrace(PTRACE_CONT,pid,NULL,NULL);
if(r<0)
{
return -1;
}
r=WaitSingal(pid,SIGSTOP);
if(r<0)
{
return -1;
}
r=MyPtrace(PTRACE_GETREGS,pid,NULL,&retRegs);
if(r<0)
return -1;
r=PtraceWriteMemory(pid,pc,orginCode,codeSize);
if(r<0)
return -1;
r=MyPtrace(PTRACE_SETREGS,pid,NULL,®s);
if(r<0)
return -1;
return retRegs.ARM_r0;
}