-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrootkit_scan.sh
More file actions
283 lines (252 loc) · 10.3 KB
/
Copy pathrootkit_scan.sh
File metadata and controls
283 lines (252 loc) · 10.3 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
#!/usr/bin/env bash
# ============================================================
# Name : rootkit_scan.sh
# Version : 1.0.0
# Author : Anamicca23
# Tested : Ubuntu 22.04 LTS, Debian 12 Bookworm
# Min OS : Ubuntu 20.04 / Debian 11
# Risk : LOW (read-only security scanning)
# Sudo : Required
# Reversible: Yes
# Desc : Installs rkhunter and chkrootkit if missing,
# runs both scanners, combines output into a
# timestamped report. Highlights WARNING and
# INFECTED lines in red on the terminal.
# ============================================================
set -uo pipefail
RED='\033[0;31m'; YELLOW='\033[1;33m'; GREEN='\033[0;32m'
CYAN='\033[0;36m'; BOLD='\033[1m'; NC='\033[0m'
DASH="────────────────────────────────────────────────────────"
if [[ $EUID -ne 0 ]]; then
echo -e "\n ${RED}[ERROR]${NC} Must be run as root: ${BOLD}sudo ./rootkit_scan.sh${NC}\n"
exit 1
fi
REPORT_DIR="/root"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
REPORT_FILE="${REPORT_DIR}/security_scan_${TIMESTAMP}.txt"
show_menu() {
clear
echo -e "${BOLD}${CYAN}"
echo " ╔══════════════════════════════════════════════════════╗"
echo " ║ ROOTKIT SCANNER v1.0.0 ║"
echo " ║ rkhunter + chkrootkit — full threat scan ║"
echo " ╚══════════════════════════════════════════════════════╝"
echo -e "${NC}"
echo -e " ${BOLD}[1]${NC} Full Scan (rkhunter + chkrootkit)"
echo -e " ${BOLD}[2]${NC} rkhunter Scan Only"
echo -e " ${BOLD}[3]${NC} chkrootkit Scan Only"
echo -e " ${BOLD}[4]${NC} Update rkhunter Database"
echo -e " ${BOLD}[5]${NC} Install Scanners (if missing)"
echo -e " ${BOLD}[6]${NC} View Last Scan Report"
echo -e " ${BOLD}[7]${NC} About — What These Tools Detect"
echo -e " ${BOLD}[0]${NC} Exit"
echo
printf " Enter Option: "
read -r choice
}
install_scanners() {
local needs_install=0
echo
for tool in rkhunter chkrootkit; do
if ! command -v "$tool" &>/dev/null; then
echo -e " Installing ${tool}..."
apt-get install -y "$tool" 2>&1 | tail -3 | sed 's/^/ /'
needs_install=1
else
echo -e " ${GREEN}[OK]${NC} $tool already installed."
fi
done
[[ $needs_install -eq 1 ]] && echo -e "\n ${GREEN}[DONE]${NC} Scanners installed." || \
echo -e " All required tools are already installed."
echo
}
update_rkhunter() {
if ! command -v rkhunter &>/dev/null; then
echo -e " ${YELLOW}rkhunter not installed. Run option [5] first.${NC}"
return
fi
echo -e "\n Updating rkhunter database..."
rkhunter --update 2>&1 | sed 's/^/ /'
echo -e " ${GREEN}[OK]${NC} Database updated."
echo
}
run_rkhunter() {
if ! command -v rkhunter &>/dev/null; then
echo -e " ${YELLOW}rkhunter not installed.${NC}"
install_scanners
command -v rkhunter &>/dev/null || return
fi
echo -e "\n ${BOLD}Running rkhunter...${NC}"
echo -e " This may take 2-5 minutes.\n"
local rk_out; rk_out=$(mktemp)
rkhunter --check --sk --rwo 2>/dev/null > "$rk_out" || true
# Display with highlights
local warn_count=0 ok_count=0
while IFS= read -r line; do
if echo "$line" | grep -qiE "warning|suspect"; then
echo -e " ${YELLOW}${line}${NC}"
warn_count=$(( warn_count + 1 ))
elif echo "$line" | grep -qiE "infected|found"; then
echo -e " ${RED}${line}${NC}"
warn_count=$(( warn_count + 1 ))
elif echo "$line" | grep -qiE "\[ OK \]|not found|not set"; then
echo -e " ${GREEN}${line}${NC}"
ok_count=$(( ok_count + 1 ))
else
echo " $line"
fi
done < "$rk_out"
echo
echo -e " ${BOLD}rkhunter summary:${NC} ${GREEN}${ok_count} OK${NC} ${YELLOW}${warn_count} warnings${NC}"
cat "$rk_out" >> "$REPORT_FILE" 2>/dev/null
rm -f "$rk_out"
}
run_chkrootkit() {
if ! command -v chkrootkit &>/dev/null; then
echo -e " ${YELLOW}chkrootkit not installed.${NC}"
install_scanners
command -v chkrootkit &>/dev/null || return
fi
echo -e "\n ${BOLD}Running chkrootkit...${NC}"
echo -e " This may take 1-3 minutes.\n"
local ck_out; ck_out=$(mktemp)
chkrootkit 2>/dev/null > "$ck_out" || true
# Display with highlights
local infected_count=0
while IFS= read -r line; do
if echo "$line" | grep -qiE "INFECTED|Possible"; then
echo -e " ${RED}${BOLD}${line}${NC}"
infected_count=$(( infected_count + 1 ))
elif echo "$line" | grep -qi "not infected"; then
echo -e " ${GREEN}${line}${NC}"
else
echo " $line"
fi
done < "$ck_out"
echo
if [[ $infected_count -eq 0 ]]; then
echo -e " ${GREEN}[OK]${NC} chkrootkit found no infections."
else
echo -e " ${RED}[ALERT]${NC} chkrootkit found ${infected_count} potential infection(s)."
echo -e " ${YELLOW}Note:${NC} Some are false positives. Investigate before taking action."
fi
cat "$ck_out" >> "$REPORT_FILE" 2>/dev/null
rm -f "$ck_out"
}
full_scan() {
install_scanners
# Init report file
{
echo "========================================================"
echo " ROOTKIT SCAN REPORT"
echo " Generated: $(date)"
echo " Host: $(hostname)"
echo " Kernel: $(uname -r)"
echo "========================================================"
echo
} > "$REPORT_FILE"
clear
echo -e "${BOLD}${CYAN} RUNNING FULL SECURITY SCAN...${NC}"
echo -e " Report will be saved to: $REPORT_FILE\n"
echo -e "${BOLD}${CYAN} ── rkhunter ────────────────────────${NC}"
{ echo; echo "=== RKHUNTER ==="; } >> "$REPORT_FILE"
run_rkhunter
echo -e "\n${BOLD}${CYAN} ── chkrootkit ──────────────────────${NC}"
{ echo; echo "=== CHKROOTKIT ==="; } >> "$REPORT_FILE"
run_chkrootkit
{
echo
echo "========================================================"
echo " END OF REPORT"
echo "========================================================"
} >> "$REPORT_FILE"
echo
echo -e "${BOLD}${GREEN} [DONE]${NC} Full scan complete."
echo -e " Report saved: ${BOLD}${REPORT_FILE}${NC}"
echo
# Show highlighted summary
echo -e " ${BOLD}Findings summary:${NC}"
grep -iE "warning|INFECTED|Possible" "$REPORT_FILE" 2>/dev/null | \
head -20 | sed 's/^/ /' | while IFS= read -r line; do
echo -e " ${RED}${line}${NC}"
done || echo -e " ${GREEN}No warnings or infections found in report.${NC}"
echo
}
view_last_report() {
clear
local latest; latest=$(ls -t /root/security_scan_*.txt 2>/dev/null | head -1)
if [[ -z "$latest" ]]; then
echo -e "\n ${YELLOW}No scan reports found.${NC}"
echo -e " Run a scan first (option [1]).\n"
return
fi
echo -e "${BOLD}${CYAN} LAST SCAN REPORT: $latest${NC}"
echo -e " ${DASH}\n"
# Show warnings highlighted
while IFS= read -r line; do
if echo "$line" | grep -qiE "INFECTED|Possible"; then
echo -e " ${RED}${line}${NC}"
elif echo "$line" | grep -qi "warning"; then
echo -e " ${YELLOW}${line}${NC}"
else
echo " $line"
fi
done < "$latest"
echo
}
about_scanners() {
clear
echo -e "${BOLD}${CYAN} ABOUT ROOTKIT SCANNERS${NC}"
echo -e " ${DASH}\n"
cat << 'INFO'
rkhunter (Rootkit Hunter):
──────────────────────────
Checks for known rootkits by examining:
- Known rootkit signatures and files
- Hidden files/directories
- Suspicious file permissions
- System binary modifications (checks against known-good hashes)
- Network interfaces in promiscuous mode
- Listening applications and ports
- Startup files and scripts
chkrootkit:
────────────
Shell script that scans for known rootkit signatures by checking:
- Modifications to system binaries (ps, ls, netstat, etc.)
- Loadable kernel module (LKM) rootkits
- Known rootkit files and directories
- Network interfaces in promiscuous mode
- Log file tampering signs
False Positives:
─────────────────
Both tools may report false positives. Common ones include:
- SSH root login warnings (expected if root SSH is enabled)
- PACKET_SNIFFER warnings (may be from legit apps like Wireshark)
- rkhunter warnings about package manager binary hashes
(can occur after OS updates — run rkhunter --propupd after updates)
What to do if infections are found:
─────────────────────────────────────
1. Boot from a live USB to scan in a clean environment
2. Preserve evidence before attempting cleanup
3. Consider a full OS reinstall if rootkit is confirmed
4. Change all passwords from a clean machine
INFO
}
while true; do
show_menu
case "$choice" in
1) full_scan; read -rp " Press Enter..." ;;
2) clear; REPORT_FILE="${REPORT_DIR}/security_scan_${TIMESTAMP}_rk.txt"
{ echo "=== RKHUNTER $(date) ==="; } > "$REPORT_FILE"
run_rkhunter; read -rp " Press Enter..." ;;
3) clear; REPORT_FILE="${REPORT_DIR}/security_scan_${TIMESTAMP}_ck.txt"
{ echo "=== CHKROOTKIT $(date) ==="; } > "$REPORT_FILE"
run_chkrootkit; read -rp " Press Enter..." ;;
4) update_rkhunter; read -rp " Press Enter..." ;;
5) install_scanners; read -rp " Press Enter..." ;;
6) view_last_report; read -rp " Press Enter..." ;;
7) about_scanners; read -rp " Press Enter..." ;;
0) echo -e "\n Goodbye!\n"; exit 0 ;;
*) echo -e " ${RED}Invalid option.${NC}"; sleep 1 ;;
esac
done