typescript-virtual-container 1.1.4 → 1.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +42 -0
- package/HONEYPOT.md +358 -0
- package/README.md +471 -16
- package/dist/Honeypot/index.d.ts +132 -0
- package/dist/Honeypot/index.d.ts.map +1 -0
- package/dist/Honeypot/index.js +289 -0
- package/dist/SSHMimic/index.d.ts +2 -1
- package/dist/SSHMimic/index.d.ts.map +1 -1
- package/dist/SSHMimic/index.js +12 -1
- package/dist/SSHMimic/sftp.d.ts +3 -1
- package/dist/SSHMimic/sftp.d.ts.map +1 -1
- package/dist/SSHMimic/sftp.js +20 -1
- package/dist/VirtualFileSystem/index.d.ts +2 -1
- package/dist/VirtualFileSystem/index.d.ts.map +1 -1
- package/dist/VirtualFileSystem/index.js +8 -1
- package/dist/VirtualShell/index.d.ts +2 -1
- package/dist/VirtualShell/index.d.ts.map +1 -1
- package/dist/VirtualShell/index.js +6 -1
- package/dist/VirtualUserManager/index.d.ts +2 -1
- package/dist/VirtualUserManager/index.d.ts.map +1 -1
- package/dist/VirtualUserManager/index.js +19 -1
- package/dist/honeypot.d.ts +132 -0
- package/dist/honeypot.d.ts.map +1 -0
- package/dist/honeypot.js +289 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/examples/README.md +210 -0
- package/examples/honeypot-audit.ts +180 -0
- package/examples/honeypot-export.ts +253 -0
- package/examples/honeypot-quickstart.ts +110 -0
- package/package.json +1 -1
- package/src/Honeypot/index.ts +422 -0
- package/src/SSHMimic/index.ts +13 -1
- package/src/SSHMimic/sftp.ts +21 -1
- package/src/VirtualFileSystem/index.ts +8 -1
- package/src/VirtualShell/index.ts +6 -1
- package/src/VirtualUserManager/index.ts +21 -3
- package/src/index.ts +6 -0
package/dist/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { HoneyPot } from "./Honeypot";
|
|
1
2
|
import { SshClient } from "./SSHClient";
|
|
2
3
|
import { SftpMimic, SshMimic } from "./SSHMimic/index";
|
|
3
4
|
import VirtualFileSystem from "./VirtualFileSystem";
|
|
4
5
|
import { VirtualShell } from "./VirtualShell";
|
|
5
6
|
import { VirtualUserManager } from "./VirtualUserManager";
|
|
6
|
-
export { SshClient, VirtualFileSystem, SftpMimic as VirtualSftpServer, VirtualShell, SshMimic as VirtualSshServer, VirtualUserManager, };
|
|
7
|
+
export { HoneyPot, SshClient, VirtualFileSystem, SftpMimic as VirtualSftpServer, VirtualShell, SshMimic as VirtualSshServer, VirtualUserManager, };
|
|
7
8
|
export { getArg, getFlag, ifFlag, } from "./commands/command-helpers";
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
# HoneyPot Examples
|
|
2
|
+
|
|
3
|
+
This directory contains practical examples demonstrating how to use the `HoneyPot` auditing and event tracking utility.
|
|
4
|
+
|
|
5
|
+
## Quick Start with HoneyPot
|
|
6
|
+
|
|
7
|
+
### 1. Basic Introduction (Recommended First)
|
|
8
|
+
|
|
9
|
+
**File:** `honeypot-quickstart.ts`
|
|
10
|
+
|
|
11
|
+
A beginner-friendly, step-by-step introduction to HoneyPot:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
bun run examples/honeypot-quickstart.ts
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
**What it covers:**
|
|
18
|
+
- Creating a virtual environment
|
|
19
|
+
- Initializing HoneyPot
|
|
20
|
+
- Attaching HoneyPot to components
|
|
21
|
+
- Collecting statistics
|
|
22
|
+
- Viewing recent events
|
|
23
|
+
- Querying filtered logs
|
|
24
|
+
- Detecting anomalies
|
|
25
|
+
|
|
26
|
+
**Output:** Console display with colored examples and activity statistics
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
### 2. Comprehensive Auditing
|
|
31
|
+
|
|
32
|
+
**File:** `honeypot-audit.ts`
|
|
33
|
+
|
|
34
|
+
A complete auditing scenario with multiple users and suspicious activities:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
bun run examples/honeypot-audit.ts
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
**What it covers:**
|
|
41
|
+
- Normal user activity tracking
|
|
42
|
+
- Suspicious operation attempts
|
|
43
|
+
- Detailed activity summaries
|
|
44
|
+
- Event filtering by type and source
|
|
45
|
+
- File system activity tracking
|
|
46
|
+
- Anomaly detection with severity levels
|
|
47
|
+
- Audit log export preparation
|
|
48
|
+
|
|
49
|
+
**Output:** Detailed audit report with sections for each component
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
### 3. Advanced: Export & Analysis
|
|
54
|
+
|
|
55
|
+
**File:** `honeypot-export.ts`
|
|
56
|
+
|
|
57
|
+
Professional audit report generation with file exports:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
bun run examples/honeypot-export.ts
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
**What it covers:**
|
|
64
|
+
- Generating structured audit reports
|
|
65
|
+
- Exporting to JSON format
|
|
66
|
+
- Exporting to CSV format (for spreadsheet analysis)
|
|
67
|
+
- Exporting statistics
|
|
68
|
+
- Integration patterns with external systems
|
|
69
|
+
- Query examples for custom analysis
|
|
70
|
+
|
|
71
|
+
**Output:**
|
|
72
|
+
- `audit_report.json` - Complete audit report
|
|
73
|
+
- `audit_events.csv` - Timeline in spreadsheet format
|
|
74
|
+
- `audit_stats.json` - Summary statistics
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
## HoneyPot API Quick Reference
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
// Create instance
|
|
82
|
+
const honeypot = new HoneyPot(maxLogSize);
|
|
83
|
+
|
|
84
|
+
// Attach to components
|
|
85
|
+
honeypot.attach(shell, vfs, users, ssh, sftp);
|
|
86
|
+
|
|
87
|
+
// Get statistics
|
|
88
|
+
const stats = honeypot.getStats();
|
|
89
|
+
|
|
90
|
+
// Get audit log
|
|
91
|
+
const allLogs = honeypot.getAuditLog();
|
|
92
|
+
const typeFiltered = honeypot.getAuditLog("auth:failure");
|
|
93
|
+
const sourceFiltered = honeypot.getAuditLog(undefined, "SshMimic");
|
|
94
|
+
|
|
95
|
+
// Get recent entries
|
|
96
|
+
const recent = honeypot.getRecent(50);
|
|
97
|
+
|
|
98
|
+
// Detect anomalies
|
|
99
|
+
const anomalies = honeypot.detectAnomalies();
|
|
100
|
+
|
|
101
|
+
// Reset tracking
|
|
102
|
+
honeypot.reset();
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Common Use Cases
|
|
106
|
+
|
|
107
|
+
### Use Case 1: Real-Time Monitoring
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
honeypot.on("auth:failure", (count) => {
|
|
111
|
+
if (count > 3) {
|
|
112
|
+
console.log("⚠️ Potential brute-force attack detected!");
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Use Case 2: Post-Execution Audit Report
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
// After operations complete
|
|
121
|
+
const report = {
|
|
122
|
+
timestamp: new Date(),
|
|
123
|
+
stats: honeypot.getStats(),
|
|
124
|
+
anomalies: honeypot.detectAnomalies(),
|
|
125
|
+
auditLog: honeypot.getAuditLog(),
|
|
126
|
+
};
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Use Case 3: Security Analysis
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
// Find all failed auth attempts by user
|
|
133
|
+
const failures = honeypot
|
|
134
|
+
.getAuditLog("auth:failure")
|
|
135
|
+
.reduce((map, entry) => {
|
|
136
|
+
const user = entry.details.username;
|
|
137
|
+
map[user] = (map[user] || 0) + 1;
|
|
138
|
+
return map;
|
|
139
|
+
}, {});
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Use Case 4: Compliance & Audit Trail
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
// Export complete trail for compliance
|
|
146
|
+
const auditData = {
|
|
147
|
+
exportDate: new Date().toISOString(),
|
|
148
|
+
entries: honeypot.getAuditLog(),
|
|
149
|
+
stats: honeypot.getStats(),
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
// Store in database, send to SIEM, or archive
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## Integration Examples
|
|
156
|
+
|
|
157
|
+
### With Database
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
const entries = honeypot.getAuditLog();
|
|
161
|
+
await database.insertMany("audit_logs", entries);
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### With Monitoring System
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
const anomalies = honeypot.detectAnomalies();
|
|
168
|
+
if (anomalies.length > 0) {
|
|
169
|
+
await monitoring.alert({
|
|
170
|
+
type: "security",
|
|
171
|
+
level: "high",
|
|
172
|
+
anomalies,
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### With Message Queue
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
const report = honeypot.getRecent(1000);
|
|
181
|
+
await queue.publish("audit-topic", report);
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## Performance Notes
|
|
185
|
+
|
|
186
|
+
- HoneyPot maintains an in-memory log with configurable size limit
|
|
187
|
+
- Older entries are automatically trimmed when max size is exceeded
|
|
188
|
+
- Statistics are computed efficiently and cached
|
|
189
|
+
- Anomaly detection runs in O(1) time
|
|
190
|
+
|
|
191
|
+
## Troubleshooting
|
|
192
|
+
|
|
193
|
+
**No events logged?**
|
|
194
|
+
- Ensure `honeypot.attach()` is called after all components are created
|
|
195
|
+
- Check that operations are actually performed (file writes, auth attempts, etc.)
|
|
196
|
+
|
|
197
|
+
**Memory growth?**
|
|
198
|
+
- Adjust `maxLogSize` in the constructor to limit retention
|
|
199
|
+
- Call `honeypot.reset()` to clear logs between test phases
|
|
200
|
+
|
|
201
|
+
**Missing events?**
|
|
202
|
+
- Use `honeypot.getAuditLog(type, source)` to filter and verify
|
|
203
|
+
- Check the exact event names in the [API Reference](../README.md#honeypot-auditing--event-tracking)
|
|
204
|
+
|
|
205
|
+
## More Information
|
|
206
|
+
|
|
207
|
+
See the main [README.md](../README.md) for:
|
|
208
|
+
- [HoneyPot API Reference](../README.md#honeypot-auditing--event-tracking)
|
|
209
|
+
- [Example 8: Security Auditing with HoneyPot](../README.md#example-8-security-auditing-with-honeypot)
|
|
210
|
+
- Complete [Event Types Documentation](../README.md#events)
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HoneyPot Auditing Example
|
|
3
|
+
*
|
|
4
|
+
* Demonstrates how to use the HoneyPot utility to track all activity
|
|
5
|
+
* in a virtual environment, collect statistics, and detect anomalies.
|
|
6
|
+
*
|
|
7
|
+
* Run with: bun run examples/honeypot-audit.ts
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
HoneyPot,
|
|
12
|
+
SshClient,
|
|
13
|
+
VirtualShell,
|
|
14
|
+
VirtualSshServer,
|
|
15
|
+
} from "../src/index";
|
|
16
|
+
|
|
17
|
+
async function demonstrateHoneypot() {
|
|
18
|
+
console.log("🍯 HoneyPot Auditing Example\n");
|
|
19
|
+
|
|
20
|
+
// Initialize the virtual environment
|
|
21
|
+
const shell = new VirtualShell("security-lab");
|
|
22
|
+
const ssh = new VirtualSshServer({ port: 2222, shell });
|
|
23
|
+
await ssh.start();
|
|
24
|
+
|
|
25
|
+
const users = shell.getUsers()!;
|
|
26
|
+
const vfs = shell.getVfs()!;
|
|
27
|
+
|
|
28
|
+
// Create HoneyPot instance with 1000-entry log limit
|
|
29
|
+
const honeypot = new HoneyPot(1000);
|
|
30
|
+
|
|
31
|
+
// Attach HoneyPot to all components
|
|
32
|
+
honeypot.attach(shell, vfs, users, ssh);
|
|
33
|
+
|
|
34
|
+
console.log("✅ HoneyPot attached to all components\n");
|
|
35
|
+
|
|
36
|
+
// ------ Scenario 1: Normal user activity ------
|
|
37
|
+
console.log("--- Scenario 1: Normal User Activity ---\n");
|
|
38
|
+
|
|
39
|
+
await users.addUser("alice", "alice_pass123");
|
|
40
|
+
await users.addUser("bob", "bob_pass456");
|
|
41
|
+
|
|
42
|
+
const alice = new SshClient(shell, "alice");
|
|
43
|
+
await alice.mkdir("/home/alice/work", true);
|
|
44
|
+
await alice.writeFile("/home/alice/work/notes.txt", "Project notes");
|
|
45
|
+
await alice.ls("/home/alice/work");
|
|
46
|
+
await alice.cat("/home/alice/work/notes.txt");
|
|
47
|
+
|
|
48
|
+
console.log("✓ Alice performed normal operations\n");
|
|
49
|
+
|
|
50
|
+
// ------ Scenario 2: Bob attempts suspicious operations ------
|
|
51
|
+
console.log("--- Scenario 2: Suspicious Attempt ---\n");
|
|
52
|
+
|
|
53
|
+
const bob = new SshClient(shell, "bob");
|
|
54
|
+
// These will fail but are tracked
|
|
55
|
+
await bob.readFile("/etc/shadow");
|
|
56
|
+
await bob.readFile("/etc/passwd");
|
|
57
|
+
await bob.readFile("/root/.ssh/id_rsa");
|
|
58
|
+
|
|
59
|
+
console.log("✓ Bob attempted unauthorized file access\n");
|
|
60
|
+
|
|
61
|
+
// ------ Activity Summary ------
|
|
62
|
+
console.log("--- Activity Summary ---\n");
|
|
63
|
+
|
|
64
|
+
const stats = honeypot.getStats();
|
|
65
|
+
console.log("📊 Audit Statistics:");
|
|
66
|
+
console.log(` • Auth attempts: ${stats.authAttempts}`);
|
|
67
|
+
console.log(` • Auth successes: ${stats.authSuccesses}`);
|
|
68
|
+
console.log(` • Auth failures: ${stats.authFailures}`);
|
|
69
|
+
console.log(` • Commands executed: ${stats.commands}`);
|
|
70
|
+
console.log(` • File reads: ${stats.fileReads}`);
|
|
71
|
+
console.log(` • File writes: ${stats.fileWrites}`);
|
|
72
|
+
console.log(` • Users created: ${stats.userCreated}`);
|
|
73
|
+
console.log(` • Sessions started: ${stats.sessionStarts}\n`);
|
|
74
|
+
|
|
75
|
+
// ------ Recent Events ------
|
|
76
|
+
console.log("--- Most Recent Events ---\n");
|
|
77
|
+
|
|
78
|
+
const recent = honeypot.getRecent(10);
|
|
79
|
+
console.log(`📋 Last ${recent.length} events:\n`);
|
|
80
|
+
recent.forEach((entry) => {
|
|
81
|
+
console.log(` [${entry.timestamp}]`);
|
|
82
|
+
console.log(` Source: ${entry.source}`);
|
|
83
|
+
console.log(` Event: ${entry.type}`);
|
|
84
|
+
console.log(` Details: ${JSON.stringify(entry.details)}\n`);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// ------ Filtered Audit Log ------
|
|
88
|
+
console.log("--- Filtered Audit Log ---\n");
|
|
89
|
+
|
|
90
|
+
const authFailures = honeypot.getAuditLog("auth:failure");
|
|
91
|
+
console.log(`🚨 Auth Failures (${authFailures.length} total):\n`);
|
|
92
|
+
if (authFailures.length > 0) {
|
|
93
|
+
authFailures.forEach((entry) => {
|
|
94
|
+
console.log(
|
|
95
|
+
` • User "${entry.details.username}" from ${entry.details.remoteAddress}`,
|
|
96
|
+
);
|
|
97
|
+
});
|
|
98
|
+
} else {
|
|
99
|
+
console.log(" • None detected");
|
|
100
|
+
}
|
|
101
|
+
console.log();
|
|
102
|
+
|
|
103
|
+
// ------ SSH-specific events ------
|
|
104
|
+
console.log("--- SSH-Specific Events ---\n");
|
|
105
|
+
|
|
106
|
+
const sshEvents = honeypot.getAuditLog(undefined, "SshMimic");
|
|
107
|
+
console.log(`🔗 SSH events (${sshEvents.length} total):\n`);
|
|
108
|
+
sshEvents.forEach((entry) => {
|
|
109
|
+
console.log(` • ${entry.type}: ${JSON.stringify(entry.details)}`);
|
|
110
|
+
});
|
|
111
|
+
console.log();
|
|
112
|
+
|
|
113
|
+
// ------ File System Activity ------
|
|
114
|
+
console.log("--- File System Activity ---\n");
|
|
115
|
+
|
|
116
|
+
const fileWrites = honeypot.getAuditLog("file:write", "VirtualFileSystem");
|
|
117
|
+
const fileReads = honeypot.getAuditLog("file:read", "VirtualFileSystem");
|
|
118
|
+
|
|
119
|
+
console.log(`📁 File Operations:`);
|
|
120
|
+
console.log(` • File writes: ${fileWrites.length}`);
|
|
121
|
+
fileWrites.forEach((entry) => {
|
|
122
|
+
console.log(` - ${entry.details.path} (${entry.details.size} bytes)`);
|
|
123
|
+
});
|
|
124
|
+
console.log(` • File reads: ${fileReads.length}`);
|
|
125
|
+
fileReads.forEach((entry) => {
|
|
126
|
+
console.log(` - ${entry.details.path} (${entry.details.size} bytes)`);
|
|
127
|
+
});
|
|
128
|
+
console.log();
|
|
129
|
+
|
|
130
|
+
// ------ Anomaly Detection ------
|
|
131
|
+
console.log("--- Security Analysis ---\n");
|
|
132
|
+
|
|
133
|
+
const anomalies = honeypot.detectAnomalies();
|
|
134
|
+
if (anomalies.length > 0) {
|
|
135
|
+
console.log("⚠️ Anomalies Detected:\n");
|
|
136
|
+
anomalies.forEach((anomaly) => {
|
|
137
|
+
const severity = {
|
|
138
|
+
low: "ℹ️ ",
|
|
139
|
+
medium: "⚠️ ",
|
|
140
|
+
high: "🚨",
|
|
141
|
+
}[anomaly.severity];
|
|
142
|
+
console.log(` ${severity} [${anomaly.type}]`);
|
|
143
|
+
console.log(` ${anomaly.message}\n`);
|
|
144
|
+
});
|
|
145
|
+
} else {
|
|
146
|
+
console.log("✅ No anomalies detected\n");
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// ------ Export Audit Log ------
|
|
150
|
+
console.log("--- Full Audit Export ---\n");
|
|
151
|
+
|
|
152
|
+
const allAuditEntries = honeypot.getAuditLog();
|
|
153
|
+
console.log(`📊 Total audit entries: ${allAuditEntries.length}`);
|
|
154
|
+
console.log(`💾 Audit log is ready for export/storage\n`);
|
|
155
|
+
|
|
156
|
+
// Example export to JSON
|
|
157
|
+
const exportData = {
|
|
158
|
+
timestamp: new Date().toISOString(),
|
|
159
|
+
environment: "security-lab",
|
|
160
|
+
stats,
|
|
161
|
+
auditLog: allAuditEntries.slice(-50), // Last 50 entries
|
|
162
|
+
anomalies,
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
console.log("📄 Sample export structure:");
|
|
166
|
+
console.log(`${JSON.stringify(exportData, null, 2).substring(0, 300)}...\n`);
|
|
167
|
+
|
|
168
|
+
// Cleanup
|
|
169
|
+
ssh.stop();
|
|
170
|
+
|
|
171
|
+
console.log(
|
|
172
|
+
"✅ Example completed! HoneyPot auditing demonstration finished.\n",
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Run the example
|
|
177
|
+
demonstrateHoneypot().catch((error) => {
|
|
178
|
+
console.error("❌ Error:", error);
|
|
179
|
+
process.exit(1);
|
|
180
|
+
});
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HoneyPot Advanced: Audit Export & Analysis
|
|
3
|
+
*
|
|
4
|
+
* Shows how to export audit data for external analysis, storage,
|
|
5
|
+
* or integration with security monitoring systems.
|
|
6
|
+
*
|
|
7
|
+
* Run with: bun run examples/honeypot-export.ts
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import * as fs from "node:fs";
|
|
11
|
+
import {
|
|
12
|
+
HoneyPot,
|
|
13
|
+
SshClient,
|
|
14
|
+
VirtualShell,
|
|
15
|
+
VirtualSshServer,
|
|
16
|
+
} from "../src/index";
|
|
17
|
+
|
|
18
|
+
interface AuditReport {
|
|
19
|
+
timestamp: string;
|
|
20
|
+
environment: string;
|
|
21
|
+
durationMs: number;
|
|
22
|
+
summary: {
|
|
23
|
+
totalEvents: number;
|
|
24
|
+
totalUsers: number;
|
|
25
|
+
totalCommands: number;
|
|
26
|
+
failedAuthAttempts: number;
|
|
27
|
+
};
|
|
28
|
+
statistics: Record<string, number>;
|
|
29
|
+
anomalies: Array<{
|
|
30
|
+
type: string;
|
|
31
|
+
severity: string;
|
|
32
|
+
message: string;
|
|
33
|
+
}>;
|
|
34
|
+
timeline: Array<{
|
|
35
|
+
time: string;
|
|
36
|
+
event: string;
|
|
37
|
+
user?: string;
|
|
38
|
+
details: Record<string, unknown>;
|
|
39
|
+
}>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function generateAuditReport() {
|
|
43
|
+
const startTime = Date.now();
|
|
44
|
+
|
|
45
|
+
console.log("📊 HoneyPot Advanced: Generating Audit Report\n");
|
|
46
|
+
|
|
47
|
+
// Setup
|
|
48
|
+
const shell = new VirtualShell("audit-lab");
|
|
49
|
+
const ssh = new VirtualSshServer({ port: 2222, shell });
|
|
50
|
+
await ssh.start();
|
|
51
|
+
|
|
52
|
+
const users = shell.getUsers()!;
|
|
53
|
+
const vfs = shell.getVfs()!;
|
|
54
|
+
|
|
55
|
+
const honeypot = new HoneyPot(5000);
|
|
56
|
+
honeypot.attach(shell, vfs, users, ssh);
|
|
57
|
+
|
|
58
|
+
console.log("Running simulated workload...\n");
|
|
59
|
+
|
|
60
|
+
// Simulate various user activities
|
|
61
|
+
await users.addUser("analyst", "pass123");
|
|
62
|
+
await users.addUser("developer", "pass456");
|
|
63
|
+
await users.removeSudoer("developer");
|
|
64
|
+
|
|
65
|
+
// Analyst activities (authorized)
|
|
66
|
+
const analyst = new SshClient(shell, "analyst");
|
|
67
|
+
await analyst.mkdir("/data/reports", true);
|
|
68
|
+
await analyst.writeFile(
|
|
69
|
+
"/data/reports/analysis.txt",
|
|
70
|
+
"Security analysis report",
|
|
71
|
+
);
|
|
72
|
+
await analyst.ls("/data/reports");
|
|
73
|
+
|
|
74
|
+
// Developer activities
|
|
75
|
+
const dev = new SshClient(shell, "developer");
|
|
76
|
+
await dev.mkdir("/code/project", true);
|
|
77
|
+
await dev.writeFile("/code/project/main.ts", "export function main() {}");
|
|
78
|
+
|
|
79
|
+
// Some failed operations (tracked)
|
|
80
|
+
try {
|
|
81
|
+
await dev.readFile("/etc/shadow"); // Will fail
|
|
82
|
+
} catch {
|
|
83
|
+
// Ignored
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
try {
|
|
87
|
+
await dev.writeFile("/root/.bashrc", "malicious"); // Will fail
|
|
88
|
+
} catch {
|
|
89
|
+
// Ignored
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Get final duration
|
|
93
|
+
const duration = Date.now() - startTime;
|
|
94
|
+
|
|
95
|
+
console.log("Generating audit report...\n");
|
|
96
|
+
|
|
97
|
+
// Build comprehensive audit report
|
|
98
|
+
const stats = honeypot.getStats();
|
|
99
|
+
const anomalies = honeypot.detectAnomalies();
|
|
100
|
+
const auditLog = honeypot.getAuditLog();
|
|
101
|
+
|
|
102
|
+
const report: AuditReport = {
|
|
103
|
+
timestamp: new Date().toISOString(),
|
|
104
|
+
environment: "audit-lab",
|
|
105
|
+
durationMs: duration,
|
|
106
|
+
|
|
107
|
+
summary: {
|
|
108
|
+
totalEvents: auditLog.length,
|
|
109
|
+
totalUsers: stats.userCreated,
|
|
110
|
+
totalCommands: stats.commands,
|
|
111
|
+
failedAuthAttempts: stats.authFailures,
|
|
112
|
+
},
|
|
113
|
+
|
|
114
|
+
statistics: {
|
|
115
|
+
authAttempts: stats.authAttempts,
|
|
116
|
+
authSuccesses: stats.authSuccesses,
|
|
117
|
+
authFailures: stats.authFailures,
|
|
118
|
+
commandsExecuted: stats.commands,
|
|
119
|
+
fileReads: stats.fileReads,
|
|
120
|
+
fileWrites: stats.fileWrites,
|
|
121
|
+
sessionsStarted: stats.sessionStarts,
|
|
122
|
+
sessionsEnded: stats.sessionEnds,
|
|
123
|
+
usersCreated: stats.userCreated,
|
|
124
|
+
usersDeleted: stats.userDeleted,
|
|
125
|
+
clientConnects: stats.clientConnects,
|
|
126
|
+
clientDisconnects: stats.clientDisconnects,
|
|
127
|
+
},
|
|
128
|
+
|
|
129
|
+
anomalies: anomalies.map((a) => ({
|
|
130
|
+
type: a.type,
|
|
131
|
+
severity: a.severity,
|
|
132
|
+
message: a.message,
|
|
133
|
+
})),
|
|
134
|
+
|
|
135
|
+
timeline: auditLog.map((entry) => ({
|
|
136
|
+
time: entry.timestamp,
|
|
137
|
+
event: `${entry.source}:${entry.type}`,
|
|
138
|
+
user: (entry.details.username as string) || undefined,
|
|
139
|
+
details: entry.details,
|
|
140
|
+
})),
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
// Display summary
|
|
144
|
+
console.log("📋 Audit Report Summary\n");
|
|
145
|
+
console.log(` Environment: ${report.environment}`);
|
|
146
|
+
console.log(` Generated: ${report.timestamp}`);
|
|
147
|
+
console.log(` Duration: ${report.durationMs}ms\n`);
|
|
148
|
+
|
|
149
|
+
console.log("📊 Statistics:");
|
|
150
|
+
console.log(` • Total events: ${report.summary.totalEvents}`);
|
|
151
|
+
console.log(` • Total users: ${report.summary.totalUsers}`);
|
|
152
|
+
console.log(` • Commands executed: ${report.summary.totalCommands}`);
|
|
153
|
+
console.log(
|
|
154
|
+
` • Failed auth attempts: ${report.summary.failedAuthAttempts}\n`,
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
// Display anomalies if any
|
|
158
|
+
if (report.anomalies.length > 0) {
|
|
159
|
+
console.log("⚠️ Anomalies:");
|
|
160
|
+
report.anomalies.forEach((a) => {
|
|
161
|
+
console.log(` • [${a.severity}] ${a.type}`);
|
|
162
|
+
console.log(` ${a.message}`);
|
|
163
|
+
});
|
|
164
|
+
console.log();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Export to JSON file
|
|
168
|
+
const reportPath = "./audit_report.json";
|
|
169
|
+
fs.writeFileSync(reportPath, JSON.stringify(report, null, 2));
|
|
170
|
+
console.log(`✅ Report exported to: ${reportPath}\n`);
|
|
171
|
+
|
|
172
|
+
// Export CSV for spreadsheet analysis
|
|
173
|
+
const csvPath = "./audit_events.csv";
|
|
174
|
+
const csvHeader = "Timestamp,Source,Event,User,Details\n";
|
|
175
|
+
const csvRows = report.timeline
|
|
176
|
+
.map((entry) => {
|
|
177
|
+
const details = JSON.stringify(entry.details).replace(/"/g, '""');
|
|
178
|
+
return `"${entry.time}","${entry.event.split(":")[0]}","${
|
|
179
|
+
entry.event.split(":")[1]
|
|
180
|
+
}","${entry.user || ""}","${details}"`;
|
|
181
|
+
})
|
|
182
|
+
.join("\n");
|
|
183
|
+
|
|
184
|
+
fs.writeFileSync(csvPath, csvHeader + csvRows);
|
|
185
|
+
console.log(`✅ CSV export to: ${csvPath}\n`);
|
|
186
|
+
|
|
187
|
+
// Generate summary stats file
|
|
188
|
+
const statsPath = "./audit_stats.json";
|
|
189
|
+
fs.writeFileSync(
|
|
190
|
+
statsPath,
|
|
191
|
+
JSON.stringify(
|
|
192
|
+
{
|
|
193
|
+
summary: report.summary,
|
|
194
|
+
statistics: report.statistics,
|
|
195
|
+
anomalies: report.anomalies,
|
|
196
|
+
},
|
|
197
|
+
null,
|
|
198
|
+
2,
|
|
199
|
+
),
|
|
200
|
+
);
|
|
201
|
+
console.log(`✅ Stats export to: ${statsPath}\n`);
|
|
202
|
+
|
|
203
|
+
// Show sample data
|
|
204
|
+
console.log("📄 Sample Report Data:");
|
|
205
|
+
console.log(`${JSON.stringify(report, null, 2).substring(0, 500)}...\n`);
|
|
206
|
+
|
|
207
|
+
// Integration example: Send to external system
|
|
208
|
+
console.log("🔗 Integration Example:");
|
|
209
|
+
console.log("To send this data to external systems:");
|
|
210
|
+
console.log(" • Database: INSERT INTO audit_logs VALUES (...)");
|
|
211
|
+
console.log(" • API: POST /api/audit-reports (JSON payload)");
|
|
212
|
+
console.log(" • Message Queue: PUBLISH audit_report (for async processing)");
|
|
213
|
+
console.log(" • SIEM: Send via syslog or CEF format\n");
|
|
214
|
+
|
|
215
|
+
// Query examples
|
|
216
|
+
console.log("🔍 Query Examples:");
|
|
217
|
+
|
|
218
|
+
// Auth failures by user
|
|
219
|
+
const authFailures = honeypot.getAuditLog("auth:failure");
|
|
220
|
+
const failuresByUser = new Map<string, number>();
|
|
221
|
+
authFailures.forEach((entry) => {
|
|
222
|
+
const user = entry.details.username as string;
|
|
223
|
+
failuresByUser.set(user, (failuresByUser.get(user) || 0) + 1);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
if (failuresByUser.size > 0) {
|
|
227
|
+
console.log("\n Auth Failures by User:");
|
|
228
|
+
failuresByUser.forEach((count, user) => {
|
|
229
|
+
console.log(` • ${user}: ${count} failures`);
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// File operations
|
|
234
|
+
const fileWrites = honeypot.getAuditLog("file:write");
|
|
235
|
+
if (fileWrites.length > 0) {
|
|
236
|
+
console.log(`\n File Writes: ${fileWrites.length}`);
|
|
237
|
+
fileWrites.slice(-3).forEach((entry) => {
|
|
238
|
+
console.log(` • ${entry.details.path} (${entry.details.size} B)`);
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
console.log();
|
|
243
|
+
|
|
244
|
+
// Cleanup
|
|
245
|
+
ssh.stop();
|
|
246
|
+
|
|
247
|
+
console.log("✅ Audit report generation complete!");
|
|
248
|
+
console.log(
|
|
249
|
+
"💡 Tip: Open the generated .json files to view full audit trails.\n",
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
generateAuditReport().catch(console.error);
|