-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnamely-secscan-v1.1.1.sh
More file actions
1366 lines (1184 loc) · 49.6 KB
/
Copy pathnamely-secscan-v1.1.1.sh
File metadata and controls
1366 lines (1184 loc) · 49.6 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
# Namely SecScan v1.1.1
# Read-only VPS security scanner that generates a full audit report.
# Repo: namely-secscan
#
# v1.1.1 hotfix:
# - Fixed AIDE initialization hanging issue
# - Added warning about AIDE init taking 5-30+ minutes
# - Made AIDE initialization optional during install
# - Added progress messages for all tool installations
#
# v1.1.0 improvements:
# - Fixed ASCII art to properly say "NamelyCorp"
# - Added progress indicators for each scan stage
# - Enhanced summary with actual findings and actionable insights
# - Added dependency checking with install prompts
# - Improved REPORT.md with detailed section explanations
#
set -euo pipefail
VERSION="1.1.1"
SCRIPT_NAME="$(basename "$0")"
# ---------------------------
# Defaults (override via args)
# ---------------------------
MODE="quick" # quick | deep
OUT_BASE="/var/reports/namely-secscan" # team-friendly default
WEBROOT="/var/www" # typical webroot; override if needed
MAKE_JSON="false"
# Interactive UX defaults
INTERACTIVE="auto" # auto | true | false
ASSUME_YES="false" # if true, answer "yes" to all prompts
NO_COLOR="false"
# Module toggles (may be prompted)
DO_SYSTEM="true"
DO_USERS="true"
DO_NETWORK="true"
DO_WEBROOT="true"
DO_LOGS="true"
DO_PERSISTENCE="true"
DO_MALWARE="false" # default off unless deep or user says yes
DO_SUID="false" # deep only unless user says yes
# Progress tracking
TOTAL_STEPS=0
CURRENT_STEP=0
# Findings tracking
declare -A FINDINGS
FINDINGS[critical]=0
FINDINGS[warning]=0
FINDINGS[info]=0
FINDINGS[details]=""
# ---------------
# Helper functions
# ---------------
die() { echo "ERROR: $*" >&2; exit 2; }
have() { command -v "$1" >/dev/null 2>&1; }
ts() { date -u +"%Y-%m-%dT%H:%M:%SZ"; }
color() { # $1=code $2=text
if [[ "$NO_COLOR" == "true" ]]; then echo -e "$2"; else echo -e "\033[${1}m${2}\033[0m"; fi
}
hr() { printf "%s\n" "$(color "2" "────────────────────────────────────────────────────────────────────────")"; }
banner() {
cat <<'EOF'
_ _ _ _____
| \ | | | | / ____|
| \| | __ _ _ __ ___ ___| |_ _| | ___ _ __ _ __
| . ` |/ _` | '_ ` _ \ / _ \ | | | | | / _ \| '__| '_ \
| |\ | (_| | | | | | | __/ | |_| | |___| (_) | | | |_) |
|_| \_|\__,_|_| |_| |_|\___|_|\__, |\_____\___/|_| | .__/
__/ | | |
|___/ |_|
NamelyCorp • Security Scanner
EOF
}
info() { echo "$(color "1;34" "[INFO]") $*"; }
warn() { echo "$(color "1;33" "[WARN]") $*"; }
good() { echo "$(color "1;32" "[ OK ]") $*"; }
head1() { hr; echo "$(color "1;36" "▶ $*")"; hr; }
progress() {
CURRENT_STEP=$((CURRENT_STEP + 1))
local percent=$((CURRENT_STEP * 100 / TOTAL_STEPS))
local bar_length=40
local filled=$((percent * bar_length / 100))
local empty=$((bar_length - filled))
printf "\r$(color "1;36" "Progress:") ["
printf "%${filled}s" | tr ' ' '█'
printf "%${empty}s" | tr ' ' '░'
printf "] %3d%% (%d/%d) - %s" "$percent" "$CURRENT_STEP" "$TOTAL_STEPS" "$1"
if [[ $CURRENT_STEP -eq $TOTAL_STEPS ]]; then
echo ""
fi
}
add_finding() {
local severity="$1" # critical, warning, info
local message="$2"
FINDINGS[$severity]=$((FINDINGS[$severity] + 1))
FINDINGS[details]+="[$severity] $message"$'\n'
}
sanitize_text() {
# Redact common secret patterns from REPORT.md (best-effort)
sed -E \
-e 's/(AWS(_| )?ACCESS(_| )?KEY(_| )?ID|AWS_ACCESS_KEY_ID|AWS_SECRET_ACCESS_KEY)[[:space:]]*[:=][[:space:]]*[A-Za-z0-9\/+=._-]+/\1: [REDACTED]/Ig' \
-e 's/(OPENAI_API_KEY|ANTHROPIC_API_KEY|GITHUB_TOKEN|API_KEY|SECRET_KEY|PRIVATE_KEY)[[:space:]]*[:=][[:space:]]*[^[:space:]]+/\1: [REDACTED]/Ig' \
-e 's/(Bearer)[[:space:]]+[A-Za-z0-9\._=-]+/\1 [REDACTED]/g' \
-e 's/(eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,})/[REDACTED_JWT]/g'
}
safe_mkdir() { mkdir -p "$1"; }
write_section_header() {
local title="$1"
local description="$2"
cat >>"$REPORT_MD" <<EOF
---
## ${title}
**Purpose:** ${description}
**What to look for:**
EOF
}
# Execute once; write to human report (redacted) and raw file (unredacted)
run() {
local title="$1"; shift
local fname="$1"; shift
local description="$1"; shift
local lookfor="$1"; shift
local cmd=( "$@" )
write_section_header "$title" "$description"
echo "" >>"$REPORT_MD"
echo "$lookfor" >>"$REPORT_MD"
echo "" >>"$REPORT_MD"
echo "**Command:** \`${cmd[*]}\`" >>"$REPORT_MD"
echo "" >>"$REPORT_MD"
echo '```' >>"$REPORT_MD"
set +e
local output
output="$("${cmd[@]}" 2>&1)"
local rc=$?
set -e
printf "%s\n" "$output" >"${RAW_DIR}/${fname}"
printf "%s\n" "$output" | sanitize_text >>"$REPORT_MD"
echo '```' >>"$REPORT_MD"
echo "" >>"$REPORT_MD"
echo "**Exit code:** ${rc}" >>"$REPORT_MD"
# Analyze output for findings
analyze_output "$title" "$fname" "$output" "$rc"
return 0
}
analyze_output() {
local title="$1"
local fname="$2"
local output="$3"
local exitcode="$4"
# System checks
if [[ "$fname" == "uid0.txt" ]] && [[ $(echo "$output" | wc -l) -gt 1 ]]; then
add_finding "warning" "Multiple UID 0 accounts found (check ${fname})"
fi
# Web exposure checks
if [[ "$fname" == "webroot-find.txt" ]]; then
local env_count=$(echo "$output" | grep -c "\.env" 2>/dev/null || true)
local git_count=$(echo "$output" | grep -c "\.git" 2>/dev/null || true)
local backup_count=$(echo "$output" | grep -c -E "\.(bak|old|backup)" 2>/dev/null || true)
if [[ $env_count -gt 0 ]]; then
add_finding "critical" "Found $env_count .env file(s) in webroot - IMMEDIATE ACTION REQUIRED"
fi
if [[ $git_count -gt 0 ]]; then
add_finding "critical" "Found $git_count .git director(y/ies) in webroot - source code exposure risk"
fi
if [[ $backup_count -gt 0 ]]; then
add_finding "warning" "Found $backup_count backup file(s) in webroot - potential info disclosure"
fi
fi
# SSH security
if [[ "$fname" == "ssh-fails.txt" ]]; then
local fail_count=$(echo "$output" | wc -l)
if [[ $fail_count -gt 100 ]]; then
add_finding "warning" "High SSH brute force activity: $fail_count failed attempts from top IPs"
fi
fi
# Web probes
if [[ "$fname" =~ web-probes.*\.txt ]]; then
local probe_count=$(echo "$output" | wc -l)
if [[ $probe_count -gt 50 ]]; then
add_finding "warning" "High web scanning activity: $probe_count suspicious requests detected"
fi
fi
# Malware scans
if [[ "$fname" == "clamav-scan.txt" ]] && echo "$output" | grep -q "FOUND"; then
add_finding "critical" "ClamAV detected potential malware - review ${fname} immediately"
fi
if [[ "$fname" == "rkhunter-check.txt" ]] && echo "$output" | grep -q "Warning"; then
add_finding "warning" "rkhunter detected warnings - review ${fname}"
fi
# SUID findings
if [[ "$fname" == "suid-sgid.txt" ]]; then
local unusual_suid=$(echo "$output" | grep -vE "(sudo|su|ping|mount|umount|passwd)" | wc -l)
if [[ $unusual_suid -gt 5 ]]; then
add_finding "info" "Found $unusual_suid unusual SUID/SGID binaries - review for legitimacy"
fi
fi
}
write_kv() { printf "%-32s %s\n" "$1" "$2" >>"$SUMMARY_TXT"; }
is_tty() { [[ -t 0 && -t 1 ]]; }
ask_yn() {
# ask_yn "Question?" default(Y/N) -> returns 0 for yes, 1 for no
local q="$1"
local def="${2:-Y}"
local prompt def_show
if [[ "$def" == "Y" ]]; then def_show="Y/n"; else def_show="y/N"; fi
if [[ "$ASSUME_YES" == "true" ]]; then
good "$q -> yes (assume-yes)"
return 0
fi
if [[ "$INTERACTIVE" == "false" ]]; then
# non-interactive: use default
if [[ "$def" == "Y" ]]; then return 0; else return 1; fi
fi
while true; do
read -r -p "$(color "1;35" "?") $q [$def_show]: " prompt || true
prompt="${prompt:-$def}"
case "${prompt,,}" in
y|yes) return 0 ;;
n|no) return 1 ;;
*) echo "Please answer y or n." ;;
esac
done
}
# ----------------
# Dependency check
# ----------------
check_dependencies() {
echo ""
head1 "Dependency Check"
local missing=()
local optional=()
# Core tools (should be present)
for tool in grep sed awk find ss systemctl; do
if ! have "$tool"; then
missing+=("$tool")
fi
done
# Security scanning tools (optional but recommended)
declare -A optional_tools=(
["clamscan"]="ClamAV - Antivirus scanner"
["rkhunter"]="rkhunter - Rootkit hunter"
["chkrootkit"]="chkrootkit - Rootkit checker"
["aide"]="AIDE - File integrity checker"
)
echo ""
echo "$(color "1;37" "Core tools check:")"
for tool in grep sed awk find ss systemctl; do
if have "$tool"; then
good "$tool is installed"
else
warn "$tool is MISSING"
fi
done
echo ""
echo "$(color "1;37" "Security scanning tools:")"
for tool in "${!optional_tools[@]}"; do
if have "$tool"; then
good "$tool is installed - ${optional_tools[$tool]}"
else
warn "$tool is not installed - ${optional_tools[$tool]}"
optional+=("$tool")
fi
done
if [[ ${#missing[@]} -gt 0 ]]; then
echo ""
die "Missing required tools: ${missing[*]}. Please install them first."
fi
if [[ ${#optional[@]} -gt 0 ]] && [[ "$INTERACTIVE" == "true" ]]; then
echo ""
echo "$(color "1;33" "Optional security tools are missing.")"
echo "These tools provide additional malware and rootkit detection capabilities."
echo ""
if ask_yn "Would you like installation commands for missing tools?" "Y"; then
echo ""
echo "$(color "1;36" "Installation commands:")"
echo ""
# Detect package manager
if have apt-get; then
echo "# Debian/Ubuntu:"
for tool in "${optional[@]}"; do
case "$tool" in
clamscan) echo " sudo apt-get install -y clamav clamav-daemon && sudo freshclam" ;;
rkhunter) echo " sudo apt-get install -y rkhunter" ;;
chkrootkit) echo " sudo apt-get install -y chkrootkit" ;;
aide) echo " sudo apt-get install -y aide && sudo aideinit" ;;
esac
done
elif have yum; then
echo "# RHEL/CentOS:"
for tool in "${optional[@]}"; do
case "$tool" in
clamscan) echo " sudo yum install -y clamav clamav-update && sudo freshclam" ;;
rkhunter) echo " sudo yum install -y rkhunter" ;;
chkrootkit) echo " sudo yum install -y chkrootkit" ;;
aide) echo " sudo yum install -y aide && sudo aide --init" ;;
esac
done
fi
echo ""
if ask_yn "Install missing tools now?" "N"; then
echo ""
info "Installing missing security tools..."
# Check if AIDE is in the list and warn about init time
local has_aide=false
for tool in "${optional[@]}"; do
if [[ "$tool" == "aide" ]]; then
has_aide=true
break
fi
done
if [[ "$has_aide" == "true" ]]; then
echo ""
warn "AIDE requires initialization which scans ALL files on your system."
warn "This can take 5-30 minutes or longer depending on system size."
echo ""
fi
if have apt-get; then
for tool in "${optional[@]}"; do
case "$tool" in
clamscan)
info "Installing ClamAV..."
sudo apt-get update && sudo apt-get install -y clamav clamav-daemon
info "Updating virus definitions (this may take a minute)..."
sudo freshclam || warn "freshclam failed, but clamav is installed"
good "ClamAV installed"
;;
rkhunter)
info "Installing rkhunter..."
sudo apt-get install -y rkhunter
good "rkhunter installed"
;;
chkrootkit)
info "Installing chkrootkit..."
sudo apt-get install -y chkrootkit
good "chkrootkit installed"
;;
aide)
info "Installing AIDE..."
sudo apt-get install -y aide
good "AIDE package installed"
echo ""
warn "AIDE needs initialization before use. This scans all files and takes 5-30+ minutes."
if ask_yn "Initialize AIDE database now? (You can skip and run 'sudo aideinit' later)" "N"; then
info "Initializing AIDE database... This will take a while. Please be patient."
sudo aideinit || warn "aideinit failed, but aide is installed. Run 'sudo aideinit' manually later."
good "AIDE initialized"
else
warn "AIDE installed but not initialized. Run 'sudo aideinit' before using AIDE."
fi
;;
esac
done
elif have yum; then
for tool in "${optional[@]}"; do
case "$tool" in
clamscan)
info "Installing ClamAV..."
sudo yum install -y clamav clamav-update
info "Updating virus definitions..."
sudo freshclam || warn "freshclam failed, but clamav is installed"
good "ClamAV installed"
;;
rkhunter)
info "Installing rkhunter..."
sudo yum install -y rkhunter
good "rkhunter installed"
;;
chkrootkit)
info "Installing chkrootkit..."
sudo yum install -y chkrootkit
good "chkrootkit installed"
;;
aide)
info "Installing AIDE..."
sudo yum install -y aide
good "AIDE package installed"
echo ""
warn "AIDE needs initialization before use. This scans all files and takes 5-30+ minutes."
if ask_yn "Initialize AIDE database now? (You can skip and run 'sudo aide --init' later)" "N"; then
info "Initializing AIDE database... This will take a while. Please be patient."
sudo aide --init || warn "aide init failed, but aide is installed. Run 'sudo aide --init' manually later."
good "AIDE initialized"
else
warn "AIDE installed but not initialized. Run 'sudo aide --init' before using AIDE."
fi
;;
esac
done
fi
echo ""
good "Installation complete! Re-run the scan to use new tools."
echo ""
fi
fi
fi
echo ""
}
# ----------------
# Argument parsing
# ----------------
usage() {
cat <<EOF
Namely SecScan v${VERSION}
Usage:
sudo bash ${SCRIPT_NAME} [options]
Core options:
--quick Fast checks (default)
--deep Includes heavier checks (suggested for incident response)
--out PATH Output base directory (default: ${OUT_BASE})
--webroot PATH Web root to scan for .env/.git/backup exposure patterns (default: ${WEBROOT})
--json Produce a machine-readable summary.json in the report directory
UI / interactive:
--interactive Force Y/N prompts
--no-interactive Disable prompts (use defaults)
--yes Assume "yes" to prompts (useful for automation)
--no-color Disable ANSI colors
Examples:
sudo bash ${SCRIPT_NAME} --quick
sudo bash ${SCRIPT_NAME} --deep --interactive
sudo bash ${SCRIPT_NAME} --deep --webroot /var/www --out /var/reports/namely-secscan --json
sudo bash ${SCRIPT_NAME} --no-interactive --deep --yes
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
--quick) MODE="quick"; shift ;;
--deep) MODE="deep"; shift ;;
--out) OUT_BASE="${2:-}"; [[ -n "$OUT_BASE" ]] || die "--out requires a path"; shift 2 ;;
--webroot) WEBROOT="${2:-}"; [[ -n "$WEBROOT" ]] || die "--webroot requires a path"; shift 2 ;;
--json) MAKE_JSON="true"; shift ;;
--interactive) INTERACTIVE="true"; shift ;;
--no-interactive) INTERACTIVE="false"; shift ;;
--yes) ASSUME_YES="true"; shift ;;
--no-color) NO_COLOR="true"; shift ;;
-h|--help) usage; exit 0 ;;
*) die "Unknown argument: $1 (try --help)" ;;
esac
done
# Decide interactive default
if [[ "$INTERACTIVE" == "auto" ]]; then
if is_tty; then INTERACTIVE="true"; else INTERACTIVE="false"; fi
fi
# Default deep-mode module toggles
if [[ "$MODE" == "deep" ]]; then
DO_MALWARE="true"
DO_SUID="true"
fi
# -------------------------
# Root check
# -------------------------
[[ $EUID -ne 0 ]] && die "This script must be run as root (sudo)"
# -------------------------
# Banner and dependency check
# -------------------------
if [[ "$INTERACTIVE" == "true" ]]; then
echo ""
banner
hr
echo "Mode: $(color "1;36" "$MODE") Output base: $(color "1;36" "$OUT_BASE") Webroot: $(color "1;36" "$WEBROOT")"
hr
fi
check_dependencies
# -------------------------
# Prompt for modules (TTY)
# -------------------------
if [[ "$INTERACTIVE" == "true" ]]; then
echo ""
echo "$(color "1;37" "Choose scan modules (recommended defaults shown):")"
echo ""
ask_yn "System overview checks?" "Y" && DO_SYSTEM="true" || DO_SYSTEM="false"
ask_yn "Users + SSH hardening checks?" "Y" && DO_USERS="true" || DO_USERS="false"
ask_yn "Network exposure checks (ports/firewall)?" "Y" && DO_NETWORK="true" || DO_NETWORK="false"
ask_yn "Webroot exposure scan (.env/.git/backups)?" "Y" && DO_WEBROOT="true" || DO_WEBROOT="false"
ask_yn "Log analysis (SSH + web probes)?" "Y" && DO_LOGS="true" || DO_LOGS="false"
ask_yn "Persistence checks (cron/systemd)?" "Y" && DO_PERSISTENCE="true" || DO_PERSISTENCE="false"
if ask_yn "Malware/rootkit tools (ClamAV/rkhunter/chkrootkit) if installed? (slower)" "$( [[ "$MODE" == "deep" ]] && echo Y || echo N )"; then
DO_MALWARE="true"
else
DO_MALWARE="false"
fi
if ask_yn "SUID/SGID sweep (can take time)?" "$( [[ "$MODE" == "deep" ]] && echo Y || echo N )"; then
DO_SUID="true"
else
DO_SUID="false"
fi
fi
# -------------------------
# Calculate total steps for progress bar
# -------------------------
TOTAL_STEPS=0
[[ "$DO_SYSTEM" == "true" ]] && TOTAL_STEPS=$((TOTAL_STEPS + 5))
[[ "$DO_USERS" == "true" ]] && TOTAL_STEPS=$((TOTAL_STEPS + 4))
[[ "$DO_NETWORK" == "true" ]] && TOTAL_STEPS=$((TOTAL_STEPS + 3))
[[ "$DO_WEBROOT" == "true" ]] && TOTAL_STEPS=$((TOTAL_STEPS + 2))
[[ "$DO_LOGS" == "true" ]] && TOTAL_STEPS=$((TOTAL_STEPS + 6))
[[ "$DO_PERSISTENCE" == "true" ]] && TOTAL_STEPS=$((TOTAL_STEPS + 5))
[[ "$DO_SUID" == "true" ]] && TOTAL_STEPS=$((TOTAL_STEPS + 1))
[[ "$DO_MALWARE" == "true" ]] && TOTAL_STEPS=$((TOTAL_STEPS + 3))
TOTAL_STEPS=$((TOTAL_STEPS + 1)) # Summary step
# -------------------------
# Set up output paths
# -------------------------
RUN_ID="$(hostname -s)_$(date -u +"%Y%m%d_%H%M%S")"
OUT_DIR="${OUT_BASE}/${RUN_ID}"
RAW_DIR="${OUT_DIR}/raw"
REPORT_MD="${OUT_DIR}/REPORT.md"
SUMMARY_TXT="${OUT_DIR}/SUMMARY.txt"
SUMMARY_JSON="${OUT_DIR}/summary.json"
safe_mkdir "$OUT_DIR"
safe_mkdir "$RAW_DIR"
# -------------------------
# Initialize REPORT.md
# -------------------------
cat >"$REPORT_MD" <<EOF
# Namely SecScan Security Report
**Generated:** $(ts)
**Version:** v${VERSION}
**Host:** $(hostname -f 2>/dev/null || hostname)
**Mode:** ${MODE}
**Webroot:** ${WEBROOT}
---
## Executive Summary
This report provides a comprehensive security audit of your VPS server. Each section below examines different aspects of system security, from basic configuration to potential compromise indicators.
**How to use this report:**
1. Start with sections marked with ⚠️ CRITICAL or WARNING findings
2. Review each section's "What to look for" guidance
3. Cross-reference suspicious findings with raw output files in the \`raw/\` directory
4. Consult SUMMARY.txt for quick overview and action items
**Report Sections:**
- System Overview: Basic system health and configuration
- User Security: Account security and SSH hardening
- Network Security: Port exposure and firewall configuration
- Webroot Exposure: Sensitive file detection in web-accessible areas
- Log Analysis: Authentication attempts and web probes
- Persistence Mechanisms: Cron jobs and systemd services
- Advanced Scans: SUID binaries and malware detection (if enabled)
---
EOF
# -------------------------
# Start scanning
# -------------------------
echo ""
head1 "Starting security scan"
echo ""
# -------------------------
# System checks
# -------------------------
if [[ "$DO_SYSTEM" == "true" ]]; then
head1 "System overview"
progress "System info"
run "System Information" "system.txt" \
"Basic system information including kernel version and hardware architecture." \
"- Verify the kernel version is current and receiving security updates
- Check for any unusual architecture or configuration" \
uname -a
progress "Uptime and load"
run "System Uptime and Load Average" "uptime.txt" \
"Shows how long the system has been running and current load average." \
"- Unusual recent restarts may indicate compromise or system issues
- High load average could indicate resource exhaustion or cryptomining" \
uptime
progress "Disk usage"
run "Disk Usage" "df.txt" \
"Filesystem usage across all mounted volumes." \
"- Look for filesystems approaching 90%+ capacity
- Unexplained disk usage growth may indicate log flooding or malware
- Check for unusual mounted filesystems" \
df -hT
progress "Memory usage"
run "Memory Usage" "memory.txt" \
"Current RAM and swap utilization." \
"- High swap usage may indicate memory pressure or performance issues
- Sudden memory consumption changes could indicate resource-intensive malware" \
free -h
progress "Top CPU processes"
run "Top CPU Processes" "ps_cpu.txt" \
"Processes consuming the most CPU resources." \
"- Look for unfamiliar process names or unusual resource consumption
- Multiple instances of web servers or unusual binaries may be suspicious
- Check for known cryptominer process names (xmrig, minergate, etc.)" \
bash -lc "ps aux --sort=-%cpu | head -25"
fi
# -------------------------
# User + SSH checks
# -------------------------
if [[ "$DO_USERS" == "true" ]]; then
head1 "User security + SSH hardening"
progress "UID 0 accounts"
run "Accounts with UID 0 (Root Privileges)" "uid0.txt" \
"Lists all accounts with root privileges (UID 0)." \
"- There should typically be ONLY the 'root' account with UID 0
- Multiple UID 0 accounts are a serious security concern
- Any non-root UID 0 account indicates potential compromise" \
awk -F: '($3==0){print $0}' /etc/passwd
progress "Password policies"
run "Password Policy Settings" "login.defs.txt" \
"System password aging and complexity requirements." \
"- PASS_MAX_DAYS should be ≤90 for password expiration
- PASS_MIN_DAYS should be ≥1 to prevent rapid password changes
- PASS_MIN_LEN should be ≥12 for adequate complexity" \
grep -E "^(PASS_MAX_DAYS|PASS_MIN_DAYS|PASS_MIN_LEN|PASS_WARN_AGE)" /etc/login.defs
progress "Currently logged in users"
run "Currently Logged-In Users" "who.txt" \
"Active user sessions on the system." \
"- Verify you recognize all logged-in users
- Check for suspicious login times (middle of night, etc.)
- Multiple concurrent root sessions may be unusual" \
w
progress "Recent login history"
run "Recent Login History" "last.txt" \
"Historical record of user logins and system boots." \
"- Look for logins from unfamiliar IP addresses
- Check for login attempts during unusual hours
- Multiple failed login attempts followed by success may indicate brute-force
- System reboots should align with known maintenance windows" \
bash -lc "last -a | head -50"
fi
# -------------------------
# Network checks
# -------------------------
if [[ "$DO_NETWORK" == "true" ]]; then
head1 "Network exposure (ports + firewall)"
progress "Listening ports"
run "Listening Network Ports" "ports.txt" \
"All services listening for network connections." \
"- Verify you recognize all listening services
- Common secure ports: 22 (SSH), 80 (HTTP), 443 (HTTPS)
- Unexpected ports may indicate backdoors or unauthorized services
- Pay attention to services listening on 0.0.0.0 (all interfaces) vs 127.0.0.1 (localhost only)
- Database ports (3306, 5432, etc.) should NOT be exposed to 0.0.0.0 in production" \
ss -tulpn
progress "IPTables firewall"
run "Firewall: iptables Rules" "iptables.txt" \
"Current iptables firewall configuration." \
"- Policy should be DROP or REJECT for INPUT chain (default deny)
- Verify only necessary ports are ACCEPT
- Look for unusual ACCEPT rules or suspicious IP addresses
- Empty ruleset means no firewall protection" \
bash -lc 'iptables -L -n -v 2>/dev/null || echo "iptables not available or not configured"'
progress "NFTables firewall"
run "Firewall: nftables Rules" "nft.txt" \
"Current nftables firewall configuration (modern replacement for iptables)." \
"- Similar to iptables, default policy should be drop
- Verify ruleset matches your security requirements
- Empty output means nftables is not in use" \
bash -lc 'nft list ruleset 2>/dev/null || echo "nftables not available or not configured"'
fi
# -------------------------
# Webroot exposure
# -------------------------
if [[ "$DO_WEBROOT" == "true" ]]; then
head1 "Webroot exposure scan"
progress "Searching for sensitive files"
run "Webroot: Sensitive Files Detection" "webroot-find.txt" \
"Searches webroot for exposed configuration files, repositories, and backups." \
"- ⚠️ .env files contain secrets (API keys, passwords) - CRITICAL if found
- .git directories expose source code - CRITICAL security risk
- .svn, .hg - other version control systems that shouldn't be web-accessible
- wp-config.php, phpinfo.php - WordPress and PHP info files with sensitive data
- .bak, .old, .backup, .zip, .tar.gz - backup files may contain old credentials or code
- ANY findings here require immediate remediation" \
bash -lc '
root="'"$WEBROOT"'"
[[ -d "$root" ]] || { echo "Webroot not found: $root"; exit 0; }
find "$root" -maxdepth 6 \( \
-name ".env" -o -name ".env.*" -o -name ".git" -o -name ".svn" -o -name ".hg" \
-o -name "wp-config.php" -o -name "phpinfo.php" \
-o -iname "*.bak" -o -iname "*.old" -o -iname "*.backup" -o -iname "*.zip" -o -iname "*.tar.gz" \
\) 2>/dev/null | sed "s|^|FOUND: |" || echo "No sensitive files found (good!)"
'
progress "Checking .htaccess protection"
run "Webroot: .htaccess Protection Review" "htaccess-sample.txt" \
"Reviews .htaccess files for security directives protecting sensitive files." \
"- Look for 'Deny from all' or 'Require all denied' for .env, .git, etc.
- FilesMatch directives should block access to sensitive file patterns
- If sensitive files were found above, .htaccess should be protecting them
- Missing protection rules are a security concern if sensitive files exist" \
bash -lc '
root="'"$WEBROOT"'"
[[ -d "$root" ]] || { echo "Webroot not found: $root"; exit 0; }
find "$root" -maxdepth 6 -name ".htaccess" 2>/dev/null | head -n 50 | while read -r f; do
echo "== $f =="
egrep -in "deny from all|require all denied|filesmatch|\\\.env|\\\.git" "$f" || echo "No protective rules found in this file"
echo
done
'
fi
# -------------------------
# Log analysis
# -------------------------
AUTH_LOG=""
if [[ -f /var/log/auth.log ]]; then AUTH_LOG="/var/log/auth.log"; fi
if [[ -z "$AUTH_LOG" && -f /var/log/secure ]]; then AUTH_LOG="/var/log/secure"; fi
APACHE_ACCESS="/var/log/apache2/access.log"
NGINX_ACCESS="/var/log/nginx/access.log"
if [[ "$DO_LOGS" == "true" ]]; then
head1 "Log analysis (SSH + web probes)"
if [[ -n "$AUTH_LOG" ]]; then
progress "SSH failed attempts"
run "SSH Failed Login Attempts (Top Source IPs)" "ssh-fails.txt" \
"Analyzes authentication logs for failed SSH login attempts." \
"- High counts from single IPs indicate brute-force attacks
- Distributed attacks may show many IPs with few attempts each
- Consider implementing fail2ban if seeing high attack volume
- Legitimate failed attempts happen, but look for patterns
- Top attacking IPs should be blocked or rate-limited" \
bash -lc '
log="'"$AUTH_LOG"'"
echo "Analyzing: $log"
files=("$log" "$log".1)
for f in "${files[@]}"; do [[ -f "$f" ]] || continue; cat "$f"; done \
| egrep -h "Failed password|Invalid user|authentication failure" \
| awk "{for(i=1;i<=NF;i++){if(\$i ~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/){print \$i}}}" \
| sort | uniq -c | sort -nr | head -n 20 || echo "No failed SSH attempts found (excellent!)"
'
progress "SSH successful logins"
run "SSH Successful Logins (Recent)" "ssh-success.txt" \
"Recent successful SSH authentication events." \
"- Verify you recognize all source IPs that successfully logged in
- Check timestamps - logins during unusual hours may be suspicious
- Look for logins to accounts that shouldn't have SSH access
- Pay special attention to root logins (should be disabled ideally)
- Successful login after many failures may indicate compromised credentials" \
bash -lc '
log="'"$AUTH_LOG"'"
echo "Analyzing: $log"
files=("$log" "$log".1)
for f in "${files[@]}"; do [[ -f "$f" ]] || continue; cat "$f"; done \
| egrep -h "Accepted (password|publickey)" | tail -n 100 || echo "No successful SSH logins found in recent logs"
'
else
progress "SSH logs (journalctl)"
run "SSH Failed Attempts (journalctl fallback)" "ssh-fails.txt" \
"SSH failed authentication attempts from systemd journal." \
"Same as above - look for brute-force patterns and unfamiliar IPs" \
bash -lc '
journalctl -u ssh -u sshd --no-pager -n 5000 2>/dev/null \
| egrep "Failed password|Invalid user|authentication failure" \
| awk "{for(i=1;i<=NF;i++){if(\$i ~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/){print \$i}}}" \
| sort | uniq -c | sort -nr | head -n 20 || echo "No SSH logs available"
'
progress "SSH successful logins (journalctl)"
run "SSH Successful Logins (journalctl fallback)" "ssh-success.txt" \
"SSH successful authentication from systemd journal." \
"Verify you recognize all successful login sources and times" \
bash -lc '
journalctl -u ssh -u sshd --no-pager -n 5000 2>/dev/null \
| egrep "Accepted (password|publickey)" | tail -n 100 || echo "No SSH logs available"
'
fi
if [[ -f "$APACHE_ACCESS" ]]; then
progress "Web probes (Apache)"
run "Web Access: Suspicious Probes (Apache)" "web-probes-apache.txt" \
"HTTP requests targeting common vulnerability patterns." \
"- Requests for .env, .git, wp-config.php indicate reconnaissance
- /admin, /login attempts may be brute-force or credential stuffing
- High volume from single IPs should be investigated
- Many probes are automated scanners, but they reveal attack vectors
- If these succeed (HTTP 200), you have a serious exposure problem" \
bash -lc '
log="'"$APACHE_ACCESS"'"
egrep -h "(/\.env|/\.git|wp-config\.php|phpinfo\.php|/cgi-bin/|/vendor/|/admin|/login|/\.well-known)" "$log" \
| tail -n 200 || echo "No suspicious web probes detected in Apache logs"
'
progress "Web probe IPs (Apache)"
run "Web Access: Top IPs Targeting .env/.git (Apache)" "web-probes-apache-topips.txt" \
"Source IPs attempting to access sensitive files." \
"- These IPs are actively probing for exposed secrets
- Consider blocking repeat offenders
- CloudFlare or similar WAF can help mitigate" \
bash -lc '
log="'"$APACHE_ACCESS"'"
egrep -h "(/\.env|/\.git)" "$log" \
| awk "{print \$1}" | sort | uniq -c | sort -nr | head -n 20 || echo "No .env/.git probes detected (good!)"
'
fi
if [[ -f "$NGINX_ACCESS" ]]; then
progress "Web probes (Nginx)"
run "Web Access: Suspicious Probes (Nginx)" "web-probes-nginx.txt" \
"HTTP requests targeting common vulnerability patterns (Nginx)." \
"Same guidance as Apache probes above" \
bash -lc '
log="'"$NGINX_ACCESS"'"
egrep -h "(/\.env|/\.git|wp-config\.php|phpinfo\.php|/cgi-bin/|/vendor/|/admin|/login|/\.well-known)" "$log" \
| tail -n 200 || echo "No suspicious web probes detected in Nginx logs"
'
progress "Web probe IPs (Nginx)"
run "Web Access: Top IPs Targeting .env/.git (Nginx)" "web-probes-nginx-topips.txt" \
"Source IPs attempting to access sensitive files (Nginx)." \
"Same guidance as Apache probe IPs above" \
bash -lc '
log="'"$NGINX_ACCESS"'"
egrep -h "(/\.env|/\.git)" "$log" \
| awk "{print \$1}" | sort | uniq -c | sort -nr | head -n 20 || echo "No .env/.git probes detected (good!)"
'
fi
fi
# -------------------------
# Persistence checks
# -------------------------
if [[ "$DO_PERSISTENCE" == "true" ]]; then
head1 "Persistence checks (cron + systemd)"
progress "Cron directories"
run "Cron: System Cron Directories" "cron-dirs.txt" \
"Lists files in system cron directories where scheduled tasks are defined." \
"- /etc/cron.* directories contain system-wide scheduled tasks
- Look for unfamiliar scripts or suspicious commands
- Attackers often use cron for persistence (reboot/hourly tasks to maintain access)
- Pay attention to any scripts calling curl/wget, especially to download and execute
- Verify all cron entries are legitimate and documented" \
bash -lc 'ls -la /etc/cron.* /var/spool/cron 2>/dev/null || echo "No system cron directories found"'
progress "Root crontab"
run "Cron: Root Crontab" "cron-root.txt" \
"Scheduled tasks running as root user." \
"- Root cron jobs have full system privileges
- Look for any entries you didn't create
- Suspicious: downloads (curl/wget), reverse shells, or encoded commands
- Empty is common and fine if you don't use root cron" \
bash -lc 'crontab -l 2>/dev/null || echo "No root crontab configured"'
progress "User crontabs"
run "Cron: User Crontabs" "cron-users.txt" \
"Scheduled tasks for individual user accounts." \
"- Check each user's crontab for legitimacy
- Web server users (www-data, nginx, apache) shouldn't typically have crontabs
- Look for the same suspicious patterns as root crontab above" \
bash -lc '
for u in $(getent passwd | awk -F: '"'"'$3>=1000{print $1}'"'"'); do
echo "== User: $u =="
crontab -u "$u" -l 2>/dev/null || echo "No crontab for $u"
echo
done
'
progress "Systemd enabled units"
run "Systemd: Enabled Services" "systemd-enabled.txt" \
"Systemd services configured to start automatically." \
"- These services start on boot and may persist after reboots
- Verify you recognize all enabled services
- Look for services with unusual names or locations
- Attackers may create services for backdoors or cryptominers
- Research any unfamiliar service names before assuming they're malicious" \
bash -lc 'systemctl list-unit-files --state=enabled 2>/dev/null | head -n 300 || echo "Unable to list systemd units"'
progress "Systemd timers"
run "Systemd: Timers" "systemd-timers.txt" \
"Systemd timer units (similar to cron, but managed by systemd)." \
"- Timers are modern alternative to cron jobs
- Check what each timer activates (linked .service file)
- Look for suspicious timer names or schedules
- Verify timer targets are legitimate services" \
bash -lc 'systemctl list-timers --all 2>/dev/null || echo "No systemd timers configured"'
fi
# -------------------------
# SUID sweep
# -------------------------
if [[ "$DO_SUID" == "true" ]]; then
head1 "SUID/SGID sweep (deep)"
progress "Finding SUID/SGID binaries"
run "SUID/SGID Binaries (Privilege Escalation Risks)" "suid-sgid.txt" \
"Executables with SUID/SGID bits that run with elevated privileges." \
"- SUID binaries run with owner's permissions (often root)
- Common legitimate SUID: sudo, su, ping, mount, passwd
- Unexpected SUID binaries can be privilege escalation vectors
- Look for SUID on shell interpreters (bash, python, perl) - these are HIGH RISK
- Check SUID binaries in /tmp, /var/tmp, or user home directories - likely malicious
- Research any unfamiliar SUID binaries before assuming compromise" \
bash -lc 'find / -xdev \( -perm -4000 -o -perm -2000 \) -type f -printf "%p\n" 2>/dev/null | sort | head -n 200'
fi
# -------------------------
# Malware/rootkit tools
# -------------------------
if [[ "$DO_MALWARE" == "true" ]]; then
head1 "Malware/rootkit tools (deep, if installed)"