-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathc4p.sh
executable file
·192 lines (176 loc) · 5.22 KB
/
c4p.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
#!/usr/bin/env bash
# Install common pentester tools in containers as if they were native
#
# Copyright 2022 TheArqsz
SCRIPT_PATH="$( cd -- "$(dirname -- $( realpath "${BASH_SOURCE[0]:-$0}"; ))" &> /dev/null && pwd 2> /dev/null; )"
TOOLS_DIR="$SCRIPT_PATH/tools"
SCRIPTS_DIR="$SCRIPT_PATH/lib"
ctrl_c() {
echo
echo "Interrupting script..."
exit 1
}
trap ctrl_c SIGINT
trap ctrl_c INT
cleanup() {
unset C4P_LOG_LEVEL
unset C4P_LOG_FILE
}
trap cleanup EXIT
source "$SCRIPTS_DIR/methods.sh"
# Default options
tool_to_install_or_run_raw=
force_install=0
force_fail_on_error=0
raw=0
raw_options=""
log_file=
log_level=3
export C4P_LOG_LEVEL=$log_level
config_file="$HOME/.c4p_config"
global_install_path="/usr/local/bin"
install_globally=0
# Loop that sets arguments for the script
while [ -n "$1" ]; do
case "$1" in
-h|--help)
show_banner
show_help
exit;;
-t|--tool)
tool_to_install_or_run_raw="$2"
shift
;;
-r|--run-raw)
raw=1
shift 0
;;
--run-raw-options)
raw_options="$2"
shift
;;
-f|--force)
force_install=1
shift 0
;;
--fail)
force_fail_on_error=1
shift 0
;;
-l|--list)
list_tools $TOOLS_DIR
exit 0
;;
--log-level)
log_level=$2
verify_log_level $log_level
export C4P_LOG_LEVEL=$log_level
shift
;;
--log-file)
log_file=$2
export C4P_LOG_FILE=$log_file
echo -n "" > "$log_file"
shift
;;
--only-log-file)
export C4P_ONLY_LOG_FILE=1
shift 0
;;
-i|--install-globally)
install_globally=1
shift 0
;;
--install-glob-path)
global_install_path=$2
shift
install_globally=1
;;
--config)
config_file=$2
shift
;;
-v|--verbose)
set -x
shift 0
;;
*)
echo "Option '$1' is not recognized"
echo
show_banner
show_help
exit 1
;;
esac
shift
done
source "$SCRIPTS_DIR/logging.sh"
# Check if tool to install was specified
if [ -z "$tool_to_install_or_run_raw" ]; then
log_info "Tool wasn't specified - exiting"
show_help
exit 0
fi
verify_tool_exists $TOOLS_DIR $tool_to_install_or_run_raw
# Initiate script
show_banner
log_debug "c4p - starting"
if [ "$raw" -eq "1" ] && [ -z "$tool_to_install_or_run_raw" ]; then
log_error "Tool wasn't specified - cannot run raw command"
exit 1
elif [ "$raw" -eq "1" ] && [ "$tool_to_install_or_run_raw" == "all" ]; then
log_error "Cannot run all tools simultanously - exiting"
exit 1
elif [ "$raw" -eq "1" ] && ! [ -z "$tool_to_install_or_run_raw" ]; then
bash $SCRIPTS_DIR/install.sh "$tool_to_install_or_run_raw" 1
$LIB_PATH/run.sh $tool_to_install_or_run_raw "$raw_options"
exit 0
fi
# Check if c4p config file exists
if ! [ -f "$config_file" ]; then
touch "$config_file"
fi
# Iterate over all tools in directory
for current_tool in $(ls -1 $TOOLS_DIR/); do
current_tool_path="$TOOLS_DIR/$current_tool"
if ! validate_tool_directory "$current_tool_path"; then
continue
fi
if [ "$tool_to_install_or_run_raw" != "all" ] && [ "$tool_to_install_or_run_raw" != "$current_tool" ]; then
log_debug "Skipping installation of tool $current_tool"
continue
fi
bash "$current_tool_path/test.sh" 1 2>/dev/null
if [ $? -eq 0 ] && [ $force_install -eq 0 ]; then
log_info "Tool $current_tool already pulled or built - skipping full installation"
bash "$SCRIPTS_DIR/install.sh" $current_tool 0 $force_install 0 "" 1
continue
else
log_info "Tool $current_tool is not installed or is forced to be reinstalled - installing"
fi
if [ $install_globally -eq 1 ] && [ "$(id -u)" -ne 0 ]; then
log_error "Tool $current_tool cannot be installed globally without root access - run as sudo or root"
exit 1
elif [ $install_globally -eq 1 ]; then
bash "$SCRIPTS_DIR/install.sh" $current_tool 0 $force_install $install_globally $global_install_path
else
bash "$SCRIPTS_DIR/install.sh" $current_tool 0 $force_install 0 ""
fi
bash "$current_tool_path/test.sh" 2>/dev/null
test_status=$?
if [ $test_status -eq 1 ] && [ $force_fail_on_error -eq 0 ]; then
log_error "Tool $current_tool failed to be installed. Flag --fail not set - skipping"
continue
elif [ $test_status -eq 1 ] && [ $force_fail_on_error -eq 1 ]; then
log_error "Tool $current_tool failed to be installed - exiting"
exit 1
elif [ $test_status -eq 0 ]; then
log_info "Tool $current_tool successfully installed."
fi
done
if ! grep ". \$HOME/.c4p_config" "$HOME/.bashrc" &>/dev/null; then
echo ". \$HOME/.c4p_config" >> $HOME/.bashrc
fi
if ! grep ". \$HOME/.c4p_config" "$HOME/.zshrc" &>/dev/null; then
echo ". \$HOME/.c4p_config" >> $HOME/.zshrc
fi