Repository navigation
Expand file tree
/
Copy pathskill_scanner.sh
More file actions
executable file
·1742 lines (1537 loc) · 70.9 KB
/
Copy pathskill_scanner.sh
File metadata and controls
executable file
·1742 lines (1537 loc) · 70.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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# =============================================================================
# skill_scanner.sh — AI Coding Assistant Skill/Plugin Security Scanner
# =============================================================================
# Standalone scanner for detecting malicious patterns in AI assistant skills,
# plugins, rules files, and instruction documents across 11 ecosystems.
#
# Requires: bash 4+, grep, awk, sed
# No Python, Node, jq, or LLM dependency.
#
# Usage: ./skill_scanner.sh [OPTIONS] <file_or_directory>
# =============================================================================
set -euo pipefail
VERSION="1.0.0"
# === Severity weights for scoring ===
declare -A SEVERITY_WEIGHT=(
[CRITICAL]=10
[HIGH]=5
[MEDIUM]=3
[LOW]=1
[INFO]=0
)
# === Color codes ===
RED='\033[0;31m'
BRED='\033[1;31m'
YELLOW='\033[0;33m'
BYELLOW='\033[1;33m'
GREEN='\033[0;32m'
BGREEN='\033[1;32m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
GRAY='\033[0;90m'
BOLD='\033[1m'
RESET='\033[0m'
# === Global state ===
FINDINGS=() # Array of "severity|category|pattern|line|match|description"
OUTPUT_JSON=false
VERBOSE=false
SEVERITY_THRESHOLD="INFO"
RECURSIVE=false
MAX_SEVERITY="CLEAN"
TOTAL_SCORE=0
SCANNED_FILES=0
DETECTED_ECOSYSTEM=""
# === Severity ordering ===
severity_rank() {
case "$1" in
CRITICAL) echo 5 ;;
HIGH) echo 4 ;;
MEDIUM) echo 3 ;;
LOW) echo 2 ;;
INFO) echo 1 ;;
CLEAN) echo 0 ;;
*) echo 0 ;;
esac
}
severity_meets_threshold() {
local sev="$1"
local threshold="$2"
[[ $(severity_rank "$sev") -ge $(severity_rank "$threshold") ]]
}
update_max_severity() {
local sev="$1"
if [[ $(severity_rank "$sev") -gt $(severity_rank "$MAX_SEVERITY") ]]; then
MAX_SEVERITY="$sev"
fi
}
# === Finding management ===
add_finding() {
local severity="$1"
local category="$2"
local pattern="$3"
local line="$4"
local match="$5"
local description="$6"
# Truncate match to 200 chars
if [[ ${#match} -gt 200 ]]; then
match="${match:0:200}..."
fi
# Remove newlines from match
match="${match//$'\n'/ }"
if severity_meets_threshold "$severity" "$SEVERITY_THRESHOLD"; then
FINDINGS+=("${severity}|${category}|${pattern}|${line}|${match}|${description}")
update_max_severity "$severity"
TOTAL_SCORE=$((TOTAL_SCORE + ${SEVERITY_WEIGHT[$severity]:-0}))
fi
}
# === Content preprocessing ===
extract_code_blocks() {
# Extract content from fenced code blocks in markdown
local file="$1"
awk '
/^```/ { if (in_block) { in_block=0; next } else { in_block=1; next } }
in_block { print }
' "$file"
}
extract_preprocess_commands() {
# Extract !`command` preprocessing syntax from Claude SKILL.md
local file="$1"
grep -noE '!\x60[^\x60]+\x60' "$file" 2>/dev/null || true
}
collapse_continuations() {
# Join lines ending with backslash
local file="$1"
sed ':a; /\\$/ { N; s/\\\n//; ta }' "$file"
}
# === Ecosystem detection ===
detect_ecosystem() {
local file="$1"
local basename
basename="$(basename "$file")"
local dirpath
dirpath="$(dirname "$file")"
case "$basename" in
SKILL.md)
# Distinguish Claude SKILL.md from Codex SKILL.md
if [[ "$dirpath" == *".agents/skills"* ]] || [[ "$dirpath" == *"/etc/codex/"* ]]; then
echo "codex_skill"
elif head -5 "$file" 2>/dev/null | grep -q '^---'; then
echo "claude_skill"
else
echo "unknown_skill"
fi
;;
CLAUDE.md)
echo "claude_instructions"
;;
claude_desktop_config.json)
echo "claude_desktop"
;;
.mcp.json|mcp.json)
echo "mcp_config"
;;
AGENTS.md)
echo "codex_instructions"
;;
openai.yaml)
if [[ "$dirpath" == *"/agents" ]] || [[ "$dirpath" == *"/agents/" ]]; then
echo "codex_skill_config"
else
echo "unknown"
fi
;;
.cursorrules)
echo "cursorrules"
;;
.windsurfrules)
echo "windsurfrules"
;;
package.json)
if grep -q '"vscode"\|"onCommand"\|"onLanguage"\|"activationEvents"' "$file" 2>/dev/null; then
echo "vscode_extension"
else
echo "unknown"
fi
;;
.aider.conf.yml)
echo "aider_config"
;;
copilot-instructions.md)
echo "copilot_instructions"
;;
config.json)
if grep -q '"continuedev"\|"slashCommands"\|"customCommands"\|"contextProviders"' "$file" 2>/dev/null; then
echo "continue_config"
else
echo "unknown"
fi
;;
*.yaml|*.yml)
# Check for Codex openai.yaml / skill config patterns
if grep -q 'interface:\|dependencies:' "$file" 2>/dev/null; then
echo "codex_skill_config"
elif grep -qE 'transport.*streamable_http|type.*mcp' "$file" 2>/dev/null; then
echo "codex_skill_config"
elif [[ "$dirpath" == *"codex"* ]] || [[ "$dirpath" == *"agents"* ]]; then
echo "codex_skill_config"
elif [[ "$dirpath" == *"aider"* ]]; then
echo "aider_config"
else
echo "generic"
fi
;;
*.json)
# Check for OpenAPI spec (ChatGPT actions)
if grep -q '"openapi"\|"paths"\|"servers"' "$file" 2>/dev/null; then
echo "chatgpt_action"
elif grep -q '"mcpServers"\|"mcp"' "$file" 2>/dev/null; then
echo "mcp_config"
else
echo "unknown"
fi
;;
*)
# Check for rules files in cursor directories
if [[ "$dirpath" == *".cursor/rules"* ]]; then
echo "cursorrules"
# Check for files in known skill/test directories by parent dir name
elif [[ "$dirpath" == *"claude_skill"* ]] || [[ "$dirpath" == *"claude/skills"* ]]; then
echo "claude_skill"
elif [[ "$dirpath" == *"claude_desktop"* ]]; then
echo "claude_desktop"
elif [[ "$dirpath" == *"codex"* ]] || [[ "$dirpath" == *".agents/skills"* ]]; then
echo "codex_skill"
elif [[ "$dirpath" == *"cursorrules"* ]]; then
echo "cursorrules"
elif [[ "$dirpath" == *"windsurfrules"* ]]; then
echo "windsurfrules"
elif [[ "$dirpath" == *"aider"* ]]; then
echo "aider_config"
elif [[ "$dirpath" == *"chatgpt"* ]]; then
echo "chatgpt_action"
elif [[ "$dirpath" == *"copilot"* ]]; then
echo "copilot_instructions"
# Check content for skill-like patterns
elif head -5 "$file" 2>/dev/null | grep -q '^---'; then
echo "generic_skill"
else
echo "generic"
fi
;;
esac
}
# === Detection rules ===
# Each function scans content and calls add_finding() for matches
check_network_exfil() {
local file="$1"
# Direct network commands with data sending indicators
local line_match
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "CRITICAL" "network_exfil" "curl_data_upload" "$linenum" "$content" \
"curl with data upload flag (-d, -F, --data, --upload-file)"
done < <(grep -nEi 'curl\s.+(-d\s|-F\s|--data[ =]|--upload-file[ =]|-T\s)' "$file" 2>/dev/null || true)
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "network_exfil" "curl_general" "$linenum" "$content" \
"curl command detected (potential data exfiltration)"
done < <(grep -nEi '(^|[^a-zA-Z])curl\s+(-[sSkLvofO]+ )*https?://' "$file" 2>/dev/null || true)
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "network_exfil" "wget_post" "$linenum" "$content" \
"wget with POST data"
done < <(grep -nEi 'wget\s.*(--post-data|--post-file)' "$file" 2>/dev/null || true)
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "MEDIUM" "network_exfil" "wget_general" "$linenum" "$content" \
"wget command detected"
done < <(grep -nEi '(^|[^a-zA-Z])wget\s+' "$file" 2>/dev/null || true)
# Netcat / socat
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "network_exfil" "netcat" "$linenum" "$content" \
"Netcat/socat network connection"
done < <(grep -nEi '(^|[|\s])(nc|ncat|netcat|socat|telnet)\s' "$file" 2>/dev/null || true)
# Pipe to network command
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "CRITICAL" "network_exfil" "pipe_to_network" "$linenum" "$content" \
"Data piped to network command"
done < <(grep -nEi '\|\s*(curl|wget|nc|ncat|socat|ssh)\s' "$file" 2>/dev/null || true)
# DNS exfiltration
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "network_exfil" "dns_exfil" "$linenum" "$content" \
"Potential DNS exfiltration (dig/nslookup with variable data)"
done < <(grep -nEi '(dig|nslookup)\s.*\$' "$file" 2>/dev/null || true)
# Indirect via runtimes
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "network_exfil" "python_network" "$linenum" "$content" \
"Python one-liner with network capabilities"
done < <(grep -nEi 'python[23]?\s+-c\s.*\b(urllib|requests|http\.client|socket|urlopen|httplib)\b' "$file" 2>/dev/null || true)
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "network_exfil" "node_network" "$linenum" "$content" \
"Node.js one-liner with network capabilities"
done < <(grep -nEi 'node\s+-e\s.*\b(http|https|fetch|request|net|axios)\b' "$file" 2>/dev/null || true)
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "network_exfil" "ruby_network" "$linenum" "$content" \
"Ruby one-liner with network capabilities"
done < <(grep -nEi 'ruby\s+-e\s.*\b(Net::HTTP|open-uri|HTTParty)\b' "$file" 2>/dev/null || true)
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "network_exfil" "perl_network" "$linenum" "$content" \
"Perl one-liner with network capabilities"
done < <(grep -nEi 'perl\s+-e\s.*\b(LWP|HTTP::Tiny|IO::Socket|Net::HTTP)\b' "$file" 2>/dev/null || true)
# Git push to non-standard remotes
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "MEDIUM" "network_exfil" "git_push_exfil" "$linenum" "$content" \
"git remote add or push to potentially attacker-controlled remote"
done < <(grep -nEi 'git\s+(remote\s+add|push)\s' "$file" 2>/dev/null || true)
# SSH with data
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "MEDIUM" "network_exfil" "ssh_data" "$linenum" "$content" \
"SSH command that may transfer data"
done < <(grep -nEi 'ssh\s.*[<|]' "$file" 2>/dev/null || true)
# JavaScript fetch() to external URL
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "network_exfil" "js_fetch_external" "$linenum" "$content" \
"JavaScript fetch() to external URL"
done < <(grep -nEi "fetch\s*\(\s*['\"]https?://" "$file" 2>/dev/null || true)
# Cloud sync exfiltration
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "MEDIUM" "network_exfil" "cloud_sync_exfil" "$linenum" "$content" \
"Copy to cloud-synced directory (iCloud/Dropbox/Google Drive)"
done < <(grep -nEi '(cp|mv|rsync|ln|>>?)\s+.*?(Mobile Documents|CloudDocs|Dropbox|Google Drive)|(Mobile Documents|CloudDocs|Dropbox|Google Drive).*?(cp|mv|rsync|>>)' "$file" 2>/dev/null || true)
# OpenAPI/GPT Action server URLs (external endpoints that receive data)
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
# Skip common legitimate API hosts
[[ "$content" == *"api.github.com"* ]] && continue
[[ "$content" == *"api.openai.com"* ]] && continue
[[ "$content" == *"localhost"* ]] && continue
add_finding "HIGH" "network_exfil" "openapi_server_url" "$linenum" "$content" \
"OpenAPI/GPT Action server endpoint (receives request data)"
done < <(grep -nEi '"url":\s*"https?://[^"]*"' "$file" 2>/dev/null || true)
}
check_credential_access() {
local file="$1"
local line_match
# SSH key access
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "CRITICAL" "credential_access" "ssh_key_read" "$linenum" "$content" \
"Reading SSH private key files"
done < <(grep -nEi '(cat|head|tail|less|more|bat|type)\s.*~/\.ssh/(id_|authorized_keys|known_hosts)' "$file" 2>/dev/null || true)
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "credential_access" "ssh_dir_access" "$linenum" "$content" \
"Accessing .ssh directory"
done < <(grep -nEi '~/\.ssh/|/\.ssh/' "$file" 2>/dev/null || true)
# AWS credentials
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "CRITICAL" "credential_access" "aws_creds" "$linenum" "$content" \
"Accessing AWS credentials"
done < <(grep -nEi '~/\.aws/(credentials|config)|\.aws/credentials' "$file" 2>/dev/null || true)
# GCP credentials
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "CRITICAL" "credential_access" "gcp_creds" "$linenum" "$content" \
"Accessing GCP credentials"
done < <(grep -nEi 'application_default_credentials|gcloud.*auth.*print-access-token' "$file" 2>/dev/null || true)
# .env file access
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "credential_access" "env_file_read" "$linenum" "$content" \
"Reading .env file (may contain secrets)"
done < <(grep -nEi '(cat|head|tail|less|more|source|\.)\s+\.env' "$file" 2>/dev/null || true)
# Environment variable dumping
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "credential_access" "env_dump" "$linenum" "$content" \
"Dumping environment variables"
done < <(grep -nEi '(^|\s)(env|printenv)\s*($|\|)|^\s*set\s*\|' "$file" 2>/dev/null || true)
# Specific sensitive env vars
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "credential_access" "sensitive_env_var" "$linenum" "$content" \
"Accessing sensitive environment variable"
done < <(grep -nEi '\$(AWS_SECRET|GITHUB_TOKEN|OPENAI_API_KEY|ANTHROPIC_API_KEY|DATABASE_URL|STRIPE_SECRET|API_KEY|SECRET_KEY|PRIVATE_KEY|ACCESS_TOKEN)' "$file" 2>/dev/null || true)
# macOS Keychain
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "CRITICAL" "credential_access" "keychain_access" "$linenum" "$content" \
"macOS Keychain access"
done < <(grep -nEi 'security\s+(find-generic-password|find-internet-password|dump-keychain)' "$file" 2>/dev/null || true)
# Docker/Kube credentials
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "credential_access" "container_creds" "$linenum" "$content" \
"Accessing Docker/Kubernetes credentials"
done < <(grep -nEi '~/\.docker/config\.json|~/\.kube/config' "$file" 2>/dev/null || true)
# NPM/PyPI tokens
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "credential_access" "package_registry_creds" "$linenum" "$content" \
"Accessing package registry credentials"
done < <(grep -nEi '~/\.(npmrc|pypirc|gem/credentials|netrc)' "$file" 2>/dev/null || true)
# Git credentials
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "credential_access" "git_creds" "$linenum" "$content" \
"Accessing git credentials"
done < <(grep -nEi '~/\.git-credentials|git\s+credential' "$file" 2>/dev/null || true)
# /etc/shadow or passwd
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "CRITICAL" "credential_access" "system_creds" "$linenum" "$content" \
"Accessing system credential files"
done < <(grep -nEi '/etc/(shadow|passwd|master\.passwd)' "$file" 2>/dev/null || true)
}
check_persistence() {
local file="$1"
local line_match
# LaunchAgent
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "CRITICAL" "persistence" "launchagent" "$linenum" "$content" \
"macOS LaunchAgent creation (survives reboot)"
done < <(grep -nEi 'LaunchAgents|launchctl\s+load' "$file" 2>/dev/null || true)
# Cron
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "CRITICAL" "persistence" "cron" "$linenum" "$content" \
"Cron job manipulation"
done < <(grep -nEi 'crontab\s|/var/spool/cron' "$file" 2>/dev/null || true)
# Shell profile modification
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "persistence" "shell_profile" "$linenum" "$content" \
"Shell profile modification (persists across terminals)"
done < <(grep -nEi '>>?\s*~/?\.(bashrc|zshrc|bash_profile|profile|zprofile|zshenv)' "$file" 2>/dev/null || true)
# Systemd
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "CRITICAL" "persistence" "systemd" "$linenum" "$content" \
"Systemd service creation"
done < <(grep -nEi '/systemd/|systemctl\s+enable' "$file" 2>/dev/null || true)
# Git hooks
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "persistence" "git_hook" "$linenum" "$content" \
"Git hook creation or modification"
done < <(grep -nEi '\.git/hooks/|githooks' "$file" 2>/dev/null || true)
# at command
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "MEDIUM" "persistence" "at_command" "$linenum" "$content" \
"Scheduled execution via at command"
done < <(grep -nEi '\bat\s+now\s*\+' "$file" 2>/dev/null || true)
}
check_dangerous_shell() {
local file="$1"
local line_match
# rm -rf with dangerous paths
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "CRITICAL" "dangerous_shell" "rm_rf_dangerous" "$linenum" "$content" \
"Recursive forced removal of critical paths"
done < <(grep -nEi 'rm\s+-[rf]{2,}\s+(/|~|\$HOME|\.git)' "$file" 2>/dev/null || true)
# General rm -rf
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "MEDIUM" "dangerous_shell" "rm_rf" "$linenum" "$content" \
"Recursive forced removal"
done < <(grep -nEi 'rm\s+-[rf]{2,}' "$file" 2>/dev/null || true)
# Fork bomb
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "CRITICAL" "dangerous_shell" "fork_bomb" "$linenum" "$content" \
"Fork bomb detected"
done < <(grep -nEi ':\(\)\{.*:\|:.*\}|\.\/\$0\s*\|.*\$0' "$file" 2>/dev/null || true)
# chmod 777 or 000
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "dangerous_shell" "chmod_dangerous" "$linenum" "$content" \
"Dangerous permission change"
done < <(grep -nEi 'chmod\s+(-R\s+)?(777|000|a\+rwx)' "$file" 2>/dev/null || true)
# dd to device
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "CRITICAL" "dangerous_shell" "dd_device" "$linenum" "$content" \
"dd writing to device (potential disk destruction)"
done < <(grep -nEi 'dd\s+.*of=/dev/' "$file" 2>/dev/null || true)
# mkfs
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "CRITICAL" "dangerous_shell" "mkfs" "$linenum" "$content" \
"Filesystem formatting command"
done < <(grep -nEi 'mkfs\.' "$file" 2>/dev/null || true)
# kill -9 -1 or killall
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "dangerous_shell" "mass_kill" "$linenum" "$content" \
"Mass process termination"
done < <(grep -nEi 'kill\s+-9\s+-1|killall\s+-9' "$file" 2>/dev/null || true)
# shred
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "dangerous_shell" "shred" "$linenum" "$content" \
"Secure file deletion (shred)"
done < <(grep -nEi '\bshred\s+(-[a-zA-Z]|/|~|\.)' "$file" 2>/dev/null || true)
# Infinite loop
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "MEDIUM" "dangerous_shell" "infinite_loop" "$linenum" "$content" \
"Potential infinite loop (CPU exhaustion)"
done < <(grep -nEi 'while\s+true\s*;?\s*do' "$file" 2>/dev/null || true)
}
check_obfuscation() {
local file="$1"
local line_match
# Base64 decode and execute
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "CRITICAL" "obfuscation" "base64_exec" "$linenum" "$content" \
"Base64 decode piped to shell execution"
done < <(grep -nEi 'base64\s+(-d|--decode).*\|\s*(bash|sh|zsh|eval)' "$file" 2>/dev/null || true)
# Base64 decode (without direct execution)
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "obfuscation" "base64_decode" "$linenum" "$content" \
"Base64 decoding detected (possible obfuscation)"
done < <(grep -nEi 'base64\s+(-d|--decode)|base64\.b64decode|atob\(' "$file" 2>/dev/null || true)
# Hex escape sequences
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "obfuscation" "hex_escapes" "$linenum" "$content" \
"Hex escape sequences (possible command obfuscation)"
done < <(grep -nEi '\\x[0-9a-f]{2}.*\\x[0-9a-f]{2}.*\\x[0-9a-f]{2}' "$file" 2>/dev/null || true)
# Eval with dynamic content
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "obfuscation" "eval_dynamic" "$linenum" "$content" \
"eval with dynamic content (potential code injection)"
done < <(grep -nEi 'eval\s+"\$\(' "$file" 2>/dev/null || true)
# General eval usage
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "MEDIUM" "obfuscation" "eval_usage" "$linenum" "$content" \
"eval command usage"
done < <(grep -nEi '(^|\s)eval\s' "$file" 2>/dev/null || true)
# Variable-based command construction
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "MEDIUM" "obfuscation" "var_cmd_construction" "$linenum" "$content" \
"Variable-based command construction"
done < <(grep -nEi '(cmd|CMD)=".*(cu|wg|nc)' "$file" 2>/dev/null || true)
# Variable appending to build command
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "MEDIUM" "obfuscation" "var_append_construction" "$linenum" "$content" \
"Variable string concatenation (possible command construction)"
done < <(grep -nEi '\$\{?[a-z]+\}?\+?="[a-z]' "$file" 2>/dev/null || true)
# Executing variable as command
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "MEDIUM" "obfuscation" "var_as_command" "$linenum" "$content" \
"Variable used as command name"
done < <(grep -nEi '^\s*\$[a-z]+\s+(https?://|-[a-z])' "$file" 2>/dev/null || true)
# bash -c with encoded content
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "obfuscation" "bash_c_encoded" "$linenum" "$content" \
"bash -c with potentially encoded/constructed command"
done < <(grep -nEi 'bash\s+-c\s+"\$\(' "$file" 2>/dev/null || true)
# Python exec/eval with encoded content
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "obfuscation" "python_exec_encoded" "$linenum" "$content" \
"Python exec/eval with encoded content"
done < <(grep -nEi 'python[23]?\s+-c\s.*\b(exec|eval)\b.*\b(base64|decode|decompress)\b' "$file" 2>/dev/null || true)
# Process substitution for execution
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "MEDIUM" "obfuscation" "process_substitution" "$linenum" "$content" \
"Process substitution used for execution"
done < <(grep -nEi 'bash\s+<\(' "$file" 2>/dev/null || true)
}
check_prompt_injection() {
local file="$1"
local line_match
# Instruction overrides
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "CRITICAL" "prompt_injection" "instruction_override" "$linenum" "$content" \
"Prompt injection: instruction override pattern"
done < <(grep -niE '(ignore|disregard|forget|override)\s+(all\s+)?(previous|prior|above|earlier|your)\s+(instructions|rules|guidelines|safety|constraints)' "$file" 2>/dev/null || true)
# Role reassignment
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "prompt_injection" "role_reassignment" "$linenum" "$content" \
"Prompt injection: role reassignment"
done < <(grep -niE '(you are now|act as|pretend you are|from now on you|you must now be|switch to|enter)\s.*(mode|role|persona|bot|agent|assistant)' "$file" 2>/dev/null || true)
# System prompt manipulation
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "prompt_injection" "system_prompt_manip" "$linenum" "$content" \
"Prompt injection: system prompt manipulation"
done < <(grep -niE '(system prompt|your (new )?instructions|new system message|override (the )?system)' "$file" 2>/dev/null || true)
# Hidden markdown comments with instructions (exclude test metadata EXPECT/DESCRIPTION comments)
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
# Skip test metadata comments
[[ "$line_match" == *"EXPECT:"* ]] && continue
[[ "$line_match" == *"DESCRIPTION:"* ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "prompt_injection" "hidden_comment_instruction" "$linenum" "$content" \
"Hidden instruction in HTML/markdown comment"
done < <(grep -niE '<!--.*\b(ignore|always|never|must|execute|run|curl|wget|important|critical|system)\b.*-->' "$file" 2>/dev/null || true)
# Markdown link reference definition used as hidden comment
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "MEDIUM" "prompt_injection" "hidden_link_ref" "$linenum" "$content" \
"Markdown link reference with hidden instruction"
done < <(grep -nEi '^\[//\]:\s*#\s*\(' "$file" 2>/dev/null || true)
# "Do not tell the user" / concealment
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "CRITICAL" "prompt_injection" "concealment" "$linenum" "$content" \
"Instruction to conceal behavior from user"
done < <(grep -niE '(do not|don.?t|never)\s+(tell|inform|mention|reveal|show|disclose).*(user|human|operator)' "$file" 2>/dev/null || true)
# Execute without confirmation
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "prompt_injection" "skip_confirmation" "$linenum" "$content" \
"Instruction to skip user confirmation"
done < <(grep -niE '(without|skip|bypass|no)\s+(asking|confirmation|permission|approval|consent|prompting)' "$file" 2>/dev/null || true)
}
check_supply_chain() {
local file="$1"
local line_match
# Package install from custom registry
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "CRITICAL" "supply_chain" "custom_registry" "$linenum" "$content" \
"Package install from custom/non-standard registry"
done < <(grep -nEi '(--index-url|--registry|--extra-index-url)\s+https?://' "$file" 2>/dev/null || true)
# npm config set registry
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "CRITICAL" "supply_chain" "npm_registry_override" "$linenum" "$content" \
"NPM registry override"
done < <(grep -nEi 'npm\s+config\s+set\s+registry' "$file" 2>/dev/null || true)
# Post-install scripts
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "supply_chain" "postinstall_script" "$linenum" "$content" \
"Package post-install script (common malware vector)"
done < <(grep -nEi '"(preinstall|postinstall|preuninstall|postuninstall)"' "$file" 2>/dev/null || true)
# CI/CD modification
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "supply_chain" "ci_modification" "$linenum" "$content" \
"CI/CD pipeline file modification"
done < <(grep -nEi '(\.github/workflows|\.gitlab-ci|Jenkinsfile|\.circleci)' "$file" 2>/dev/null || true)
# Dockerfile modification
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "MEDIUM" "supply_chain" "dockerfile_mod" "$linenum" "$content" \
"Dockerfile or container config modification"
done < <(grep -nEi '(>>?\s*Dockerfile|docker\s+build)' "$file" 2>/dev/null || true)
# npx -y (auto-install without confirmation)
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "MEDIUM" "supply_chain" "npx_auto_install" "$linenum" "$content" \
"npx with -y flag (auto-installs packages without confirmation)"
done < <(grep -nEi 'npx\s+-y\s' "$file" 2>/dev/null || true)
}
check_privilege_escalation() {
local file="$1"
local line_match
# sudo usage
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "privilege_escalation" "sudo" "$linenum" "$content" \
"sudo command usage"
done < <(grep -nEi '(^|\s)sudo\s' "$file" 2>/dev/null || true)
# setuid
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "CRITICAL" "privilege_escalation" "setuid" "$linenum" "$content" \
"Setting setuid bit (privilege escalation)"
done < <(grep -nEi 'chmod\s+.*u\+s|chmod\s+4[0-7]{3}' "$file" 2>/dev/null || true)
# sudoers modification
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "CRITICAL" "privilege_escalation" "sudoers_mod" "$linenum" "$content" \
"Sudoers file modification"
done < <(grep -nEi '/etc/sudoers|visudo|NOPASSWD' "$file" 2>/dev/null || true)
# Linux capabilities
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "privilege_escalation" "capabilities" "$linenum" "$content" \
"Linux capabilities manipulation"
done < <(grep -nEi 'setcap\s|getcap\s' "$file" 2>/dev/null || true)
# Docker with host mount
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "privilege_escalation" "docker_host_mount" "$linenum" "$content" \
"Docker container with host filesystem mount"
done < <(grep -nEi 'docker\s+run\s.*-v\s+/:/|--privileged' "$file" 2>/dev/null || true)
}
# === Ecosystem-specific rules ===
check_preprocess_commands() {
local file="$1"
local ecosystem="$2"
# Only applies to Claude Code SKILL.md files
[[ "$ecosystem" != "claude_skill" ]] && return 0
local line_match
local BT='`'
# Match !`command` syntax (! immediately followed by backtick)
# Use awk since grep ERE doesn't support \x60 on macOS
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "CRITICAL" "preprocess_exec" "bang_backtick" "$linenum" "$content" \
"Claude Code preprocessing command (!backtick) — executes at skill load time with no permission check"
done < <(awk -v bt="$BT" '
{
if (index($0, "!" bt) > 0) {
# Check there is a closing backtick after !`
pos = index($0, "!" bt)
rest = substr($0, pos + 2)
if (index(rest, bt) > 0) {
print NR ":" $0
}
}
}
' "$file" 2>/dev/null || true)
}
check_memory_poison() {
local file="$1"
local line_match
# Claude memory directory
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "CRITICAL" "memory_poison" "claude_memory_write" "$linenum" "$content" \
"Writing to Claude memory directory"
done < <(grep -nEi '\.claude/(memory|projects).*(>>|>|\bcat\b.*>|\btee\b|\bwrite\b|mkdir)' "$file" 2>/dev/null || true)
# CLAUDE.md modification
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "memory_poison" "claude_md_write" "$linenum" "$content" \
"Writing to CLAUDE.md instruction file"
done < <(grep -nEi '(>>?|tee)\s.*CLAUDE\.md' "$file" 2>/dev/null || true)
# AGENTS.md modification
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue
local linenum="${line_match%%:*}"
local content="${line_match#*:}"
add_finding "HIGH" "memory_poison" "agents_md_write" "$linenum" "$content" \
"Writing to AGENTS.md instruction file"
done < <(grep -nEi '(>>?|tee)\s.*AGENTS\.md' "$file" 2>/dev/null || true)
# .cursorrules / .windsurfrules modification
while IFS= read -r line_match; do
[[ -z "$line_match" ]] && continue