-
Notifications
You must be signed in to change notification settings - Fork 2
/
chita.js
executable file
·418 lines (325 loc) · 10.1 KB
/
chita.js
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/usr/bin/env node
'use strict';
// TODO
//
// - Search jumps and shit like that
// - ROP chain debugger
// - ROP output for C/Python/js
// - Stack pivots
const r2pipe = require('r2pipe');
const fs = require('fs');
const mod_getopt = require('posix-getopt');
const misc = require('./lib/misc');
const fmt = require('./lib/fmt');
const rop = require('./lib/rop');
const search = require('./lib/search');
/* Globals */
global.r2 = null;
global.r2bin = {};
global.everyRet = false;
const help = {};
/* Comnand parameters */
const _opts = {
len: 1024,
file: '',
chain: '',
base: '0x0',
written: 0,
extended: false
};
help.info = function () {
console.error(
'\n' +
'Prints useful exploitation info for the loaded binary\n' +
'Usage: #!pipe chita info\n\n' +
'Examples:\n' +
' [0x00000000]> #!pipe chita info\n\n'
);
process.exit(1);
};
help.pattern = function () {
console.error(
'\n' +
'Prints a random pattern which could be useful to find memory locations\n' +
'Usage: #!pipe chita pattern [-l len]\n\n' +
'Options:\n' +
' -l len: length of the pattern\n\n' +
'Examples:\n' +
' [0x00000000]> #!pipe chita pattern -l 1024\n\n'
);
process.exit(1);
};
help.fmt = function () {
console.error(
'\n' +
'Format string exploitation helper\n' +
'Usage: #!pipe chita fmt -p pos (-a addr -d data) | -f file [-w written]\n\n' +
'Options:\n' +
' -p pos: The position of the fmt string in the stack\n' +
' -a addr: Destination addr to write at\n' +
' -d data: Data we want to write (hexstring)\n' +
' -f file: File with addr/data pairs (used for multiple writes)\n' +
' -w written: Number of bytes already written by the fmt string (default 0)\n\n' +
'Examples:\n' +
' [0x00000000]> #!pipe chita fmt -p 7 -a 0x0804a018 -d 0x41414141\n' +
' [0x00000000]> #!pipe chita fmt -p 7 -f /tmp/fmt.writes\n\n' +
'Multiple writes file format example (to use with -f):\n' +
' 0x0804a140 0x41414141\n' +
' 0x0804a150 42424242\n' +
' 0x0804a160 4343434343434343\n\n'
);
process.exit(1);
};
help.rop2c = function () {
console.error(
'\n' +
'Generate C/python code from ropchain file\n' +
'Usage: #!pipe chita rop2c -r ropfile [-l lang]\n\n' +
'Options:\n' +
' -r ropfile: The file containing the rop gadgets file\n' +
' -l lang: Supported output languages: C, python, text (default C)\n\n' +
'Examples:\n' +
' [0x00000000]> #!pipe chita rop2c -r /tmp/gadgets\n' +
' [0x00000000]> #!pipe chita rop2c -r /tmp/gadgets -l text\n' +
' [0x00000000]> #!pipe chita rop2c -r /tmp/gadgets -l python\n\n' +
'ROP gadgets file format (to use with -r):\n' +
' 0x08048504: les ecx, [eax]; pop ebx; ret;\n' +
' 080485de: inc ecx; ret;\n' +
' 0x080484e7 pop edi; pop ebx; ret;\n' +
' 0xAAAAAAA // pop data\n' +
' 0xBB pop data\n\n'
);
process.exit(1);
};
help.rdbg = function () {
console.error(
'\n' +
'Generate r2 cmds file from a rop chain file in order to debug it.\n' +
'Usage: #!pipe chita rdbg -r ropfile -o outfile\n\n' +
'Options:\n' +
' -r ropfile: The file containing the rop gadgets file\n' +
' -o outfile: The file to write the output to\n\n' +
'Examples:\n' +
' [0x00000000]> #!pipe chita rdbg -r /tmp/gadgets -o /tmp/rop.r2\n' +
' $ r2 -d -i /tmp/rop.r2 bin_file\n\n' +
'ROP gadgets file format (to use with -r):\n' +
' 0x08048504: les ecx, [eax]; pop ebx; ret;\n' +
' 080485de: inc ecx; ret;\n' +
' 0x080484e7 pop edi; pop ebx; ret;\n' +
' 0xAAAAAAA // pop data\n' +
' 0xBB pop data\n\n'
);
process.exit(1);
};
help.jmp = function () {
console.error(
'\n' +
'Find jumps to the given register.\n' +
'Usage: #!pipe chita jmp -r register [-x] [-e]\n\n' +
'Options:\n' +
' -r register: The register to jump to\n' +
' -e: Display every ret (including ret imm16)\n' +
' -x: Extended output\n\n' +
'Examples:\n' +
' [0x00000000]> #!pipe chita jmp -r rsp -e -x\n' +
' [0x00000000]> #!pipe chita jmp -r rsp\n\n'
);
process.exit(1);
};
help.pivot = function () {
console.error(
'\n' +
'Search for ROP stack pivots.\n' +
'Usage: #!pipe chita pivot [-x] [-e]\n\n' +
'Options:\n' +
' -a: Display every ret (including ret imm16)\n' +
' -x: Extended output\n\n' +
'Examples:\n' +
' [0x00000000]> #!pipe chita pivot -e -x\n' +
' [0x00000000]> #!pipe chita pivot\n\n'
);
process.exit(1);
};
help.little = function () {
console.error(
'\n' +
'Convert hexstring to little endian.\n' +
'Usage: #!pipe chita little -d hexstring\n\n' +
'Options:\n' +
' -d hextring: The hexstring to convert\n\n' +
'Examples:\n' +
' [0x00000000]> #!pipe chita little -d 0x41424344\n' +
' [0x00000000]> #!pipe chita little -d 0x414243445152535461626364\n\n'
);
process.exit(1);
};
function usage () {
console.error(
'\n' +
'Usage: #!pipe chita command [options]\n\n' +
'Where valid commands are:\n' +
' pattern Generate a pseudorandom text pattern\n' +
' rdbg Generate a gdb or radare file to debug a ROP chain\n' +
' rop2c Generate C/python code from ROP chain file\n' +
' fmt Format string exploiting helper\n' +
' jmp Search for instructions such as \'jmp esp\' and so on\n' +
' pivot Search for stack pivots\n' +
' info Show executable info\n' +
' little Convert hexstring to little endian\n' +
' help Shows this help\n\n' +
'Extended help: #!pipe chita [command] -h\n\n'
);
process.exit(1);
}
function doFmtString() {
let writes = [];
if (r2bin.endian !== 'little') {
console.log('Format strings helper only works with little endian');
return;
}
if (!_opts.pos) {
console.error('Format string position is mandatory');
process.exit(1);
}
/* Writes by command line */
if (_opts.addr && _opts.data) {
writes = [{
addr: _opts.addr,
data: _opts.data
}];
/* Writes by file */
} else if (_opts.file) {
let file = fs.readFileSync(_opts.file).toString();
let lines = file.split('\n');
lines.forEach(function(line) {
line = line.trim().replace('\t', ' ');
line = line.replace(':', ' ');
//console.log(line);
if (line.length < 1)
return;
let fields = line.split(' ');
fields[1] = line.substr(fields[0].length);
let entry = {
addr: fields[0].trim(),
data: fields[1].trim()
};
//console.log(entry);
writes.push(entry);
});
/* Bad args */
} else {
console.error('Bad arguments');
process.exit(1);
}
//console.log(writes);
fmt.genFmt(_opts.pos, writes, _opts.written);
}
/* Get the command and remove it */
var cmd = process.argv[2];
process.argv.splice(2, 1);
process.argc = process.argv.length;
/* Arguments parsing */
let option;
const parser = new mod_getopt.BasicParser('l:f:b:r:d:a:p:w:o:hxe', process.argv);
while ((option = parser.getopt()) !== undefined) {
switch (option.option) {
case 'l':
_opts.len = option.optarg;
break;
case 'f':
_opts.file = option.optarg;
break;
case 'r':
_opts.chain = option.optarg;
break;
case 'b':
_opts.base = option.optarg;
break;
case 'a':
_opts.addr = option.optarg;
break;
case 'd':
_opts.data = option.optarg;
break;
case 'o':
_opts.output = option.optarg;
break;
case 'p':
_opts.pos = option.optarg;
break;
case 'w':
_opts.written = parseInt(option.optarg);
break;
case 'x':
_opts.extended = true;
break;
case 'e':
global.everyRet = true;
break;
default:
if (typeof help[cmd] === 'function')
help[cmd]();
else
usage();
break;
}
}
/* Check for unparsed args */
if (parser.optind() < process.argv.length) {
console.error('Invalid argument: ' + process.argv[parser.optind()] + '\n');
usage();
}
/* Initialize r2 data */
r2 = r2pipe.lpipeSync();
if (!process.env.R2PIPE_OUT && !process.env.R2PIPE_IN) {
console.log('This script must be executed inside an r2 session');
return 1;
}
/* Get bin info */
let info = r2.cmdj('ij');
r2bin.arch = info.bin.arch;
r2bin.bits = info.bin.bits;
r2bin.endian = info.bin.endian;
//fmt.test();
/* Command parsing */
switch (cmd) {
case 'pattern':
misc.pattern(parseInt(_opts.len));
break;
// case 'rop':
// rop.gadgets(_opts.file, _opts.onlyRet, _opts.base);
// break;
case 'info':
misc.printInfo(info);
break;
case 'rdbg':
rop.genDbgFile(_opts.chain, _opts.output);
break;
case 'rop2c':
rop.ropToLang(_opts.chain, _opts.len);
break;
case 'fmt':
doFmtString();
break;
case 'jmp':
search.jumps(_opts.chain, _opts.extended);
break;
case 'pivot':
search.pivots(_opts.extended);
break;
case 'little':
if (!_opts.data) {
usage();
return;
}
console.log(misc.hexToLittleEscaped(_opts.data));
break;
case 'help':
usage();
break;
default:
console.error('Invalid command: ' + cmd);
usage();
break;
}
//process.exit(0);