1

Following @Asian's question and my recent interest in powershell, I tried to replicate the script I provided to answer Asian's question, however I am not having the same success that I did with the batch script. If you do not want to view the previous question, here is my batch-file for changing your MAC Address:

@echo off
dism >nul
if %errorlevel% NEQ 0 goto Elevate
(call )
netsh interface set interface Wi-Fi disable
timeout /t 1 /nobreak >null
netsh interface set interface Wi-Fi enable
choice /c RC /m "Would you like to randomize your MAC adress or customize it?"
if %Errorlevel% EQU 2 goto custom
set loopcount=5
:loop
set /a loopcount=loopcount-1
if %loopcount% LEQ 0 (goto exitloop)
set /a "ascii = %random% * 26 / 32768 + 65"
cmd /c exit /b %ascii%
set "rl1=%rl1%%=ExitCodeAscii%"
goto loop
:exitloop
set MAC="0E%random:~0,2%%rl1:~0,2%%random:~0,2%%rl1:~3,2%%rl1:~-1%%random:~0,1%"
goto after
:custom
echo What would you like to change your MAC address to?
echo Remember to always have the second digit of your MAC address to always be a 2, 6, A, or E
echo Format: AABBCCDDEEFF
echo/
set /p MAC="Input your MAC address here (no spaces or hyphens)> "
:after
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\0011" /v NetworkAddress /d %MAC% /f >null
netsh interface set interface Wi-Fi disable
timeout /t 1 /nobreak >null
netsh interface set interface Wi-Fi enable
echo Operation Successful
echo %mac% is your new MAC address
pause
goto :eof
:Elevate
Echo Error: The requested operation requires elevation
Echo Run file again as admin
Echo Closing file in 10 seconds...
timeout /t 10 /nobreak >nul
goto :eof

I tried to replicate it in powershell, however the script is very volatile on whether it works or not:

[string]$admin = [bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544")
if ($admin -eq "False"){
echo "Error: The requested operation requires elevation`nRun file again as administrator"
pause
exit}
rv * -ErrorAction SilentlyContinue
echo "This file will temporarily disable your Wi-Fi adapter"
While (!($C1)){
$choice1 = Read-Host -Prompt "Would you like to proceed? [Y/N]"
switch ($choice1) {
   "N" {echo "Ok, press any key to exit this file:"
        cmd /c pause > $null; exit}
   "Y" {echo "Ok, proceeding with operation"; [int]$C1 = 1; break}
   }
if (!($C1)){echo "Invalid input"}
}
rv * -ErrorAction SilentlyContinue

netsh interface set interface Wi-Fi disable; 

$getwifi = netsh interface show interface | findstr "Wi-Fi"
if ($getwifi.substring(0,7) -ne "disable"){echo "Unexpected error: Press any key to exit"
                                           cmd /c pause > $null; exit}
echo "Wi-Fi has been succesfully disabled, proceeding with operation"

While (!($C1)){
    $choice1 = Read-Host -Prompt "Would you like to randomize your MAC Address or customize it? [R/C]"
    switch ($choice1) {
        "R" {
            $test = @(...) | get-random
            <# $test is a random value in a list of 25000+ MAC Address vendor codes provided in 
            https://gitlab.com/wireshark/wireshark/raw/master/manuf with the colons removed and columns 
            besides the MAC column removed as well #>
            $test2 = [string](get-random -minimum 10 -maximum 99)+(-join ((65..90) | Get-Random -Count 2 | % {[char]$_}));
            $test3 = [string](get-random -minimum 1 -maximum 9)+(-join ((65..90) | Get-Random -Count 1 | % {[char]$_}));
            $MAC = $test + $test2 + $test3;
            $C1 = 1}
        "C" {$C1 = 2}
    }
if (!($C1)){echo "Invalid input"}
}

if(!($MAC)){
do{
echo "What would you like to change your MAC address to?`nRemember to always have the second digit of your MAC address to always be a number`nFormat: 11BBCCDDEEFF";
$MAC = read-host -prompt "Input your MAC address here [no spaces or hyphens]";
if ($MAC.length -eq 12){$C1 = 1};
if (!($MAC.length -eq 12)){echo "Invalid input: Follow the format"; rv MAC}
} while(!($MAC))
}
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\0011" /v NetworkAddress /d $MAC /f >$null
netsh interface set interface Wi-Fi enable
echo "Operation Successful"
echo "$MAC is your new MAC address"
pause

Again, $test is a random value in a list of 25000+ MAC Address vendor codes provided in https://gitlab.com/wireshark/wireshark/raw/master/manuf with the colons removed and columns besides the MAC column removed as well. I believe the issue is in the $test variable where the computer will just reject a certain MAC address. If this is not the issue, can someone explain to me what is the issue, and if there are certain codes that will always work in changing the MAC address successfully, please provide me them. Thank you in advance.

Some addresses that work include:

  • AABBCCDDEEFF
  • 001122334455
  • 00AA11BB22CC
  • Other variants of 00----------
  • 6E80F12CC8C9

I will write more sample addresses down when I test them.

Nico Nekoru
  • 2,402
  • 1
  • 15
  • 32
  • 1
    I don't know the actual problem since the code is executing fine but sometimes my MAC address does not change as a result of the code – Nico Nekoru May 18 '20 at 04:58
  • 2
    Can you share a few mac addresses that a) do work and b) do not work, so this could be tested on different systems too? – vonPryz May 18 '20 at 05:54
  • my previous comment stepped across the line into a rant. if you had the misfortune to read it before i removed it, please accept my apology. [*blush*] – Lee_Dailey May 18 '20 at 06:00
  • your use of `rv * -ErrorAction SilentlyContinue` is _horribly dangerous_. it removes all variables ... and that includes some of the automatic variables. it is known to cause problems. **_please remove it and test again._** – Lee_Dailey May 18 '20 at 06:01
  • on the subject of needed/unneeded code ... [1] the user input is _totally unneeded_. [2] the elevation check is unneeded - just add a note to run it elevated OR use the `$requires -RunAsAdministrotor` option. – Lee_Dailey May 18 '20 at 06:03
  • 1
    @Lee_Dailey Why is the user input unneeded? Thank you for your suggestion, I will try that – Nico Nekoru May 18 '20 at 06:06
  • 1
    The `#requires -runasadministrator` worked perfectly, thank you. Is there an alternitave to `rv *` that refreshes all variables and deletes all user defined variables? – Nico Nekoru May 18 '20 at 06:10
  • 1
    @NekoMusume - instead of destroying the working setup of your powershell session by deleting ALL the variables, simply initialize the $Vars that you use at the start of your script. normally, all the $Vars that you create start off blank, so you normally only need to explicitly initialize things when you re-run the code **_in the same session_**. since your code likely is run in the console when doting things for real, the session is always new there. – Lee_Dailey May 18 '20 at 09:13
  • @NekoMusume - "user input" is not needed to fix the glitch you describe. that part is entirely UI stuff and has NO effect on the occasional glitch that you have mentioned. – Lee_Dailey May 18 '20 at 09:14
  • 1
    If I had another variable defined with the same name as one of the `while` loops, it just entirely skips it ruining my whole code so I remove all variables – Nico Nekoru May 18 '20 at 15:12

1 Answers1

2

Try macaddr=$(echo $FQDN|md5sum|sed 's/^\(..\)\(..\)\(..\)\(..\)\(..\).*$/02:\1:\2:\3:\4:\5/') &> /dev/null && echo macaddr. This will generate a random MAC address and echo it to you. This only works in Ubuntu/Bash though.

You can also do (1..12 | %{ '{0:X}' -f (Get-Random -Max 16) }) -join '' in PowerShell.

Todd
  • 422
  • 4
  • 10
  • Is there a way to do this is powershell or batch? – Nico Nekoru May 18 '20 at 22:09
  • ```(1..12 | %{ '{0:X}' -f (Get-Random -Max 16) }) -join ''``` in PowerShell. – Todd May 18 '20 at 22:11
  • So this creates a random 12 digit hexadecimal number? – Nico Nekoru May 18 '20 at 22:11
  • Yes. That is what it does. – Todd May 18 '20 at 22:12
  • 1
    This has worked 3 times in a row for me, doesn't seem like a fluke. Mac addresses that work: FAC9A113412D, 0268B329DA98, and 03D5CFA0E45E – Nico Nekoru May 18 '20 at 22:17
  • UPDATE: My friend showed me another bash command that works: `$MAC = bash -c "openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'"` and then `$MAC = $MAC -replace ":”`. Same concept, also works – Nico Nekoru May 20 '20 at 19:40
  • This will not always work. You will need openssl to do this which isn't always installed. You will first need to do ```sudo apt install openssl``` on Debian based systems such as Ubuntu and Kali Linux or the equivalent on other systems if the command isn't installed. – Todd May 20 '20 at 20:40
  • It seems as it is pre-installed on wsl since I did not need to do anything extra to get it to work in powershell – Nico Nekoru May 20 '20 at 21:01
  • I was talking about minimal versions of Ubuntu, Debian, etc. which don't always come with it. PowerShell can do it because you already have the Ubuntu app installed and WSL enabled. – Todd May 20 '20 at 21:03