Straw's B1og.

OLLVM-指令替代

字数统计: 2k阅读时长: 12 min
2024/03/18

OLLVM-指令替代

(建议结合代码后的注释食用!)

例如a+b=a-(-b),a^b= ( ~ a&b) | (a& ~ b) 且仅支持整数

加法替代

指令1

减法替代

指令2

与替换

指令3

或替换

指令4

异或替换

指令5

代码

Substitution.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
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Pass.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/Instructions.h"
#include "llvm/Transforms/Utils.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Transforms/Utils/Local.h"
#include "SplitBasicBlock.h"
#include "Utils.h"
#include <vector>
#include <cstdlib>
#include <ctime>
#define NUMBER_ADD_SUBST 4
#define NUMBER_SUB_SUBST 3
#define NUMBER_AND_SUBST 2
#define NUMBER_OR_SUBST 2
#define NUMBER_XOR_SUBST 2
using namespace llvm;
using std::vector;
static cl::opt<int> obfuTimes("sub_loop",cl::init(1),cl::desc("Obfucase a function <sub_loop> time()."));
namespace {
class Substitution:public FunctionPass{
public:
static char ID;
Substitution():FunctionPass(ID){
srand(time(NULL));
}
bool runOnFunction(Function &F) ;
void substitute(BinaryOperator *BI);
// 替换 Add 指令
void substituteAdd(BinaryOperator *BI) ;
// 加法替换:a = b + c -> a = b - (-c)
void addNeg(BinaryOperator *BI) ;
// 加法替换:a = b + c -> a = -(-b + (-c))
void addDoubleNeg(BinaryOperator *BI) ;
// 加法替换:a = b + c -> r = rand (); a = b + r; a = a + c; a = a - r
void addRand(BinaryOperator *BI) ;
// 加法替换:a = b + c -> r = rand (); a = b - r; a = a + b; a = a + r
void addRand2(BinaryOperator *BI) ;

// 替换 Sub 指令
void substituteSub(BinaryOperator *BI) ;
// 减法替换:a = b - c -> a = b + (-c)
void subNeg(BinaryOperator *BI) ;
// 减法替换:a = b - c -> r = rand (); a = b + r; a = a - c; a = a - r
void subRand(BinaryOperator *BI) ;
// 减法替换:a = b - c -> a = b - r; a = a - c; a = a + r
void subRand2(BinaryOperator *BI) ;

// 替换 And 指令
void substituteAnd(BinaryOperator *BI) ;
// 与替换:a = b & c -> a = (b ^ ~c) & b
void andSubstitute(BinaryOperator *BI) ;
// 与替换:a = b & c -> a = ~(~b | ~c) & (r | ~r)
void andSubstituteRand(BinaryOperator *BI) ;

// 替换 Or 指令
void substituteOr(BinaryOperator *BI) ;
// 或替换:a = b | c -> a = (b & c) | (b ^ c)
void orSubstitute(BinaryOperator *BI) ;
// 或替换:a = b | c -> a = ~(~b & ~c) & (r | ~r)
void orSubstituteRand(BinaryOperator *BI) ;

// 替换 Xor 指令
void substituteXor(BinaryOperator *BI) ;
// 异或替换:a = b ^ c -> a = ~b & c | b & ~c
void xorSubstitute(BinaryOperator *BI) ;
// 异或替换:a = b ^ c -> (b ^ r) ^ (c ^ r) <=> (~b & r | b & ~r) ^ (~c & r |
// c & ~r)
void xorSubstituteRand(BinaryOperator *BI) ;
};

}
bool Substitution::runOnFunction(Function &F){
for(int i=0;i<obfuTimes;i++){
for(BasicBlock &BB:F){
vector<Instruction*>origInst;
for(Instruction &I:BB){
origInst.push_back(&I);
}
for(Instruction *I:origInst){ //遍历所有指令
if(BinaryOperator *BI=dyn_cast<BinaryOperator>(I)){
substitute(BI);
}
// if(isa<BinaryOperator>(I)){
// BinaryOperator *BI=cast<BinaryOperator>(I);
// substitute(BI);
// }
}
}
}
}
void Substitution::substitute(BinaryOperator *BI){
switch (BI->getOpcode())
{
case BinaryOperator::Add:
substituteAdd(BI);
break;
case BinaryOperator::Sub:
substituteSub(BI);
break;
case BinaryOperator::Xor:
substituteXor(BI);
break;
case BinaryOperator::And:
substituteAnd(BI);
case BinaryOperator::Or:
substituteOr(BI);
default:
break;
}
//选择替换类型
}
void Substitution::substituteAdd(BinaryOperator *BI){
int choice=rand()%NUMBER_ADD_SUBST;
switch(choice){
case 0:
addNeg(BI);
break;
case 1:
addDoubleNeg(BI);
break;
case 2:
addRand(BI);
break;
case 3:
addRand2(BI);
break;
default:
break;
}
}
// 加法替换:a = b + c -> a = b - (-c)
void Substitution::addNeg(BinaryOperator *BI) {
BinaryOperator *opt1=BinaryOperator::CreateNeg(BI->getOperand(1),"",BI);
//-c
BinaryOperator *opt2=BinaryOperator::CreateSub(BI->getOperand(0),opt1,"",BI);
//b-(-c)
BI->replaceAllUsesWith(opt2);
}
// 加法替换:a = b + c -> a = -(-b + (-c))
void Substitution::addDoubleNeg(BinaryOperator *BI) {
BinaryOperator *op1 = BinaryOperator::CreateNeg(BI->getOperand(0),"",BI);
//-b
BinaryOperator *op2 = BinaryOperator::CreateNeg(BI->getOperand(1),"",BI);
//-c
BinaryOperator *op3 = BinaryOperator::CreateAdd(op1, op2,"",BI);
//-b+(-c)
BinaryOperator *op = BinaryOperator::CreateNeg(op3,"",BI);
//-(-b+(-c))
BI->replaceAllUsesWith(op);
}
// 加法替换:a = b + c -> r = rand (); a = b + r; a = a + c; a = a - r
void Substitution::addRand(BinaryOperator *BI) {
ConstantInt *r =(ConstantInt*)CONST(BI->getType(),rand());
//r
BinaryOperator * op1 = BinaryOperator::CreateAdd(BI->getOperand(0), r,"",BI);
//b+r
BinaryOperator *op2 = BinaryOperator::CreateAdd(op1, BI->getOperand(1),"",BI);
//(b+r)+c
BinaryOperator *op = BinaryOperator::CreateSub(op2, r,"",BI);
//((b+r)+c)-r
BI->replaceAllUsesWith(op);
}
// 加法替换:a = b + c -> r = rand (); a = b - r; a = a + c; a = a + r
void Substitution::addRand2(BinaryOperator *BI) {
ConstantInt *r =(ConstantInt*)CONST(BI->getType(),rand());
//r
BinaryOperator * op1 = BinaryOperator::CreateSub(BI->getOperand(0), r,"",BI);
//b-r
BinaryOperator *op2 = BinaryOperator::CreateAdd(op1, BI->getOperand(1),"",BI);
//(b-r)+c
BinaryOperator *op = BinaryOperator::CreateAdd(op2, r,"",BI);
//((b-r)+c)+r
BI->replaceAllUsesWith(op);
}
// 替换 Sub 指令
void Substitution::substituteSub(BinaryOperator *BI) {
int choice=rand()%NUMBER_SUB_SUBST;
switch(choice){
case 0:
subNeg(BI);
break;
case 1:
subRand(BI);
break;
case 2:
subRand2(BI);
break;
default:
break;
}
}
// 减法替换:a = b - c -> a = b + (-c)
void Substitution::subNeg(BinaryOperator *BI) {
BinaryOperator *opt1=BinaryOperator::CreateNeg(BI->getOperand(1),"",BI);
//-c
BinaryOperator *opt2=BinaryOperator::CreateAdd(BI->getOperand(0),opt1,"",BI);
//b+(-c)
BI->replaceAllUsesWith(opt2);
}
// 减法替换:a = b - c -> r = rand (); a = b + r; a = a - c; a = a - r
void Substitution::subRand(BinaryOperator *BI) {
ConstantInt *r =(ConstantInt*)CONST(BI->getType(),rand());
//r
BinaryOperator * op1 = BinaryOperator::CreateAdd(BI->getOperand(0), r,"",BI);
//b+r
BinaryOperator *op2 = BinaryOperator::CreateSub(op1, BI->getOperand(1),"",BI);
//(b+r)-c
BinaryOperator *op = BinaryOperator::CreateSub(op2, r,"",BI);
//((b+r)-c)-r
BI->replaceAllUsesWith(op);
}
// 减法替换:a = b - c -> a = b - r; a = a - c; a = a + r
void Substitution::subRand2(BinaryOperator *BI) {
ConstantInt *r =(ConstantInt*)CONST(BI->getType(),rand());
//r
BinaryOperator * op1 = BinaryOperator::CreateSub(BI->getOperand(0), r,"",BI);
//b-r
BinaryOperator *op2 = BinaryOperator::CreateSub(op1, BI->getOperand(1),"",BI);
//(b-r)-c
BinaryOperator *op = BinaryOperator::CreateAdd(op2, r,"",BI);
//((b-r)-c)+r
BI->replaceAllUsesWith(op);
}
// 替换 And 指令
void Substitution::substituteAnd(BinaryOperator *BI) {
int choice=rand()%NUMBER_AND_SUBST;
switch(choice){
case 0:
andSubstitute(BI);
break;
case 1:
andSubstituteRand(BI);
break;
default:
break;
}
}
// 与替换:a = b & c -> a = (b ^ ~c) & b
void Substitution::andSubstitute(BinaryOperator *BI) {
BinaryOperator *opt1=BinaryOperator::CreateNot(BI->getOperand(1),"",BI);
//~c
BinaryOperator *opt2=BinaryOperator::CreateXor(BI->getOperand(0),opt1,"",BI);
//b^~c
BinaryOperator *opt3=BinaryOperator::CreateAnd(BI->getOperand(0),opt2,"",BI);
//(b^~c)&b
BI->replaceAllUsesWith(opt3);
}
// 与替换:a = b & c -> a = ~(~b | ~c) & (r | ~r)
void Substitution::andSubstituteRand(BinaryOperator *BI) {
ConstantInt *r=(ConstantInt*)CONST(BI->getType(),rand());
//r
BinaryOperator *opt1=BinaryOperator::CreateNot(BI->getOperand(1),"",BI);
//~b
BinaryOperator *opt2=BinaryOperator::CreateNot(BI->getOperand(0),"",BI);
//~c
BinaryOperator *opt3=BinaryOperator::CreateOr(opt1,opt2,"",BI);
//~b | ~c
BinaryOperator *opt4=BinaryOperator::CreateNot(opt3,"",BI);
//~(~b | ~c)
BinaryOperator *opt5=BinaryOperator::CreateNot(r,"",BI);
//~r
BinaryOperator *opt6=BinaryOperator::CreateOr(r,opt5,"",BI);
//r | ~r
BinaryOperator *opt7=BinaryOperator::CreateAnd(opt6,opt4,"",BI);
//~(~b | ~c)&(r | ~r)
BI->replaceAllUsesWith(opt7);
}

// 替换 Or 指令
void Substitution::substituteOr(BinaryOperator *BI) {
int choice=rand()%NUMBER_OR_SUBST;
switch(choice){
case 0:
orSubstitute(BI);
break;
case 1:
orSubstituteRand(BI);
break;
default:
break;
}
}
// 或替换:a = b | c -> a = (b & c) | (b ^ c)
void Substitution::orSubstitute(BinaryOperator *BI) {
BinaryOperator *opt1=BinaryOperator::CreateAnd(BI->getOperand(0),BI->getOperand(1),"",BI);
//b&c
BinaryOperator *opt2=BinaryOperator::CreateXor(BI->getOperand(0),BI->getOperand(1),"",BI);
//b^c
BinaryOperator *opt3=BinaryOperator::CreateOr(opt1,opt2,"",BI);
//(b&c)|(b^c)
BI->replaceAllUsesWith(opt3);
}
// 或替换:a = b | c -> a = ~(~b & ~c) & (r | ~r)
void Substitution::orSubstituteRand(BinaryOperator *BI) {
ConstantInt *r=(ConstantInt*)CONST(BI->getType(),rand());
//r
BinaryOperator *opt1=BinaryOperator::CreateNot(BI->getOperand(1),"",BI);
//~b
BinaryOperator *opt2=BinaryOperator::CreateNot(BI->getOperand(0),"",BI);
//~c
BinaryOperator *opt3=BinaryOperator::CreateAnd(opt1,opt2,"",BI);
//~b&~c
BinaryOperator *opt4=BinaryOperator::CreateNot(opt3,"",BI);
//~(~b&~c)
BinaryOperator *opt5=BinaryOperator::CreateNot(r,"",BI);
//~r
BinaryOperator *opt6=BinaryOperator::CreateOr(r,opt5,"",BI);
//r|~r
BinaryOperator *opt7=BinaryOperator::CreateAnd(opt6,opt4,"",BI);
//~(~b & ~c) & (r | ~r)
BI->replaceAllUsesWith(opt7);
}

// 替换 Xor 指令
void Substitution::substituteXor(BinaryOperator *BI) {
int choice=rand()%NUMBER_XOR_SUBST;
switch(choice){
case 0:
xorSubstitute(BI);
break;
case 1:
xorSubstituteRand(BI);
break;
default:
break;
}
}
// 异或替换:a = b ^ c -> a = ~b & c | b & ~c
void Substitution::xorSubstitute(BinaryOperator *BI) {
BinaryOperator *opt1=BinaryOperator::CreateNot(BI->getOperand(0),"",BI);
//~c
BinaryOperator *opt2=BinaryOperator::CreateAnd(BI->getOperand(1),opt1,"",BI);
//b&~c
BinaryOperator *opt3=BinaryOperator::CreateNot(BI->getOperand(1),"",BI);
//~b
BinaryOperator *opt4=BinaryOperator::CreateAnd(BI->getOperand(0),opt3,"",BI);
//~b&c
BinaryOperator *opt5=BinaryOperator::CreateOr(opt4,opt2,"",BI);
//~b&c|b&~c
BI->replaceAllUsesWith(opt5);
}
// 异或替换:a = b ^ c -> (b ^ r) ^ (c ^ r) <=> (~b & r | b & ~r) ^ (~c & r | c & ~r)
void Substitution::xorSubstituteRand(BinaryOperator *BI) {
ConstantInt *r=(ConstantInt*)CONST(BI->getType(),rand());
//r
BinaryOperator *opt1=BinaryOperator::CreateNot(BI->getOperand(0),"",BI);
//~b
BinaryOperator *opt2=BinaryOperator::CreateAnd(r,opt1,"",BI);
//~b&r
BinaryOperator *opt3=BinaryOperator::CreateNot(r,"",BI);
//~r
BinaryOperator *opt4=BinaryOperator::CreateAnd(opt3,BI->getOperand(0),"",BI);
//b&~r
BinaryOperator *opt5=BinaryOperator::CreateOr(opt4,opt2,"",BI);
//~b&r|b&r
BinaryOperator *opt6=BinaryOperator::CreateNot(BI->getOperand(1),"",BI);
BinaryOperator *opt7=BinaryOperator::CreateAnd(r,opt6,"",BI);
BinaryOperator *opt8=BinaryOperator::CreateNot(r,"",BI);
BinaryOperator *opt9=BinaryOperator::CreateAnd(opt8,BI->getOperand(1),"",BI);
BinaryOperator *opt10=BinaryOperator::CreateOr(opt7,opt9,"",BI);
BinaryOperator *opt11=BinaryOperator::CreateXor(opt10,opt5,"",BI);
//(~b&r|b&~r)^(~c&r|c&~r)
BI->replaceAllUsesWith(opt11);
}
char Substitution::ID=0;
static RegisterPass<Substitution> X("sub","Replace a operator with equivalent instructions");

效果

效果

感觉就汇编多了点东西,伪代码直接被ida修正了,可能是源文件的问题。。。

CATALOG
  1. 1. OLLVM-指令替代
    1. 1.1. 加法替代
    2. 1.2. 减法替代
    3. 1.3. 与替换
    4. 1.4. 或替换
    5. 1.5. 异或替换
    6. 1.6. 代码
    7. 1.7. 效果