1+ #! /bin/bash
2+ # SecuLite Security Check Script
3+ # Usage:
4+ # ZAP_TARGET="http://dein-ziel:port" HTML_REPORT=1 ./scripts/security-check.sh
5+ # oder
6+ # ./scripts/security-check.sh [ZAP_TARGET_URL]
7+ # Default: http://localhost:8000
8+
9+ # === DEBUG: Print environment and ZAP script status ===
10+ echo " [DEBUG] PATH: $PATH "
11+ echo " [DEBUG] ls -l /usr/local/bin/"
12+ ls -l /usr/local/bin/
13+ echo " [DEBUG] ls -l /opt/ZAP_2.16.1/"
14+ ls -l /opt/ZAP_2.16.1/ || echo " /opt/ZAP_2.16.1/ not found"
15+ echo " [DEBUG] command -v zap-baseline.py: $( command -v zap-baseline.py || echo not found) "
16+
17+ # Check for python3 availability (required for ZAP)
18+ if ! command -v python3 & > /dev/null; then
19+ echo " [ERROR] python3 is not installed or not in PATH. ZAP scan cannot run. Please install python3." | tee -a " $LOG_FILE "
20+ # Do not exit, continue
21+ fi
22+
23+ # Ziel-URL für ZAP bestimmen
24+ ZAP_TARGET=" ${ZAP_TARGET:- ${1:- http:// localhost: 8000} } "
25+ HTML_REPORT=" ${HTML_REPORT:- 0} "
26+
27+ # Usage: ./scripts/security-check.sh [TARGET_PATH]
28+ # Set scan target to /target (App-Code im Container)
29+ TARGET_PATH=" /target"
30+ RESULTS_DIR=" /seculite/results"
31+ LOGS_DIR=" /seculite/logs"
32+ LOG_FILE=" $LOGS_DIR /security-check.log"
33+ SUMMARY_TXT=" $RESULTS_DIR /security-summary.txt"
34+ SUMMARY_JSON=" $RESULTS_DIR /security-summary.json"
35+
36+ # At the very top, after variable definitions
37+ LOCK_FILE=" $RESULTS_DIR /.scan-running"
38+ touch " $LOCK_FILE "
39+
40+ mkdir -p " $RESULTS_DIR " " $LOGS_DIR "
41+ echo " [DEBUG] Listing $RESULTS_DIR before ZAP scan:"
42+ ls -l " $RESULTS_DIR "
43+
44+ # Clear previous results
45+ > " $SUMMARY_TXT "
46+ > " $SUMMARY_JSON "
47+ > " $LOG_FILE "
48+
49+ # Check for required tools
50+ MISSING_TOOLS=()
51+ for tool in semgrep trivy; do
52+ if ! command -v $tool & > /dev/null; then
53+ MISSING_TOOLS+=(" $tool " )
54+ fi
55+ done
56+ if [ ${# MISSING_TOOLS[@]} -ne 0 ]; then
57+ echo " [SecuLite] Missing required tools: ${MISSING_TOOLS[*]} " | tee -a " $LOG_FILE "
58+ echo " Please install all required tools before running the script." | tee -a " $LOG_FILE "
59+ # Do not exit, continue
60+ fi
61+
62+ # Ensure jq is installed
63+ if ! command -v jq & > /dev/null; then
64+ echo " [SecuLite] jq not found, installing jq..." | tee -a " $LOG_FILE "
65+ if command -v apt-get & > /dev/null; then
66+ sudo apt-get update && sudo apt-get install -y jq
67+ elif command -v yum & > /dev/null; then
68+ sudo yum install -y jq
69+ else
70+ echo " [SecuLite] Please install jq manually." | tee -a " $LOG_FILE "
71+ # Do not exit, continue
72+ fi
73+ fi
74+
75+ echo " [DEBUG] Testing write permissions in $RESULTS_DIR "
76+ touch " $RESULTS_DIR /test-write.txt" && echo " [DEBUG] Write test succeeded" || echo " [DEBUG] Write test FAILED"
77+ rm -f " $RESULTS_DIR /test-write.txt"
78+
79+ # Run ZAP Baseline Scan
80+ if command -v zap-baseline.py & > /dev/null; then
81+ export ZAP_PATH=/opt/ZAP_2.16.1
82+ export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
83+ echo " [ZAP] ENV: ZAP_PATH=$ZAP_PATH JAVA_HOME=$JAVA_HOME " | tee -a " $LOG_FILE "
84+ echo " [ZAP] Running baseline scan on $ZAP_TARGET ..." | tee -a " $LOG_FILE "
85+ ZAP_REPORT_XML=" $RESULTS_DIR /zap-report.xml"
86+ ZAP_REPORT_HTML=" $RESULTS_DIR /zap-report.html"
87+ ZAP_REPORT_XMLHTML=" $RESULTS_DIR /zap-report.xml.html"
88+ REL_ZAP_REPORT_XML=" zap-report.xml"
89+ REL_ZAP_REPORT_HTML=" zap-report.html"
90+ echo " [DEBUG] Running ZAP with absolute path: $ZAP_REPORT_XML "
91+ python3 /usr/local/bin/zap-baseline.py -d -t " $ZAP_TARGET " -x " $ZAP_REPORT_XML " 2>> " $LOG_FILE " || {
92+ echo " [ZAP] Scan failed (absolute path)" >> " $LOG_FILE "
93+ }
94+ # Fallback: search for zap-report.xml anywhere and copy to results if missing
95+ if [ ! -f " $ZAP_REPORT_XML " ]; then
96+ FOUND_XML=$( find / -type f -name ' zap-report.xml' 2> /dev/null | head -n 1)
97+ if [ -n " $FOUND_XML " ]; then
98+ cp " $FOUND_XML " " $ZAP_REPORT_XML "
99+ echo " [DEBUG] Copied fallback zap-report.xml from $FOUND_XML to $ZAP_REPORT_XML " >> " $LOG_FILE "
100+ else
101+ echo " [DEBUG] No zap-report.xml found anywhere in container." >> " $LOG_FILE "
102+ fi
103+ fi
104+ echo " [DEBUG] Running ZAP with relative path: $REL_ZAP_REPORT_XML "
105+ python3 /usr/local/bin/zap-baseline.py -d -t " $ZAP_TARGET " -r " $REL_ZAP_REPORT_XML " 2>> " $LOG_FILE " || {
106+ echo " [ZAP] Scan failed (relative path)" >> " $LOG_FILE "
107+ }
108+ python3 /usr/local/bin/zap-baseline.py -d -t " $ZAP_TARGET " -f html -o " $ZAP_REPORT_HTML " 2>> " $LOG_FILE " || echo " [ZAP] HTML report failed" >> " $LOG_FILE "
109+ echo " [DEBUG] Listing $RESULTS_DIR after ZAP scan:"
110+ ls -l " $RESULTS_DIR "
111+ echo " [DEBUG] Searching for any zap-report* files and copying to $RESULTS_DIR "
112+ find / -type f -name ' zap-report*' -exec cp --no-clobber {} " $RESULTS_DIR " \; 2> /dev/null
113+ ls -l " $RESULTS_DIR "
114+ if [ -f " $ZAP_REPORT_XML " ] || [ -f " $ZAP_REPORT_HTML " ] || [ -f " $ZAP_REPORT_XMLHTML " ]; then
115+ echo " [ZAP] Report(s) erfolgreich erzeugt:"
116+ [ -f " $ZAP_REPORT_XML " ] && echo " - $ZAP_REPORT_XML "
117+ [ -f " $ZAP_REPORT_HTML " ] && echo " - $ZAP_REPORT_HTML "
118+ [ -f " $ZAP_REPORT_XMLHTML " ] && echo " - $ZAP_REPORT_XMLHTML "
119+ else
120+ echo " [ZAP] ERROR: Kein Report wurde erzeugt!" | tee -a " $LOG_FILE "
121+ # Do not exit, continue
122+ fi
123+ echo " [ZAP] Baseline scan complete." | tee -a " $SUMMARY_TXT "
124+ else
125+ echo " [ZAP] zap-baseline.py not found, skipping ZAP scan." | tee -a " $LOG_FILE "
126+ fi
127+
128+ # Run Semgrep
129+ if command -v semgrep & > /dev/null; then
130+ echo " [Semgrep] Running code scan on $TARGET_PATH ..." | tee -a " $LOG_FILE "
131+ semgrep --config /seculite/rules $TARGET_PATH --json > " $RESULTS_DIR /semgrep.json" 2>> " $LOG_FILE " || echo " [Semgrep] Scan failed" >> " $LOG_FILE "
132+ semgrep --config /seculite/rules $TARGET_PATH --text > " $RESULTS_DIR /semgrep.txt" 2>> " $LOG_FILE "
133+ echo " [Semgrep] Code scan complete." | tee -a " $SUMMARY_TXT "
134+ else
135+ echo " [Semgrep] semgrep not found, skipping code scan." | tee -a " $LOG_FILE "
136+ fi
137+
138+ # Run Trivy
139+ if command -v trivy & > /dev/null; then
140+ echo " [Trivy] Running dependency/container scan on $TARGET_PATH ..." | tee -a " $LOG_FILE "
141+ trivy fs --config /seculite/trivy/config.yaml --format json -o " $RESULTS_DIR /trivy.json" $TARGET_PATH 2>> " $LOG_FILE "
142+ trivy fs --config /seculite/trivy/config.yaml --format table -o " $RESULTS_DIR /trivy.txt" $TARGET_PATH 2>> " $LOG_FILE "
143+ echo " [Trivy] Dependency/container scan complete." | tee -a " $SUMMARY_TXT "
144+ else
145+ echo " [Trivy] trivy not found, skipping dependency/container scan." | tee -a " $LOG_FILE "
146+ fi
147+
148+ # Aggregate Results
149+ {
150+ echo " ==== ZAP Report (XML) ===="
151+ [ -f " $RESULTS_DIR /zap-report.xml" ] && cat " $RESULTS_DIR /zap-report.xml" || echo " No ZAP XML report."
152+ echo
153+ echo " ==== ZAP Report (HTML) ===="
154+ [ -f " $RESULTS_DIR /zap-report.html" ] && cat " $RESULTS_DIR /zap-report.html" || echo " No ZAP HTML report."
155+ echo
156+ echo " ==== ZAP Report (XMLHTML) ===="
157+ [ -f " $RESULTS_DIR /zap-report.xml.html" ] && cat " $RESULTS_DIR /zap-report.xml.html" || echo " No ZAP XMLHTML report."
158+ echo
159+ echo " ==== Semgrep Findings ===="
160+ [ -f " $RESULTS_DIR /semgrep.txt" ] && cat " $RESULTS_DIR /semgrep.txt" || echo " No Semgrep findings."
161+ echo
162+ echo " ==== Trivy Findings ===="
163+ [ -f " $RESULTS_DIR /trivy.txt" ] && cat " $RESULTS_DIR /trivy.txt" || echo " No Trivy findings."
164+ } > " $SUMMARY_TXT "
165+
166+ jq -s ' reduce .[] as $item ({}; . * $item)' " $RESULTS_DIR /semgrep.json" " $RESULTS_DIR /trivy.json" 2> /dev/null > " $SUMMARY_JSON " || echo ' {"error": "Could not aggregate JSON results"}' > " $SUMMARY_JSON "
167+
168+ # Always generate the unified HTML report inside the container
169+ python3 /seculite/scripts/generate-html-report.py
170+
171+ echo " [SecuLite] Security checks complete. See $SUMMARY_TXT , $SUMMARY_JSON , and security-summary.html for results." | tee -a " $LOG_FILE "
172+
173+ # At the very end, before exit 0
174+ rm -f " $LOCK_FILE "
175+
176+ # Ensure webui.js is always present in the results directory
177+ cp /seculite/scripts/webui.js /seculite/results/webui.js 2> /dev/null || cp scripts/webui.js results/webui.js 2> /dev/null
178+
179+ exit 0
0 commit comments