-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathadd_o-llvm(fixed).patch
4359 lines (4349 loc) · 170 KB
/
add_o-llvm(fixed).patch
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
Index: include/llvm/CryptoUtils.h
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/include/llvm/CryptoUtils.h b/include/llvm/CryptoUtils.h
new file mode 100644
--- /dev/null (revision 7b8dceb4f93eb9b400416afd01458d75aaf1b488)
+++ b/include/llvm/CryptoUtils.h (revision 7b8dceb4f93eb9b400416afd01458d75aaf1b488)
@@ -0,0 +1,264 @@
+//===- CryptoUtils.h - Cryptographically Secure Pseudo-Random
+// Generator------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains includes and defines for the AES CTR PRNG
+// The AES implementation has been derived and adapted
+// from libtomcrypt (see http://libtom.org)
+// Created on: 22 juin 2012
+// Author(s): jrinaldini, pjunod
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CryptoUtils_H
+#define LLVM_CryptoUtils_H
+
+#include "llvm/Support/ManagedStatic.h"
+
+#include <stdint.h>
+#include <cstdio>
+#include <string>
+
+namespace llvm {
+
+class CryptoUtils;
+extern ManagedStatic<CryptoUtils> cryptoutils;
+
+#define BYTE(x, n) (((x) >> (8 * (n))) & 0xFF)
+
+#if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
+ defined(INTEL_CC)
+
+#ifndef ENDIAN_LITTLE
+#define ENDIAN_LITTLE
+#endif
+#define ENDIAN_32BITWORD
+#define UNALIGNED
+
+#elif defined(__alpha)
+
+#ifndef ENDIAN_LITTLE
+#define ENDIAN_LITTLE
+#endif
+#define ENDIAN_64BITWORD
+
+#elif defined(__x86_64__)
+
+#ifndef ENDIAN_LITTLE
+#define ENDIAN_LITTLE
+#endif
+#define ENDIAN_64BITWORD
+#define UNALIGNED
+
+#elif(defined(__R5900) || defined(R5900) || defined(__R5900__)) && \
+ (defined(_mips) || defined(__mips__) || defined(mips))
+
+#ifndef ENDIAN_LITTLE
+#define ENDIAN_LITTLE
+#endif
+#define ENDIAN_64BITWORD
+
+#elif defined(__sparc)
+
+#ifndef ENDIAN_BIG
+#define ENDIAN_BIG
+#endif
+#if defined(__arch64__)
+#define ENDIAN_64BITWORD
+#else
+#define ENDIAN_32BITWORD
+#endif
+
+#endif
+
+#if defined(__BIG_ENDIAN__) || defined(_BIG_ENDIAN)
+#define ENDIAN_BIG
+#endif
+
+#if !defined(ENDIAN_BIG) && !defined(ENDIAN_LITTLE)
+#error \
+ "Unknown endianness of the compilation platform, check this header aes_encrypt.h"
+#endif
+
+#ifdef ENDIAN_LITTLE
+
+#define STORE32H(y, x) \
+ { \
+ (y)[0] = (uint8_t)(((x) >> 24) & 0xFF); \
+ (y)[1] = (uint8_t)(((x) >> 16) & 0xFF); \
+ (y)[2] = (uint8_t)(((x) >> 8) & 0xFF); \
+ (y)[3] = (uint8_t)(((x) >> 0) & 0xFF); \
+ }
+#define LOAD32H(x, y) \
+ { \
+ (x) = ((uint32_t)((y)[0] & 0xFF) << 24) | \
+ ((uint32_t)((y)[1] & 0xFF) << 16) | \
+ ((uint32_t)((y)[2] & 0xFF) << 8) | ((uint32_t)((y)[3] & 0xFF) << 0); \
+ }
+
+#define LOAD64H(x, y) \
+ { \
+ (x) = ((uint64_t)((y)[0] & 0xFF) << 56) | \
+ ((uint64_t)((y)[1] & 0xFF) << 48) | \
+ ((uint64_t)((y)[2] & 0xFF) << 40) | \
+ ((uint64_t)((y)[3] & 0xFF) << 32) | \
+ ((uint64_t)((y)[4] & 0xFF) << 24) | \
+ ((uint64_t)((y)[5] & 0xFF) << 16) | \
+ ((uint64_t)((y)[6] & 0xFF) << 8) | ((uint64_t)((y)[7] & 0xFF) << 0); \
+ }
+
+#define STORE64H(y, x) \
+ { \
+ (y)[0] = (uint8_t)(((x) >> 56) & 0xFF); \
+ (y)[1] = (uint8_t)(((x) >> 48) & 0xFF); \
+ (y)[2] = (uint8_t)(((x) >> 40) & 0xFF); \
+ (y)[3] = (uint8_t)(((x) >> 32) & 0xFF); \
+ (y)[4] = (uint8_t)(((x) >> 24) & 0xFF); \
+ (y)[5] = (uint8_t)(((x) >> 16) & 0xFF); \
+ (y)[6] = (uint8_t)(((x) >> 8) & 0xFF); \
+ (y)[7] = (uint8_t)(((x) >> 0) & 0xFF); \
+ }
+
+#endif /* ENDIAN_LITTLE */
+
+#ifdef ENDIAN_BIG
+
+#define STORE32H(y, x) \
+ { \
+ (y)[3] = (uint8_t)(((x) >> 24) & 0xFF); \
+ (y)[2] = (uint8_t)(((x) >> 16) & 0xFF); \
+ (y)[1] = (uint8_t)(((x) >> 8) & 0xFF); \
+ (y)[0] = (uint8_t)(((x) >> 0) & 0xFF); \
+ }
+#define STORE64H(y, x) \
+ { \
+ (y)[7] = (uint8_t)(((x) >> 56) & 0xFF); \
+ (y)[6] = (uint8_t)(((x) >> 48) & 0xFF); \
+ (y)[5] = (uint8_t)(((x) >> 40) & 0xFF); \
+ (y)[4] = (uint8_t)(((x) >> 32) & 0xFF); \
+ (y)[3] = (uint8_t)(((x) >> 24) & 0xFF); \
+ (y)[2] = (uint8_t)(((x) >> 16) & 0xFF); \
+ (y)[1] = (uint8_t)(((x) >> 8) & 0xFF); \
+ (y)[0] = (uint8_t)(((x) >> 0) & 0xFF); \
+ }
+#define LOAD32H(x, y) \
+ { \
+ (x) = ((uint32_t)((y)[3] & 0xFF) << 24) | \
+ ((uint32_t)((y)[2] & 0xFF) << 16) | \
+ ((uint32_t)((y)[1] & 0xFF) << 8) | ((uint32_t)((y)[0] & 0xFF) << 0); \
+}
+
+#define LOAD64H(x, y) \
+ { \
+ (x) = ((uint64_t)((y)[7] & 0xFF) << 56) | \
+ ((uint64_t)((y)[6] & 0xFF) << 48) | \
+ ((uint64_t)((y)[5] & 0xFF) << 40) | \
+ ((uint64_t)((y)[4] & 0xFF) << 32) | \
+ ((uint64_t)((y)[3] & 0xFF) << 24) | \
+ ((uint64_t)((y)[2] & 0xFF) << 16) | \
+ ((uint64_t)((y)[1] & 0xFF) << 8) | ((uint64_t)((y)[0] & 0xFF) << 0); \
+ }
+
+#endif /* ENDIAN_BIG */
+
+#define AES_TE0(x) AES_PRECOMP_TE0[(x)]
+#define AES_TE1(x) AES_PRECOMP_TE1[(x)]
+#define AES_TE2(x) AES_PRECOMP_TE2[(x)]
+#define AES_TE3(x) AES_PRECOMP_TE3[(x)]
+
+#define AES_TE4_0(x) AES_PRECOMP_TE4_0[(x)]
+#define AES_TE4_1(x) AES_PRECOMP_TE4_1[(x)]
+#define AES_TE4_2(x) AES_PRECOMP_TE4_2[(x)]
+#define AES_TE4_3(x) AES_PRECOMP_TE4_3[(x)]
+
+#define CryptoUtils_POOL_SIZE (0x1 << 17) // 2^17
+
+#define DUMP(x, l, s) \
+ fprintf(stderr, "%s :", (s)); \
+ for (int ii = 0; ii < (l); ii++) { \
+ fprintf(stderr, "%02hhX", *((x) + ii)); \
+ } \
+ fprintf(stderr, "\n");
+
+// SHA256
+/* Various logical functions */
+#define Ch(x, y, z) (z ^ (x &(y ^ z)))
+#define Maj(x, y, z) (((x | y) & z) | (x &y))
+#define S(x, n) RORc((x), (n))
+#define R1(x, n) (((x) & 0xFFFFFFFFUL) >> (n))
+#define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
+#define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
+#define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R1(x, 3))
+#define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R1(x, 10))
+#define MIN(x, y) (((x) < (y)) ? (x) : (y))
+
+#define RND(a, b, c, d, e, f, g, h, i, ki) \
+ t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
+ t1 = Sigma0(a) + Maj(a, b, c); \
+ d += t0; \
+ h = t0 + t1;
+
+#define RORc(x, y) \
+ (((((unsigned long)(x) & 0xFFFFFFFFUL) >> (unsigned long)((y) & 31)) | \
+ ((unsigned long)(x) << (unsigned long)(32 - ((y) & 31)))) & \
+ 0xFFFFFFFFUL)
+
+class CryptoUtils {
+public:
+ CryptoUtils();
+ ~CryptoUtils();
+
+ char *get_seed();
+ void get_bytes(char *buffer, const int len);
+ char get_char();
+ bool prng_seed(const std::string seed);
+
+ // Returns a uniformly distributed 8-bit value
+ uint8_t get_uint8_t();
+ // Returns a uniformly distributed 32-bit value
+ uint32_t get_uint32_t();
+ // Returns an integer uniformly distributed on [0, max[
+ uint32_t get_range(const uint32_t max);
+ // Returns a uniformly distributed 64-bit value
+ uint64_t get_uint64_t();
+
+ // Scramble a 32-bit value depending on a 128-bit value
+ unsigned scramble32(const unsigned in, const char key[16]);
+
+ int sha256(const char *msg, unsigned char *hash);
+
+private:
+ uint32_t ks[44];
+ char key[16];
+ char ctr[16];
+ char pool[CryptoUtils_POOL_SIZE];
+ uint32_t idx;
+ std::string seed;
+ bool seeded;
+
+ typedef struct {
+ uint64_t length;
+ uint32_t state[8], curlen;
+ unsigned char buf[64];
+ } sha256_state;
+
+ void aes_compute_ks(uint32_t *ks, const char *k);
+ void aes_encrypt(char *out, const char *in, const uint32_t *ks);
+ bool prng_seed();
+ void inc_ctr();
+ void populate_pool();
+ int sha256_done(sha256_state *md, unsigned char *out);
+ int sha256_init(sha256_state *md);
+ static int sha256_compress(sha256_state *md, unsigned char *buf);
+ int sha256_process(sha256_state *md, const unsigned char *in,
+ unsigned long inlen);
+};
+}
+
+#endif // LLVM_CryptoUtils_H
+
Index: include/llvm/Transforms/Obfuscation/BogusControlFlow.h
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/include/llvm/Transforms/Obfuscation/BogusControlFlow.h b/include/llvm/Transforms/Obfuscation/BogusControlFlow.h
new file mode 100644
--- /dev/null (revision 7b8dceb4f93eb9b400416afd01458d75aaf1b488)
+++ b/include/llvm/Transforms/Obfuscation/BogusControlFlow.h (revision 7b8dceb4f93eb9b400416afd01458d75aaf1b488)
@@ -0,0 +1,49 @@
+//===- BogusControlFlow.h - BogusControlFlow Obfuscation pass-------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--------------------------------------------------------------------------------===//
+//
+// This file contains includes and defines for the bogusControlFlow pass
+//
+//===--------------------------------------------------------------------------------===//
+
+#ifndef _BOGUSCONTROLFLOW_H_
+#define _BOGUSCONTROLFLOW_H_
+
+
+// LLVM include
+#include "llvm/ADT/Statistic.h"
+#include "llvm/CodeGen/ISDOpcodes.h"
+#include "llvm/CryptoUtils.h"
+#include "llvm/IR/BasicBlock.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/GlobalValue.h"
+#include "llvm/IR/InstrTypes.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/Type.h"
+#include "llvm/Pass.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/Debug.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/Transforms/IPO.h"
+#include "llvm/Transforms/Utils/BasicBlockUtils.h"
+#include "llvm/Transforms/Utils/Cloning.h"
+#include <list>
+
+using namespace std;
+using namespace llvm;
+
+// Namespace
+namespace llvm {
+ Pass *createBogus ();
+ Pass *createBogus (bool flag);
+}
+#endif
+
Index: include/llvm/Transforms/Obfuscation/EncodeFuncName.h
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/include/llvm/Transforms/Obfuscation/EncodeFuncName.h b/include/llvm/Transforms/Obfuscation/EncodeFuncName.h
new file mode 100644
--- /dev/null (revision 7b8dceb4f93eb9b400416afd01458d75aaf1b488)
+++ b/include/llvm/Transforms/Obfuscation/EncodeFuncName.h (revision 7b8dceb4f93eb9b400416afd01458d75aaf1b488)
@@ -0,0 +1,14 @@
+//
+// Created by Smile on 2021/6/27.
+//
+
+#ifndef LLVM_ENCODEFUNCNAME_H
+#define LLVM_ENCODEFUNCNAME_H
+// LLVM include
+#include "llvm/Pass.h"
+
+namespace llvm {
+Pass* createEncodeFuncName();
+} // namespace llvm
+
+#endif // LLVM_ENCODEFUNCNAME_H
\ No newline at end of file
Index: include/llvm/Transforms/Obfuscation/Flattening.h
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/include/llvm/Transforms/Obfuscation/Flattening.h b/include/llvm/Transforms/Obfuscation/Flattening.h
new file mode 100644
--- /dev/null (revision 7b8dceb4f93eb9b400416afd01458d75aaf1b488)
+++ b/include/llvm/Transforms/Obfuscation/Flattening.h (revision 7b8dceb4f93eb9b400416afd01458d75aaf1b488)
@@ -0,0 +1,39 @@
+//===- FlatteningIncludes.h - Flattening Obfuscation pass------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains includes and defines for the flattening pass
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _FLATTENING_INCLUDES_
+#define _FLATTENING_INCLUDES_
+
+
+// LLVM include
+#include "llvm/ADT/Statistic.h"
+#include "llvm/CryptoUtils.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Pass.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Transforms/IPO.h"
+#include "llvm/Transforms/Obfuscation/Utils.h"
+#include "llvm/Transforms/Scalar.h"
+#include "llvm/Transforms/Utils/Local.h" // For DemoteRegToStack and DemotePHIToStack
+
+// Namespace
+using namespace std;
+
+namespace llvm {
+ Pass *createFlattening();
+ Pass *createFlattening(bool flag);
+}
+
+#endif
+
Index: include/llvm/Transforms/Obfuscation/Split.h
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/include/llvm/Transforms/Obfuscation/Split.h b/include/llvm/Transforms/Obfuscation/Split.h
new file mode 100644
--- /dev/null (revision 7b8dceb4f93eb9b400416afd01458d75aaf1b488)
+++ b/include/llvm/Transforms/Obfuscation/Split.h (revision 7b8dceb4f93eb9b400416afd01458d75aaf1b488)
@@ -0,0 +1,35 @@
+//===- FlatteningIncludes.h - Flattening Obfuscation pass------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains includes and defines for the split basicblock pass
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _SPLIT_INCLUDES_
+#define _SPLIT_INCLUDES_
+
+
+// LLVM include
+#include "llvm/ADT/Statistic.h"
+#include "llvm/CryptoUtils.h"
+#include "llvm/IR/BasicBlock.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Pass.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Transforms/IPO.h"
+#include "llvm/Transforms/Obfuscation/Utils.h"
+#include "llvm/Transforms/Scalar.h"
+#include "llvm/Transforms/Utils/Local.h" // For DemoteRegToStack and DemotePHIToStack
+
+// Namespace
+namespace llvm {
+ Pass *createSplitBasicBlock(bool flag);
+}
+#endif
+
Index: include/llvm/Transforms/Obfuscation/StringEncryption.h
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/include/llvm/Transforms/Obfuscation/StringEncryption.h b/include/llvm/Transforms/Obfuscation/StringEncryption.h
new file mode 100644
--- /dev/null (revision 7b8dceb4f93eb9b400416afd01458d75aaf1b488)
+++ b/include/llvm/Transforms/Obfuscation/StringEncryption.h (revision 7b8dceb4f93eb9b400416afd01458d75aaf1b488)
@@ -0,0 +1,11 @@
+//
+// Created by Smile on 2021/6/28.
+//
+
+#ifndef LLVM_STRINGENCRYPTION_H
+#define LLVM_STRINGENCRYPTION_H
+#include "llvm/Pass.h"
+namespace llvm {
+ModulePass *createStringEncryptionPass();
+} // namespace llvm
+#endif // LLVM_STRINGENCRYPTION_H
\ No newline at end of file
Index: include/llvm/Transforms/Obfuscation/Substitution.h
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/include/llvm/Transforms/Obfuscation/Substitution.h b/include/llvm/Transforms/Obfuscation/Substitution.h
new file mode 100644
--- /dev/null (revision 7b8dceb4f93eb9b400416afd01458d75aaf1b488)
+++ b/include/llvm/Transforms/Obfuscation/Substitution.h (revision 7b8dceb4f93eb9b400416afd01458d75aaf1b488)
@@ -0,0 +1,38 @@
+//===- SubstitutionIncludes.h - Substitution Obfuscation pass-------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains includes and defines for the substitution pass
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _SUBSTITUTIONS_H_
+#define _SUBSTITUTIONS_H_
+
+
+// LLVM include
+#include "llvm/ADT/Statistic.h"
+#include "llvm/CryptoUtils.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Pass.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Transforms/IPO.h"
+
+// Namespace
+using namespace llvm;
+using namespace std;
+
+namespace llvm {
+ Pass *createSubstitution ();
+ Pass *createSubstitution (bool flag);
+}
+
+#endif
+
Index: include/llvm/Transforms/Obfuscation/Utils.h
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/include/llvm/Transforms/Obfuscation/Utils.h b/include/llvm/Transforms/Obfuscation/Utils.h
new file mode 100644
--- /dev/null (revision 7b8dceb4f93eb9b400416afd01458d75aaf1b488)
+++ b/include/llvm/Transforms/Obfuscation/Utils.h (revision 7b8dceb4f93eb9b400416afd01458d75aaf1b488)
@@ -0,0 +1,15 @@
+#ifndef __UTILS_OBF__
+#define __UTILS_OBF__
+
+#include "llvm/IR/Function.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/Transforms/Utils/Local.h" // For DemoteRegToStack and DemotePHIToStack
+#include <stdio.h>
+
+using namespace llvm;
+
+void fixStack(Function *f);
+std::string readAnnotate(Function *f);
+bool toObfuscate(bool flag, Function *f, std::string attribute);
+void LowerConstantExpr(llvm::Function &F);
+#endif
Index: lib/Transforms/CMakeLists.txt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/lib/Transforms/CMakeLists.txt b/lib/Transforms/CMakeLists.txt
--- a/lib/Transforms/CMakeLists.txt (revision 5bc995a6e8b37224e94c8b15d9435b08ed73a5a5)
+++ b/lib/Transforms/CMakeLists.txt (revision 7b8dceb4f93eb9b400416afd01458d75aaf1b488)
@@ -8,3 +8,4 @@
add_subdirectory(Hello)
add_subdirectory(ObjCARC)
add_subdirectory(Coroutines)
+add_subdirectory(Obfuscation)
Index: lib/Transforms/IPO/LLVMBuild.txt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/lib/Transforms/IPO/LLVMBuild.txt b/lib/Transforms/IPO/LLVMBuild.txt
--- a/lib/Transforms/IPO/LLVMBuild.txt (revision 5bc995a6e8b37224e94c8b15d9435b08ed73a5a5)
+++ b/lib/Transforms/IPO/LLVMBuild.txt (revision 7b8dceb4f93eb9b400416afd01458d75aaf1b488)
@@ -19,4 +19,4 @@
name = IPO
parent = Transforms
library_name = ipo
-required_libraries = AggressiveInstCombine Analysis BitReader BitWriter Core InstCombine IRReader Linker Object ProfileData Scalar Support TransformUtils Vectorize Instrumentation
+required_libraries = AggressiveInstCombine Analysis BitReader BitWriter Core InstCombine IRReader Linker Object ProfileData Scalar Support TransformUtils Vectorize Instrumentation Obfuscation
Index: lib/Transforms/IPO/PassManagerBuilder.cpp
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/lib/Transforms/IPO/PassManagerBuilder.cpp b/lib/Transforms/IPO/PassManagerBuilder.cpp
--- a/lib/Transforms/IPO/PassManagerBuilder.cpp (revision 5bc995a6e8b37224e94c8b15d9435b08ed73a5a5)
+++ b/lib/Transforms/IPO/PassManagerBuilder.cpp (revision 7b8dceb4f93eb9b400416afd01458d75aaf1b488)
@@ -46,6 +46,14 @@
#include "llvm/Transforms/Vectorize.h"
#include "llvm/Transforms/Vectorize/LoopVectorize.h"
#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
+//ollvm pass
+#include "llvm/CryptoUtils.h"
+#include "llvm/Transforms/Obfuscation/BogusControlFlow.h"
+#include "llvm/Transforms/Obfuscation/EncodeFuncName.h"
+#include "llvm/Transforms/Obfuscation/Flattening.h"
+#include "llvm/Transforms/Obfuscation/Split.h"
+#include "llvm/Transforms/Obfuscation/StringEncryption.h"
+#include "llvm/Transforms/Obfuscation/Substitution.h"
using namespace llvm;
@@ -128,6 +136,27 @@
cl::desc("Enable the simple loop unswitch pass. Also enables independent "
"cleanup passes integrated into the loop pass manager pipeline."));
+// Flags for obfuscation
+static cl::opt<bool> Flattening("fla", cl::init(false),
+ cl::desc("Enable the flattening pass"));
+
+static cl::opt<bool> BogusControlFlow("bcf", cl::init(false),
+ cl::desc("Enable bogus control flow"));
+
+static cl::opt<bool> Substitution("sub", cl::init(false),
+ cl::desc("Enable instruction substitutions"));
+
+static cl::opt<std::string> AesSeed("aesSeed", cl::init(""),
+ cl::desc("seed for the AES-CTR PRNG"));
+
+static cl::opt<bool> Split("split", cl::init(false),
+ cl::desc("Enable basic block splitting"));
+
+static cl::opt<bool> StringEncryption("sobf", cl::init(false),
+ cl::desc("Enable String"));
+
+static cl::opt<bool> EncodeFuncName("fun", cl::init(false),
+ cl::desc("Enable String"));
static cl::opt<bool> EnableGVNSink(
"enable-gvn-sink", cl::init(false), cl::Hidden,
cl::desc("Enable the GVN sinking pass (default = off)"));
@@ -175,6 +204,14 @@
PrepareForThinLTO = EnablePrepareForThinLTO;
PerformThinLTO = EnablePerformThinLTO;
DivergentTarget = false;
+
+ // Initialization of the global cryptographically
+ // secure pseudo-random generator
+ if(!AesSeed.empty()) {
+ if(!llvm::cryptoutils->prng_seed(AesSeed.c_str())) {
+ exit(1);
+ }
+ }
}
PassManagerBuilder::~PassManagerBuilder() {
@@ -441,6 +478,17 @@
// Allow forcing function attributes as a debugging and tuning aid.
MPM.add(createForceFunctionAttrsLegacyPass());
+ //ollvm pass
+ MPM.add(createSplitBasicBlock(Split));
+ MPM.add(createBogus(BogusControlFlow));
+ MPM.add(createLowerSwitchPass());
+ MPM.add(createFlattening(Flattening));
+ if(EncodeFuncName){
+ MPM.add(createEncodeFuncName());
+ }
+ if(StringEncryption){
+ MPM.add(createStringEncryptionPass());
+ }
// If all optimizations are disabled, just run the always-inline pass and,
// if enabled, the function merging pass.
if (OptLevel == 0) {
@@ -468,6 +516,8 @@
MPM.add(createGlobalDCEPass());
}
+ MPM.add(createSubstitution(Substitution));
+
addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
if (PrepareForLTO || PrepareForThinLTO) {
@@ -772,6 +822,8 @@
// flattening of blocks.
MPM.add(createDivRemPairsPass());
+ MPM.add(createSubstitution(Substitution));
+
// LoopSink (and other loop passes since the last simplifyCFG) might have
// resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
MPM.add(createCFGSimplificationPass());
Index: lib/Transforms/LLVMBuild.txt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/lib/Transforms/LLVMBuild.txt b/lib/Transforms/LLVMBuild.txt
--- a/lib/Transforms/LLVMBuild.txt (revision 5bc995a6e8b37224e94c8b15d9435b08ed73a5a5)
+++ b/lib/Transforms/LLVMBuild.txt (revision 7b8dceb4f93eb9b400416afd01458d75aaf1b488)
@@ -15,7 +15,7 @@
;===------------------------------------------------------------------------===;
[common]
-subdirectories = AggressiveInstCombine Coroutines IPO InstCombine Instrumentation Scalar Utils Vectorize ObjCARC
+subdirectories = AggressiveInstCombine Coroutines IPO InstCombine Instrumentation Scalar Utils Vectorize ObjCARC Obfuscation
[component_0]
type = Group
Index: lib/Transforms/Obfuscation/BogusControlFlow.cpp
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/lib/Transforms/Obfuscation/BogusControlFlow.cpp b/lib/Transforms/Obfuscation/BogusControlFlow.cpp
new file mode 100644
--- /dev/null (revision 7b8dceb4f93eb9b400416afd01458d75aaf1b488)
+++ b/lib/Transforms/Obfuscation/BogusControlFlow.cpp (revision 7b8dceb4f93eb9b400416afd01458d75aaf1b488)
@@ -0,0 +1,619 @@
+//===- BogusControlFlow.cpp - BogusControlFlow Obfuscation pass-------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------------------===//
+//
+// This file implements BogusControlFlow's pass, inserting bogus control flow.
+// It adds bogus flow to a given basic block this way:
+//
+// Before :
+// entry
+// |
+// ______v______
+// | Original |
+// |_____________|
+// |
+// v
+// return
+//
+// After :
+// entry
+// |
+// ____v_____
+// |condition*| (false)
+// |__________|----+
+// (true)| |
+// | |
+// ______v______ |
+// +-->| Original* | |
+// | |_____________| (true)
+// | (false)| !-----------> return
+// | ______v______ |
+// | | Altered |<--!
+// | |_____________|
+// |__________|
+//
+// * The results of these terminator's branch's conditions are always true, but these predicates are
+// opacificated. For this, we declare two global values: x and y, and replace the FCMP_TRUE
+// predicate with (y < 10 || x * (x + 1) % 2 == 0) (this could be improved, as the global
+// values give a hint on where are the opaque predicates)
+//
+// The altered bloc is a copy of the original's one with junk instructions added accordingly to the
+// type of instructions we found in the bloc
+//
+// Each basic block of the function is choosen if a random number in the range [0,100] is smaller
+// than the choosen probability rate. The default value is 30. This value can be modify using
+// the option -boguscf-prob=[value]. Value must be an integer in the range [0, 100], otherwise
+// the default value is taken. Exemple: -boguscf -boguscf-prob=60
+//
+// The pass can also be loop many times on a function, including on the basic blocks added in
+// a previous loop. Be careful if you use a big probability number and choose to run the loop
+// many times wich may cause the pass to run for a very long time. The default value is one loop,
+// but you can change it with -boguscf-loop=[value]. Value must be an integer greater than 1,
+// otherwise the default value is taken. Exemple: -boguscf -boguscf-loop=2
+//
+//
+// Defined debug types:
+// - "gen" : general informations
+// - "opt" : concerning the given options (parameter)
+// - "cfg" : printing the various function's cfg before transformation
+// and after transformation if it has been modified, and all
+// the functions at end of the pass, after doFinalization.
+//
+// To use them all, simply use the -debug option.
+// To use only one of them, follow the pass' command by -debug-only=name.
+// Exemple, -boguscf -debug-only=cfg
+//
+//
+// Stats:
+// The following statistics will be printed if you use
+// the -stats command:
+//
+// a. Number of functions in this module
+// b. Number of times we run on each function
+// c. Initial number of basic blocks in this module
+// d. Number of modified basic blocks
+// e. Number of added basic blocks in this module
+// f. Final number of basic blocks in this module
+//
+// file : lib/Transforms/Obfuscation/BogusControlFlow.cpp
+// date : june 2012
+// version: 1.0
+// author : julie.michielin@gmail.com
+// modifications: pjunod, Rinaldini Julien
+// project: Obfuscator
+// option : -boguscf
+//
+//===----------------------------------------------------------------------------------===//
+
+#include "llvm/Transforms/Obfuscation/BogusControlFlow.h"
+#include "llvm/Transforms/Obfuscation/Utils.h"
+
+// Stats
+#define DEBUG_TYPE "BogusControlFlow"
+STATISTIC(NumFunction, "a. Number of functions in this module");
+STATISTIC(NumTimesOnFunctions, "b. Number of times we run on each function");
+STATISTIC(InitNumBasicBlocks, "c. Initial number of basic blocks in this module");
+STATISTIC(NumModifiedBasicBlocks, "d. Number of modified basic blocks");
+STATISTIC(NumAddedBasicBlocks, "e. Number of added basic blocks in this module");
+STATISTIC(FinalNumBasicBlocks, "f. Final number of basic blocks in this module");
+
+
+// Options for the pass
+const int defaultObfRate = 30, defaultObfTime = 1;
+
+static cl::opt<int>
+ObfProbRate("bcf_prob", cl::desc("Choose the probability [%] each basic blocks will be obfuscated by the -bcf pass"), cl::value_desc("probability rate"), cl::init(defaultObfRate), cl::Optional);
+
+static cl::opt<int>
+ObfTimes("bcf_loop", cl::desc("Choose how many time the -bcf pass loop on a function"), cl::value_desc("number of times"), cl::init(defaultObfTime), cl::Optional);
+
+namespace {
+ struct BogusControlFlow : public FunctionPass {
+ static char ID; // Pass identification
+ bool flag;
+ BogusControlFlow() : FunctionPass(ID) {}
+ BogusControlFlow(bool flag) : FunctionPass(ID) {this->flag = flag; BogusControlFlow();}
+
+ /* runOnFunction
+ *
+ * Overwrite FunctionPass method to apply the transformation
+ * to the function. See header for more details.
+ */
+ virtual bool runOnFunction(Function &F){
+ // Check if the percentage is correct
+ if (ObfTimes <= 0) {
+ errs()<<"BogusControlFlow application number -bcf_loop=x must be x > 0";
+ return false;
+ }
+
+ // Check if the number of applications is correct
+ if ( !((ObfProbRate > 0) && (ObfProbRate <= 100)) ) {
+ errs()<<"BogusControlFlow application basic blocks percentage -bcf_prob=x must be 0 < x <= 100";
+ return false;
+ }
+ std::vector<BasicBlock *> orginalBBs;
+ // check for compatible
+ for (BasicBlock &bb : F.getBasicBlockList()) {
+ if (isa<InvokeInst>(bb.getTerminator())) {
+ return false;
+ }
+ }
+ // If fla annotations
+ if(toObfuscate(flag,&F,"bcf")) {
+ bogus(F);
+ doF(*F.getParent());
+ return true;
+ }
+
+ return false;
+ } // end of runOnFunction()
+
+ void bogus(Function &F) {
+ // For statistics and debug
+ ++NumFunction;
+ int NumBasicBlocks = 0;
+ bool firstTime = true; // First time we do the loop in this function
+ bool hasBeenModified = false;
+ DEBUG_WITH_TYPE("opt", errs() << "bcf: Started on function " << F.getName() << "\n");
+ DEBUG_WITH_TYPE("opt", errs() << "bcf: Probability rate: "<< ObfProbRate<< "\n");
+ if(ObfProbRate < 0 || ObfProbRate > 100){
+ DEBUG_WITH_TYPE("opt", errs() << "bcf: Incorrect value,"
+ << " probability rate set to default value: "
+ << defaultObfRate <<" \n");
+ ObfProbRate = defaultObfRate;
+ }
+ DEBUG_WITH_TYPE("opt", errs() << "bcf: How many times: "<< ObfTimes<< "\n");
+ if(ObfTimes <= 0){
+ DEBUG_WITH_TYPE("opt", errs() << "bcf: Incorrect value,"
+ << " must be greater than 1. Set to default: "
+ << defaultObfTime <<" \n");
+ ObfTimes = defaultObfTime;
+ }
+ NumTimesOnFunctions = ObfTimes;
+ int NumObfTimes = ObfTimes;
+
+ // Real begining of the pass
+ // Loop for the number of time we run the pass on the function
+ do{
+ DEBUG_WITH_TYPE("cfg", errs() << "bcf: Function " << F.getName()
+ <<", before the pass:\n");
+ DEBUG_WITH_TYPE("cfg", F.viewCFG());
+ // Put all the function's block in a list
+ std::list<BasicBlock *> basicBlocks;
+ for (Function::iterator i=F.begin();i!=F.end();++i) {
+ basicBlocks.push_back(&*i);
+ }
+ DEBUG_WITH_TYPE("gen", errs() << "bcf: Iterating on the Function's Basic Blocks\n");
+
+ while(!basicBlocks.empty()){
+ NumBasicBlocks ++;
+ // Basic Blocks' selection
+ if((int)llvm::cryptoutils->get_range(100) <= ObfProbRate){
+ DEBUG_WITH_TYPE("opt", errs() << "bcf: Block "
+ << NumBasicBlocks <<" selected. \n");
+ hasBeenModified = true;
+ ++NumModifiedBasicBlocks;
+ NumAddedBasicBlocks += 3;
+ FinalNumBasicBlocks += 3;
+ // Add bogus flow to the given Basic Block (see description)
+ BasicBlock *basicBlock = basicBlocks.front();
+ addBogusFlow(basicBlock, F);
+ }
+ else{
+ DEBUG_WITH_TYPE("opt", errs() << "bcf: Block "
+ << NumBasicBlocks <<" not selected.\n");
+ }
+ // remove the block from the list
+ basicBlocks.pop_front();
+
+ if(firstTime){ // first time we iterate on this function
+ ++InitNumBasicBlocks;
+ ++FinalNumBasicBlocks;
+ }
+ } // end of while(!basicBlocks.empty())
+ DEBUG_WITH_TYPE("gen", errs() << "bcf: End of function " << F.getName() << "\n");
+ if(hasBeenModified){ // if the function has been modified
+ DEBUG_WITH_TYPE("cfg", errs() << "bcf: Function " << F.getName()
+ <<", after the pass: \n");
+ DEBUG_WITH_TYPE("cfg", F.viewCFG());
+ }
+ else{
+ DEBUG_WITH_TYPE("cfg", errs() << "bcf: Function's not been modified \n");
+ }
+ firstTime = false;
+ }while(--NumObfTimes > 0);
+ }
+
+ /* addBogusFlow
+ *
+ * Add bogus flow to a given basic block, according to the header's description
+ */
+ virtual void addBogusFlow(BasicBlock * basicBlock, Function &F){
+
+
+ // Split the block: first part with only the phi nodes and debug info and terminator
+ // created by splitBasicBlock. (-> No instruction)
+ // Second part with every instructions from the original block
+ // We do this way, so we don't have to adjust all the phi nodes, metadatas and so on
+ // for the first block. We have to let the phi nodes in the first part, because they
+ // actually are updated in the second part according to them.
+ Instruction *i1 = &*basicBlock->begin();
+ if(basicBlock->getFirstNonPHIOrDbgOrLifetime())
+ i1 = basicBlock->getFirstNonPHIOrDbgOrLifetime();
+ Twine *var;
+ var = new Twine("originalBB");
+ BasicBlock *originalBB = basicBlock->splitBasicBlock(i1, *var);
+ DEBUG_WITH_TYPE("gen", errs() << "bcf: First and original basic blocks: ok\n");
+
+ // Creating the altered basic block on which the first basicBlock will jump
+ Twine * var3 = new Twine("alteredBB");
+ BasicBlock *alteredBB = createAlteredBasicBlock(originalBB, *var3, &F);
+ DEBUG_WITH_TYPE("gen", errs() << "bcf: Altered basic block: ok\n");
+
+ // Now that all the blocks are created,
+ // we modify the terminators to adjust the control flow.
+
+ alteredBB->getTerminator()->eraseFromParent();
+ basicBlock->getTerminator()->eraseFromParent();
+ DEBUG_WITH_TYPE("gen", errs() << "bcf: Terminator removed from the altered"
+ <<" and first basic blocks\n");
+
+ // Preparing a condition..
+ // For now, the condition is an always true comparaison between 2 float
+ // This will be complicated after the pass (in doFinalization())
+ Value * LHS = ConstantFP::get(Type::getFloatTy(F.getContext()), 1.0);
+ Value * RHS = ConstantFP::get(Type::getFloatTy(F.getContext()), 1.0);
+ DEBUG_WITH_TYPE("gen", errs() << "bcf: Value LHS and RHS created\n");
+
+
+ // The always true condition. End of the first block
+ Twine * var4 = new Twine("condition");
+ FCmpInst * condition = new FCmpInst(*basicBlock, FCmpInst::FCMP_TRUE , LHS, RHS, *var4);
+ DEBUG_WITH_TYPE("gen", errs() << "bcf: Always true condition created\n");
+
+ // Jump to the original basic block if the condition is true or
+ // to the altered block if false.
+ BranchInst::Create(originalBB, alteredBB, (Value *)condition, basicBlock);
+ DEBUG_WITH_TYPE("gen",
+ errs() << "bcf: Terminator instruction in first basic block: ok\n");
+
+ // The altered block loop back on the original one.
+ BranchInst::Create(originalBB, alteredBB);
+ DEBUG_WITH_TYPE("gen", errs() << "bcf: Terminator instruction in altered block: ok\n");
+
+
+ // The end of the originalBB is modified to give the impression that sometimes
+ // it continues in the loop, and sometimes it return the desired value
+ // (of course it's always true, so it always use the original terminator..
+ // but this will be obfuscated too;) )
+
+ // iterate on instruction just before the terminator of the originalBB
+ BasicBlock::iterator i = originalBB->end();
+