forked from trimstray/sandmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__
299 lines (209 loc) · 7.54 KB
/
__init__
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
# shellcheck shell=bash
################################################################################
######################### Main function (script init) ##########################
################################################################################
function __main__() {
local _FUNCTION_ID="__main__"
local _STATE="0"
# Stores the current date.
readonly _cdate=$(date +%Y%m%d)
# Variables related to the log file. Divided into three parts due
# to the better possibility of manipulation for the user.
# shellcheck disable=SC2154
readonly _log_directory="${_rel}/log"
# shellcheck disable=SC2154
readonly _log_file="${_init_name}.${_cdate}.log"
readonly _log_stdout="${_log_directory}/stdout.log"
readonly _log_path="${_log_directory}/${_log_file}"
# We check if we are a root user.
if [[ "$EUID" -ne 0 ]] ; then
printf "EUID is not equal 0 (no root user)\\n"
_exit_ "1"
fi
# Path to import file.
# shellcheck disable=SC2154
readonly _import_path="${_src}/import"
# Path to modules directory.
# shellcheck disable=SC2034,SC2154
readonly _modules="${_dat}/modules"
# Store modules list.
# shellcheck disable=SC2034
modules_list=()
# shellcheck disable=SC2034
modules_full_list=()
# shellcheck disable=SC2034
_module_commands_count=0
# shellcheck disable=SC2034
_module_commands_count_all=0
# External configuration file (-c|--config script param).
config=""
load_state="0"
# Declaration of output variables (--debug and --verbose params).
stdout_mode=""
printf_mode=""
# Enable/disable output colors.
# shellcheck disable=SC2034
s_color="true"
# Declaration of total execution time.
time_tracking="false"
time_mode="0"
export _cmdtime_state="0"
# We place here used commands at script runtime, as strings to anything
# unnecessarily run.
readonly commands=("basename" "dirname" "stat" "date" "grep" "egrep" "cut" \
"sed" "gzip" "tar" "nmap" "xterm" "proxychains")
# If you intend to specify the full path to the command we do it like:
# readonly exec_gzip="/bin/gzip"
# Stores the names of the missing commands.
missing_hash=()
missing_counter="0"
for i in "${commands[@]}" ; do
if [[ ! -z "$i" ]] ; then
hash "$i" >/dev/null 2>&1 ; state="$?"
# If the command was not found put it in the array
if [[ "$state" -ne 0 ]] ; then
missing_hash+=("$i")
((missing_counter++))
fi
fi
done
# It is a good idea to terminate the script at this stage
# with information for the user to fix the errors if at least one
# of the required commands in the commands array is not found.
if [[ "$missing_counter" -gt 0 ]] ; then
printf "not found in PATH: %s\\n" "${missing_hash[*]}" >&2
_exit_ "1"
fi
if [[ "$time_tracking" == "true" ]] ; then
_begtime=$(date +%s) ; fi
# shellcheck disable=SC2154
_logger "init" \
"init '${_init_name}' in '${_init_directory}'" && \
_logger "info" \
"__init_params[] = (${__init_params[*]})," \
"__script_params[] = (${__script_params[*]})"
# Include import file.
_load "null" "$_import_path"
# Specifies the call parameters of the script, the exact description
# can be found in _help_ and file README.md.
local _short_opt=""
local _long_opt="help"
_GETOPT_PARAMS=$(getopt -o "${_short_opt}" --long "${_long_opt}" \
-n "${_init_name}" -- "${__script_params[@]}")
# With this structure, in the case of problems with the parameters placed
# in the _GETOPT_PARAMS variable we finish the script. Keep this in mind
# because it has some consequences - the __main __() function will not be
# executed.
# Ends an error if the parameter or its argument is not valid.
_getopt_state="$?"
if [ "$_getopt_state" != 0 ] ; then
_exit_ "1"
fi
eval set -- "$_GETOPT_PARAMS"
while true ; do
case $1 in
--help)
_help_
shift ; _exit_ "0" ;;
*)
if [[ "$2" == "-" ]] || [[ ! -z "$2" ]] ; then
printf "%s: invalid option -- '%s'\\n" "$_init_name" "$2"
_exit_ "1"
# elif [[ -z "$2" ]] ; then break ; fi
else break ; fi
;;
esac
done
# If you run the script in debug mode, the information
# will be displayed on the screen from this point.
if [[ "$stdout_mode" == "debug" ]] ; then
_logger "info" \
"${_FUNCTION_ID}()" \
"starting debug mode"
fi
# Running tasks before start user functions.
_before_init
################################# USER SPACE #################################
# ````````````````````````````````````````````````````````````````````````````
# Put here all your variable declarations, function calls
# and all the other code blocks.
# In this section we add external file (for -c|--config script param).
if [[ "$load_state" -eq 1 ]] ; then _load "head" "$config" ; fi
# shellcheck disable=SC2034
# Generate random value.
_random=$(date +"%s")
# Array that stores the names of variables used that are part of the script
# call parameters (_GETOPT_PARAMS). Useful when checking whether all
# or selected parameters without which the script can not work properly
# have been used. Do not add the load_state variable to the _opt_values array,
# which is supported above.
_opt_values=()
# Checking the value of the variables (if they are unset or empty):
# - variables for call parameters
# - variables from the additional configuration files
if [[ "${#_opt_values[@]}" -ne 0 ]] ; then
for i in "${_opt_values[@]}" ; do
_i="" ; eval _i='$'"$i"
_logger "info" \
"${_FUNCTION_ID}()" \
"$i: '$_i'"
if [[ -z "$_i" ]] ; then
_sprintf "stop" "error of argument value: '$i' is unset or empty"
_logger "stop" \
"${_FUNCTION_ID}()" \
"error of argument value: '$i' is unset or empty"
fi
done
fi
# Only if you used --time, --verbose and --debug script parameters
# - checking the relationship between call parameters.
if [[ "$time_mode" -eq 1 ]] ; then
if [[ "$printf_mode" != "verbose" ]] ; then
_sprintf "stop" "missing argument: '--time' is dependent on '--verbose'"
_logger "stop" \
"${_FUNCTION_ID}()" \
"missing argument: '--time' occurs only with '--verbose'"
fi
elif [[ "$stdout_mode" == "debug" ]] ; then
if [[ "$printf_mode" == "verbose" ]] || [[ "$time_mode" -eq 1 ]] ; then
_sprintf "stop" "debug mode can not occur together with '--verbose' and '--time'"
_logger "stop" \
"${_FUNCTION_ID}()" \
"debug mode can not occur together with '--verbose' and '--time'"
fi
fi
export _cmd_dest
export _cmd_params
export _cmd_report
export _cmd_tor
export _cmd_terminal
# Initialize configuration.
init_config
# shellcheck disable=SC2154
_cmd_dest="$dest"
# shellcheck disable=SC2154
_cmd_params="$params"
# shellcheck disable=SC2154
_cmd_report="$report"
# shellcheck disable=SC2154
_cmd_tor="$tor"
# shellcheck disable=SC2154
_cmd_terminal="$terminal"
init_session
# Initialize modules.
load_modules
# Initialize cli.
clear
init_cli
# ````````````````````````````````````````````````````````````````````````````
if [[ "$time_tracking" == "true" ]] ; then
# Counting the execution time.
_endtime=$(date +%s)
_totaltime=$((_endtime - _begtime))
# Print time header.
printf '\e[m\e[1;39mTOTAL TIME: %dh:%dm:%ds\e[m\n' \
$((_totaltime/3600)) $((_totaltime%3600/60)) $((_totaltime%60))
fi
return "$_STATE"
}