MicrosoftDocs / PowerShell-Docs

The official PowerShell documentation sources
https://learn.microsoft.com/powershell
Creative Commons Attribution 4.0 International
1.93k stars 1.55k forks source link

For loop issue #11088

Closed max9836 closed 2 months ago

max9836 commented 2 months ago

Type of issue

Code doesn't work

Feedback

I was creating a program that brute forces the mac address and try to connect to a network named "XXX". However, I encountered a issue while using the for loop. I am providing my full code for support. Please help me, thank you.

function Connect-ToWiFi {
    param (
        [string]$ssid
    )
    try {
        netsh wlan connect SSID="$ssid" interface="Wi-Fi" name="$ssid"
        Write-Host "Connected to Wi-Fi network: $ssid"
    } catch {
        Write-Host "Failed to connect to Wi-Fi network: $ssid"
    }
}

# Function to set the MAC address in the registry
function Set-MacAddressInRegistry {
    param (
        [string]$newMacAddress
    )
    $regPath = "HKLM:\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\0002" 
    Set-ItemProperty -Path $regPath -Name "NetworkAddress" -Value $newMacAddress
}
$a = "0123456789ABCDEF"
for ($i = 0; $i -lt $a.Length; $i++) {
    for ($j = 0; $j -lt $a.Length; $j++) {
        for ($k = 0; $k -lt $a.Lengths; $k++) {
            for ($l = 0; $l -lt $a.Length; $l++) {
                for ($m = 0; $m -lt $a.Length; $m++) {
                    for ($n = 0; $n -lt $a.Length; $n++) {
                        for ($o = 0; $o -lt $a.Length; $o++) {
                            for ($p = 0; $p -lt $a.Length; $p++) {
                                $macAddress = "02-$($a[$i])$($a[$j])-$($a[$k])$($a[$l])-$($a[$m])$($a[$n])-$($a[$o])$($a[$p])"
                                Write-Host "Attempting to set MAC address in the registry: $macAddress"
                                Set-MacAddressInRegistry -newMacAddress $macAddress
                               Connect-ToWiFi -ssid "XXX"
                            }
                        }
                    }
                }
            }
        }
    }
}

Page URL

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/get-help?view=powershell-7.4

Content source URL

https://github.com/MicrosoftDocs/PowerShell-Docs/blob/main/reference/7.4/Microsoft.PowerShell.Core/Get-Help.md

Author

@sdwheeler

Document Id

dfbcf6a9-b84a-5269-75e5-4623602a0701

### Tasks
sdwheeler commented 2 months ago

This repository is for PowerShell core documentation. You seem to be looking for support, which we can't provide here.

I suggest you try posting your issue with context in one of the available community support forums.

sdwheeler commented 2 months ago
function makeMAC {
    $OFS=''
    for ($i=0; $i -le 0xFFFFFFFFu; $i++) {
        $hex = "{0:X8}" -f $i
        "02-00-$($hex[0..1])-$($hex[2..3])-$($hex[4..5])-$($hex[6..7])"
    }
}