intercept-commands 0.1.1 → 0.1.2
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/README.md +16 -0
- package/index.js +2 -3
- package/lib/interceptor.js +215 -0
- package/package.json +7 -2
package/README.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# intercept-commands
|
|
2
|
+
|
|
3
|
+
Lightweight interceptor to detect potentially dangerous shell and database commands.
|
|
4
|
+
|
|
5
|
+
Usage:
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
const interceptor = require('intercept-commands');
|
|
9
|
+
const result = interceptor.analyzeCommand('rm -rf /');
|
|
10
|
+
console.log(result);
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
API:
|
|
14
|
+
- `analyzeCommand(text)` → { isHarmful: boolean, findings: Array }
|
|
15
|
+
- `detectDangerousCommands(text)` → Array of findings
|
|
16
|
+
- `isHarmful(text)` → boolean
|
package/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
//
|
|
2
|
-
const
|
|
3
|
-
const interceptor = require(path.join('..', 'lib', 'interceptor'));
|
|
1
|
+
// Re-export the packaged interceptor implementation
|
|
2
|
+
const interceptor = require('./lib/interceptor');
|
|
4
3
|
|
|
5
4
|
module.exports = interceptor;
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
// Core interception library for detecting dangerous shell/database commands
|
|
2
|
+
const RULES = [
|
|
3
|
+
{
|
|
4
|
+
id: 'fs-rm-recursive-force',
|
|
5
|
+
description: 'Recursive/forced file deletion (Unix)',
|
|
6
|
+
platforms: ['unix','linux','mac'],
|
|
7
|
+
severity: 'high',
|
|
8
|
+
regex: /\b(rm|unlink)\b[^\n]*(-rf|-r\s|-f\b|--no-preserve-root|--force)\b/gi,
|
|
9
|
+
example: 'rm -rf /',
|
|
10
|
+
recommendation: 'Block or require explicit approval; refuse near root or system paths.'
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
id: 'fs-rm-root-protect',
|
|
14
|
+
description: 'rm with root path or no-preserve-root',
|
|
15
|
+
platforms: ['unix','linux','mac'],
|
|
16
|
+
severity: 'critical',
|
|
17
|
+
regex: /\b(rm)\b[^\n]*(?:\/\s*(?:\$|;|\||&|$)|\\\s*(?:\$|;|\||&|$)|--no-preserve-root|^\s*rm\s+.*-[a-z]*f[a-z]*.*\s+\/(?:\s|$))/gi,
|
|
18
|
+
example: 'rm -rf / --no-preserve-root',
|
|
19
|
+
recommendation: 'Treat as critical and block automatically.'
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
id: 'windows-del-recursive',
|
|
23
|
+
description: 'Recursive forced delete on Windows',
|
|
24
|
+
platforms: ['windows'],
|
|
25
|
+
severity: 'high',
|
|
26
|
+
regex: /\b(del|erase)\b[^\n]*(?:\/s|\/q|\/f|\\\\|C:\\|%SYSTEMROOT%)/gi,
|
|
27
|
+
example: 'del /s /q C:\\*',
|
|
28
|
+
recommendation: 'Block or require approval.'
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
id: 'format-disk',
|
|
32
|
+
description: 'Disk formatting commands',
|
|
33
|
+
platforms: ['windows','unix','linux','mac'],
|
|
34
|
+
severity: 'critical',
|
|
35
|
+
regex: /\b(format|format-volume|mkfs\.|fdisk|diskpart)\b/gi,
|
|
36
|
+
example: 'format C:',
|
|
37
|
+
recommendation: 'Critical — block by default.'
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
id: 'dd-overwrite',
|
|
41
|
+
description: 'Raw device overwrite via dd',
|
|
42
|
+
platforms: ['unix','linux','mac'],
|
|
43
|
+
severity: 'critical',
|
|
44
|
+
regex: /\bdd\b[^\n]*(if=|of=)/gi,
|
|
45
|
+
example: 'dd if=/dev/zero of=/dev/sda bs=1M',
|
|
46
|
+
recommendation: 'Critical — block by default.'
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
id: 'fork-bomb',
|
|
50
|
+
description: 'Process fork bomb patterns',
|
|
51
|
+
platforms: ['unix','linux','mac'],
|
|
52
|
+
severity: 'critical',
|
|
53
|
+
regex: /:\(\)\s*\{\s*:|\bwhile\s*.*:\s*;|\$\(\([a-z].*[+]\)\)/gi,
|
|
54
|
+
example: ':(){ :|:& };:',
|
|
55
|
+
recommendation: 'Block — destructive to system availability.'
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
id: 'chmod-unsafe',
|
|
59
|
+
description: 'Changing permissions to remove all access or make executable broadly',
|
|
60
|
+
platforms: ['unix','linux','mac'],
|
|
61
|
+
severity: 'medium',
|
|
62
|
+
regex: /\bchmod\b[^\n]*(?:000|777|a\+x|\+x\b)/gi,
|
|
63
|
+
example: 'chmod 000 -R /',
|
|
64
|
+
recommendation: 'Flag for review; context-sensitive.'
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
id: 'chown-root-recursive',
|
|
68
|
+
description: 'Recursive chown to root or other system user',
|
|
69
|
+
platforms: ['unix','linux','mac'],
|
|
70
|
+
severity: 'high',
|
|
71
|
+
regex: /\bchown\b[^\n]*(-R\b|--recursive)\s+[\w:]+/gi,
|
|
72
|
+
example: 'chown -R root:root /',
|
|
73
|
+
recommendation: 'Flag — may break services.'
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
id: 'disk-wipe-windows',
|
|
77
|
+
description: 'Windows disk formatting/wiping utilities',
|
|
78
|
+
platforms: ['windows'],
|
|
79
|
+
severity: 'critical',
|
|
80
|
+
regex: /\b(format|clean)\b[^\n]*\b(volume|disk|partition|fs)\b|format\s+[A-Z]:/gi,
|
|
81
|
+
example: 'format-volume -DriveLetter D',
|
|
82
|
+
recommendation: 'Block by default.'
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
id: 'power-off-shutdown',
|
|
86
|
+
description: 'Shutdown, reboot or power-off commands',
|
|
87
|
+
platforms: ['unix','linux','mac','windows'],
|
|
88
|
+
severity: 'medium',
|
|
89
|
+
regex: /\b(shutdown|reboot|poweroff|halt|shutdown.exe|Stop-Computer)\b/gi,
|
|
90
|
+
example: 'shutdown -h now',
|
|
91
|
+
recommendation: 'Require approval; context-sensitive.'
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
id: 'services-disable',
|
|
95
|
+
description: 'Disabling or removing critical system services',
|
|
96
|
+
platforms: ['unix','linux','mac','windows'],
|
|
97
|
+
severity: 'high',
|
|
98
|
+
regex: /\b(systemctl|service|sc)\b[^\n]*(?:disable|stop|remove|delete|mask|uninstall)\b/gi,
|
|
99
|
+
example: 'systemctl disable sshd',
|
|
100
|
+
recommendation: 'Flag/require approval.'
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
id: 'docker-prune-rm',
|
|
104
|
+
description: 'Docker destructive cleanup',
|
|
105
|
+
platforms: ['unix','linux','mac','windows'],
|
|
106
|
+
severity: 'high',
|
|
107
|
+
regex: /\b(docker)\b[^\n]*(?:system\s+prune|rm\s+-f|rmi\s+-f|volume\s+rm)\b/gi,
|
|
108
|
+
example: 'docker system prune -a --volumes',
|
|
109
|
+
recommendation: 'Require approval.'
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
id: 'docker-rm-all',
|
|
113
|
+
description: 'Remove all containers/images via subshell',
|
|
114
|
+
platforms: ['unix','linux','mac'],
|
|
115
|
+
severity: 'high',
|
|
116
|
+
regex: /\b(docker\s+(rm|rmi)\s+-f)\b|\$\(docker\s+ps\s+-aq\)/gi,
|
|
117
|
+
example: 'docker rm -f $(docker ps -aq)',
|
|
118
|
+
recommendation: 'Require approval.'
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
id: 'git-destructive',
|
|
122
|
+
description: 'Git operations that rewrite history or wipe worktrees',
|
|
123
|
+
platforms: ['unix','linux','mac','windows'],
|
|
124
|
+
severity: 'medium',
|
|
125
|
+
regex: /\b(git)\b[^\n]*(?:reset\s+--hard|clean\s+-fdx|push\s+--force|-D\s*HEAD)\b/gi,
|
|
126
|
+
example: 'git reset --hard',
|
|
127
|
+
recommendation: 'Flag and require confirmation.'
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
id: 'db-drop-truncate',
|
|
131
|
+
description: 'SQL destructive statements',
|
|
132
|
+
platforms: ['sql','generic'],
|
|
133
|
+
severity: 'critical',
|
|
134
|
+
regex: /\b(DROP\s+DATABASE|DROP\s+TABLE|TRUNCATE\s+TABLE|DELETE\s+FROM)\b/gi,
|
|
135
|
+
example: 'DROP DATABASE mydb',
|
|
136
|
+
recommendation: 'Critical — block or require high-level approval.'
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
id: 'db-cli-drop',
|
|
140
|
+
description: 'Database client commands invoking drops',
|
|
141
|
+
platforms: ['generic'],
|
|
142
|
+
severity: 'critical',
|
|
143
|
+
regex: /\b(mongosh|psql|mysql|sqlcmd|redis-cli)\b[^\n]*(?:dropDatabase|DROP\s+DATABASE|FLUSHALL|--eval)\b/gi,
|
|
144
|
+
example: 'mongosh --eval "db.dropDatabase()"',
|
|
145
|
+
recommendation: 'Block by default.'
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
id: 'package-remove-system',
|
|
149
|
+
description: 'Package manager removing system packages',
|
|
150
|
+
platforms: ['linux','unix','mac'],
|
|
151
|
+
severity: 'medium',
|
|
152
|
+
regex: /\b(apt-get|apt|yum|dnf|pacman|brew)\b[^\n]*(remove|purge|uninstall|--remove|--purge)\b/gi,
|
|
153
|
+
example: 'apt-get remove --purge important-package',
|
|
154
|
+
recommendation: 'Flag for review.'
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
id: 'npm-uninstall-global',
|
|
158
|
+
description: 'Removing global packages that may break system',
|
|
159
|
+
platforms: ['unix','linux','mac','windows'],
|
|
160
|
+
severity: 'low',
|
|
161
|
+
regex: /\b(npm|yarn|pnpm)\b[^\n]*(?:(uninstall|remove)\s+(-g|--global|global)|(global|-g|--global)\s+(uninstall|remove))\b/gi,
|
|
162
|
+
example: 'npm uninstall -g some-package',
|
|
163
|
+
recommendation: 'Flag; low severity.'
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
id: 'iptables-flush',
|
|
167
|
+
description: 'Flushing firewall / iptables rules',
|
|
168
|
+
platforms: ['unix','linux','mac'],
|
|
169
|
+
severity: 'high',
|
|
170
|
+
regex: /\b(iptables|nft|ufw|netsh)\b[^\n]*(?:flush|reset|delete|-F|-X)\b/gi,
|
|
171
|
+
example: 'iptables -F',
|
|
172
|
+
recommendation: 'Require approval.'
|
|
173
|
+
}
|
|
174
|
+
];
|
|
175
|
+
|
|
176
|
+
function detectDangerousCommands(text) {
|
|
177
|
+
const results = [];
|
|
178
|
+
if (!text || typeof text !== 'string') return results;
|
|
179
|
+
const normalized = text;
|
|
180
|
+
for (const rule of RULES) {
|
|
181
|
+
try {
|
|
182
|
+
const re = rule.regex instanceof RegExp ? rule.regex : new RegExp(rule.regex, 'gi');
|
|
183
|
+
let m;
|
|
184
|
+
while ((m = re.exec(normalized)) !== null) {
|
|
185
|
+
if (m.index === re.lastIndex) re.lastIndex++;
|
|
186
|
+
results.push({
|
|
187
|
+
id: rule.id,
|
|
188
|
+
description: rule.description,
|
|
189
|
+
severity: rule.severity,
|
|
190
|
+
match: m[0],
|
|
191
|
+
index: m.index,
|
|
192
|
+
example: rule.example,
|
|
193
|
+
recommendation: rule.recommendation
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
} catch (err) {
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return results.sort((a,b) => {
|
|
201
|
+
const sev = { critical: 3, high: 2, medium: 1, low: 0 };
|
|
202
|
+
return (sev[b.severity] - sev[a.severity]) || (a.index - b.index);
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function isHarmful(text) {
|
|
207
|
+
const hits = detectDangerousCommands(text);
|
|
208
|
+
return hits.length > 0;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function analyzeCommand(text) {
|
|
212
|
+
return { isHarmful: isHarmful(text), findings: detectDangerousCommands(text) };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
module.exports = { RULES, detectDangerousCommands, isHarmful, analyzeCommand };
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "intercept-commands",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Lightweight interceptor to detect potentially dangerous shell and DB commands",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"files": ["index.js","lib","README.md"],
|
|
6
7
|
"keywords": [
|
|
7
8
|
"interceptor",
|
|
8
9
|
"security",
|
|
@@ -10,5 +11,9 @@
|
|
|
10
11
|
"dangerous"
|
|
11
12
|
],
|
|
12
13
|
"author": "",
|
|
13
|
-
"license": "MIT"
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/your-user/intercept-commands"
|
|
18
|
+
}
|
|
14
19
|
}
|