-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetupPentest.sh
More file actions
executable file
·299 lines (271 loc) · 9.51 KB
/
SetupPentest.sh
File metadata and controls
executable file
·299 lines (271 loc) · 9.51 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
#!/usr/bin/env bash
set -uo pipefail
IFS=$'\n\t'
# =============================================================================
# NAME : SetupPentest.sh
# DESCRIPTION :
# AUTHOR : Adam Compton
# DATE CREATED: 2024-12-16 16:51:35
# =============================================================================
# EDIT HISTORY:
# DATE | EDITED BY | DESCRIPTION OF CHANGE
# ---------------------|--------------|----------------------------------------
# 2024-12-16 16:51:35 | Adam Compton | Initial creation.
# =============================================================================
# Minimal placeholders (until more robust functions can be defined later)
function fail() {
local timestamp
timestamp=$(date +"[%Y-%m-%d %H:%M:%S]")
echo "${timestamp} [- FAIL ] $*" >&2
}
function pass() {
local timestamp
timestamp=$(date +"[%Y-%m-%d %H:%M:%S]")
echo "${timestamp} [+ PASS ] $*"
}
function info() {
local timestamp
timestamp=$(date +"[%Y-%m-%d %H:%M:%S]")
echo "${timestamp} [* INFO ] $*"
}
function warn() {
local timestamp
timestamp=$(date +"[%Y-%m-%d %H:%M:%S]")
echo "${timestamp} [! WARN ] $*"
}
function debug() {
local timestamp
timestamp=$(date +"[%Y-%m-%d %H:%M:%S]")
echo "${timestamp} [! DEBUG ] $*"
}
# Initialize the error flag
ERROR_FLAG=false
# Ensure the script is being run under Bash
if [[ -z "${BASH_VERSION:-}" ]]; then
fail "Error: This script must be run under Bash."
ERROR_FLAG=true
fi
# Ensure Bash version is 4.0 or higher
if [[ -n "${BASH_VERSION:-}" && "${BASH_VERSINFO[0]}" -lt 4 ]]; then
fail "Error: This script requires Bash version 4.0 or higher. Current version: ${BASH_VERSION}"
ERROR_FLAG=true
else
info "Detected bash version: ${BASH_VERSION}"
fi
# Ensure the script is run as root (user ID 0)
if [[ ${EUID} -ne 0 ]]; then
fail "Error: This script must be run as root."
ERROR_FLAG=true
fi
###############################################################################
# _Pause
#==============================
# Pauses execution until user presses any key (interactive mode only).
#————————————————————
# Usage:
# _Pause
#
# Arguments:
# None
#
# Return Values:
# None (returns immediately if non-interactive)
###############################################################################
function _Pause() {
if [[ -t 0 ]]; then # check if running interactively
echo
echo "-----------------------------------"
read -n 1 -s -r -p "Press any key to continue..."
echo # Move to the next line after key press
# Use ANSI escape codes to move the cursor up and clear lines
tput cuu 3 # Move the cursor up 3 lines
tput el # Clear the current line
tput el # Clear the next line
tput el # Clear the third line
fi
}
# If any errors occurred, display a summary and exit
if ${ERROR_FLAG}; then
echo
fail "--------------------------------------------------"
fail "One or more errors occurred:"
fail " - Ensure you are using Bash version 4.0 or higher."
fail " - Ensure you are running this script as root as"
fail " some tools will need to be installed and other"
fail " 'root' tasks will possibly be performed"
fail
fail "-----------------------------------"
_Pause
#exit 1 # Exit with a failure status code
fi
# Success message if no errors
pass "All checks passed. Continuing script execution."
# -----------------------------------------------------------------------------
# ---------------------------------- IMPORTS/SOURCES --------------------------
# -----------------------------------------------------------------------------
# Check if the HOME environment variable is set
if [[ -n "${HOME}" ]]; then
# If HOME is set, use it
info "HOME environment variable is set. Using HOME: ${HOME}"
elif command -v getent > /dev/null 2>&1; then
# If getent is available, use it to retrieve the home directory
HOME_TEMP=$(getent passwd "$(whoami)" | cut -d: -f6)
export HOME="${HOME_TEMP}"
if [[ -n "${HOME}" ]]; then
info "Using getent to determine HOME: ${HOME}"
else
fail "Failed to determine HOME using getent."
exit 1
fi
else
# Fallback: Use eval to get the home directory
HOME_TEMP=$(eval echo ~)
export HOME="${HOME_TEMP}"
if [[ -n "${HOME}" ]]; then
warn "HOME and getent are unavailable. Using fallback with eval: HOME=${HOME}"
else
fail "Failed to determine HOME. Unable to proceed."
exit 1
fi
fi
# Determine the script's root directory
# The SCRIPT_DIR variable points to the directory containing the script.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [[ -n "${SCRIPT_DIR}" && -d "${SCRIPT_DIR}" ]]; then
export SCRIPT_DIR
info "Script directory determined: ${SCRIPT_DIR}"
else
fail "Failed to determine the script directory. Exiting."
exit 1
fi
# Define required files in an array
# These files must exist and be sourced for the script to work correctly.
declare -a REQUIRED_FILES=(
"${SCRIPT_DIR}/config/config.sh"
"${SCRIPT_DIR}/config/lists.sh"
"${SCRIPT_DIR}/lib/common_core/lib/utils.sh"
"${SCRIPT_DIR}/lib/common_core/lib/menu.sh"
"${SCRIPT_DIR}/menu/init_tasks.sh"
"${SCRIPT_DIR}/menu/install_tasks.sh"
"${SCRIPT_DIR}/menu/menu_tasks.sh"
)
# Source required files and verify their existence
for file in "${REQUIRED_FILES[@]}"; do
if [[ -f "${file}" ]]; then
# Source the file if it exists
source "${file}" || {
echo "Failed to source file: ${file}. Exiting."
exit 1
}
else
# Log an error if the file is missing
echo "Required file is missing: ${file}. Exiting."
exit 1
fi
done
# -----------------------------------------------------------------------------
# ---------------------------------- GLOBAL VARIABLES/CHECKS ------------------
# -----------------------------------------------------------------------------
# Check if proxychains is required
_check_proxy_needed
# -----------------------------------------------------------------------------
# ---------------------------------- UTILITY ----------------------------------
# -----------------------------------------------------------------------------
###############################################################################
# ensure_command
#==============================
# Checks whether a given command is installed and functional.
# Prompts the user to install it if missing.
#————————————————————
# Usage:
# ensure_command <command_name>
#
# Arguments:
# command_name → Name of the binary/command to check and install if missing
#
# Return Values:
# 0 → Command exists or installed successfully
# non-zero → User declined install or install failed
#————————————————————
# Requirements:
# check_command
# _install_package
# info, warn, fail functions
# OS_NAME variable
###############################################################################
function ensure_command() {
local cmd="$1"
if [[ -z "${cmd}" ]]; then
fail "No command name provided to ensure_command."
return "${_FAIL}"
fi
if ! check_command "${cmd}"; then
read -r -p "Command '${cmd}' not found. Do you want to install it? (Y/n): " answer
# Default to 'Y' if user just hits Enter
answer="${answer:-Y}"
case "${answer}" in
[Yy]*)
_install_package "${cmd}"
return $?
;;
[Nn]*)
warn "${cmd} will not be installed. Exiting."
exit "${_FAIL}"
;;
*)
fail "Invalid response. Exiting."
exit "${_FAIL}"
;;
esac
else
info "Command '${cmd}' is already installed."
fi
}
# -----------------------------------------------------------------------------
# ---------------------------------- MAIN -------------------------------------
# -----------------------------------------------------------------------------
###############################################################################
# main
#==============================
# Main entry point for the SetupPentest script.
#————————————————————
# Usage:
# main [options]
#
# Arguments:
# -bash → Bypass menu and run Setup_Dot_Files directly
#
# Return Values:
# 0 → Success
# 1 → Invalid argument
###############################################################################
function main() {
# Check if any arguments are passed
if [[ $# -eq 0 ]]; then
# No arguments, display the menu
_display_menu "Setup Pentest Environment" "_Process_Start_Menu" false "${SETUP_MENU_ITEMS[@]}"
return
fi
# Process arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-bash)
# Bypass menu and run Setup_Dot_Files directly
Setup_Dot_Files
return
;;
*)
# Handle invalid arguments
echo "[ERROR] Invalid argument: $1" >&2
echo "Usage: $0 [-bash]" >&2
return 1
;;
esac
done
}
# Run ensure_command to check if required commands are present
ensure_command "fzf"
ensure_command "proxychains"
ensure_command "proxychains4"
# Call the main function, passing all script arguments
main "$@"