forked from pointblank-club/oaas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_complete_implementation.sh
More file actions
543 lines (458 loc) · 17.1 KB
/
test_complete_implementation.sh
File metadata and controls
543 lines (458 loc) · 17.1 KB
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
#!/bin/bash
# Complete Implementation Test Script for LLVM Obfuscator
# Tests: MLIR passes, ClangIR pipeline, Default pipeline, All obfuscation features
#
# Usage: ./test_complete_implementation.sh
#
# Tests performed:
# 1. MLIR library build verification
# 2. MLIR passes standalone tests
# 3. Default pipeline (CLANG) tests
# 4. ClangIR pipeline tests (if available)
# 5. Cryptographic hash pass tests
# 6. Constant obfuscation tests
# 7. End-to-end integration tests
set -e # Exit on error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Test counters
TESTS_PASSED=0
TESTS_FAILED=0
TESTS_SKIPPED=0
# Helper functions
print_header() {
echo ""
echo -e "${BLUE}=========================================="
echo -e "$1"
echo -e "==========================================${NC}"
echo ""
}
print_success() {
echo -e "${GREEN}✅ $1${NC}"
((TESTS_PASSED++))
}
print_error() {
echo -e "${RED}❌ $1${NC}"
((TESTS_FAILED++))
}
print_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
print_skip() {
echo -e "${YELLOW}⏭️ $1${NC}"
((TESTS_SKIPPED++))
}
print_info() {
echo -e "${BLUE}ℹ️ $1${NC}"
}
# Get script directory
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$SCRIPT_DIR"
print_header "LLVM Obfuscator - Complete Implementation Test"
# ============================================================================
# PHASE 1: Environment Verification
# ============================================================================
print_header "Phase 1: Verifying Environment"
# Check required tools
echo "Checking required tools..."
# LLVM/Clang
if command -v clang &> /dev/null; then
CLANG_VERSION=$(clang --version | head -1)
print_success "Clang found: $CLANG_VERSION"
else
print_error "Clang not found - required for compilation"
exit 1
fi
# LLVM Config
if command -v llvm-config &> /dev/null; then
LLVM_VERSION=$(llvm-config --version)
print_success "LLVM found: $LLVM_VERSION"
else
print_error "LLVM not found - required for compilation"
exit 1
fi
# MLIR tools
if command -v mlir-opt &> /dev/null; then
MLIR_VERSION=$(mlir-opt --version | head -1 || echo "unknown")
print_success "mlir-opt found: $MLIR_VERSION"
else
print_error "mlir-opt not found - required for MLIR passes"
exit 1
fi
if command -v mlir-translate &> /dev/null; then
print_success "mlir-translate found"
else
print_error "mlir-translate not found - required for MLIR lowering"
exit 1
fi
# ClangIR (optional)
if command -v clangir &> /dev/null; then
CLANGIR_VERSION=$(clangir --version | head -1 || echo "unknown")
print_success "ClangIR found: $CLANGIR_VERSION (optional - enables advanced pipeline)"
HAS_CLANGIR=true
else
print_warning "ClangIR not found (optional - advanced pipeline will be skipped)"
HAS_CLANGIR=false
fi
# Python
if command -v python3 &> /dev/null; then
PYTHON_VERSION=$(python3 --version)
print_success "Python found: $PYTHON_VERSION"
else
print_error "Python3 not found - required for CLI"
exit 1
fi
# OpenSSL
if command -v openssl &> /dev/null; then
OPENSSL_VERSION=$(openssl version)
print_success "OpenSSL found: $OPENSSL_VERSION"
else
print_warning "OpenSSL not found (cryptographic hashing may fail)"
fi
# ============================================================================
# PHASE 2: Build MLIR Library
# ============================================================================
print_header "Phase 2: Building MLIR Obfuscation Library"
cd "$SCRIPT_DIR/mlir-obs"
if [ ! -f "./build.sh" ]; then
print_error "build.sh not found in mlir-obs/"
exit 1
fi
echo "Running build script..."
if ./build.sh > build.log 2>&1; then
print_success "MLIR library built successfully"
else
print_error "MLIR library build failed - check mlir-obs/build.log"
cat build.log | tail -20
exit 1
fi
# Verify library exists
LIBRARY=$(find build -name "*MLIRObfuscation.*" -type f | head -1)
if [ -z "$LIBRARY" ]; then
print_error "MLIR library not found after build"
exit 1
fi
print_success "MLIR library found: $LIBRARY"
cd "$SCRIPT_DIR"
# ============================================================================
# PHASE 3: Test MLIR Passes Standalone
# ============================================================================
print_header "Phase 3: Testing MLIR Passes Standalone"
cd "$SCRIPT_DIR/mlir-obs"
if [ ! -f "./test.sh" ]; then
print_warning "test.sh not found in mlir-obs/ - skipping standalone tests"
print_skip "MLIR standalone tests skipped"
else
echo "Running MLIR standalone tests..."
if ./test.sh > test.log 2>&1; then
print_success "MLIR standalone tests passed"
# Show summary
tail -20 test.log
else
print_warning "MLIR standalone tests failed - check mlir-obs/test.log"
tail -20 test.log
fi
fi
cd "$SCRIPT_DIR"
# ============================================================================
# PHASE 4: Create Test Files
# ============================================================================
print_header "Phase 4: Creating Test Files"
TEST_DIR="$SCRIPT_DIR/test_output_complete"
mkdir -p "$TEST_DIR"
cd "$TEST_DIR"
# Create comprehensive test C file
echo "Creating test C file..."
cat > test_auth.c << 'EOF'
#include <stdio.h>
#include <string.h>
// Test constants of different types
const char* MASTER_PASSWORD = "SuperSecret2024!";
const char* API_KEY = "sk_live_abc123xyz";
const int MAX_ATTEMPTS = 3;
const float THRESHOLD = 0.95;
// Test functions
int authenticate(const char* password) {
return strcmp(password, MASTER_PASSWORD) == 0;
}
int validate_api_key(const char* key) {
return strcmp(key, API_KEY) == 0;
}
int check_threshold(float value) {
return value >= THRESHOLD;
}
int main() {
printf("Starting authentication test...\n");
if (authenticate("SuperSecret2024!")) {
printf("✅ Authentication successful\n");
} else {
printf("❌ Authentication failed\n");
}
if (validate_api_key("sk_live_abc123xyz")) {
printf("✅ API key valid\n");
} else {
printf("❌ API key invalid\n");
}
if (check_threshold(0.98)) {
printf("✅ Threshold check passed\n");
} else {
printf("❌ Threshold check failed\n");
}
printf("Max attempts: %d\n", MAX_ATTEMPTS);
return 0;
}
EOF
print_success "Test file created: test_auth.c"
# ============================================================================
# PHASE 5: Test Default Pipeline (CLANG)
# ============================================================================
print_header "Phase 5: Testing Default Pipeline (CLANG)"
cd "$SCRIPT_DIR"
echo "Test 5.1: Basic compilation (no obfuscation)"
python3 -m cmd.llvm-obfuscator.cli.obfuscate compile "$TEST_DIR/test_auth.c" \
--output "$TEST_DIR/output_baseline" \
> "$TEST_DIR/test_5_1.log" 2>&1 || {
print_error "Baseline compilation failed"
cat "$TEST_DIR/test_5_1.log" | tail -20
exit 1
}
print_success "Baseline compilation successful"
echo "Test 5.2: String encryption only"
python3 -m cmd.llvm-obfuscator.cli.obfuscate compile "$TEST_DIR/test_auth.c" \
--enable-string-encrypt \
--output "$TEST_DIR/output_string_encrypt" \
> "$TEST_DIR/test_5_2.log" 2>&1 || {
print_error "String encryption failed"
cat "$TEST_DIR/test_5_2.log" | tail -20
exit 1
}
print_success "String encryption successful"
# Verify strings are hidden
if strings "$TEST_DIR/output_string_encrypt/test_auth" 2>/dev/null | grep -q "SuperSecret"; then
print_warning "Secret strings still visible after encryption"
else
print_success "Secret strings successfully hidden"
fi
echo "Test 5.3: Symbol obfuscation (RNG-based)"
python3 -m cmd.llvm-obfuscator.cli.obfuscate compile "$TEST_DIR/test_auth.c" \
--enable-symbol-obfuscate \
--output "$TEST_DIR/output_symbol_rng" \
> "$TEST_DIR/test_5_3.log" 2>&1 || {
print_error "Symbol obfuscation (RNG) failed"
cat "$TEST_DIR/test_5_3.log" | tail -20
exit 1
}
print_success "Symbol obfuscation (RNG) successful"
# Verify symbols are obfuscated
if nm "$TEST_DIR/output_symbol_rng/test_auth" 2>/dev/null | grep -v ' U ' | grep -q "authenticate"; then
print_warning "Original function names still visible"
else
print_success "Function names successfully obfuscated"
fi
echo "Test 5.4: Cryptographic hash obfuscation (SHA256)"
python3 -m cmd.llvm-obfuscator.cli.obfuscate compile "$TEST_DIR/test_auth.c" \
--enable-crypto-hash \
--crypto-hash-algorithm sha256 \
--crypto-hash-salt "test-salt-2024" \
--crypto-hash-length 12 \
--output "$TEST_DIR/output_crypto_hash" \
> "$TEST_DIR/test_5_4.log" 2>&1 || {
print_warning "Crypto hash obfuscation failed (may not be implemented yet)"
cat "$TEST_DIR/test_5_4.log" | tail -10
}
if [ -f "$TEST_DIR/output_crypto_hash/test_auth" ]; then
print_success "Cryptographic hash obfuscation successful"
else
print_skip "Crypto hash test skipped (feature may not be available)"
fi
echo "Test 5.5: Constant obfuscation (all types)"
python3 -m cmd.llvm-obfuscator.cli.obfuscate compile "$TEST_DIR/test_auth.c" \
--enable-constant-obfuscate \
--output "$TEST_DIR/output_constant_obf" \
> "$TEST_DIR/test_5_5.log" 2>&1 || {
print_warning "Constant obfuscation failed (may not be implemented yet)"
cat "$TEST_DIR/test_5_5.log" | tail -10
}
if [ -f "$TEST_DIR/output_constant_obf/test_auth" ]; then
print_success "Constant obfuscation successful"
else
print_skip "Constant obfuscation test skipped (feature may not be available)"
fi
echo "Test 5.6: Combined obfuscation (all passes)"
python3 -m cmd.llvm-obfuscator.cli.obfuscate compile "$TEST_DIR/test_auth.c" \
--enable-string-encrypt \
--enable-crypto-hash \
--enable-constant-obfuscate \
--crypto-hash-algorithm blake2b \
--crypto-hash-salt "test-salt-2024" \
--output "$TEST_DIR/output_combined_default" \
> "$TEST_DIR/test_5_6.log" 2>&1 || {
print_warning "Combined obfuscation failed"
cat "$TEST_DIR/test_5_6.log" | tail -20
}
if [ -f "$TEST_DIR/output_combined_default/test_auth" ]; then
print_success "Combined obfuscation successful"
# Test binary execution
if "$TEST_DIR/output_combined_default/test_auth" > "$TEST_DIR/test_5_6_output.txt" 2>&1; then
print_success "Binary execution successful"
echo "Binary output:"
cat "$TEST_DIR/test_5_6_output.txt"
else
print_warning "Binary execution failed (may have runtime issues)"
fi
else
print_skip "Combined obfuscation test skipped"
fi
# ============================================================================
# PHASE 6: Test ClangIR Pipeline (if available)
# ============================================================================
print_header "Phase 6: Testing ClangIR Pipeline"
if [ "$HAS_CLANGIR" = false ]; then
print_skip "ClangIR not available - skipping ClangIR pipeline tests"
print_info "To enable ClangIR tests, install ClangIR (see CLANGIR_PIPELINE_GUIDE.md)"
else
cd "$SCRIPT_DIR"
echo "Test 6.1: ClangIR frontend test (basic)"
python3 -m cmd.llvm-obfuscator.cli.obfuscate compile "$TEST_DIR/test_auth.c" \
--mlir-frontend clangir \
--output "$TEST_DIR/output_clangir_baseline" \
> "$TEST_DIR/test_6_1.log" 2>&1 || {
print_error "ClangIR baseline compilation failed"
cat "$TEST_DIR/test_6_1.log" | tail -20
}
if [ -f "$TEST_DIR/output_clangir_baseline/test_auth" ]; then
print_success "ClangIR baseline compilation successful"
else
print_warning "ClangIR baseline compilation failed"
fi
echo "Test 6.2: ClangIR with string encryption"
python3 -m cmd.llvm-obfuscator.cli.obfuscate compile "$TEST_DIR/test_auth.c" \
--mlir-frontend clangir \
--enable-string-encrypt \
--output "$TEST_DIR/output_clangir_string" \
> "$TEST_DIR/test_6_2.log" 2>&1 || {
print_warning "ClangIR string encryption failed"
cat "$TEST_DIR/test_6_2.log" | tail -20
}
if [ -f "$TEST_DIR/output_clangir_string/test_auth" ]; then
print_success "ClangIR string encryption successful"
else
print_skip "ClangIR string encryption test skipped"
fi
echo "Test 6.3: ClangIR with all obfuscation passes"
python3 -m cmd.llvm-obfuscator.cli.obfuscate compile "$TEST_DIR/test_auth.c" \
--mlir-frontend clangir \
--enable-string-encrypt \
--enable-crypto-hash \
--enable-constant-obfuscate \
--crypto-hash-algorithm sha256 \
--crypto-hash-salt "test-salt-2024" \
--output "$TEST_DIR/output_clangir_full" \
> "$TEST_DIR/test_6_3.log" 2>&1 || {
print_warning "ClangIR full obfuscation failed"
cat "$TEST_DIR/test_6_3.log" | tail -20
}
if [ -f "$TEST_DIR/output_clangir_full/test_auth" ]; then
print_success "ClangIR full obfuscation successful"
# Test execution
if "$TEST_DIR/output_clangir_full/test_auth" > "$TEST_DIR/test_6_3_output.txt" 2>&1; then
print_success "ClangIR obfuscated binary execution successful"
echo "Binary output:"
cat "$TEST_DIR/test_6_3_output.txt"
else
print_warning "ClangIR binary execution failed"
fi
else
print_skip "ClangIR full obfuscation test skipped"
fi
fi
# ============================================================================
# PHASE 7: Verification Tests
# ============================================================================
print_header "Phase 7: Obfuscation Verification"
cd "$TEST_DIR"
echo "Verification 7.1: Symbol count reduction"
if [ -f "output_baseline/test_auth" ] && [ -f "output_combined_default/test_auth" ]; then
BASELINE_SYMBOLS=$(nm output_baseline/test_auth 2>/dev/null | grep -v ' U ' | wc -l)
OBFUSCATED_SYMBOLS=$(nm output_combined_default/test_auth 2>/dev/null | grep -v ' U ' | wc -l)
echo " Baseline symbols: $BASELINE_SYMBOLS"
echo " Obfuscated symbols: $OBFUSCATED_SYMBOLS"
if [ "$OBFUSCATED_SYMBOLS" -lt "$BASELINE_SYMBOLS" ]; then
REDUCTION=$((100 * (BASELINE_SYMBOLS - OBFUSCATED_SYMBOLS) / BASELINE_SYMBOLS))
print_success "Symbol reduction: $REDUCTION%"
else
print_warning "No symbol reduction achieved"
fi
else
print_skip "Symbol count verification skipped (missing binaries)"
fi
echo "Verification 7.2: Secret string hiding"
BASELINE_SECRETS=0
OBFUSCATED_SECRETS=0
for secret in "SuperSecret" "sk_live" "API_KEY"; do
if [ -f "output_baseline/test_auth" ]; then
if strings output_baseline/test_auth 2>/dev/null | grep -q "$secret"; then
((BASELINE_SECRETS++))
fi
fi
if [ -f "output_combined_default/test_auth" ]; then
if strings output_combined_default/test_auth 2>/dev/null | grep -q "$secret"; then
((OBFUSCATED_SECRETS++))
fi
fi
done
echo " Baseline secrets found: $BASELINE_SECRETS"
echo " Obfuscated secrets found: $OBFUSCATED_SECRETS"
if [ "$OBFUSCATED_SECRETS" -eq 0 ] && [ "$BASELINE_SECRETS" -gt 0 ]; then
print_success "All secrets successfully hidden"
elif [ "$OBFUSCATED_SECRETS" -lt "$BASELINE_SECRETS" ]; then
print_warning "Some secrets still visible ($OBFUSCATED_SECRETS/$BASELINE_SECRETS)"
else
print_warning "No improvement in secret hiding"
fi
echo "Verification 7.3: Binary size comparison"
if [ -f "output_baseline/test_auth" ] && [ -f "output_combined_default/test_auth" ]; then
BASELINE_SIZE=$(stat -f%z output_baseline/test_auth 2>/dev/null || stat -c%s output_baseline/test_auth 2>/dev/null)
OBFUSCATED_SIZE=$(stat -f%z output_combined_default/test_auth 2>/dev/null || stat -c%s output_combined_default/test_auth 2>/dev/null)
echo " Baseline size: $BASELINE_SIZE bytes"
echo " Obfuscated size: $OBFUSCATED_SIZE bytes"
if [ "$OBFUSCATED_SIZE" -gt "$BASELINE_SIZE" ]; then
OVERHEAD=$((100 * (OBFUSCATED_SIZE - BASELINE_SIZE) / BASELINE_SIZE))
print_info "Binary size overhead: +$OVERHEAD%"
fi
print_success "Binary size comparison complete"
else
print_skip "Binary size comparison skipped (missing binaries)"
fi
# ============================================================================
# FINAL SUMMARY
# ============================================================================
print_header "Test Summary"
echo "Total tests:"
echo " ✅ Passed: $TESTS_PASSED"
echo " ❌ Failed: $TESTS_FAILED"
echo " ⏭️ Skipped: $TESTS_SKIPPED"
echo ""
if [ "$TESTS_FAILED" -eq 0 ]; then
print_success "ALL TESTS PASSED! 🎉"
echo ""
print_info "Next steps:"
echo " 1. Review test artifacts in: $TEST_DIR"
echo " 2. Test with your own code"
echo " 3. See MLIR_INTEGRATION_GUIDE.md for advanced usage"
echo " 4. See CLANGIR_PIPELINE_GUIDE.md for ClangIR details"
exit 0
else
print_error "SOME TESTS FAILED"
echo ""
print_info "Troubleshooting:"
echo " 1. Check logs in: $TEST_DIR/*.log"
echo " 2. Verify LLVM/MLIR installation: llvm-config --version"
echo " 3. Check MLIR library: $SCRIPT_DIR/mlir-obs/build/"
echo " 4. See MLIR_INTEGRATION_GUIDE.md troubleshooting section"
exit 1
fi