SSID password recovery (Windows)

Share your advanced PureBasic knowledge/code with the community.
User avatar
minimy
Enthusiast
Enthusiast
Posts: 617
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

SSID password recovery (Windows)

Post by minimy »

Tell me if it work in your country please, because cmd returned string is in your own lenguage.

Code: Select all

Procedure.s CLIreturn(prg.s, comando.s)
  Protected salida.s
  comando = ReplaceString(comando,"'",Chr(34))
  idprg= RunProgram(prg, comando, GetPathPart(prg), #PB_Program_Open|#PB_Program_Read|#PB_Program_Error|#PB_Program_Hide)
  If IsProgram(idprg)
    While ProgramRunning(idprg)
      If AvailableProgramOutput(idprg)
        salida + ReadProgramString(idprg) + #CRLF$
      EndIf
      error$ = ReadProgramError(idprg)
      If error$ <> ""
        salida + error$ + #CRLF$
      EndIf
    Wend
  EndIf
  ProcedureReturn salida
EndProcedure
Procedure.s netGetRedes()
  Protected.s o, router, ll, l= CLIreturn("cmd","/c netsh wlan show profiles")
  Protected   n, p= FindString(l,"     : ")-Len("     : ")
  l= ReplaceString(l,Chr(10),"")
  For p=10 To CountString(l,Chr(13))-1
    router= StringField(StringField(l,p,Chr(13)),2,": ")
    ll= CLIreturn("cmd","/c netsh wlan show profile name="+router+" key=clear")
    ll= ReplaceString(ll,Chr(10),"")
    For n= 1 To CountString(ll,Chr(13))-1
      If n=33
        o+ router+" : "+StringField(StringField(ll,n,Chr(13)),2,": ") + Chr(13)
      EndIf
    Next n
  Next p
  l= CLIreturn("cmd","/c netsh wlan show interfaces")
  For n=1 To CountString(l,Chr(13))-1
    If FindString(StringField(l,n,Chr(13)),"SSID                   :")
      ll= "NETWORK : "+StringField(StringField(l,n,Chr(13)),2,": ") + Chr(13)
    EndIf
  Next n
  ProcedureReturn ll+o
EndProcedure

MessageRequester("REDES INFO - minimy 2024", netGetRedes(),#PB_MessageRequester_Info)

If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
Bisonte
Addict
Addict
Posts: 1313
Joined: Tue Oct 09, 2007 2:15 am

Re: SSID password recovery (Windows)

Post by Bisonte »

German W10x64 : only returns the correct SSID... no password...
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
minimy
Enthusiast
Enthusiast
Posts: 617
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: SSID password recovery (Windows)

Post by minimy »

Bisonte wrote: Fri Sep 06, 2024 9:17 pm German W10x64 : only returns the correct SSID... no password...
With this, show all info received. In my case is 33 the line with the password. May be other in german OS.. Idk. :mrgreen:

Run this and watch in the output debug what line is the right line.
Later uncomment, and change line 28 in PB with 'if n=33' with your number.

I hope work. And thanks for comment.

Code: Select all

Procedure.s CLIreturn(prg.s, comando.s)
  Protected salida.s
  comando = ReplaceString(comando,"'",Chr(34))
  idprg= RunProgram(prg, comando, GetPathPart(prg), #PB_Program_Open|#PB_Program_Read|#PB_Program_Error|#PB_Program_Hide)
  If IsProgram(idprg)
    While ProgramRunning(idprg)
      If AvailableProgramOutput(idprg)
        salida + ReadProgramString(idprg) + #CRLF$
      EndIf
      error$ = ReadProgramError(idprg)
      If error$ <> ""
        salida + error$ + #CRLF$
      EndIf
    Wend
  EndIf
  ProcedureReturn salida
EndProcedure
Procedure.s netGetRedes()
  Protected.s o, router, ll, l= CLIreturn("cmd","/c netsh wlan show profiles")
  Protected   n, p= FindString(l,"     : ")-Len("     : ")
  l= ReplaceString(l,Chr(10),"")
  For p=10 To CountString(l,Chr(13))-1
    router= StringField(StringField(l,p,Chr(13)),2,": ")
    ll= CLIreturn("cmd","/c netsh wlan show profile name="+router+" key=clear")
    ll= ReplaceString(ll,Chr(10),"")
    For n= 1 To CountString(ll,Chr(13))-1
;       If n=33
      Debug Str(n)+"  "+StringField(ll,n,Chr(13))
;         o+ router+" : "+StringField(StringField(ll,n,Chr(13)),2,": ") + Chr(13)
;       EndIf
    Next n
  Next p
  l= CLIreturn("cmd","/c netsh wlan show interfaces")
  For n=1 To CountString(l,Chr(13))-1
    If FindString(StringField(l,n,Chr(13)),"SSID                   :")
      ll= "NETWORK : "+StringField(StringField(l,n,Chr(13)),2,": ") + Chr(13)
    EndIf
  Next n
  ProcedureReturn ll+o
EndProcedure
If translation=Error: reply="Sorry, Im Spanish": Endif
infratec
Always Here
Always Here
Posts: 7616
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: SSID password recovery (Windows)

Post by infratec »

You have to use:

Code: Select all

CLIreturn("cmd","/c netsh wlan show profile name=" + #DQUOTE$ + router + #DQUOTE$ + " key=clear")
Because a FritzBox have spaces in the name like "FRITZ!Box 7430 PF"

And yes the key is shown (german version Win10 x64).
User avatar
Bisonte
Addict
Addict
Posts: 1313
Joined: Tue Oct 09, 2007 2:15 am

Re: SSID password recovery (Windows)

Post by Bisonte »

with infratec's correction it works with the version in the first post on a german W10 x64 ! ;)
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
minimy
Enthusiast
Enthusiast
Posts: 617
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: SSID password recovery (Windows)

Post by minimy »

infratec wrote: Sat Sep 07, 2024 9:11 am You have to use:

Code: Select all

CLIreturn("cmd","/c netsh wlan show profile name=" + #DQUOTE$ + router + #DQUOTE$ + " key=clear")
Because a FritzBox have spaces in the name like "FRITZ!Box 7430 PF"

And yes the key is shown (german version Win10 x64).
Thanks infratec for your help, yes, I forgot that names can contain spaces.
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: SSID password recovery (Windows)

Post by ChrisR »

It works well here on Windows 10 x64 except for special characters.
No problem if I add the Oem2Unicode procedure below, also, netsh can be used directly without going through cmd /c

Code: Select all

Procedure.s Oem2Unicode(OEM_in_unicode.s)
  Protected Oem_in_Ascii.s, ByteLength, Unicode.s 
  ByteLength = Len(OEM_in_unicode) + 2      
  Oem_in_Ascii = Space(ByteLength)
  PokeS(@Oem_in_Ascii, OEM_in_unicode, -1, #PB_Ascii)
  Unicode = Space(ByteLength)
  OemToChar_(@Oem_in_Ascii, @Unicode)
  ProcedureReturn Unicode
EndProcedure

Procedure.s CLIreturn(prg.s, comando.s)
  Protected salida.s
  comando = ReplaceString(comando,"'",Chr(34))
  idprg= RunProgram(prg, comando, GetPathPart(prg), #PB_Program_Open|#PB_Program_Read|#PB_Program_Error|#PB_Program_Hide)
  If IsProgram(idprg)
    While ProgramRunning(idprg)
      If AvailableProgramOutput(idprg)
        salida + Oem2Unicode(ReadProgramString(idprg, #PB_Ascii)) + #CRLF$
      EndIf
      error$ = ReadProgramError(idprg, #PB_Ascii)
      
      If error$ <> ""
        salida + error$ + #CRLF$
      EndIf
    Wend
  EndIf
  ProcedureReturn salida
EndProcedure

Procedure.s netGetRedes()
  Protected.s o, router, ll, l= CLIreturn("netsh","wlan show profiles")
  Protected   n, p= FindString(l,"     : ")-Len("     : ")
  l= ReplaceString(l,Chr(10),"")
  For p=10 To CountString(l,Chr(13))-1
    router= StringField(StringField(l,p,Chr(13)),2,": ")
    ll= CLIreturn("netsh","wlan show profile name=" + #DQUOTE$ + router + #DQUOTE$ + " key=clear")
    ll= ReplaceString(ll,Chr(10),"")
    For n= 1 To CountString(ll,Chr(13))-1
      If n=33
        o+ router+" : "+StringField(StringField(ll,n,Chr(13)),2,": ") + Chr(13)
      EndIf
    Next n
  Next p
  l= CLIreturn("netsh","wlan show interfaces")
  For n=1 To CountString(l,Chr(13))-1
    If FindString(StringField(l,n,Chr(13)),"SSID                   :")
      ll= "NETWORK : "+StringField(StringField(l,n,Chr(13)),2,": ") + Chr(13)
    EndIf
  Next n
  ProcedureReturn ll+o
EndProcedure

MessageRequester("REDES INFO - minimy 2024", netGetRedes(),#PB_MessageRequester_Info)
infratec
Always Here
Always Here
Posts: 7616
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: SSID password recovery (Windows)

Post by infratec »

@minimy

Btw. you should avoid this:

Code: Select all

For p=10 To CountString(l,Chr(13))-1
Because the expression CountString(l,Chr(13))-1 is executed for each loop.
Much better/faster:

Code: Select all

pMax = CountString(l,Chr(13))-1
For p=10 To pMax
At least with the asm backend.
Quin
Addict
Addict
Posts: 1133
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: SSID password recovery (Windows)

Post by Quin »

infratec wrote: Sat Sep 07, 2024 5:58 pm At least with the asm backend.
Interesting point, I wonder if this is still a rule when using the C backend? I always do it to be safe, but wonder if you have to?
infratec
Always Here
Always Here
Posts: 7616
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: SSID password recovery (Windows)

Post by infratec »

I did a test with the /commented option on the command line.

ASM: (I reduced the listing it to the essential parts)

Code: Select all

; For i = 0 To Len("1234")
  MOV    dword [v_i],0
_For1:
  MOV    eax,_S1
  PUSH   eax
  CALL  _PB_Len@4
  CMP    eax,dword [v_i]
  JL    _Next2
; Print(Str(i))
  CALL  _PB_Str@12
; Next i
_NextContinue2:
  INC    dword [v_i]
  JNO   _For1
_Next2:
C ( I reduced the listing to the essential parts)

Code: Select all

// For i = 0 To Len("1234")
v_i=0;
while(1) {
integer r1=PB_Len(_S1);
if (!(((integer)r1>=v_i))) { break; }
// Print(Str(i))
PB_Str(v_i,SYS_PopStringBasePosition());
integer r2=PB_Print(p0);
// Next i
next1:
v_i+=1;
}
In both cases the Len("1234") procedure is always executed.
I don't think that the C compiler can optimize this.
For my surprise the for loop is converted to a while loop in the C compiler.
User avatar
minimy
Enthusiast
Enthusiast
Posts: 617
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: SSID password recovery (Windows)

Post by minimy »

infratec wrote: Sat Sep 07, 2024 5:58 pm @minimy

Btw. you should avoid this:

Code: Select all

For p=10 To CountString(l,Chr(13))-1
Because the expression CountString(l,Chr(13))-1 is executed for each loop.
Much better/faster:

Code: Select all

pMax = CountString(l,Chr(13))-1
For p=10 To pMax
At least with the asm backend.
Thanks Infratec! Good lesson.
You'll never go to bed without knowing something new :D
If translation=Error: reply="Sorry, Im Spanish": Endif
Post Reply