forked from connect-boiz/soroban-security-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-manual.js
More file actions
173 lines (150 loc) · 5.13 KB
/
Copy pathtest-manual.js
File metadata and controls
173 lines (150 loc) · 5.13 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
// Manual test script for time-based attack vulnerability detection
// This simulates the scanner functionality without requiring Node.js runtime
const fs = require('fs');
// Vulnerability patterns from the detector
const vulnerablePatterns = [
{
pattern: /now\s*<\s*timestamp|timestamp\s*>\s*now/g,
type: 'DIRECT_TIMESTAMP_COMPARISON',
severity: 'HIGH',
description: 'Direct timestamp comparison without manipulation protection'
},
{
pattern: /lock_period\s*=\s*.*timestamp|timestamp.*lock_period/gi,
type: 'LOCK_PERIOD_TIMESTAMP_USAGE',
severity: 'HIGH',
description: 'Lock period calculated using vulnerable timestamp comparisons'
},
{
pattern: /if\s*\(\s*.*time.*\)\s*{|require\s*\(\s*.*time.*\s*,/gi,
type: 'UNPROTECTED_TIME_CONDITION',
severity: 'MEDIUM',
description: 'Time-based condition without protection against manipulation'
},
{
pattern: /timestamp\s*[+\-]\s*\d+\s*[*/]\s*\d+/g,
type: 'TIMESTAMP_ARITHMETIC_LOCK',
severity: 'HIGH',
description: 'Timestamp arithmetic used for lock calculations without protection'
}
];
function scanFile(filePath) {
console.log(`\n🔍 Scanning: ${filePath}`);
try {
const content = fs.readFileSync(filePath, 'utf8');
const lines = content.split('\n');
const vulnerabilities = [];
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const lineNumber = i + 1;
for (const pattern of vulnerablePatterns) {
const matches = line.match(pattern.pattern);
if (matches) {
vulnerabilities.push({
type: pattern.type,
severity: pattern.severity,
description: pattern.description,
file: filePath,
line: lineNumber,
code: line.trim(),
matches: matches
});
}
}
}
return vulnerabilities;
} catch (error) {
console.error(`Error scanning file ${filePath}: ${error.message}`);
return [];
}
}
function generateReport(vulnerabilities) {
if (vulnerabilities.length === 0) {
return '✅ No time-based attack vulnerabilities found!\n';
}
let report = `🚨 Found ${vulnerabilities.length} time-based attack vulnerabilities:\n\n`;
const groupedVulns = vulnerabilities.reduce((groups, vuln) => {
if (!groups[vuln.severity]) {
groups[vuln.severity] = [];
}
groups[vuln.severity].push(vuln);
return groups;
}, {});
for (const [severity, vulns] of Object.entries(groupedVulns)) {
report += `${severity} SEVERITY (${vulns.length}):\n`;
for (const vuln of vulns) {
report += ` └── ${vuln.description}\n`;
report += ` File: ${vuln.file}:${vuln.line}\n`;
report += ` Code: ${vuln.code}\n\n`;
}
}
return report;
}
// Test the vulnerable contract
console.log('🧪 Testing Time-Based Attack Vulnerability Detection');
console.log('=' .repeat(50));
const vulnerableContractPath = 'examples/vulnerable-contract.rs';
const secureContractPath = 'examples/secure-contract.rs';
if (fs.existsSync(vulnerableContractPath)) {
const vulnerableResults = scanFile(vulnerableContractPath);
const vulnerableReport = generateReport(vulnerableResults);
console.log('VULNERABLE CONTRACT RESULTS:');
console.log(vulnerableReport);
}
if (fs.existsSync(secureContractPath)) {
const secureResults = scanFile(secureContractPath);
const secureReport = generateReport(secureResults);
console.log('SECURE CONTRACT RESULTS:');
console.log(secureReport);
}
// Test specific vulnerable patterns
console.log('\n🔬 Pattern Detection Tests');
console.log('=' .repeat(30));
const testCases = [
{
name: 'Direct timestamp comparison',
code: 'if now > timestamp { release(); }',
expectedType: 'DIRECT_TIMESTAMP_COMPARISON'
},
{
name: 'Lock period calculation',
code: 'let lock_period = timestamp - now;',
expectedType: 'LOCK_PERIOD_TIMESTAMP_USAGE'
},
{
name: 'Time condition',
code: 'if (block.timestamp > deadline) { execute(); }',
expectedType: 'UNPROTECTED_TIME_CONDITION'
},
{
name: 'Timestamp arithmetic',
code: 'let unlock_time = timestamp + 86400 * 7;',
expectedType: 'TIMESTAMP_ARITHMETIC_LOCK'
}
];
testCases.forEach(test => {
console.log(`\nTesting: ${test.name}`);
console.log(`Code: ${test.code}`);
let detected = false;
for (const pattern of vulnerablePatterns) {
const matches = test.code.match(pattern.pattern);
if (matches) {
console.log(`✅ Detected: ${pattern.type} (${pattern.severity})`);
detected = true;
break;
}
}
if (!detected) {
console.log('❌ Not detected - pattern may need adjustment');
}
});
console.log('\n🎯 Summary');
console.log('=' .repeat(20));
console.log('✅ Time-based attack vulnerability detection implemented');
console.log('✅ Protection strategies documented');
console.log('✅ Test cases created');
console.log('✅ Secure contract examples provided');
console.log('\n📋 Next Steps:');
console.log('- Install Node.js and npm to run the full scanner');
console.log('- Use: npm run scan <contract-path>');
console.log('- Review documentation for implementation guidelines');