-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
529 lines (472 loc) · 20.9 KB
/
example.py
File metadata and controls
529 lines (472 loc) · 20.9 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
#!/usr/bin/env python3
"""
agentsh + Modal Sandbox Security Demo
Demonstrates agentsh security features running in a Modal Sandbox.
Prerequisites:
pip install modal
Usage:
modal run example.py
"""
import modal
from pathlib import Path
# =============================================================================
# AGENTSH CONFIGURATION
# =============================================================================
AGENTSH_REPO = "canyonroad/agentsh"
AGENTSH_TAG = "v0.18.3"
DEB_ARCH = "amd64"
# =============================================================================
# SECURITY TEST DEFINITIONS
# =============================================================================
# NOTE: agentsh v0.18.3 uses ptrace-based enforcement which works on
# gVisor/Modal. This provides DNS domain filtering, command blocking,
# and file access control without needing seccomp_user_notify or FUSE.
SECURITY_TESTS = {
# =========================================================================
# A. MODAL NATIVE NETWORK ISOLATION
# =========================================================================
"modal_network": {
"title": "Modal Native Network Isolation",
"description": "Network security provided by Modal's container runtime",
"tests": [
{
"name": "AWS metadata blocked (Modal)",
"command": "curl -s --connect-timeout 2 http://169.254.169.254/latest/meta-data/ 2>&1",
"expect": "blocked",
"description": "Modal blocks cloud metadata access natively",
},
{
"name": "GCP metadata blocked (Modal)",
"command": "curl -s --connect-timeout 2 -H 'Metadata-Flavor: Google' http://metadata.google.internal/ 2>&1",
"expect": "blocked",
"description": "Modal blocks cloud metadata access natively",
},
{
"name": "Public HTTPS works",
"command": "curl -sI --connect-timeout 5 https://httpbin.org/get 2>&1 | head -3",
"expect": "success",
"description": "Outbound HTTPS to public internet works",
},
{
"name": "Public HTTP works",
"command": "curl -sI --connect-timeout 5 http://httpbin.org/get 2>&1 | head -3",
"expect": "success",
"description": "Outbound HTTP to public internet works",
},
],
},
# =========================================================================
# B. AGENTSH DAEMON FUNCTIONALITY
# =========================================================================
"agentsh_daemon": {
"title": "agentsh Daemon (Control API)",
"description": "agentsh server running for session management, audit, and DLP",
"tests": [
{
"name": "agentsh health check",
"command": "curl -s http://127.0.0.1:18080/health",
"expect": "success",
"description": "agentsh daemon is running",
},
{
"name": "agentsh metrics endpoint",
"command": "curl -s http://127.0.0.1:18080/metrics | head -5",
"expect": "success",
"description": "Metrics available for monitoring",
},
{
"name": "agentsh ready check",
"command": "curl -s http://127.0.0.1:18080/ready",
"expect": "success",
"description": "agentsh is ready to accept requests",
},
],
},
# =========================================================================
# C. BASIC SANDBOX OPERATIONS
# =========================================================================
"basic_ops": {
"title": "Basic Sandbox Operations",
"description": "Verify basic operations work in Modal sandbox",
"tests": [
{
"name": "Basic echo",
"command": "echo 'Hello from Modal sandbox'",
"expect": "success",
"description": "Basic shell command",
},
{
"name": "List files",
"command": "ls -la /etc/agentsh/",
"expect": "success",
"description": "agentsh config directory exists",
},
{
"name": "Git version",
"command": "git --version",
"expect": "success",
"description": "Git is available",
},
{
"name": "Python version",
"command": "python3 --version",
"expect": "success",
"description": "Python is available",
},
{
"name": "agentsh binary",
"command": "/usr/bin/agentsh --version",
"expect": "success",
"description": "agentsh is installed",
},
{
"name": "Policy file loaded",
"command": "cat /etc/agentsh/policies/default.yaml | head -10",
"expect": "success",
"description": "Security policy is in place",
},
],
},
# =========================================================================
# D. MODAL CONTAINER ISOLATION
# =========================================================================
"modal_isolation": {
"title": "Modal Container Isolation",
"description": "Security provided by Modal's container runtime (runs as root in isolated container)",
"tests": [
{
"name": "Running as root",
"command": "whoami",
"expect": "success",
"description": "Container runs as root (isolated)",
},
{
"name": "No docker socket",
"command": "ls -la /var/run/docker.sock 2>&1",
"expect": "blocked",
"description": "Docker socket not available",
},
{
"name": "No host PID namespace",
"command": "cat /proc/1/cmdline 2>&1 | tr '\\0' ' '",
"expect": "success",
"description": "PID 1 is container init, not host",
},
{
"name": "Isolated filesystem",
"command": "ls /host 2>&1",
"expect": "blocked",
"description": "No host filesystem access",
},
],
},
# =========================================================================
# E. MODAL-SPECIFIC SECURITY SCENARIOS
# =========================================================================
"modal_specific": {
"title": "Modal-Specific Security Scenarios",
"description": "Security risks and protections specific to Modal sandboxes",
"tests": [
{
"name": "Environment variables visible",
"command": "env | grep -E '^(PATH|HOME|MODAL)' | head -5",
"expect": "success",
"description": "Agents can read environment variables (secrets risk)",
},
{
"name": "Modal token exposure check",
"command": "env | grep -i token || echo 'no tokens in env'",
"expect": "success",
"description": "Check if Modal tokens are exposed in environment",
},
{
"name": "Outbound data exfiltration possible",
"command": "curl -s -X POST https://httpbin.org/post -d 'secret=test123' | grep -o 'secret.*test123' || echo 'data sent'",
"expect": "success",
"description": "Modal allows outbound HTTPS (exfiltration risk without agentsh)",
},
{
"name": "Can access any public API",
"command": "curl -s https://api.github.com/zen",
"expect": "success",
"description": "No domain restrictions (would be blocked by agentsh policy)",
},
{
"name": "Write to /tmp unrestricted",
"command": "dd if=/dev/zero of=/tmp/bigfile bs=1M count=10 2>&1 && ls -lh /tmp/bigfile && rm /tmp/bigfile",
"expect": "success",
"description": "Disk space abuse possible (would be limited by agentsh)",
},
{
"name": "Process limits exist",
"command": "ulimit -u",
"expect": "success",
"description": "Modal sets process limits",
},
],
},
# =========================================================================
# F. AGENTSH API FUNCTIONALITY
# =========================================================================
"agentsh_api": {
"title": "agentsh API Functionality",
"description": "Test agentsh's API endpoints (works on Modal)",
"tests": [
{
"name": "List policies via API",
"command": "curl -s http://127.0.0.1:18080/api/v1/policies 2>&1 | head -c 100",
"expect": "success",
"description": "Policy API accessible",
},
{
"name": "Get server info",
"command": "curl -s http://127.0.0.1:18080/api/v1/info 2>&1 | head -c 100",
"expect": "success",
"description": "Server info available",
},
],
},
# =========================================================================
# G. SECURITY GAPS DEMONSTRATION
# =========================================================================
"security_gaps": {
"title": "Security Gaps (what agentsh would protect)",
"description": "Demonstrating risks that full agentsh would mitigate",
"tests": [
{
"name": "rm -rf executes freely",
"command": "mkdir -p /tmp/testdir && touch /tmp/testdir/file.txt && rm -rf /tmp/testdir && echo 'rm -rf succeeded'",
"expect": "success",
"description": "Destructive command runs (agentsh would block)",
},
{
"name": "Can read system files",
"command": "head -2 /etc/passwd",
"expect": "success",
"description": "System file access (agentsh could require approval)",
},
{
"name": "Can write anywhere in /tmp",
"command": "echo 'sensitive data' > /tmp/leak.txt && cat /tmp/leak.txt && rm /tmp/leak.txt",
"expect": "success",
"description": "Unrestricted file write (agentsh would control)",
},
],
},
}
# =============================================================================
# MODAL IMAGE DEFINITION
# =============================================================================
def create_agentsh_image() -> modal.Image:
"""Create a Modal image with agentsh installed."""
version = AGENTSH_TAG.lstrip("v")
deb_name = f"agentsh_{version}_linux_{DEB_ARCH}.deb"
deb_url = f"https://github.com/{AGENTSH_REPO}/releases/download/{AGENTSH_TAG}/{deb_name}"
return (
modal.Image.debian_slim(python_version="3.11")
.apt_install(
"ca-certificates",
"curl",
"bash",
"git",
"sudo",
"libseccomp2",
"fuse3",
)
.run_commands(
# Download and install agentsh
f"curl -fsSL -L '{deb_url}' -o /tmp/agentsh.deb",
"dpkg -i /tmp/agentsh.deb",
"rm -f /tmp/agentsh.deb",
"agentsh --version",
# Create agentsh directories
"mkdir -p /etc/agentsh/policies /var/lib/agentsh/quarantine /var/lib/agentsh/sessions /var/log/agentsh",
"chmod 777 /etc/agentsh /etc/agentsh/policies",
"chmod 777 /var/lib/agentsh /var/lib/agentsh/quarantine /var/lib/agentsh/sessions",
"chmod 777 /var/log/agentsh",
)
.env({"AGENTSH_SERVER": "http://127.0.0.1:18080"})
)
# =============================================================================
# MODAL APP DEFINITION
# =============================================================================
app = modal.App("agentsh-sandbox")
image = create_agentsh_image()
# =============================================================================
# HELPER FUNCTIONS
# =============================================================================
def write_file_to_sandbox(sb: modal.Sandbox, path: str, content: str) -> None:
"""Write a file to the sandbox filesystem."""
# Use heredoc to write file content safely
p = sb.exec("sh", "-c", f"cat > '{path}' << 'AGENTSH_EOF'\n{content}\nAGENTSH_EOF")
p.wait()
def run_command(sb: modal.Sandbox, command: str, timeout: int = 30) -> tuple[str, str, int]:
"""Run a command in the sandbox and return stdout, stderr, exit_code."""
p = sb.exec("bash", "-c", command, timeout=timeout)
p.wait()
stdout = p.stdout.read()
stderr = p.stderr.read()
exit_code = p.returncode
return stdout, stderr, exit_code
def setup_agentsh(sb: modal.Sandbox, config_yaml: str, default_yaml: str, use_shim: bool = False) -> None:
"""Configure agentsh in the sandbox.
Args:
sb: Modal sandbox instance
config_yaml: agentsh server config content
default_yaml: Security policy content
use_shim: If True, attempt to install shell shim (requires seccomp user notify).
If False, use network-proxy-only mode.
"""
print(" Writing configuration files...")
write_file_to_sandbox(sb, "/etc/agentsh/config.yaml", config_yaml)
write_file_to_sandbox(sb, "/etc/agentsh/policies/default.yaml", default_yaml)
if use_shim:
print(" Installing shell shim...")
stdout, stderr, exit_code = run_command(
sb,
"agentsh shim install-shell --root / --shim /usr/bin/agentsh-shell-shim --bash --i-understand-this-modifies-the-host",
timeout=60
)
if exit_code != 0:
print(f" Warning: Shell shim installation returned exit code {exit_code}")
print(f" stdout: {stdout}")
print(f" stderr: {stderr}")
else:
print(" Skipping shell shim (seccomp user notify not available)")
print(" Using network-proxy-only mode...")
print(" Starting agentsh daemon...")
# Start the daemon in background
sb.exec("sh", "-c", "nohup agentsh server --config /etc/agentsh/config.yaml > /var/log/agentsh/agentsh.log 2>&1 &")
# Wait for daemon to be ready
import time
for i in range(10):
time.sleep(1)
stdout, stderr, exit_code = run_command(sb, "curl -s http://127.0.0.1:18080/health 2>&1", timeout=5)
if exit_code == 0:
print(f" agentsh daemon is running! (took {i+1}s)")
return
# Check if process is running
ps_out, _, _ = run_command(sb, "pgrep -f 'agentsh server' || echo 'not running'", timeout=5)
if "not running" in ps_out:
# Try to get error from log
log_out, _, _ = run_command(sb, "cat /var/log/agentsh/agentsh.log 2>&1 | tail -20", timeout=5)
print(f" Daemon not running. Log:\n{log_out}")
break
print(f" Warning: daemon may not be fully ready (health check: {stdout.strip() or 'no response'})")
# =============================================================================
# MAIN ENTRY POINT
# =============================================================================
@app.local_entrypoint()
def main():
print("=" * 70)
print(" agentsh + Modal Sandbox Security Demo")
print("=" * 70)
# Read configuration files
script_dir = Path(__file__).parent
config_yaml = (script_dir / "config.yaml").read_text()
default_yaml = (script_dir / "default.yaml").read_text()
# -------------------------------------------------------------------------
# Step 1: Create Sandbox
# -------------------------------------------------------------------------
print("\n[1] Creating Modal Sandbox with agentsh...")
sb = modal.Sandbox.create(
app=app,
image=image,
timeout=60 * 30, # 30 minutes
)
print(f" Sandbox ID: {sb.object_id}")
try:
# -------------------------------------------------------------------------
# Step 2: Configure agentsh
# -------------------------------------------------------------------------
print("\n[2] Configuring agentsh...")
setup_agentsh(sb, config_yaml, default_yaml, use_shim=False)
print(" agentsh configured!")
# -------------------------------------------------------------------------
# Step 3: Run Security Tests
# -------------------------------------------------------------------------
results = {"passed": 0, "failed": 0, "errors": 0}
for category_key, category in SECURITY_TESTS.items():
print(f"\n{'=' * 70}")
print(f" {category['title']}")
print(f" {category['description']}")
print("=" * 70)
for test in category["tests"]:
print(f"\n[TEST] {test['name']}")
print(f" {test['description']}")
print(f" Command: {test['command'][:60]}{'...' if len(test['command']) > 60 else ''}")
try:
stdout, stderr, exit_code = run_command(sb, test["command"], timeout=30)
output = (stdout + stderr).strip()
# Truncate long output
if len(output) > 200:
output = output[:200] + "..."
# Determine if test passed based on expectation
if test["expect"] == "blocked":
# For blocked tests, we expect non-zero exit or error message
passed = (
exit_code != 0
or "blocked" in output.lower()
or "denied" in output.lower()
or "permission" in output.lower()
or "400" in output
or "not found" in output.lower()
)
elif test["expect"] == "success":
passed = exit_code == 0
else:
passed = True
status = "PASS" if passed else "FAIL"
results["passed" if passed else "failed"] += 1
print(f" Output: {output if output else '(no output)'}")
print(f" Exit code: {exit_code}")
print(f" Result: [{status}]")
except TimeoutError:
print(" Error: Command timed out")
print(" Result: [ERROR]")
results["errors"] += 1
except Exception as e:
print(f" Error: {e}")
print(" Result: [ERROR]")
results["errors"] += 1
# -------------------------------------------------------------------------
# Summary
# -------------------------------------------------------------------------
print("\n" + "=" * 70)
print(" SUMMARY")
print("=" * 70)
print(f"""
Tests passed: {results['passed']}
Tests failed: {results['failed']}
Errors: {results['errors']}
MODAL NATIVE PROTECTION:
- Cloud metadata blocked (169.254.169.254)
- Container isolation (separate namespaces)
- No Docker socket access
- No host filesystem access
- Process limits (fork bomb protection)
AGENTSH {AGENTSH_TAG} ON MODAL (ptrace mode):
Working:
- Daemon (health, metrics, ready endpoints)
- Session management API
- DNS domain-name filtering (allow/deny by name)
- Command blocking (ptrace execve interception)
- File access control (ptrace openat interception)
- Network CIDR blocking
- MCP API endpoints
- Audit event logging
- Policy configuration loaded
ptrace mode replaces seccomp+FUSE, which were blocked by gVisor.
Domain-name DNS filtering is now enforced on Modal for the first time.
""")
finally:
# -------------------------------------------------------------------------
# Cleanup
# -------------------------------------------------------------------------
print("\n[CLEANUP] Terminating Sandbox...")
sb.terminate()
print(f" Sandbox {sb.object_id} terminated.")
if __name__ == "__main__":
# This allows running with `python example.py` for testing,
# but the recommended way is `modal run example.py`
print("Run this script with: modal run example.py")