-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmac.sh
executable file
·269 lines (214 loc) · 7.17 KB
/
nmac.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
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
#!/bin/zsh
# This script is written by snw7 and licensed under MIT License.
# Check if script has sudo privileges
if [ "$EUID" -ne 0 ]; then
echo "\nThis script requires sudo privileges. Please run with sudo.\n"
exit 1
fi
# Load config
readonly CURRENT_PATH="$(dirname "$0")"
path_to_config="$CURRENT_PATH/.config"
if [ ! -f "$path_to_config" ]; then
printf "\nThe config file doesn't exist. Please update the path in the script.\n\n"
exit 1
fi
readonly PRESET_MAC=$(awk -F'=' '/^preset_mac=/ { print $2}' $path_to_config)
readonly PRESET_HOST=$(awk -F'=' '/^preset_host=/ { print $2}' $path_to_config)
readonly ORIGINAL_MAC=$(awk -F'=' '/^original_mac=/ { print $2}' $path_to_config)
readonly ORIGINAL_HOST=$(awk -F'=' '/^original_host=/ { print $2}' $path_to_config)
readonly HOSTLIST="$CURRENT_PATH/$(awk -F'=' '/^hostlist=/ { print $2}' $path_to_config)"
readonly VENDORLIST="$CURRENT_PATH/$(awk -F'=' '/^vendorlist=/ { print $2}' $path_to_config)"
if [[ ! -n $PRESET_MAC ]] || [[ ! -n $PRESET_HOST ]] || [[ ! -n $ORIGINAL_MAC ]] || [[ ! -n $ORIGINAL_HOST ]];then
echo "\nERROR: Your .config file is incomplete. Please check for missing values.\nTERMINATING\n"
exit 1
fi
if [[ $HOSTLIST == $CURRENT_PATH"/" ]] || [[ ! -f $HOSTLIST ]];then
echo "\nERROR: Hostlist file is missing. Please add it to your .config file and save it to the defined relative path.\nTERMINATING\n"
exit 1
fi
if [[ $VENDORLIST == $CURRENT_PATH"/" ]] || [[ ! -f $VENDORLIST ]];then
echo "\nERROR: Vendorlist file is missing. Please add it to your .config file and save it to the defined relative path.\nTERMINATING\n"
exit 1
fi
# Parse options
while getopts ":nrphmds:" opt; do
case $opt in
n)
option='n'
;;
r)
arg_mac="$ORIGINAL_MAC"
arg_host="$ORIGINAL_HOST"
option='r'
;;
m)
option='m'
;;
s)
arg_mac="$OPTARG"
shift 1
arg_host="$2"
shift 1
option='s'
;;
d)
arg_mac="$PRESET_MAC"
arg_host="$PRESET_HOST"
option='d'
;;
h)
option='h'
;;
p)
# dont echo mac and ip
in_public="true"
;;
*)
echo "Invalid option: -$OPTARG"
exit 1
;;
esac
done
if [[ $# -eq 0 ]]; then
echo '\nnmac 1.1\nType "-h" for more information.\n'
exit 1
fi
# Help
if [[ $option == 'h' ]];then
echo " written by snw7 (08/2023)
this programm is used to change MAC, IP and HOSTNAME of a device.
OPTIONS:
-h get this help menu
-d change IP, MAC and HOSTNAME to preset device
-m stealth mode - set to realistic consumer host
-n change IP, MAC and HOSTNAME to random
-r change IP, MAC and HOSTNAME to original
-s MAC HOSTNAME set MAC and HOSTNAME to custom values
-p add to other option for \"in_public\" mode - f.E. (-np)
For mode '-m' save a file with hostnames as 'hostlist.txt' to the directory defined in your .config file.\n"
exit 1
fi
if [[ ! $option ]];then
echo '\nERROR: No option defined.\nTERMINATING\n'
exit 1
fi
## GENERATE MAC address
vendor_name="-"
if [[ $option == 'n' ]] || [[ $option == 'm' ]];then
vendor=$(shuf -n 1 $VENDORLIST)
vendor_prefix=$(echo $vendor | cut -d ',' -f1)
vendor_name=$(echo "$vendor" | awk -F ',' '{if ($2 ~ /^".*"$/) {gsub(/"/, "", $2); print $2} else {print $2}}'|sed 's/^"//')
vendor_second_half=",$(echo "$vendor" | awk -F '"' '{print $2}' | awk -F ',' '{print $2}')"
if [[ $vendor_second_half != "," ]]; then
vendor_name="$vendor_name$vendor_second_half"
fi
random_mac=$(openssl rand -hex 4 | sed 's/\(..\)/\1:/g; s/.$//')
max_length=$((17 - ${#vendor_prefix}))
mac="${vendor_prefix}${random_mac: -$max_length}"
elif [[ $option == 's' ]] || [[ $option == 'd' ]] || [[ $option == 'r' ]];then
mac=$arg_mac
fi
## COMPUTE HOSTNAME
if [[ $option == 'm' ]];then
# fetch random from file
arg_host=$(shuf -n 1 $HOSTLIST)
fi
if [[ $option == 'n' ]];then
newHostname="PC-${mac[-17,-1]//[:]/-}"
fi
if [[ $option == 's' ]] || [[ $option == 'd' ]]|| [[ $option == 'm' ]] || [[ $option == 'r' ]];then
newHostname=$arg_host
fi
## GET old IP, MAC
ip_address_old=$(ifconfig en0 | awk '/inet / {print $2}') # ipconfig getifaddr en0
mac_address_old=$(ifconfig en0 | grep ether)
###
# Start network activity
###
ssid=$(sudo wdutil info|awk -F': ' '/ SSID/{print $2}')
if [[ $(sudo wdutil info | grep -A 2 "MAC Address" | awk -F': ' '/Power/{print $2}') == "Off [Off]" ]]; then
previous_wifi_state="down";
else
previous_wifi_state="up";
fi
networksetup -setairportpower en0 off
networksetup -setairportpower en0 on
sudo ifconfig en0 up
# CHANGE mac
sudo ifconfig en0 ether $mac
# GET new MAC
mac_address_new=$(ifconfig en0 | grep ether)
# change HOSTNAME
sudo scutil --set ComputerName $newHostname
sudo scutil --set HostName $newHostname
sudo scutil --set LocalHostName $newHostname
dscacheutil -flushcache
# reset IPv4
sudo ipconfig set en0 DHCP
sudo ipconfig set en0 BOOTP
#reset IPv6
sudo networksetup -setv6off Wi-Fi
sudo networksetup -setv6automatic Wi-Fi
if [[ $previous_wifi_state == "up" ]] && [[ $ssid != "None" ]]; then
# reconnect to wifi
for count in {0..15}; do
# initiate reconnect every 7s
if [[ $count -eq 0 || $((count % 8)) -eq 0 ]]; then
networksetup -setairportnetwork en0 $ssid> /dev/null
fi
ip_address_new=$(ifconfig en0 | awk '/inet / {print $2}')
# terminate when new IP is read or waiting time exceeds 15s
if [[ -n "${ip_address_new// }" ]]; then
break
fi
sleep 1
done
new_ssid=$(sudo wdutil info|awk -F': ' '/ SSID/{print $2}')
if [[ $new_ssid != "None" ]]; then
state="Reconnected to: $new_ssid\n"
else
state="Failed to reconnect to: $ssid\n"
fi
else
# disable wifi for enhanced privacy
networksetup -setairportpower en0 off
fi
###
# PRINT changes
###
hostname=$(hostname)
if [[ $mac_address_new != $mac_address_old ]]; then
mac_changed="true"
fi
if [[ $mac_changed != "true" ]]; then
vendor_name="-"
fi
if [[ $in_public == "true" ]]; then
mac_address_old="00:00:00:00:00:00"
mac_address_new="00:00:00:00:00:00"
ip_address_old="0.0.0.0"
ip_address_new="0.0.0.0"
hostname="-"
vendor_name="-"
fi
echo "\nnew HOSTNAME: $hostname"
echo "\nold MAC: ${mac_address_old[-17,-1]}" # -18, -2 (before macOS SONOMA)
echo "new MAC: ${mac_address_new[-17,-1]}"
if [[ $mac_changed == "true" ]] && [[ $in_public == "true" ]]; then
echo "-> changed"
fi
echo "\nVendor: $vendor_name\n"
if [[ $ssid != 'None' ]]; then
echo "old IP: ${ip_address_old}"
echo "new IP: ${ip_address_new}\n"
fi
if [[ -n $state ]]; then
echo $state;
fi
if [[ $option == 'n' ]] || [[ $option == 'm' ]];then
echo "Identity switched.\n"
elif [[ $option == 'r' ]];then
echo "Identity reset.\n"
elif [[ $option == 's' ]] || [[ $option == 'd' ]];then
echo "Identity cloned.\n"
fi