random mac addr option

This commit is contained in:
garywill 2020-12-24 20:36:09 +08:00
parent b993285995
commit d3eaf9c71b
2 changed files with 42 additions and 13 deletions

View File

@ -245,6 +245,7 @@ Options:
hosts file hosts file
--mac <MAC> Set MAC address --mac <MAC> Set MAC address
--random-mac Use random MAC address
--tp <port> Transparent proxy, --tp <port> Transparent proxy,
redirect non-LAN TCP and UDP traffic to port. redirect non-LAN TCP and UDP traffic to port.
@ -253,7 +254,8 @@ Options:
Wifi hotspot options: Wifi hotspot options:
--ap <wifi interface> <SSID> --ap <wifi interface> <SSID>
Create Wifi access point Create Wifi access point
--password <password> Wifi password -p, --password <password>
Wifi password
--hidden Hide access point (not broadcast SSID) --hidden Hide access point (not broadcast SSID)
--no-virt Do not create virtual interface --no-virt Do not create virtual interface
@ -307,17 +309,14 @@ Options:
- iproute2 - iproute2
- dnsmasq - dnsmasq
- iptables - iptables
- WiFi hotspot dependencies
Wifi hotspot: - hostapd
- iw
- hostapd - iwconfig (you only need this if 'iw' can not recognize your adapter)
- iw - haveged (optional)
- iwconfig (you only need this if 'iw' can not recognize your adapter)
- haveged (optional)
## TODO ## TODO
- Option to randomize MAC
- Explictly ban forwarding if not needed - Explictly ban forwarding if not needed
## Donate ## Donate

View File

@ -58,6 +58,7 @@ Options:
hosts file hosts file
--mac <MAC> Set MAC address --mac <MAC> Set MAC address
--random-mac Use random MAC address
--tp <port> Transparent proxy, --tp <port> Transparent proxy,
redirect non-LAN TCP and UDP traffic to port. redirect non-LAN TCP and UDP traffic to port.
@ -66,7 +67,8 @@ Options:
Wifi hotspot options: Wifi hotspot options:
--ap <wifi interface> <SSID> --ap <wifi interface> <SSID>
Create Wifi access point Create Wifi access point
--password <password> Wifi password -p, --password <password>
Wifi password
--hidden Hide access point (not broadcast SSID) --hidden Hide access point (not broadcast SSID)
--no-virt Do not create virtual interface --no-virt Do not create virtual interface
@ -111,8 +113,8 @@ Options:
Examples: Examples:
$PROGNAME -i eth1 $PROGNAME -i eth1
$PROGNAME --ap wlan0 MyAccessPoint $PROGNAME --ap wlan0 MyAccessPoint
$PROGNAME --ap wlan0 MyAccessPoint --password MyPassPhrase $PROGNAME --ap wlan0 MyAccessPoint -p MyPassPhrase
$PROGNAME -n --ap wlan0 MyAccessPoint --password MyPassPhrase $PROGNAME -n --ap wlan0 MyAccessPoint -p MyPassPhrase
$PROGNAME -i eth1 --tp <transparent-proxy> --dns <dns-proxy> $PROGNAME -i eth1 --tp <transparent-proxy> --dns <dns-proxy>
EOF EOF
} }
@ -145,6 +147,7 @@ SHARE_METHOD=nat
TP_PORT= TP_PORT=
DNS= DNS=
USE_RANDOM_MAC=0
NEW_MACADDR= NEW_MACADDR=
OLD_MACADDR= OLD_MACADDR=
DAEMONIZE=0 DAEMONIZE=0
@ -244,6 +247,10 @@ while [[ -n "$1" ]]; do
NEW_MACADDR="$1" NEW_MACADDR="$1"
shift shift
;; ;;
--random-mac)
shift
USE_RANDOM_MAC=1
;;
--dns) --dns)
shift shift
@ -303,7 +310,7 @@ while [[ -n "$1" ]]; do
SSID="$1" SSID="$1"
shift shift
;; ;;
--password) -p|--password)
shift shift
PASSPHRASE="$1" PASSPHRASE="$1"
shift shift
@ -622,6 +629,26 @@ get_new_macaddr() {
echo $NEWMAC echo $NEWMAC
} }
generate_random_mac() {
local r1 r2 r3 r4 r5 r6
while :; do
r1=$( printf "%02x" $(($RANDOM%256/4*4)) )
r2=$( printf "%02x" $(($RANDOM%256)) )
r3=$( printf "%02x" $(($RANDOM%256)) )
r4=$( printf "%02x" $(($RANDOM%256)) )
r5=$( printf "%02x" $(($RANDOM%256)) )
r6=$( printf "%02x" $(($RANDOM%256)) )
RAND_MAC="$r1:$r2:$r3:$r4:$r5:$r6"
( ! ip link | grep "link" | grep $RAND_MAC > /dev/null 2>&1 ) && \
( ! ip maddress | grep "link" | grep $RAND_MAC > /dev/null 2>&1 ) && \
( ! ip neigh | grep "lladdr $RAND_MAC" > /dev/null 2>&1 ) && \
( ! get_all_macaddrs | grep $RAND_MAC ) && \
break
done
NEW_MACADDR=$RAND_MAC
}
is_ip4_range_available() { is_ip4_range_available() {
( ip -4 address | grep "inet 192\.168\.$1\." > /dev/null 2>&1 ) && return 1 ( ip -4 address | grep "inet 192\.168\.$1\." > /dev/null 2>&1 ) && return 1
( ip -4 route | grep "^192\.168\.$1\." > /dev/null 2>&1 ) && return 1 ( ip -4 route | grep "^192\.168\.$1\." > /dev/null 2>&1 ) && return 1
@ -1331,6 +1358,8 @@ if [[ $WIFI_IFACE ]]; then
fi fi
[[ "$USE_RANDOM_MAC" -eq 1 ]] && generate_random_mac
if [[ -n "$NEW_MACADDR" ]]; then if [[ -n "$NEW_MACADDR" ]]; then
if ! is_unicast_macaddr "$NEW_MACADDR"; then if ! is_unicast_macaddr "$NEW_MACADDR"; then
echo "ERROR: The first byte of MAC address (${NEW_MACADDR}) must be even" >&2 echo "ERROR: The first byte of MAC address (${NEW_MACADDR}) must be even" >&2
@ -1359,6 +1388,7 @@ else
fi fi
echo "Target interface is ${TARGET_IFACE}" echo "Target interface is ${TARGET_IFACE}"
[[ "$USE_RANDOM_MAC" -eq 1 ]] && echo "Use random MAC address $NEW_MACADDR"
if [[ ! -n $GATEWAY ]]; then if [[ ! -n $GATEWAY ]]; then
generate_random_ip4 generate_random_ip4