-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodesentinel.py
More file actions
261 lines (211 loc) · 7.64 KB
/
codesentinel.py
File metadata and controls
261 lines (211 loc) · 7.64 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
#!/usr/bin/env python3
"""
CodeSentinel
Single-file secure Python code analyzer & sandbox
Windows-safe
"""
import ast
import sys
import os
import multiprocessing as mp
import builtins
# ======================================================
# SECURITY CONFIGURATION
# ======================================================
DANGEROUS_MODULES = {
"os": 9,
"sys": 6,
"subprocess": 10,
"shutil": 9,
"socket": 9,
"requests": 8,
"urllib": 8,
"ctypes": 10,
}
DANGEROUS_FUNCTIONS = {
"eval": 10,
"exec": 10,
"compile": 7,
"open": 6,
}
DANGEROUS_ATTRIBUTES = {
("os", "system"): 10,
("os", "remove"): 9,
("os", "rmdir"): 9,
("subprocess", "Popen"): 10,
("subprocess", "run"): 9,
}
SANDBOX_TIMEOUT = 10 # seconds
# ======================================================
# AST ANALYZER
# ======================================================
class SentinelAnalyzer(ast.NodeVisitor):
def __init__(self, source_lines):
self.issues = []
self.score = 0
self.source_lines = source_lines
def add(self, severity, message, lineno):
self.issues.append((severity, message, lineno))
self.score += severity
def get_code_snippet(self, lineno):
if 0 < lineno <= len(self.source_lines):
return self.source_lines[lineno - 1].strip()
return "Unable to retrieve code snippet"
def visit_Import(self, node):
for n in node.names:
root = n.name.split(".")[0]
if root in DANGEROUS_MODULES:
self.add(
DANGEROUS_MODULES[root],
f"Import of dangerous module '{root}'",
node.lineno
)
def visit_ImportFrom(self, node):
if node.module:
root = node.module.split(".")[0]
if root in DANGEROUS_MODULES:
self.add(
DANGEROUS_MODULES[root],
f"Import from dangerous module '{root}'",
node.lineno
)
def visit_Call(self, node):
if isinstance(node.func, ast.Name):
if node.func.id in DANGEROUS_FUNCTIONS:
self.add(
DANGEROUS_FUNCTIONS[node.func.id],
f"Use of dangerous function '{node.func.id}()'",
node.lineno
)
if isinstance(node.func, ast.Attribute):
mod = getattr(node.func.value, "id", None)
if (mod, node.func.attr) in DANGEROUS_ATTRIBUTES:
self.add(
DANGEROUS_ATTRIBUTES[(mod, node.func.attr)],
f"Dangerous call '{mod}.{node.func.attr}()'",
node.lineno
)
self.generic_visit(node)
# ======================================================
# AI HEURISTIC (SIMPLE)
# ======================================================
def ai_probability(code: str) -> int:
score = 0
for line in code.splitlines():
if len(line.strip()) > 120:
score += 2
return min(100, score * 5)
# ======================================================
# SANDBOX EXECUTION
# ======================================================
def sandbox_exec_restricted(code: str):
def blocked_open(*args, **kwargs):
raise PermissionError("Filesystem access is blocked by CodeSentinel")
real_import = __import__
def safe_import(name, globals=None, locals=None, fromlist=(), level=0):
root = name.split(".")[0]
if root in DANGEROUS_MODULES:
raise ImportError(f"Import of '{root}' is blocked by CodeSentinel")
return real_import(name, globals, locals, fromlist, level)
SAFE_BUILTINS = {
"print": print,
"input": input,
"range": range,
"len": len,
"int": int,
"float": float,
"str": str,
"bool": bool,
"enumerate": enumerate,
"abs": abs,
"__import__": safe_import,
"open": blocked_open,
}
try:
exec(code, {"__builtins__": SAFE_BUILTINS}, {})
sys.exit(0)
except Exception as e:
sys.stderr.write(f"\n[!] SANDBOX ERROR: {type(e).__name__}: {e}\n")
sys.exit(1)
def sandbox_exec_unrestricted(code: str):
try:
exec(code, {"__builtins__": builtins}, {})
sys.exit(0)
except Exception as e:
sys.stderr.write(f"\n[!] RUNTIME ERROR: {type(e).__name__}: {e}\n")
sys.exit(1)
def run_sandbox(code: str, restricted=True):
target = sandbox_exec_restricted if restricted else sandbox_exec_unrestricted
p = mp.Process(target=target, args=(code,))
p.start()
p.join(SANDBOX_TIMEOUT)
if p.is_alive():
p.terminate()
print("❌ Code exceeded time limit and was terminated")
return False
return p.exitcode == 0
# ======================================================
# ANALYSIS & REPORT
# ======================================================
def analyze_file(path: str):
with open(path, "r", encoding="utf-8") as f:
code = f.read()
source_lines = code.splitlines()
try:
tree = ast.parse(code)
except SyntaxError:
print("❌ File contains syntax errors and cannot be analyzed.")
return None, False
analyzer = SentinelAnalyzer(source_lines)
analyzer.visit(tree)
risk = min(10, analyzer.score // 5)
ai_score = ai_probability(code)
print("\n🔱 CodeSentinel — REPORT\n")
analyzer.issues.sort(key=lambda x: x[2])
for sev, msg, lineno in analyzer.issues:
level = "HIGH" if sev >= 8 else "MEDIUM" if sev >= 5 else "LOW"
snippet = analyzer.get_code_snippet(lineno)
print(f"[{level}] Line {lineno}: {msg}")
print(f" > {snippet}\n")
print(f"🧮 Risk Score: {risk}/10")
print(f"🤖 AI-generated probability: {ai_score}%")
return code, risk > 0
# ======================================================
# CLI
# ======================================================
def main():
if len(sys.argv) != 3:
print("Usage: python codesentinel.py [scan|run] <file.py>")
sys.exit(1)
command, file = sys.argv[1], sys.argv[2]
if not os.path.exists(file):
print("❌ File not found")
sys.exit(1)
code, has_issues = analyze_file(file)
if code is None:
return
if command == "scan":
print("\n✅ Scan finished.")
return
if command == "run":
print("-------------------------------------------------")
print("⚠️ Restricted execution mode enabled by default")
print("-------------------------------------------------\n")
ok = run_sandbox(code, restricted=True)
if ok:
print("\n✅ Restricted execution completed safely")
else:
print("\n❌ Restricted execution blocked or failed")
if has_issues:
print("\n❗ HIGH-RISK CODE DETECTED")
print("❗ Running with FULL ACCESS can harm your system")
confirm = input("Type EXACTLY 'I UNDERSTAND' to run UNSAFE mode: ")
if confirm == "I UNDERSTAND":
print("\n🚨 RUNNING WITH FULL SYSTEM ACCESS 🚨\n")
ok = run_sandbox(code, restricted=False)
print("✅ Execution completed" if ok else "❌ Execution failed")
else:
print("Full access execution cancelled.")
if __name__ == "__main__":
mp.freeze_support()
main()