-1

Im making a app that show's the key of all WLAN profiles on the computer.

    '' GET PROFILES
    Dim temp_direct As String = My.Computer.FileSystem.SpecialDirectories.Temp
    Dim cmd As String = "Netsh WLAN show profiles > " & temp_direct & "\profiles.txt"

    Shell("cmd.exe /c " + cmd, AppWinStyle.Hide, True)

    '' SHOW PROFILES
    Dim texto As String = File.ReadAllText(temp_direct & "\profiles.txt").Replace(" ", "")
    Dim pattern As String = "AllUserProfile:(.*)"
    Dim matches As MatchCollection = Regex.Matches(texto, pattern)

    For Each m As Match In matches
        For Each c As Capture In m.Captures
            RichTextBox1.AppendText("SSID: " & m.Groups(1).Value)

            '' SAVE PROFILE WITH CLEAR KEY
            Dim cmd_info As String = "Netsh WLAN show profiles " & m.Groups(1).Value & " key=clear" & " > " & temp_direct & "\" & m.Groups(1).Value & ".txt"
            Shell("cmd.exe /c " + cmd_info, AppWinStyle.Hide, True)

            '' SHOW KEY ON RICHTEXTBOX
            Dim ficheiro As String = m.Groups(1).Value & ".txt"
            Dim pass_file As String = File.ReadAllText(Path.Combine(temp_direct, ficheiro)).Replace(" ", "")
            Dim pattern_pass As String = "KeyContent:(.*)"
            Dim match_key As Match = Regex.Match(pass_file, pattern_pass)

            If match_key.Success Then
                RichTextBox1.AppendText("Key: " & match_key.Groups(1).Value)
            End If

        Next
    Next

I get an exception(Illegal characters in path) on:

Dim pass_file As String = File.ReadAllText(Path.Combine(temp_direct, ficheiro)).Replace(" ", "")

So i think the problem it's on the 'm.Groups(1).Value' for some reason it doesn't get recognize as a string? because if I use

Path.Combine(temp_direct, "NAME.txt")

Works that way, I tried to change the match to '.ToString', without the '.value' with the file type separated, etc, but it doesn't work. I also tried to search on forums but with no luck.

Milton Cardoso
  • 358
  • 3
  • 14

1 Answers1

0

It's working, thanks to @Slai!

I added a function that creates the filename

Public Function GetSafeFilename(filename As String) As String

    Return String.Join(".", filename.Split(Path.GetInvalidFileNameChars()))

End Function

And the final code:

'' GET PROFILES
    Dim temp_direct As String = My.Computer.FileSystem.SpecialDirectories.Temp
    Dim cmd As String = "Netsh WLAN show profiles > " & temp_direct & "\profiles.txt"

    Shell("cmd.exe /c " + cmd, AppWinStyle.Hide, True)

    '' SHOW PROFILES
    Dim texto As String = File.ReadAllText(temp_direct & "\profiles.txt").Replace(" ", "")
    Dim pattern As String = "AllUserProfile:(.*)"
    Dim matches As MatchCollection = Regex.Matches(texto, pattern)

    For Each m As Match In matches
        For Each c As Capture In m.Captures
            RichTextBox1.AppendText("SSID: " & m.Groups(1).Value)

            '' SAVE PROFILE WITH CLEAR KEY
            Dim cmd_info As String = "Netsh WLAN show profiles " & m.Groups(1).Value & " key=clear" & " > " & temp_direct & "\" & m.Groups(1).Value & ".txt"
            Shell("cmd.exe /c " + cmd_info, AppWinStyle.Hide, True)

            '' SHOW KEY ON RICHTEXTBOX
            Dim ficheiro As String = m.Groups(1).Value & "txt"
            Dim pass_file As String = File.ReadAllText(Path.Combine(temp_direct, GetSafeFilename(ficheiro))).Replace(" ", "")
            Dim pattern_pass As String = "KeyContent:(.*)"
            Dim match_key As MatchCollection = Regex.Matches(pass_file, pattern_pass)

            For Each mk As Match In match_key
                For Each ck As Capture In mk.Captures
                    RichTextBox1.AppendText("Key: " & mk.Groups(1).Value)
                Next
            Next
        Next
    Next
Milton Cardoso
  • 358
  • 3
  • 14