-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccedit.j2
880 lines (703 loc) · 26.9 KB
/
ccedit.j2
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
/// This file is autogenerated
#include "{{ class_header }}"
#include <cstdint>
#include <iterator> // for std::size
#include <random>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <CLI11.hpp>
/// ------------------------------ DUMPED DATA ------------------------------
namespace target {
/// Structure representing argument
struct Argument {
bool is_pointer;
size_t size;
const char *tname;
};
/// Max amount of arguments in a call
const size_t ARG_LIMIT = {{ arg_limit }};
/// Summarized data about arguments
struct AData {
Argument list[ARG_LIMIT];
size_t size;
size_t bytesize;
};
/// -------------- CONSTRUCTORS --------------
/// Constructor data
struct CData {
AData args;
{{ class_name }} (*fn)(const uint8_t *);
};
{% for c in constructors %}
{{ class_name }} constr_{{ loop.index }}(const uint8_t *data) {
{% if length(c.args) == 0 %} return {{ class_name }}();
{% else %} size_t size = 0;
{% for a in c.args -%}
{{ a.name }} *arg_{{ loop.index }} = ({{ a.name }} *)(data + size);
size += sizeof({{ a.name }});
{% endfor %}
return {{ class_name }}({% for a in c.args %}{% if loop.index != 0 %}, {% endif %}*arg_{{loop.index}}{% endfor %});
{% endif %}{{ "};" }}
{% endfor %}
/// List of all constructors info
const CData c_list[] = {
{% for c in constructors -%}
{
.args = {
.list = {
{% for a in c.args %}{{ "{" }}{{ a.is_pointer }}, sizeof({{ a.name }}), "{{ a.name }}"},
{% endfor %}
},
.size = {{ length(c.args) }},
.bytesize = 0{% for a in c.args %} + sizeof({{ a.name }}){% endfor %},
},
.fn = constr_{{ loop.index }},
},{% endfor %}
};
constexpr size_t c_size = std::size(c_list);
/// -------------- METHODS --------------
/// Method data
struct MData {
AData args;
const char *name;
void (*fn)({{ class_name }} *, const uint8_t *);
};
{% for m in methods %}
void method_{{ loop.index }}({{ class_name }} *obj, const uint8_t *data) {
{% if length(m.args) == 0 %} obj->{{ m.name }}();
{% else %} size_t size = 0;
{% for a in m.args -%}
{{ a.name }} *arg_{{ loop.index }} = ({{ a.name }} *)(data + size);
size += sizeof({{ a.name }});
{% endfor %}
obj->{{ m.name }}({% for a in m.args %}{% if loop.index != 0 %}, {% endif %}*arg_{{ loop.index }}{% endfor %});
{% endif %}{{ "};"}}
{% endfor %}
/// List of all method info
const MData m_list[] = {
{% for m in methods -%}
{
.args = {
.list = {
{% for a in m.args %}{{ "{" }}{{ a.is_pointer }}, sizeof({{ a.name }}), "{{ a.name }}"},
{% endfor %}
},
.size = {{ length(m.args) }},
.bytesize = 0{% for a in m.args %} + sizeof({{ a.name }}){% endfor %},
},
.name = "{{ m.name }}",
.fn = method_{{ loop.index }},
},{% endfor %}
};
constexpr size_t m_size = std::size(m_list);
} // namespace
/// ------------------------------ CALL CHAIN AND MUTATOR ------------------------------
/**
* This is class for working with Call Chains (mutations and other userful functions)
* Note that parsing takes time and for simple classes it's better to used standart binary mutations
*/
class CallChain {
public:
/// MAX amount of methods allowed (class uses static memory)
static const size_t MLIMIT = {{ cc_limit }};
/// Evaluate CC while reading
static int eval(const uint8_t *data, size_t size) {
if (size == 0)
return 1;
size_t ptr = 0;
auto c = target::c_list[data[ptr] % target::c_size];
ptr++;
if (ptr + c.args.bytesize > size)
return 1;
auto obj = c.fn(data + ptr);
ptr += c.args.bytesize;
if (ptr == size) // CC with only constructor
return 1;
auto m = target::m_list[data[ptr] % target::m_size];
ptr++;
for (size_t i = 0; i < MLIMIT; ++i) {
if (ptr + m.args.bytesize > size)
break;
m.fn(&obj, data + ptr);
ptr += m.args.bytesize;
if (ptr == size) // No room for method id
return 1;
m = target::m_list[data[ptr] % target::m_size];
ptr++;
}
return 0;
}
CallChain(const uint8_t *data, size_t size) : d1(data), d2(nullptr), d_size(size) { update(); }
CallChain(uint8_t *data, size_t size) : d1(data), d2(data), d_size(size) { update(); }
inline bool is_const() const {
return d2 == nullptr;
}
inline bool is_empty() const {
return c_size == 0;
}
inline bool is_methodless() const {
return m_size == 0;
}
/// Returns amount of methods (not counting constructor)
inline size_t get_msize() const {
return m_size;
}
/// Get used bytes size
inline size_t get_dsize() const {
return d_size;
}
/// Get constructor id
size_t get_cid() const {
if (!is_empty())
return d1[0] % target::c_size;
throw std::logic_error("Attempt to get constructor id of an empty CC");
}
/// Get method id
size_t get_mid(size_t n) const {
if (!is_methodless())
return d1[m[n % m_size]] % target::m_size;
throw std::logic_error("Attempt to get method id of an empty CC");
}
/// Is constructor argless
bool is_c_argless() const {
if (is_empty())
throw std::logic_error("Attempt to check if constructor argless when there is no constructor");
return c_size == 1;
}
/// Get arg bytearray for constructor
std::tuple<const uint8_t *, size_t> get_c_arg(size_t arg_n) const {
if (is_empty())
throw std::logic_error("Attemplt to get constructor argument of an empty CC");
auto c_args = target::c_list[d1[0] % target::c_size].args;
if (c_args.size == 0)
throw std::logic_error("Attempt to get contructor argument from a contructor with no arguments");
arg_n %= c_args.size;
size_t i, bytes = 1;
for (i = 0; i < arg_n; ++i)
bytes += c_args.list[i].size;
return std::make_tuple(d1 + bytes, c_args.list[i].size);
}
/// Get mutable arg bytearray for constructor
std::tuple<uint8_t *, size_t> get_c_arg(size_t arg_n) {
if (is_empty() || is_const())
throw std::logic_error("Attemplt to get constructor argument of an empty/const CC");
auto c_args = target::c_list[d2[0] % target::c_size].args;
if (c_args.size == 0)
throw std::logic_error("Attempt to get contructor argument from a contructor with no arguments");
arg_n %= c_args.size;
size_t i, bytes = 1;
for (i = 0; i < arg_n; ++i)
bytes += c_args.list[i].size;
return std::make_tuple(d2 + bytes, c_args.list[i].size);
}
bool is_m_argless(size_t n) const {
if (is_methodless())
throw std::logic_error("Attempt to check if method argless when there is no methods");
auto m_args = target::m_list[d1[m[n % m_size]] % target::m_size].args;
return m_args.size == 0;
}
/// Get arg bytearray for method
std::tuple<const uint8_t *, size_t> get_m_arg(size_t n, size_t arg_n) const {
if (is_methodless())
throw std::logic_error("Attempt to get method argument of CC with no methods");
n %= m_size;
auto m_args = target::m_list[d1[m[n]] % target::m_size].args;
if (m_args.size == 0)
throw std::logic_error("Attempt to get method argument from a method with no arguments");
arg_n %= m_args.size;
size_t i, bytes = 1;
for (i = 0; i < arg_n; ++i)
bytes += m_args.list[i].size;
return std::make_tuple(d1 + m[n] + bytes, m_args.list[i].size);
}
/// Get mutable arg bytearray for method
std::tuple<uint8_t *, size_t> get_m_arg(size_t n, size_t arg_n) {
if (is_methodless() || is_const())
throw std::logic_error("Attempt to get method argument of CC with no methods or const");
n %= m_size;
auto m_args = target::m_list[d2[m[n]] % target::m_size].args;
if (m_args.size == 0)
throw std::logic_error("Attempt to get method argument from a method with no arguments");
arg_n %= m_args.size;
size_t i, bytes = 1;
for (i = 0; i < arg_n; ++i)
bytes += m_args.list[i].size;
return std::make_tuple(d2 + m[n] + bytes, m_args.list[i].size);
}
/// Get offset of method
size_t get_m_offset(size_t n) const {
if (is_empty())
return 0;
if (is_methodless())
return c_size;
return m[n % m_size];
}
/// Remove method from CC
/// Returns new byte len
size_t rm_m(size_t n) {
if (is_empty() || is_methodless() || is_const())
return d_size;
n %= m_size;
// If it's last then just cut it off
if (n == m_size - 1) {
d_size = m[n];
m_size--;
return d_size;
}
// Shift everything in d2
size_t offset = m[n + 1] - m[n];
for (size_t i = m[n + 1]; i < d_size; ++i)
d2[i - offset] = d2[i];
d_size -= offset;
// Shift values of m and substract offset
m_size--;
for (size_t i = n; i < m_size; ++i)
m[i] = m[i + 1] - offset;
return d_size;
}
/// Insert method
/// Returns new byte length
size_t ins_m(size_t n, size_t id, size_t max_size) {
id %= target::m_size;
size_t args = target::m_list[id].args.bytesize;
if (is_empty() || is_const() || d_size + 1 + args > max_size || m_size + 1 >= MLIMIT)
return d_size;
// mod + 1 because we want to add methods to the start and end
n = m_size == 0 ? 0 : n % (m_size + 1);
// If it's last then it's simple
if (n == m_size) {
// Create new empty call
d2[d_size] = id;
for (size_t i = 0; i < args; ++i)
d2[d_size + 1 + i] = 0;
// Update class info
m[m_size] = d_size;
d_size += 1 + args;
m_size++;
return d_size;
}
// Shift everything
for (size_t i = d_size - 1; i >= m[n]; --i)
d2[i + 1 + args] = d2[i];
// Set call
d2[m[n]] = id;
for (size_t i = 0; i < args; ++i)
d2[m[n] + 1 + i] = 0;
d_size += 1 + args;
m_size++;
// Shift offset values of m and add (1 + args) to them
for (size_t i = m_size; i >= n + 1; --i)
m[i] = m[i - 1] + 1 + args;
return d_size;
}
/// Set constructor
size_t set_c(size_t id, size_t max_size) {
if (is_empty() || is_const())
throw std::logic_error("Attempt to set constructor of an empty/const CC");
id %= target::c_size;
// Calculate change
int offset = target::c_list[id].args.bytesize - (c_size - 1);
if (d_size + offset > max_size)
return d_size;
// The simplest way
if (offset == 0) {
d2[0] = id;
for (size_t i = 1; i < c_size; ++i)
d2[i] = 0;
return d_size;
}
// Shrink/expand d
if (offset < 0) {
for (size_t i = c_size; i < d_size; ++i)
d2[i + offset] = d2[i];
} else {
for (size_t i = d_size; i >= c_size; --i)
d2[i + offset] = d2[i];
}
// Fix m
d_size += offset;
for (size_t i = 0; i < m_size; ++i)
m[i] += offset;
// Set call
d2[0] = id;
c_size += offset;
for (size_t i = 1; i < c_size; ++i)
d2[i] = 0;
return d_size;
}
/// Set method
size_t set_m(size_t n, size_t id, size_t max_size) {
if (is_methodless() || is_const())
throw std::logic_error("Attempt to call set method on a methodless/const CC");
n %= m_size;
id %= target::m_size;
// Calculate change
size_t old_args = target::m_list[d2[m[n]] % target::m_size].args.bytesize;
int offset = target::m_list[id].args.bytesize - old_args;
if (d_size + offset > max_size)
return d_size;
// If we don't need to shift we just set everything
// Same goes if our element is the last one
if (offset == 0 || n == m_size - 1) {
d2[m[n]] = id;
for (size_t i = 0; i < target::m_list[id].args.bytesize; ++i)
d2[m[n] + 1 + i] = 0;
d_size += offset;
return d_size;
}
// Shrink/expand d
if (offset < 0) {
for (size_t i = m[n + 1]; i < d_size; ++i)
d2[i + offset] = d2[i];
} else {
for (size_t i = d_size - 1; i >= m[n + 1]; --i)
d2[i + offset] = d2[i];
}
// Fix m
d_size += offset;
for (size_t i = n + 1; i < m_size; ++i)
m[i] += offset;
// Set call
d2[m[n]] = id;
for (size_t i = m[n] + 1; i < m[n + 1]; ++i)
d2[i] = 0;
return d_size;
}
private:
/// Source buffer data
const uint8_t *d1;
uint8_t *d2;
size_t d_size; // Note that d_size is data **used**, not allocated/stored
/// Constructor section length
size_t c_size;
// Method offsets
size_t m[MLIMIT];
size_t m_size;
/// Update method data depending on d1
void update() {
m_size = 0;
c_size = 0;
if (d_size == 0)
return;
size_t offset = 1 + target::c_list[d1[0] % target::c_size].args.bytesize;
if (offset > d_size)
return;
c_size = offset;
size_t ptr;
for (ptr = offset; ptr < d_size && m_size < MLIMIT; ++m_size) {
m[m_size] = ptr;
ptr += 1 + target::m_list[d1[ptr] % target::m_size].args.bytesize;
}
// ptr points to last call end
// if it's greater than d_size then we have to trow away last call
if (ptr != d_size) {
// Roll back to last ptr
d_size = m[m_size - 1];
m_size--;
}
}
};
/// ------------------------------ MUTATOR FNs ------------------------------
std::tuple<uint8_t *, size_t> crossover(
const uint8_t *data1, size_t size1,
const uint8_t *data2, size_t size2,
size_t n1, size_t n2
) {
CallChain cc1(data1, size1), cc2(data2, size2);
if (cc1.is_empty() || cc2.is_methodless()) {
uint8_t *data = new uint8_t[size1];
for (size_t i = 0; i < size1; ++i)
data[i] = data1[i];
return std::make_tuple(data, size1);
}
// Mod with +1 is because:
// 0 - we take constructor only
// cc1.get_msize() - we take whole CC
n1 %= (cc1.get_msize() + 1);
// Here we take methods from p2 to cc2.get_msize()
// Note that we can't take 0 methods
n2 %= cc2.get_msize();
size_t size = n1 + (size2 - n2);
uint8_t *data = new uint8_t[size];
size_t limit = n1 == cc1.get_msize() ? size1 : cc1.get_m_offset(n1);
size_t i;
for (i = 0; i < limit; ++i)
data[i] = data1[i];
for (size_t j = cc2.get_m_offset(n2); j < size2; ++j, ++i)
data[i] = data2[j];
return std::make_tuple(data, size);
}
/// ------------------------------ PRINT FNs ------------------------------
void print_class_fns() {
for (size_t i = 0; i < target::c_size; ++i, std::cout << ")" << std::endl) {
std::cout << i << " {{ class_name }}(";
if (target::c_list[i].args.size == 0) // Prevent underflow in for
continue;
size_t last = target::c_list[i].args.size;
for (size_t j = 0; j < last - 1; ++j)
std::cout << target::c_list[i].args.list[j].tname << "(" << target::c_list[i].args.list[j].size << ")" << ", ";
std::cout << target::c_list[i].args.list[last - 1].tname << "(" << target::c_list[i].args.list[last - 1].size << ")";
}
std::cout << std::endl;
for (size_t i = 0; i < target::m_size; ++i, std::cout << ")" << std::endl) {
std::cout << i << " " << target::m_list[i].name << "(";
if (target::m_list[i].args.size == 0)
continue;
size_t last = target::m_list[i].args.size;
for (size_t j = 0; j < last - 1; ++j)
std::cout << target::m_list[i].args.list[j].tname << "(" << target::m_list[i].args.list[j].size << ")" << ", ";
std::cout << target::m_list[i].args.list[last - 1].tname << "(" << target::m_list[i].args.list[last - 1].size << ")";
}
}
void print_hex(const uint8_t *data, size_t size) {
for (size_t i = 0; i < size; ++i)
std::cout << std::hex << std::setfill('0') << std::setw(2) << (int)data[i];
}
void print_raw(uint8_t *data, size_t size) {
for (size_t i = 0; i < size; ++i)
std::cout << (char)data[i];
}
void print_cc(CallChain &cc) {
std::cout << "Total " << cc.get_msize() << " methods" << std::endl << std::endl;
std::cout << "n start id call" << std::endl << "--------------" << std::endl;
size_t cid = cc.get_cid();
std::cout << "- 0 " << cid << " {{ class_name }}(";
if (cc.is_c_argless()) {}
else {
auto c_args = target::c_list[cid].args;
for (size_t i = 0; i < c_args.size; ++i) {
std::cout << std::endl << " " << c_args.list[i].tname << " : ";
const uint8_t *a_data;
size_t a_size;
std::tie(a_data, a_size) = cc.get_c_arg(i);
print_hex(a_data, a_size);
std::cout << std::endl;
}
}
std::cout << ")" << std::endl;
for (size_t i = 0; i < cc.get_msize(); ++i) {
cid = cc.get_mid(i);
std::cout << std::dec << i << " " << cc.get_m_offset(i) << " " << cid << " " << target::m_list[cid].name << "(";
if (cc.is_m_argless(i)) {}
else {
auto m_args = target::m_list[cid].args;
for (size_t j = 0; j < m_args.size; ++j) {
std::cout << std::endl << " " << m_args.list[j].tname << " : ";
const uint8_t *a_data;
size_t a_size;
std::tie(a_data, a_size) = cc.get_m_arg(i, j);
print_hex(a_data, a_size);
std::cout << std::endl;
}
}
std::cout << ")" << std::endl;
}
}
/// ------------------------------ LOAD FNs ------------------------------
const size_t MUTATION_RESERVE = 100; // In bytes
/// Sets pointer to nullptr on error
std::tuple<uint8_t *, size_t> load_file(const std::string &filename) {
std::ifstream ifs(filename);
if (!ifs)
return std::make_tuple(nullptr, 0);
ifs.seekg(0, std::ios_base::end);
std::streamsize size = ifs.tellg();
ifs.seekg(0, std::ios_base::beg);
uint8_t *data = new uint8_t[size + MUTATION_RESERVE];
if (!ifs.read(reinterpret_cast<char*>(data), size)) {
delete [] data;
return std::make_tuple(nullptr, 0);
}
return std::make_tuple(data, size);
}
/// Sets pointer to nullptr on error
std::tuple<uint8_t *, size_t> load_hex(const std::string &hexstr) {
if (hexstr.size() % 2 != 0)
return std::make_tuple(nullptr, 0);
size_t size = hexstr.size() / 2;
uint8_t *data = new uint8_t[size + MUTATION_RESERVE];
std::string tmp;
for (size_t i = 0; i < size; ++i) {
tmp = hexstr.substr(i * 2, 2);
data[i] = std::strtol(tmp.c_str(), NULL, 16);
}
return std::make_tuple(data, size);
}
/// ------------------------------ MAIN FN ------------------------------
enum class CCOutFormat : int { Hex, Raw, Printable, Quiet };
int main(int argc, char **argv) {
CLI::App app("CallChain editor");
argv = app.ensure_utf8(argv);
std::string file_in, hex_in;
bool dump_class(false);
auto cc_option = app.add_option_group("Call Chain (CC)", "Input CC to work with");
cc_option->add_option("-f,--file", file_in, "Load from file")->check(CLI::ExistingFile);
cc_option->add_option("-x,--hex", hex_in, "Load from hex");
cc_option->add_flag("-d,--dump-class", dump_class, "Print class info");
cc_option->require_option(1);
std::string file_in2, hex_in2;
size_t n1, n2;
auto crossover_cmd = app.add_subcommand("crossover", "Crossover two CC's");
auto co_cc_option = crossover_cmd->add_option_group("Second CC", "Second CC to crossover with");
co_cc_option->add_option("-f,--file", file_in2, "Load from file")->check(CLI::ExistingFile);
co_cc_option->add_option("-x,--hex", hex_in2, "Load from hex");
co_cc_option->require_option(1);
auto co_n1_option = crossover_cmd->add_option("-1,--n1", n1, "Point to cut of first chain (takes all before p1)");
auto co_n2_option = crossover_cmd->add_option("-2,--n2", n2, "Point to continue second chain (takes all starting from p2)");
auto rm_cmd = app.add_subcommand("rm", "Remove method from CC");
auto rm_n_option = rm_cmd->add_option("-n", n1, "Remove n-th method (starting from zero)");
auto ins_cmd = app.add_subcommand("ins", "Insert method into CC (all args get zeroed)");
ins_cmd->add_option("-n", n1, "Add method to the n-th place")->default_val(0);
auto ins_c_option = ins_cmd->add_option("-c", n2, "Call id");
std::string hex_arg;
int call_n, call_k;
auto seta_cmd = app.add_subcommand("seta", "Set argument(s) of the call");
seta_cmd->add_option("-n", call_n, "Call n (positive for methods, -1 for constructor)")->default_val(-1);
seta_cmd->add_option("-k", call_k, "Argument id (-1 to set all)")->default_val(-1);
auto seta_arg_option = seta_cmd->add_option("-x,--hex", hex_arg, "Argument value in hex (random if not provided)");
auto set_cmd = app.add_subcommand("set", "Set call id (change constructor/method)");
set_cmd->add_option("-n", call_n, "Call n (positive for methods, -1 for constructor)")->default_val(-1);
auto set_cmd_option = set_cmd->add_option("-c", n1, "Call id (random if not provided)");
bool eval(false);
app.add_flag("-e,--eval", eval, "Evaluate CC after all modifications");
CCOutFormat out_mode;
std::map<std::string, CCOutFormat> out_format_map{
{"h", CCOutFormat::Hex},
{"r", CCOutFormat::Raw},
{"p", CCOutFormat::Printable},
{"q", CCOutFormat::Quiet}
};
app.add_option("-o,--out", out_mode, "Output format")
->default_val(CCOutFormat::Printable)
->transform(CLI::CheckedTransformer(out_format_map, CLI::ignore_case).description("Output format"));
CLI11_PARSE(app, argc, argv);
// Print all class data indexed
if (dump_class) {
print_class_fns();
return 0;
}
// Load
uint8_t *data; size_t size;
if (!file_in.empty())
std::tie(data, size) = load_file(file_in);
else
std::tie(data, size) = load_hex(hex_in);
if (data == nullptr) {
std::cerr << "Loading error" << std::endl;
return 1;
}
CallChain cc(data, size);
// Init random if needed
std::random_device rd;
std::mt19937 rng(rd());
// CLI11 can't give out repeated subcommands so we process one at the time
if (app.got_subcommand(crossover_cmd)) {
// Load data2
uint8_t *data2; size_t size2;
if (!file_in2.empty())
std::tie(data2, size2) = load_file(file_in2);
else
std::tie(data2, size2) = load_hex(hex_in2);
if (data2 == nullptr) {
std::cerr << "CO Loading error" << std::endl;
delete [] data;
return 1;
}
// Randomize n1, n2 if needed
if (co_n1_option->count() == 0)
n1 = rng();
if (co_n2_option->count() == 0)
n2 = rng();
uint8_t *result; size_t result_size;
std::tie(result, result_size) = crossover(data, size, data2, size2, n1, n2);
// We want output of result so
delete [] data;
delete [] data2;
data = result; // for later free purposes
size = result_size;
cc = CallChain(result, result_size);
} else if (app.got_subcommand(rm_cmd)) {
// Randomize n if not set
if (rm_n_option->count() == 0)
n1 = rng();
size = cc.rm_m(n1);
} if (app.got_subcommand(ins_cmd)) {
// If call id not set then use random
if (ins_c_option->count() == 0)
n2 = rng();
size = cc.ins_m(n1, n2, size + MUTATION_RESERVE);
} else if (app.got_subcommand(seta_cmd)) {
uint8_t *arg;
size_t arg_size;
// Get arg bytearray
size_t k = call_k == -1 ? 0 : call_k;
if (call_n == -1) {
std::tie(arg, arg_size) = cc.get_c_arg(k);
if (call_k == -1)
arg_size = target::c_list[cc.get_cid()].args.bytesize;
} else {
std::tie(arg, arg_size) = cc.get_m_arg(call_n, k);
if (call_k == -1)
arg_size = target::m_list[cc.get_mid(call_n)].args.bytesize;
}
// If arg_hex is not provided then fill with random values
if (seta_arg_option->count() == 0) {
for (size_t i = 0; i < arg_size; ++i)
arg[i] = rng();
} else {
// Fix odd length
if (hex_arg.size() % 2 != 0)
hex_arg.insert(hex_arg.begin(), '0');
size_t hex_size = hex_arg.size() / 2;
// Fill with zeroes if needed
size_t diff = (hex_size - arg_size) * 2;
if (diff != 0) {
std::string fill;
for (size_t i = 0; i < diff; ++i)
fill.push_back('0');
hex_arg.insert(0, fill);
}
// Set value
std::string tmp;
for (size_t i = 0; i < arg_size; ++i) {
tmp = hex_arg.substr(i * 2, 2);
arg[i] = std::strtol(tmp.c_str(), NULL, 16);
}
}
} else if (app.got_subcommand(set_cmd)) {
// Set call id to random if it's not provided
if (set_cmd_option->count() == 0)
n1 = rng();
if (call_n == -1)
size = cc.set_c(n1, size + MUTATION_RESERVE);
else
size = cc.set_m(call_n, n1, size + MUTATION_RESERVE);
}
// Print output
switch (out_mode) {
case CCOutFormat::Hex: {
print_hex(data, size);
std::cout << std::endl;
break;
}
case CCOutFormat::Printable: {
print_cc(cc);
break;
}
case CCOutFormat::Raw: {
print_raw(data, size);
break;
}
case CCOutFormat::Quiet: {
break;
}
default: throw std::logic_error("Unknown CCOutFormat type");
}
// Evaluate
if (eval)
CallChain::eval(data, size);
// Free
delete [] data;
return 0;
}