-
Notifications
You must be signed in to change notification settings - Fork 4
/
SecondPartDisas14.cpp
1758 lines (1636 loc) · 44.9 KB
/
SecondPartDisas14.cpp
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
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "Headers/InstructionList.h"
#include "Headers/Instruction_Leaf.h"
#include "Headers/Instruction_Tree.h"
#include "Headers/PE_Reader.h"
#include "Headers/Stack.h"
#include "Headers/Emulator_LibEmu.h"
#include "Headers/PolyUnpack.h"
extern "C"{
#include "libdasm/libdasm.h"
#include <emu/emu.h>
#include <emu/emu_cpu.h>
#include <emu/emu_memory.h>
}
using namespace std;
/*
const int mem_before = 10*1024; // 10 KiB, min 1k instuctions
const int mem_after = 80*1024; //80 KiB, min 8k instructions
*/
int parse(const char* string){
int i=0;
while(string[i]!=' ')
i++;
while(string[i]==' ')
i++;
return i;
}
void get_register_name(char* & string, const char* instruction){
int n=4;
int i=0;
string=new char[strlen(instruction)];
while(instruction[i]!='\0'){
string[i]=instruction[i];
i++;
}
string[i]='\0';
}
bool compare(const char* string1, const char* string2){
int i=0;
while((string1[i]!='\0')&&(string2[i]!='\0')){
if (string1[i]!=string2[i])
return false;
i++;
}
return true;
}
void Instruction_Tree:: Add(InstructionList* & po1, InstructionList* & po2, InstructionList* & po3){
Instruction_Leaf* p;
if (root==NULL){
root=new Instruction_Leaf;
root->pointer=po1;
root->valid=1;
if (po2!=NULL){
//printf("Left Leaf=%s\n", po2->instruction);
root->left=new Instruction_Leaf;
p=root->left;
p->pointer=po2;
p->valid=0;
p->left=NULL;
p->right=NULL;
}
else{
//printf("Left Leaf=NULL");
root->left=NULL;
}
if (po3!=NULL){
//printf("Right Leaf=%s\n", po3->instruction);
root->right=new Instruction_Leaf;
p=root->right;
p->pointer=po3;
p->valid=0;
p->left=NULL;
p->right=NULL;
}
else{
//printf("Right Leaf=NULL\n");
root->right=NULL;
}
}
}
int Instruction_Tree::Obhod(const char* command, PolyUnpack& unpack){
int kol=0;
int iterator=0;
int result=0;
//Obhod2(root);
kol=0;
iterator=0;
result=0;
InstructionList* original=NULL;
Obhod(root, command, original, unpack, kol, iterator, result);
//printf("KOL_of_Valid=%d\n", kol);
if (kol==0){
Destructor(root);
return -1;
}
if (kol==1){
Destructor(root);
return result;
}
if (kol>1)
if((compare(command,"jz"))||(compare(command,"jnz"))||
(compare(command,"jns"))||(compare(command,"jmp"))||
(compare(command,"ja"))||(compare(command,"jna"))||
(compare(command,"jo"))||(compare(command,"jno"))||
(compare(command,"jnc"))||(compare(command,"jc"))||
(compare(command,"jl"))||(compare(command,"jnl"))||
(compare(command,"js"))||(compare(command,"jns"))||
(compare(command,"jecxz"))||(compare(command,"jp"))||
(compare(command,"jpo"))||(compare(command,"loopn"))||
(compare(command,"loop"))||(compare(command,"jg"))||(compare(command,"jng"))||
(compare(command,"jmpf"))){
bool flag=true;
InstructionList* po1=NULL;
InstructionList* po2=NULL;
InstructionList* po3=NULL;
Check(root, po1, po2, po3, original, flag);
if (flag==true){
Destructor(root);
//root=NULL;
//InstructionList* po2=NULL;
//InstructionList* po3=NULL;
int iterator=0;
//printf("po1->instruction=%s\n", po1->instruction);
//printf("po2->instruction=%s\n", po2->instruction);
//printf("po3->instruction=%s\n", po3->instruction);
Add(po1, po2, po3);
//Obhod2(root);
}
}
return result;
}
void Instruction_Tree::Obhod(Instruction_Leaf* & p1, const char* command,
InstructionList* & original, PolyUnpack & unpack,
int & kol, int & iterator, int & result){
Instruction_Tree* p2;
if ((p1->left!=NULL)&&(p1->right!=NULL)){
if (p1->left->valid!=-1){
Obhod(p1->left, command, original, unpack, kol, iterator, result);
}
if (p1->right->valid!=-1){
Obhod(p1->right, command, original, unpack, kol, iterator, result);
}
if ((p1->left->valid==-1)&&(p1->right->valid==-1))
p1->valid=-1;
return;
}
if ((p1->left!=NULL)&&(p1->right==NULL)){
if (p1->left->valid!=-1){
Obhod(p1->left, command, original, unpack, kol, iterator, result);
}
if (p1->left->valid==-1)
p1->valid=-1;
return;
}
if ((p1->left==NULL)&&(p1->right!=NULL)){
if (p1->right->valid!=-1)
Obhod(p1->right, command, original, unpack, kol, iterator, result);
if (p1->right->valid==-1)
p1->valid=-1;
return;
}
if ((p1->left==NULL)&&(p1->right==NULL)){
if ((p1->valid==0)){
//printf("Next Leaf=%s\n", p1->pointer->instruction);
//printf("Command=%s\n", command);
//kol++;
InstructionList* po2=p1->pointer;
InstructionList* po3=NULL;
int flag;
flag=unpack.FindNextInstruction(po2, po3, command, iterator);
if (result<flag){
result=flag;
}
if (flag==-1){
//printf("flag=-1\n");
//printf("%s\n", po2->instruction);
p1->valid=-1;
return;
}
if ((flag==1)||(flag==2)||(flag==3)){
original=p1->pointer;
p1->valid=1;
p1->left=new Instruction_Leaf;
Instruction_Leaf* p2=p1->left;
p2->pointer=po2;
p2->valid=0;
p2->left=NULL;
p2->right=NULL;
kol++;
return;
}
if (flag==0){
original=p1->pointer;
p1->valid=1;
p1->left=new Instruction_Leaf;
Instruction_Leaf* p2=p1->left;
p2->pointer=po2;
p2->valid=0;
p2->left=NULL;
p2->right=NULL;
if (po3!=NULL){
//printf("Right Leaf %s\n", po3->instruction);
p1->right=new Instruction_Leaf;
p2=p1->right;
p2->pointer=po3;
p2->valid=0;
p2->left=NULL;
p2->right=NULL;
}
else {
//printf("Right Leaf=NULL\n");
}
kol++;
}
}
}
}
void Instruction_Tree::Obhod2(Instruction_Leaf* & p1){
Instruction_Tree* p2;
if ((p1->left!=NULL)&&(p1->right!=NULL)){
//printf("(p1->left!=NULL)&&(p1->right!=NULL)\n");
//printf("instruction=%s\n valid=%d\n", p1->pointer->instruction, p1->valid);
Obhod2(p1->left);
Obhod2(p1->right);
return;
}
if ((p1->left!=NULL)&&(p1->right==NULL)){
//printf("(p1->left!=NULL)&&(p1->right==NULL)\n");
//printf("instruction=%s\n valid=%d\n", p1->pointer->instruction, p1->valid);
Obhod2(p1->left);
return;
}
if ((p1->left==NULL)&&(p1->right!=NULL)){
//printf("(p1->left==NULL)&&(p1->right!=NULL)\n");
//printf("instruction=%s\n valid=%d\n", p1->pointer->instruction, p1->valid);
Obhod2(p1->right);
return;
}
if ((p1->left==NULL)&&(p1->right==NULL)){
//printf("(p1->left==NULL)&&(p1->right==NULL)\n");
//printf("instruction=%s\n valid=%d\n", p1->pointer->instruction, p1->valid);
return;
}
}
int Instruction_Tree::Check(Instruction_Leaf* & p1, InstructionList* & po1,
InstructionList* & po2, InstructionList* & po3,
InstructionList* & original, bool & flag){
if ((p1->left!=NULL)&&(p1->right!=NULL)){
//printf("(p1->left!=NULL)&&(p1->right!=NULL)\n");
//printf("instruction=%s\n valid=%d\n", p1->pointer->instruction, p1->valid);
if (p1->left->valid!=-1){
int temp=Check(p1->left, po1, po2, po3, original, flag);
if (temp==1){
po1=p1->pointer;
po2=p1->left->pointer;
//printf("p1->pointer->instruction=%s\n", p1->pointer->instruction);
//printf("command=%s\n", original->instruction);
if (strcmp(p1->pointer->instruction, original->instruction)!=0){
flag=false;
}
}
}
if (p1->right->valid!=-1){
int temp=Check(p1->right, po1, po2, po3, original, flag);
if (temp==1){
po1=p1->pointer;
po3=p1->right->pointer;
//printf("p1->pointer->instruction=%s\n", p1->pointer->instruction);
//printf("command=%s\n", original->instruction);
if (strcmp(p1->pointer->instruction, original->instruction)!=0){
flag=false;
}
}
}
return 0;
}
if ((p1->left!=NULL)&&(p1->right==NULL)){
//printf("(p1->left!=NULL)&&(p1->right==NULL)\n");
//printf("instruction=%s\n valid=%d\n", p1->pointer->instruction, p1->valid);
if (p1->left->valid!=-1){
int temp=Check(p1->left, po1, po2, po3, original, flag);
if (temp==1){
po1=p1->pointer;
po2=p1->left->pointer;
//printf("p1->pointer->instruction=%s\n", p1->pointer->instruction);
//printf("command=%s\n", original->instruction);
if (strcmp(p1->pointer->instruction, original->instruction)!=0){
flag=false;
}
}
}
return 0;
}
if ((p1->left==NULL)&&(p1->right!=NULL)){
//printf("(p1->left==NULL)&&(p1->right!=NULL)\n");
//printf("instruction=%s\n valid=%d\n", p1->pointer->instruction, p1->valid);
if (p1->right->valid!=-1){
int temp=Check(p1->right, po1, po2, po3, original, flag);
if (temp==1){
po1=p1->pointer;
po3=p1->right->pointer;
//printf("p1->pointer->instruction=%s\n", p1->pointer->instruction);
//printf("command=%s\n", original->instruction);
if (strcmp(p1->pointer->instruction, original->instruction)!=0){
flag=false;
}
}
}
return 0;
}
if ((p1->left==NULL)&&(p1->left==NULL)&&(p1->right==NULL)&&(p1->right==NULL)){
//printf("(p1->left==NULL)&&(p1->right==NULL)\n");
return 1;
}
}
void Instruction_Tree::Destructor(Instruction_Leaf* & p1){
//printf("Destructor\n");
if ((p1->left!=NULL)&&(p1->right!=NULL)){
Destructor(p1->left);
Destructor(p1->right);
delete p1;
p1=NULL;
return;
}
if ((p1->left!=NULL)&&(p1->right==NULL)){
Destructor(p1->left);
delete p1;
p1=NULL;
return;
}
if ((p1->left==NULL)&&(p1->right!=NULL)){
Destructor(p1->right);
delete p1;
p1=NULL;
return;
}
if ((p1->left==NULL)&&(p1->right==NULL)){
delete p1;
p1=NULL;
return;
}
}
Instruction_Tree::~Instruction_Tree(){
if (root!=NULL)
Destructor(root);
}
bool Instruction_Tree::Is_Empty(){
if (root==NULL)
return true;
return false;
}
///////////////////////////////////////////////////////
PE_Reader::PE_Reader(const char* name){
int i=0;
filename=new char[strlen(name)+1];
while(name[i]!='\0'){
filename[i]=name[i];
i++;
}
filename[i]='\0';
header_offset=0;
table_offset=0;
text_offset=0;
data_offset=0;
text_size=0;
data_size=0;
number_of_entries=0;
entry_point_rva=0;
image_base=0;
text_address_rva=0;
if ((fp = fopen(filename, "r+b")) == NULL){
perror(filename);
//printf("This Error may be because of the symbols in file name!\n");
exit(1);
}
}
PE_Reader::PE_Reader(const PE_Reader* pe_reader){
int i=0;
if (filename!=NULL)
delete []filename;
filename=new char[strlen(pe_reader->filename)+1];
while (pe_reader->filename[i]!='\0'){
filename[i]=pe_reader->filename[i];
i++;
}
filename[i]='\0';
fp=pe_reader->fp;
header_offset=pe_reader->header_offset;
table_offset=pe_reader->table_offset;
text_offset=pe_reader->text_offset;
data_offset=pe_reader->data_offset;
text_size=pe_reader->text_size;
data_size=pe_reader->data_size;
number_of_entries=pe_reader->number_of_entries;
entry_point_rva=pe_reader->entry_point_rva;
image_base=pe_reader->image_base;
text_address_rva=pe_reader->text_address_rva;
}
PE_Reader::~PE_Reader(){
delete []filename;
fclose(fp);
}
void PE_Reader::PE_HeaderOffset(){
int offset=60;
int origin=0;
fseek(fp, offset, origin);
int header1=getc(fp);
int header2=getc(fp);
int header3=getc(fp);
int header4=getc(fp);
header_offset=header4*pow(16,6)+header3*pow(16,4)+header2*pow(16,2)+header1;
//printf("PE_HEADER_OFFSET=%d\n", header_offset);
}
void PE_Reader::PE_TableOffset(){
table_offset=header_offset+248;
}
void PE_Reader::PE_FileType() const{
int origin=0;
fseek(fp, header_offset, origin);
int format1=getc(fp);
int format2=getc(fp);
int format3=getc(fp);
int format4=getc(fp);
int format=format4*pow(16,6)+format3*pow(16,4)+format2*pow(16,2)+format1;
if (format==0x4550){
//printf("FORMAT = Portable Executable \n");
return;
}
else
throw "Unknown format\n";
}
void PE_Reader:: PE_NumberOfEntries(){
int origin=0;
int number_of_entries_offset=header_offset+6;
fseek(fp, number_of_entries_offset, origin);
int number_of_entries1=getc(fp);
int number_of_entries2=getc(fp);
number_of_entries=number_of_entries2*pow(16,2)+number_of_entries1;
//printf("Number_of_entries=%d\n", number_of_entries);
}
void PE_Reader::PE_EntryPoint(){
int origin=0;
int entry_point_offset=header_offset+40;
fseek(fp, entry_point_offset, origin);
int entry_point1=getc(fp);
int entry_point2=getc(fp);
int entry_point3=getc(fp);
int entry_point4=getc(fp);
entry_point_rva=entry_point4*pow(16,6)+entry_point3*pow(16,4)+entry_point2*pow(16,2)+entry_point1;
//printf("Entry_Point_Rva=%d\n", entry_point_rva);
}
void PE_Reader::PE_ImageBase(){
int origin=0;
int image_base_offset=header_offset+52;
fseek(fp, image_base_offset, origin);
int image_base1=getc(fp);
int image_base2=getc(fp);
int image_base3=getc(fp);
int image_base4=getc(fp);
image_base=image_base4*pow(16,6)+image_base3*pow(16,4)+image_base2*pow(16,2)+image_base1;
//printf("Image_base=%d\n", image_base);
}
void PE_Reader::PE_ObjectTable(){
int origin=0;
int section_offset=0;
char section_name[8];
int address_mass[number_of_entries];
int size_mass[number_of_entries];
int table_offset_mass[number_of_entries];
for (int i=0; i<number_of_entries; i++){
int table_offset=header_offset+248+section_offset;
fseek(fp, table_offset, origin);
table_offset_mass[i]=table_offset;
for (int j=0;j<8; j++){
section_name[j]=getc(fp);
}
section_name[8]='\0';
int section_address_offset=table_offset+12;
fseek(fp, section_address_offset, origin);
int section_address_rva1=getc(fp);
int section_address_rva2=getc(fp);
int section_address_rva3=getc(fp);
int section_address_rva4=getc(fp);
address_mass[i]=section_address_rva4*pow(16,6)+section_address_rva3*pow(16,4)+section_address_rva2*pow(16,2)+section_address_rva1;
int section_size_offset=table_offset+16;
fseek(fp, section_size_offset, origin);
int section_size1=getc(fp);
int section_size2=getc(fp);
int section_size3=getc(fp);
int section_size4=getc(fp);
size_mass[i]=section_size4*pow(16,6)+section_size3*pow(16,4)+section_size2*pow(16,2)+section_size1;
//printf("Section_name %s\n", section_name);
//printf("Section_address_rva %d\n", address_mass[i]);
//printf("Section_size %d\n", size_mass[i]);
section_offset=section_offset+40;
}
for (int i=0; i<number_of_entries; i++){
if ((entry_point_rva>=address_mass[i])&&(entry_point_rva<=address_mass[i]+size_mass[i])){
//printf("Text section has number=%d\n", i+1);
text_address_rva=address_mass[i];
text_size=size_mass[i];
PE_CodeSection(table_offset_mass[i]);
}
}
}
void PE_Reader::PE_CodeSection(int section_offset){
int origin=0;
int text_address_offset=section_offset+12;
fseek(fp, text_address_offset, origin);
int text_address_rva1=getc(fp);
int text_address_rva2=getc(fp);
int text_address_rva3=getc(fp);
int text_address_rva4=getc(fp);
text_address_rva=text_address_rva4*pow(16,6)+text_address_rva3*pow(16,4)+text_address_rva2*pow(16,2)+text_address_rva1;
//printf("Text_address_rva=%d\n", text_address_rva);
int text_size_offset=section_offset+16;
fseek(fp, text_size_offset, origin);
int text_size1=getc(fp);
int text_size2=getc(fp);
int text_size3=getc(fp);
int text_size4=getc(fp);
text_size=text_size4*pow(16,6)+text_size3*pow(16,4)+text_size2*pow(16,2)+text_size1;
//printf("Code_size%d\n", text_size);
int table_text_offset=section_offset+20;
fseek(fp, table_text_offset, origin);
int base_of_text1=getc(fp);
int base_of_text2=getc(fp);
text_offset=base_of_text2*pow(16,2)+base_of_text1;
//printf("Code_offset=%d\n", text_offset);
}
void PE_Reader::PE_DataSection(int section_offset){
int origin=0;
int data_size_offset=section_offset+16;
fseek(fp, data_size_offset, origin);
int data_size1=getc(fp);
int data_size2=getc(fp);
int data_size3=getc(fp);
int data_size4=getc(fp);
data_size=data_size4*pow(16,6)+data_size3*pow(16,4)+data_size2*pow(16,2)+data_size1;
//printf("Data_size%d\n", data_size);
int table_data_offset=section_offset+20;
fseek(fp, table_data_offset, origin);
int table_base_of_data1=getc(fp);
int table_base_of_data2=getc(fp);
data_offset=table_base_of_data2*pow(16,2)+table_base_of_data1;
//printf("Data_offset=%d\n", data_offset);
}
void PE_Reader::PE_LoadForStaticAnalise(char* &buf, int& len, int& start){
PE_HeaderOffset();
PE_FileType();
PE_TableOffset();
PE_NumberOfEntries();
PE_EntryPoint();
//PE_ImageBase();//not need in static analise
PE_ObjectTable();
//int physical_entry_point=entry_point_rva-text_address_rva+text_offset;
start=entry_point_rva-text_address_rva;
//printf("Entry_point_rva=%d\n", entry_point_rva);
//printf("Text_address_rva=%d\n", text_address_rva);
//printf("Text_offset=%d\n", text_offset);
//printf("Physical_entry_point!!!=%d\n", physical_entry_point);
//printf("Entry_point %d\n", start);
len=text_size;
int origin=0;
fseek(fp, text_offset, origin);
buf=new char[text_size];
if (buf==NULL){
//printf("Memory error in static analise\n");
exit (2);
}
fread(buf, 1, text_size, fp);
};
void PE_Reader::PE_LoadForDynamicAnalise(char* &buf, int& len, int & start, int & offset){
PE_HeaderOffset();
PE_FileType();
PE_TableOffset();
PE_NumberOfEntries();
PE_EntryPoint();
PE_ImageBase();
PE_ObjectTable();
//printf("Entry_point_rva=%d\n", entry_point_rva);
//printf("Text_address_rva=%d\n", text_address_rva);
//printf("Text_offset=%d\n", text_offset);
start=entry_point_rva-text_address_rva+text_offset;
offset=text_offset;
//printf("Physical_entry_point!!!=%d\n", start);
int file_size;
fseek(fp, 0, SEEK_END);
file_size = ftell(fp);
len=file_size;
buf=new char[len];
if (buf==NULL){
//printf("Memory error in dynamic analise\n");
exit (2);
}
fseek(fp, 0, 0);
int result = fread(buf, 1, file_size, fp);
if (result!=file_size){
//printf("Error in reading in dynamic analise\n");
exit (2);
}
//printf("FILE_SIZE=%d\n", file_size);
};
Emulator_LibEmu::Emulator_LibEmu(){
e = emu_new();
cpu = emu_cpu_get(e);
mem = emu_memory_get(e);
}
Emulator_LibEmu::~Emulator_LibEmu(){
emu_free(e);
}
Stack::~Stack(){
List_of_Return_Addresses *p1=start;
List_of_Return_Addresses *p2;
while (p1!=NULL){
p2=p1->pointer;
delete p1;
p1=p2;
}
}
void Stack::Push(const int a){
if (start==NULL){
start=new List_of_Return_Addresses;
start->address=a;
start->pointer=NULL;
return;
}
List_of_Return_Addresses* p=start;
while(p->pointer!=NULL){
p=p->pointer;
}
p->pointer=new List_of_Return_Addresses;
p=p->pointer;
p->address=a;
p->pointer=NULL;
return;
}
void Stack::Pop(int & a){
if (start==NULL){
//printf("Stack is empty! Don't know where to go!\n");
//exit(0);
//exit(33);
a=-1;
return;
}
List_of_Return_Addresses* p=start;
List_of_Return_Addresses* p2=start;
int kol=0;
while(p->pointer!=NULL){
p2=p;
p=p->pointer;
kol++;
}
a=p->address;
delete p;
if (kol==0)
start=NULL;
else
p2->pointer=NULL;
return;
}
void Stack::Print(){
List_of_Return_Addresses* p=start;
while (p!=NULL){
//printf("Address=%d\n", p->address);
p=p->pointer;
}
}
PolyUnpack::PolyUnpack(){
start=NULL;
n=4;
buf_size=0;
offset=0;
step_kol=10000;
invalid_instr_kol=0;
emulator=new Emulator_LibEmu;
}
PolyUnpack::~PolyUnpack(){
InstructionList* p1=start;
InstructionList* p2;
while(p1!=NULL){
p2=p1->pointer;
delete []p1->instruction;
delete p1;
p1=p2;
}
delete emulator;
}
void PolyUnpack::AddInstruction(InstructionList* &p1, const char* string, const int number, const int length, const bool flag){
if (start==NULL){
start=new InstructionList;
p1=start;
}
else{
p1->pointer=new InstructionList;
p1=p1->pointer;
}
p1->instruction=new char[strlen(string)+1];
int i=0;
while (string[i]!='\0'){
p1->instruction[i]=string[i];
i++;
}
p1->instruction[i]='\0';
/*
int j=0, k=n;
p1->instruction=new char[k];
while(string[j]!='\0'){
if(j==k){
k=k*2;
free(p1->instruction);
p1->instruction=new char[k];
for (int l=0; l<j; l++)
p1->instruction[l]=string[l];
}
p1->instruction[j]=string[j];
j++;
}
p1->instruction[j]='\0';
*/
p1->number=number;
p1->length=length;
p1->first=flag;
p1->padding=false;
p1->pointer=NULL;
}
void PolyUnpack::FindPadding() const{
InstructionList* p1=start;
InstructionList* p2=NULL;
while (p1!=NULL){
if (strcmp(p1->instruction, "add [eax],al")==0){
if (p2==NULL)
p2=p1;
}
else{
p2=NULL;
}
p1=p1->pointer;
}
p1=p2;
while (p1!=NULL){
p1->padding=true;
p1=p1->pointer;
}
}
void PolyUnpack::StaticAnalise(const char* FileName, const int key=0){
INSTRUCTION inst; // declare struct INSTRUCTION
INSTRUCTION* inst_pointer=NULL;
InstructionList* p1=NULL;
char *data=NULL;
int i=0, k=n, c=0, bytes=8, size=0, len=0, number=0;
char string[32];
PE_Reader reader(FileName);
int entry_point=0;
bool flag=false;
reader.PE_LoadForStaticAnalise(data, size, entry_point);
//printf("Static analize entry=%d\n", entry_point);
if(size==0){
//printf("There are no code section in this file. Don't know what to do!\n");
exit(-1);
}
while (c < size) {
/*
* get_instruction() has the following parameters:
*
* - &inst is a pointer to struct INSTRUCTION
* - data + c is pointer to data to be disassembled
* - disassemble in 32-bit mode: MODE_32
*/
inst_pointer=&inst;
len=get_instruction(inst_pointer, (BYTE*)(data + c), MODE_32);
// Illegal opcode or opcode longer than remaining buffer
if (!len || (len + c > size)){
if (key==1){
if (!len)
printf("Illegal opcode\n");
else
printf("Else brunch\n");
printf("%.8x ", c);
printf("db 0x%.2x\n", *(data + c));
}
c++;
continue;
}
if (key==1)
printf("%.8x ", c);
if ((bytes)&&(key==1)){
for (i = 0; i < ((bytes) < (len) ? (bytes) : (len)); i++)
printf("%.2x", data[c + i]);
printf(" ");
for (i = (bytes) < ((len) ? (bytes) : (len)); i < bytes*2 - len; i++)
printf(" ");
}
/*
* Print absolute offset and raw data bytes up to 'bytes'
* (not needed, but looks nice).
*
*/
/*
* Print the parsed instruction, format using user-supplied
* format. We could of course format the instruction in some
* other way by accessing struct INSTRUCTION members directly.
*/
inst_pointer=&inst;
get_instruction_string(inst_pointer, Format(FORMAT_INTEL), (DWORD)c, string, sizeof(string));
if (key==1)
printf("%s\n", string);
if (c==entry_point){
//printf("THIS IS ENTRY_POINT\n");
flag=true;
}
else
flag=false;
AddInstruction(p1, string, c , len, flag);
c=c+len;
}
FindPadding();
delete []data;
//printf("+++++++++++++++++++++++++++++++++++++++++++++\n");
}
void Emulator_LibEmu::begin(char* & buf, const int len, const int entry_point){
for (int i=0; i<8; i++) {
emu_cpu_reg32_set(cpu, (emu_reg32) i, 0);
}
emu_cpu_reg32_set(cpu, esp, 0x1000000);
emu_memory_clear(mem);
emu_memory_write_block(mem, 0x20000000L, (void*)buf, len);
/*int i=0;
for (i=0; i<len; i++){
emu_memory_write_byte(mem, 0x20000000L+i, buf[i]);
}
*/
emu_memory_write_byte(mem, 0x20000000L+len, '\xcc');
jump(entry_point);
}
void Emulator_LibEmu::jump(const uint pos){
emu_cpu_eip_set(cpu, 0x20000000L+pos);
}
bool Emulator_LibEmu::step(){
if (emu_cpu_parse(cpu) != 0) {
//printf("Bad parse\n");
return false;
}
if (emu_cpu_step(cpu) != 0) {
//printf("Bad Step\n");
return false;
}
return true;
}
bool Emulator_LibEmu::get_command(char *buff, uint size) const{
emu_memory_read_block(mem, emu_cpu_eip_get(cpu), buff, size);
return true;
}
unsigned int Emulator_LibEmu::get_register(const Register reg) const{
switch (reg) {
case EAX:
return emu_cpu_reg32_get(cpu, eax);
case EBX:
return emu_cpu_reg32_get(cpu, ebx);
case ECX:
return emu_cpu_reg32_get(cpu, ecx);
case EDX:
return emu_cpu_reg32_get(cpu, edx);
case ESI:
return emu_cpu_reg32_get(cpu, esi);
case EDI:
return emu_cpu_reg32_get(cpu, edi);
case ESP:
return emu_cpu_reg32_get(cpu, esp);
case EBP:
return emu_cpu_reg32_get(cpu, ebp);
case EIP:
return emu_cpu_eip_get(cpu);
default:;
}
return 0;
}
unsigned int Emulator_LibEmu::get_register(char* &string) const{
int i=0;
int delta='A'-'a';
while (string[i]!='\0'){
if ((string[i]>='A')&&(string[i]<='Z'))
string[i]=string[i]-delta;
i++;
}
if (strcmp(string,"eip")==0)
return emu_cpu_eip_get(cpu);
if (strcmp(string,"ebp")==0)
return emu_cpu_reg32_get(cpu, ebp);
if (strcmp(string,"esp")==0)
return emu_cpu_reg32_get(cpu, esp);
if (strcmp(string,"esi")==0)
return emu_cpu_reg32_get(cpu, esi);
if (strcmp(string,"edi")==0)
return emu_cpu_reg32_get(cpu, edi);
if (strcmp(string,"eax")==0)
return emu_cpu_reg32_get(cpu, eax);
if (strcmp(string,"ebx")==0)
return emu_cpu_reg32_get(cpu, ebx);
if (strcmp(string,"ecx")==0)
return emu_cpu_reg32_get(cpu, ecx);
if (strcmp(string,"edx")==0)
return emu_cpu_reg32_get(cpu, edx);
return 0;
}
bool PolyUnpack::FindFirstInstruction(InstructionList* & p) const{
InstructionList* point=start;