-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.sh
More file actions
executable file
·301 lines (273 loc) · 9.86 KB
/
Copy pathlib.sh
File metadata and controls
executable file
·301 lines (273 loc) · 9.86 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
300
301
#!/usr/bin/env bash
# Shared helpers — source this file, do not execute directly.
# Colors
if tput setaf 1 >/dev/null 2>&1; then
RED="$(tput setaf 1)$(tput bold)"
GREEN="$(tput setaf 2)$(tput bold)"
YELLOW="$(tput setaf 3)$(tput bold)"
CYAN="$(tput setaf 6)$(tput bold)"
BOLD="$(tput bold)"
DIM="$(tput dim 2>/dev/null || printf '\033[2m')"
RESET="$(tput sgr0)"
else
RED='' GREEN='' YELLOW='' CYAN='' BOLD='' DIM='' RESET=''
fi
# ── Watch Dogs / ctOS visual helpers ─────────────────────────────────────────
# banner <TITLE> [subtitle] — ctOS style tool header box
banner() {
local title="$1" sub="${2:-}"
printf '\n'
printf ' %s╔══════════════════════════════════════════════╗%s\n' "${CYAN}${BOLD}" "${RESET}"
printf ' %s║ ▶ %s%s\n' "${CYAN}${BOLD}" "${title}" "${RESET}"
[[ -n "$sub" ]] && printf ' %s║ %s%s%s\n' "${CYAN}" "${DIM}" "${sub}" "${RESET}"
printf ' %s╚══════════════════════════════════════════════╝%s\n' "${CYAN}${BOLD}" "${RESET}"
printf '\n'
}
# section <title> — styled section marker
section() {
printf '\n %s▶ %s%s\n' "${CYAN}${BOLD}" "$*" "${RESET}"
printf ' %s──────────────────────────────────────────────%s\n' "${DIM}" "${RESET}"
}
# Spinner — wrap slow operations: start_spin <msg> … stop_spin
_spin_pid=""
start_spin() {
local msg="$1"
( local frames=('⠋' '⠙' '⠹' '⠸' '⠼' '⠴' '⠦' '⠧' '⠇' '⠏')
local i=0
while true; do
printf "\r \033[1;36m%s\033[0m %s" "${frames[$i]}" "$msg"
sleep 0.12
i=$(( (i + 1) % 10 ))
done ) &
_spin_pid=$!
}
stop_spin() {
[[ -z "${_spin_pid:-}" ]] && return 0
kill "$_spin_pid" 2>/dev/null || true
wait "$_spin_pid" 2>/dev/null || true
printf '\r\033[K'
_spin_pid=""
}
# _ip_to_network <ip> <prefix>
# Pure-bash: compute network address from host IP + prefix length.
# e.g. _ip_to_network 192.168.68.92 22 -> 192.168.68.0/22
_ip_to_network() {
local ip="$1" prefix="$2"
local -i a b c d
IFS=. read -r a b c d <<< "$ip"
local -i full=$(( (a<<24) | (b<<16) | (c<<8) | d ))
local -i mask=$(( prefix > 0 ? (0xFFFFFFFF << (32 - prefix)) & 0xFFFFFFFF : 0 ))
local -i net=$(( full & mask ))
printf "%d.%d.%d.%d/%d\n" \
$(( (net>>24)&0xFF )) $(( (net>>16)&0xFF )) \
$(( (net>>8)&0xFF )) $(( net&0xFF )) \
"$prefix"
}
# _ip_usable: returns 0 only if 'ip' exists AND can actually query addresses
# (on Android rootless, 'ip' exists but fails with "Cannot bind netlink socket")
_ip_usable() {
command -v ip &>/dev/null && ip -o -4 addr show 2>/dev/null | grep -q .
}
# list_ifaces
# Prints tab-separated "iface <TAB> network/prefix" for every non-loopback IPv4 interface.
list_ifaces() {
if _ip_usable; then
# ip -o -4 addr show gives lines like:
# 2: wlan0 inet 192.168.68.92/22 brd ...
ip -o -4 addr show 2>/dev/null | while IFS= read -r line; do
local iface host_cidr
iface=$(echo "$line" | awk '{print $2}')
host_cidr=$(echo "$line" | awk '{print $4}')
[[ "$iface" == "lo" || -z "$host_cidr" ]] && continue
local host prefix
host="${host_cidr%%/*}"
prefix="${host_cidr##*/}"
[[ -z "$prefix" || "$prefix" == "$host" ]] && prefix=24
local net
net=$(_ip_to_network "$host" "$prefix")
printf "%s\t%s\n" "$iface" "$net"
done
else
# ifconfig fallback — Android/busybox format
local cur_iface=""
ifconfig 2>/dev/null | while IFS= read -r line; do
# Interface line: "wlan0: flags=..." or "wlan0 Link encap:..."
if echo "$line" | grep -qE '^[a-zA-Z][a-zA-Z0-9_.-]+'; then
cur_iface=$(echo "$line" | awk -F'[ :]' '{print $1}')
fi
# inet line: " inet 192.168.1.5 netmask 255.255.255.0 ..."
if echo "$line" | grep -q 'inet ' && [[ "$cur_iface" != "lo" ]]; then
local host mask
host=$(echo "$line" | grep -oE 'inet [0-9.]+' | awk '{print $2}')
mask=$(echo "$line" | grep -oE 'netmask [0-9.]+' | awk '{print $2}')
if [[ -n "$host" ]]; then
local prefix=24
if [[ -n "$mask" ]]; then
# Convert dotted netmask to prefix length
local IFS=.
read -r m1 m2 m3 m4 <<< "$mask"
local -i bits=0
for oct in $m1 $m2 $m3 $m4; do
local x=$oct
while (( x > 0 )); do
(( bits += x & 1 ))
(( x >>= 1 ))
done
done
prefix=$bits
fi
local net
net=$(_ip_to_network "$host" "$prefix")
printf "%s\t%s\n" "$cur_iface" "$net"
fi
fi
done
fi
}
# _get_addr <iface>
# Returns the IPv4 address for the given interface using whichever tool works.
# On Android rootless, ifconfig <iface> fails — parse full ifconfig output instead.
_get_addr() {
local _iface="$1"
if _ip_usable; then
ip -o -4 addr show "$_iface" 2>/dev/null \
| awk '{print $4}' | cut -d/ -f1 | head -1
else
ifconfig 2>/dev/null | awk -v iface="$_iface" '
/^[a-zA-Z]/ { cur = $1; gsub(/:/, "", cur) }
cur == iface && /inet / {
for (i=1; i<=NF; i++) if ($i == "inet") { print $(i+1); exit }
}
'
fi
}
# get_ip [interface]
# Returns the IPv4 address (no prefix) for the given interface, or the first
# active non-loopback interface if no argument is given.
get_ip() {
local iface="${1:-}"
local addr=""
if [[ -n "$iface" ]]; then
addr=$(_get_addr "$iface")
[[ -n "$addr" ]] && echo "$addr" && return
fi
# Try common NetHunter / mobile interfaces in order
for candidate in wlan0 wlan1 eth0 eth1 usb0 usb1 rndis0 tun0; do
addr=$(_get_addr "$candidate")
if [[ -n "$addr" ]]; then
echo "$addr"
return
fi
done
# Last resort: any non-loopback interface
if _ip_usable; then
addr=$(ip -o -4 addr show 2>/dev/null \
| awk '$2 != "lo" {print $4}' | cut -d/ -f1 | head -1)
else
addr=$(ifconfig 2>/dev/null | awk '
/^[a-zA-Z]/ { cur = $1; gsub(/:/, "", cur) }
cur != "lo" && /inet / {
for (i=1; i<=NF; i++) if ($i == "inet") { print $(i+1); exit }
}
')
fi
echo "${addr:-N/A}"
}
# require_tool <tool> [install-hint]
# Exits with an error if the tool is not found in PATH.
require_tool() {
local tool="$1"
local hint="${2:-apt install $tool}"
if ! command -v "$tool" &>/dev/null; then
echo "${RED}[!] '$tool' not found. Install it with: $hint${RESET}" >&2
exit 1
fi
}
# check_tool <tool>
# Returns 0 if found, 1 if not (non-fatal — lets callers decide).
check_tool() {
command -v "$1" &>/dev/null
}
# make_outdir
# Creates and prints a timestamped results directory under ./results/.
make_outdir() {
local dir="results/$(date '+%Y-%m-%d_%H-%M-%S')"
mkdir -p "$dir"
echo "$dir"
}
# pick_nmap_file
# Offers to load an existing nmap.txt from a results/ session instead of re-scanning.
# Prints "session_dir|nmap_path" to stdout on success, empty string if skipped/none found.
# All user-facing output goes to stderr so stdout can be captured with $(...).
pick_nmap_file() {
local -a _pnf_sessions=()
while IFS= read -r _d; do
[[ -f "${_d}nmap.txt" ]] && _pnf_sessions+=("$_d")
done < <(ls -1dt results/*/ 2>/dev/null || true)
if [[ ${#_pnf_sessions[@]} -eq 0 ]]; then
echo ""; return
fi
printf ' %s>>%s Re-use an existing nmap.txt? [y/N]: ' "${CYAN}" "${RESET}" >&2
read -r _pnf_ans </dev/tty
if [[ "${_pnf_ans,,}" != "y" ]]; then
echo ""; return
fi
printf '\n %s[+]%s Sessions with nmap.txt:\n\n' "${GREEN}" "${RESET}" >&2
local _pnf_i
for _pnf_i in "${!_pnf_sessions[@]}"; do
local _pnf_ts="${_pnf_sessions[$_pnf_i]%/}"; _pnf_ts="${_pnf_ts##*/}"
printf ' %s[%02d]%s %s\n' "${CYAN}" "$(( _pnf_i + 1 ))" "${RESET}" "$_pnf_ts" >&2
done
printf '\n %s>>%s Select [1]: ' "${CYAN}" "${RESET}" >&2
read -r _pnf_pick </dev/tty
_pnf_pick="${_pnf_pick:-1}"
if ! [[ "$_pnf_pick" =~ ^[0-9]+$ ]] || \
(( _pnf_pick < 1 || _pnf_pick > ${#_pnf_sessions[@]} )); then
printf ' %s[!]%s Invalid selection — running fresh scan.\n\n' "${RED}" "${RESET}" >&2
echo ""; return
fi
local _pnf_dir="${_pnf_sessions[$(( _pnf_pick - 1 ))]}"
printf ' %s[✔]%s Using %s%snmap.txt%s\n\n' \
"${GREEN}" "${RESET}" "${DIM}" "$_pnf_dir" "${RESET}" >&2
printf '%s|%snmap.txt' "${_pnf_dir%/}" "$_pnf_dir"
}
# prompt_target
# Prints the chosen target network CIDR or IP.
# Reads TARGET env var if already set, otherwise shows active interfaces.
prompt_target() {
if [[ -n "${TARGET:-}" ]]; then
echo "$TARGET"
return
fi
echo "${YELLOW}[?] Target selection:${RESET}" >&2
# Build list of active interfaces
local -a iface_arr=()
local -a net_arr=()
local idx=1
while IFS=$'\t' read -r _iface _net; do
[[ -z "$_iface" || -z "$_net" ]] && continue
iface_arr+=("$_iface")
net_arr+=("$_net")
local _host
_host=$(get_ip "$_iface")
echo " ${idx}) ${_iface} — ${_host} → ${_net}" >&2
(( idx++ ))
done < <(list_ifaces 2>/dev/null)
echo " ${idx}) Enter manually" >&2
read -rp " Choice [1]: " _choice </dev/tty
_choice="${_choice:-1}"
if [[ "$_choice" == "$idx" ]] || [[ "$_choice" == "m" ]]; then
read -rp " Enter IP or CIDR (e.g. 192.168.1.0/24 or 10.0.0.5): " _manual </dev/tty
echo "$_manual"
elif [[ "$_choice" =~ ^[0-9]+$ ]] && (( _choice >= 1 && _choice < idx )); then
echo "${net_arr[$((_choice - 1))]}"
else
# Fallback: first detected network, or manual
if [[ ${#net_arr[@]} -gt 0 ]]; then
echo "${net_arr[0]}"
else
read -rp " No interfaces detected. Enter target manually: " _manual </dev/tty
echo "$_manual"
fi
fi
}