-
Notifications
You must be signed in to change notification settings - Fork 0
/
zsh-logger
85 lines (73 loc) · 2.57 KB
/
zsh-logger
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
###########################################################################
# Put this at the end of the zshrc file. Then source it. #
# This will add "startlog, stoplog, statuslog" aliases and it will log #
# what you are doing during a pentest. #
###########################################################################
```
# Directory to store logfiles
LOG_DIR="$HOME/pentest-logs"
LOGFILE=""
# Commands to exclude from logging
EXCLUDE_COMMANDS=(
"cd" "ls" "ps" "clear" "exit" "pwd" "history"
"nano" "source" "cat" "vi" "vim" "apt" "pip" "pipx"
"alias" "unalias" "export" "unset" "env" "set" "printenv"
"kill" "jobs" "bg" "fg" "disown"
"touch" "rm" "mv" "cp" "mkdir" "rmdir" "ln"
"bash" "zsh" "sh" "logout" "ssh" "stty" "tty"
"less" "more" "head" "tail" "strings"
"ping" "traceroute" "curl" "wget" "nslookup" "dig" "host" "whois"
"yum" "dnf" "zypper" "brew" "npm" "yarn" "cargo"
"gcc" "g++" "make" "cmake" "gdb" "strace" "ltrace"
"git" "svn" "hg"
"startx" "xinit" "xdg-open"
"time" "date" "cal" "df" "du" "free" "uptime"
)
# Function to log commands
log_command() {
# Skip logging if LOGFILE is not set
if [[ -z "$LOGFILE" ]]; then
return
fi
local cmd=$(fc -ln -1 | sed 's/^ *//') # Get the last command
local actual_cmd=$cmd
# Strip 'sudo' if present
if [[ $cmd == sudo* ]]; then
actual_cmd=$(echo "$cmd" | sed 's/^sudo //')
fi
# Check if the command should be excluded
for exclude in "${EXCLUDE_COMMANDS[@]}"; do
if [[ $actual_cmd == "$exclude"* ]]; then
return # Skip logging excluded commands
fi
done
# Log the command
echo "$(date '+%Y-%m-%d %H:%M') - $cmd" >> "$LOGFILE"
}
# Function to enable logging
enable_logging() {
mkdir -p "$LOG_DIR" # Ensure the log directory exists
LOGFILE="$LOG_DIR/pentest-$(date '+%Y-%m-%d_%H-%M-%S').log"
precmd_functions+=(log_command) # Add log_command to precmd hooks
echo "Logging enabled. Logfile: $LOGFILE"
}
# Function to disable logging
disable_logging() {
# Remove log_command from precmd_functions
precmd_functions=(${precmd_functions:#log_command})
LOGFILE=""
echo "Logging disabled."
}
# Function to check logging status
check_logging_status() {
if [[ -n "$LOGFILE" ]]; then
echo "Logging is ENABLED. Current logfile: $LOGFILE"
else
echo "Logging is DISABLED."
fi
}
# Aliases for convenience
alias startlog="enable_logging"
alias stoplog="disable_logging"
alias logstatus="check_logging_status"
```