Instructions to set up a basic LAMP+SSH server environment
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
6.8 KiB

5 years ago
  1. #!/bin/bash
  2. # Easily set up a spoofed MAC address to a network interface on systemd-based Linux systems.
  3. # Based on: https://wiki.archlinux.org/index.php/MAC_address_spoofing#Method_1:_systemd-networkd
  4. # Copyright: Pekka Helenius (~Fincer), 2018
  5. # Randomizing/Modifying MAC address may disturb malicious ARP scans (device idenfitication) and may help circumventing MAC-based restrictions which may be present on the local network.
  6. # Use only at your own risk. Spoofing MAC address may not be accepted in all situations, may be illegal and may cause legal troubles. You have been warned.
  7. #genmac() {
  8. trap ctrl_c INT
  9. function ctrl_c() {
  10. echo -e "\nAborting.\n"
  11. exit
  12. #return
  13. }
  14. random_mac() {
  15. # Author: Aaron Toponce
  16. # https://serverfault.com/questions/299556/how-to-generate-a-random-mac-address-from-the-linux-command-line/631119#631119
  17. MAC=$(printf '%02x' $((0x$(od /dev/urandom -N1 -t x1 -An | cut -c 2-) & 0xFE | 0x02)); od /dev/urandom -N5 -t x1 -An | sed 's/ /:/g')
  18. }
  19. insert_mac() {
  20. echo -e "\nChanging MAC address information (root permission required).\n"
  21. sudo sed -i "/\[Link\]/!b;n;cMACAddress=$MAC" /etc/systemd/network/00-default.link
  22. echo -e "MAC address changed from '$MAC_OLD' to '$MAC' for interface '$MAC_DEVICE'.\n\nPlease restart this interface to apply the changes.\n\nTo restore original MAC address, either delete configuration file '/etc/systemd/network/$linkname' or set real MAC address manually into it.\n"
  23. exit 0
  24. #return 0
  25. }
  26. geninsert_mac() {
  27. gennew_mac() {
  28. while true; do
  29. unset response
  30. read -r -p "Generate a new MAC address? [Y/n] " response
  31. if [[ $(echo $response | sed 's/ //g') =~ ^([yY][eE][sS]|[yY])$ ]]; then
  32. random_mac
  33. newname_mac
  34. else
  35. echo -e "\nKeeping old MAC address configuration.\n"
  36. exit
  37. #return
  38. fi
  39. done
  40. }
  41. newname_mac() {
  42. unset response
  43. read -r -p "New MAC address for '$MAC_DEVICE' will be '$MAC'. Accept? [Y/n] " response
  44. if [[ $(echo $response | sed 's/ //g') =~ ^([yY][eE][sS]|[yY])$ ]]; then
  45. insert_mac
  46. else
  47. gennew_mac
  48. fi
  49. }
  50. newname_mac
  51. }
  52. gen_mac() {
  53. real_mac() {
  54. AVAILABLE_MACS=$(ip -br link show | sed '/LOOPBACK/d' | awk '{print NR"\t"$1"\t"$3"\t"$2}')
  55. IFS=$'\n'
  56. echo -e "\nAvailable network interfaces with their MAC addresses are:\n\n${AVAILABLE_MACS[*]}"
  57. echo -e "\nPlease select the interface which MAC address you want to spoof of\n"
  58. read -r -p "Selection [number]: " number
  59. if [[ ! $number =~ ^[0-9]+$ ]]; then
  60. echo -e "\nInvalid input value. Aborting.\n"
  61. exit 1
  62. #return 1
  63. fi
  64. for INTERFACE in $(echo -e "${AVAILABLE_MACS[*]}"); do
  65. intf_num=$(echo $INTERFACE | awk '{print $1}')
  66. if [[ $number -eq $intf_num ]]; then
  67. MAC_REAL=$(echo $INTERFACE | awk '{print $3}')
  68. MAC_DEVICE=$(echo $INTERFACE | awk '{print $2}')
  69. break
  70. fi
  71. done
  72. unset IFS
  73. if [[ $MAC_REAL == "" ]]; then
  74. echo -e "\nNot a valid MAC address found for interface number $number. Aborting.\n"
  75. exit 1
  76. #return 1
  77. fi
  78. }
  79. real_mac
  80. PREV_CONF=$(grep -Ril "$MAC_REAL" /etc/systemd/network/ | grep -E "[0-9]{,2}\-default\.link$")
  81. if [[ ! $(echo $PREV_CONF | wc -w) -eq 0 ]]; then
  82. echo -e "\nUsing existing configuration file for interface '$MAC_DEVICE':\n$PREV_CONF\n"
  83. linkname=$(basename $PREV_CONF)
  84. MAC_OLD=$(awk -F= '/\[Link\]/{getline; print $2}' $PREV_CONF)
  85. else
  86. MAC_OLD=$MAC_REAL
  87. echo -e "\nPrevious configuration file not found. Creating it (root permission required).\n"
  88. read -r -p "Configuration file name? (must follow syntax: 00-default.link, 41-default.link, 98-default.link etc.): " linkname
  89. if [[ "$linkname" =~ ^[0-9]{,2}\-default\.link$ ]]; then
  90. if [[ ! $(sudo -n true) ]]; then
  91. sudo echo ""
  92. fi
  93. echo -e "[Match]\nMACAddress=$MAC_REAL\n\n[Link]\nMACAddress=$MAC_REAL\nNamePolicy=kernel database onboard slot path" \
  94. | sudo tee /etc/systemd/network/$linkname > /dev/null
  95. echo -e "Created new configuration file: /etc/systemd/network/$linkname\n"
  96. else
  97. echo -e "\nInvalid file name given. Aborting.\n"
  98. exit 1
  99. fi
  100. fi
  101. unset response
  102. echo -e "Either randomly generated or manually specified MAC address can be used.\n"
  103. read -r -p "Do you want to use randomly generated MAC address? [Y/n] " response
  104. if [[ $(echo $response | sed 's/ //g') =~ ^([yY][eE][sS]|[yY])$ ]]; then
  105. random_mac
  106. geninsert_mac
  107. else
  108. if [[ $(echo $response | sed 's/ //g') =~ ^([nN][oO]|[nN])$ ]]; then
  109. read -r -p "Please type a new MAC address (Syntax is e.g. aa:bb:33:zz:f0:4a): " MAC
  110. maxtries=5
  111. while [[ $maxtries -gt 0 ]]; do
  112. case "$MAC" in
  113. [[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]])
  114. insert_mac
  115. ;;
  116. esac
  117. unset MAC
  118. read -r -p "Invalid MAC address given. Please type again ($maxtries tries left): " MAC
  119. let maxtries--
  120. done
  121. else
  122. echo -e "\nInvalid answer. Aborting.\n"
  123. fi
  124. fi
  125. }
  126. echo -e "\nWARNING: Changing MAC address WILL DISRUPT connections to any network device using MAC-based authentication methods. These devices may include configured routers, WiFi hotspots etc. Remember to write down the new MAC address, and make sure you are authorized to configure the MAC address to all affected network devices if needed.\n"
  127. read -r -p "You are going to spoof a MAC address of this computer. Are you sure? [Y/n] " response
  128. if [[ $(echo $response | sed 's/ //g') =~ ^([yY][eE][sS]|[yY])$ ]]; then
  129. gen_mac
  130. else
  131. echo -e "\nKeeping old MAC address configuration.\n"
  132. fi
  133. #}