Page 1 of 1

Scanning Wireless Networks

Posted: Sat May 05, 2012 9:48 am
by Toni6
Download source zip: http://www.filedropper.com/pbwirelessscanner

Code: Select all

#WLANAPI_USE_LIB = 0

IncludePath "Wireless LAN API"
XIncludeFile "Wlanapi.pbi"
IncludePath ""

;This for LinkedList's only
Macro IsValidIndex(index, linkedlist)
  (index >= 0 And index < ListSize(linkedlist))
EndMacro

Structure WLAN_Adapter
  Interface_GUID.s
  Interface_Description.s
  Interface_State.s
  InterfaceInfo.WLAN_INTERFACE_INFO
EndStructure

Global WinVersion.i
Global dwMaxClient.l


Procedure LoadAdapters(bLoad.b, List Adapters.WLAN_Adapter())
  Protected *pIfList.WLAN_INTERFACE_INFO_LIST
  Protected *pIfInfo.WLAN_INTERFACE_INFO
  Protected dwResult.l, dwCurVersion.i, hClient.i
  Protected Interface_GUID.s, Interface_Desc.s, Interface_State.s
  Protected TmpGUID.s{78}
  Protected iRet.i = 0
  
  dwResult = WlanOpenHandle(dwMaxClient, 0, @dwCurVersion, @hClient)
  
  If dwResult = #ERROR_SUCCESS
    dwResult = WlanEnumInterfaces(hClient, 0, @*pIfList)
    
    If dwResult = #ERROR_SUCCESS

      For i.i = 0 To *pIfList\dwNumberOfItems-1
        DisableDebugger
        *pIfInfo = *pIfList\InterfaceInfo[i]
        EnableDebugger

        If StringFromGUID2_(*pIfInfo\InterfaceGuid, @TmpGUID, 39)
          CompilerIf #PB_Compiler_Unicode
            Interface_GUID = TmpGUID
          CompilerElse
            Interface_GUID = PeekS(@TmpGUID, -1, #PB_Unicode)
          CompilerEndIf
        EndIf
        
        Interface_Desc.s = PeekS(@*pIfInfo\strInterfaceDescription, -1, #PB_Unicode)
        
        Select *pIfInfo\isState
          Case #wlan_interface_state_not_ready
            Interface_State = "Not ready"
          Case #wlan_interface_state_connected
            Interface_State = "Connected"
          Case #wlan_interface_state_ad_hoc_network_formed
            Interface_State = "First node in a ad hoc network"
          Case #wlan_interface_state_disconnecting
            Interface_State = "Disconnecting"
          Case #wlan_interface_state_disconnected
            Interface_State = "Not connected"
          Case #wlan_interface_state_associating
            Interface_State = "Attempting to associate with a network"
          Case #wlan_interface_state_discovering
            Interface_State = "Auto configuration is discovering settings for the network"
          Case #wlan_interface_state_authenticating
            Interface_State = "In process of authenticating"
          Default
            Interface_State = "Unknown state "+Str(*pIfInfo\isState)
        EndSelect
        
        If bLoad = #True
          AddElement(Adapters())
          Adapters()\Interface_GUID = Interface_GUID
          Adapters()\Interface_Description = Interface_Desc
          Adapters()\Interface_State = Interface_State
          CopyMemory(*pIfInfo, Adapters()\InterfaceInfo, SizeOf(WLAN_INTERFACE_INFO))
        EndIf
        
        PrintN("[" + Str(i) + "] " + Interface_Desc)
        PrintN(#TAB$+"Interface GUID: " + Interface_GUID)
        PrintN(#TAB$+"Interface State: " + Interface_State)
        PrintN("")
        iRet+1
      Next
      
      If *pIfList
        WlanFreeMemory(*pIfList)
      EndIf
    Else
      PrintN("Error - WlanEnumInterfaces failed With error: "+Str(dwResult))
    EndIf
    
    WlanCloseHandle(hClient,0)
  Else
    PrintN("Error - WlanOpenHandle failed With error: "+Str(dwResult))
  EndIf
  
  ProcedureReturn iRet 
EndProcedure

Procedure.s FormatSecurity(AuthAlgorithm.l, CipherAlgorithm.l)
  Protected sResult.s

  
  If AuthAlgorithm = #DOT11_AUTH_ALGO_80211_OPEN And CipherAlgorithm = #DOT11_CIPHER_ALGO_NONE
    sResult = "Open"
  ElseIf AuthAlgorithm = #DOT11_AUTH_ALGO_RSNA_PSK And CipherAlgorithm = #DOT11_CIPHER_ALGO_CCMP
    sResult = "WPA2-Personal"
  ElseIf AuthAlgorithm = #DOT11_AUTH_ALGO_RSNA And CipherAlgorithm = #DOT11_CIPHER_ALGO_CCMP
    sResult = "WPA2"
  ElseIf AuthAlgorithm = #DOT11_AUTH_ALGO_WPA And CipherAlgorithm = #DOT11_CIPHER_ALGO_TKIP
    sResult = "WPA"
  ElseIf CipherAlgorithm = #DOT11_CIPHER_ALGO_WEP40
    sResult = "WEP-40"
  ElseIf CipherAlgorithm = #DOT11_CIPHER_ALGO_WEP104
    sResult = "WEP-104"
  ElseIf CipherAlgorithm = #DOT11_CIPHER_ALGO_WEP
    sResult = "WEP"
  Else
    sResult = "Other"
  EndIf
  
  ProcedureReturn sResult
EndProcedure

Procedure ListNetworks(*Adapter.WLAN_Adapter)
  Protected *pBssList.WLAN_AVAILABLE_NETWORK_LIST
  Protected *pBssEntry.WLAN_AVAILABLE_NETWORK
  Protected dwResult.l, dwCurVersion.i, hClient.i
  Protected SSID.s, NetType.s, Connectable.s, SigQuality.s, SecurityOn.s, Security.s
  
  dwResult = WlanOpenHandle(dwMaxClient, 0, @dwCurVersion, @hClient)
  
  If dwResult = #ERROR_SUCCESS
    dwResult = WlanGetAvailableNetworkList(hClient, *Adapter\InterfaceInfo\InterfaceGuid,0,0, @*pBssList)
    
    If dwResult = #ERROR_SUCCESS
      PrintN("")
      PrintN(Str(*pBssList\dwNumberOfItems) +" Networks found") : PrintN("")
      
      For i.i = 0 To *pBssList\dwNumberOfItems-1
        DisableDebugger
        *pBssEntry = @*pBssList\Network[i]
        EnableDebugger
        
        SSID = PeekS(@*pBssEntry\dot11Ssid\ucSSID, *pBssEntry\dot11Ssid\uSSIDLength, #PB_Ascii)
        
        Select *pBssEntry\dot11BssType
          Case #dot11_BSS_type_infrastructure
            NetType = "Network type: Infrastructure"
          Case #dot11_BSS_type_independent
            NetType = "Network type: Independent"
          Default
            NetType = "Network type: Other ("+Str(*pBssEntry\dot11BssType)+")"
        EndSelect
        
        If *pBssEntry\bNetworkConnectable
          Connectable = "Yes"
        Else
          Connectable = "Not connectable WLAN_REASON_CODE value: "+Str(*pBssEntry\wlanNotConnectableReason)
        EndIf
        
        If *pBssEntry\wlanSignalQuality = 0
          iRSSI = -100
        ElseIf *pBssEntry\wlanSignalQuality = 100
          iRSSI = -50
        Else
          iRSSI = -100 + (*pBssEntry\wlanSignalQuality/2)
        EndIf
        
        SigQuality = Str(*pBssEntry\wlanSignalQuality)+"% (RSSI: "+Str(iRSSI)+" dBm)"
        
        If *pBssEntry\bSecurityEnabled
          SecurityOn = "Yes"
        Else
          SecurityOn = "No"
        EndIf
        
        Security = FormatSecurity(*pBssEntry\dot11DefaultAuthAlgorithm,*pBssEntry\dot11DefaultCipherAlgorithm)

        PrintN("Index: " + #TAB$+#TAB$+#TAB$+"["+Str(i)+"]")
        PrintN("SSID: " + #TAB$+#TAB$+#TAB$+SSID)
        PrintN("Connectable: "+#TAB$+#TAB$+Connectable)
        PrintN("Signal Quality: "+#TAB$+SigQuality)
        PrintN("Security Enabled: "+#TAB$+SecurityOn)
        PrintN("Security: "+#TAB$+#TAB$+Security)
        PrintN("")
      Next i
      
      If *pBssList
        WlanFreeMemory(*pBssList)
      EndIf
      
    Else
      PrintN("WlanGetAvailableNetworkList failed with error: "+Str(dwResult))
    EndIf
    
    WlanCloseHandle(hClient,0)
  Else
    PrintN("Error - WlanOpenHandle failed With error: "+Str(dwResult))
  EndIf
EndProcedure

Procedure ShowMenu()
  PrintN("[1] - List Wireless Adapters")
  PrintN("[2] - Show Wireless Networks")
  PrintN("[3] - Exit") : PrintN("")
  
EndProcedure

Procedure Load_WlanApi()
  Protected hLib.i
  
  hLib = OpenLibrary(#PB_Any, "Wlanapi.dll")
  If hLib
    WlanOpenHandle = GetFunction(hLib, "WlanOpenHandle")
    WlanCloseHandle = GetFunction(hLib, "WlanCloseHandle")
    WlanEnumInterfaces = GetFunction(hLib, "WlanEnumInterfaces")
    WlanFreeMemory = GetFunction(hLib, "WlanFreeMemory")
    WlanGetAvailableNetworkList = GetFunction(hLib, "WlanGetAvailableNetworkList")
  Else
    MessageRequester("Eror", "Failed to load Wlanapi.dll!", #MB_ICONERROR)
    End
  EndIf
  
EndProcedure

;- ENTRY POINT
If OpenConsole()
  CompilerIf #WLANAPI_USE_LIB = 0
    Load_WlanApi()
  CompilerEndIf

  NewList Adps.WLAN_Adapter()
  
  WinVersion = OSVersion()
  
  If WinVersion >= #PB_OS_Windows_XP And WinVersion < #PB_OS_Windows_Vista
    dwMaxClient = 1
  ElseIf WinVersion >= #PB_OS_Windows_Vista
    dwMaxClient = 2
  EndIf
  
  PrintN(" --- Scanning Wireless Networks in PureBasic --- ")
  PrintN("")
  ShowMenu()
  Print("Choose the option: ")
  
  iOption = Val(Input())
  PrintN("")
  
  If iOption = 1
    If Not LoadAdapters(#False, Adps())
      PrintN("No Wireless Adapters found.")
    EndIf
  ElseIf iOption = 2
    LoadAdapters(#True, Adps())
    
    If ListSize(Adps())
      Print("Choose the network adapter (index): ")
      
      iAdapter = Val(Input())
      
      ;Check if it's a valid index
      If IsValidIndex(iAdapter, Adps())
        SelectElement(Adps(), iAdapter)
        ListNetworks(@Adps())
      Else
        PrintN("Error - Invalid Adapter ("+Str(iAdapter)+")")
      EndIf 
    Else
      PrintN("No Wireless Adapters found.")
    EndIf
    
  ElseIf iOption = 3
    End
  Else
    PrintN("Error - Invalid Option")
  EndIf
  
  PrintN("Press enter to exit...")
  Input()
EndIf
Wlanapi.pbi

Code: Select all

XIncludeFile "windot11.pbi"

CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
  #WLAN_LIB_PATH = "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib\wlanapi.lib"
CompilerElse
  #WLAN_LIB_PATH = "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib\x64\wlanapi.lib"
CompilerEndIf

#WLAN_MAX_NAME_LENGTH = 256

;-ENUMS
Enumeration ;WLAN_INTERFACE_STATE
  #wlan_interface_state_not_ready
  #wlan_interface_state_connected
  #wlan_interface_state_ad_hoc_network_formed
  #wlan_interface_state_disconnecting
  #wlan_interface_state_disconnected
  #wlan_interface_state_associating
  #wlan_interface_state_discovering
  #wlan_interface_state_authenticating
EndEnumeration

Structure WLAN_INTERFACE_INFO
  InterfaceGuid.GUID
  strInterfaceDescription.u[#WLAN_MAX_NAME_LENGTH]
  isState.l
EndStructure

Structure WLAN_INTERFACE_INFO_LIST
  dwNumberOfItems.l
  dwIndex.l
  InterfaceInfo.WLAN_INTERFACE_INFO[1]
EndStructure

#WLAN_MAX_PHY_TYPE_NUMBER = 8

Structure WLAN_AVAILABLE_NETWORK
  strProfileName.u[#WLAN_MAX_NAME_LENGTH]
  dot11Ssid.DOT11_SSID
  dot11BssType.l
  uNumberOfBssids.l
  bNetworkConnectable.l
  wlanNotConnectableReason.l
  uNumberOfPhyTypes.l
  dot11PhyTypes.l[#WLAN_MAX_PHY_TYPE_NUMBER]
  ; bMorePhyTypes is set To TRUE If the PHY types For the network
  ; exceeds WLAN_MAX_PHY_TYPE_NUMBER.
  ; In this Case, uNumerOfPhyTypes is WLAN_MAX_PHY_TYPE_NUMBER And the
  ; first WLAN_MAX_PHY_TYPE_NUMBER PHY types are returned.
  bMorePhyTypes.l
  wlanSignalQuality.l
  bSecurityEnabled.l
  dot11DefaultAuthAlgorithm.l
  dot11DefaultCipherAlgorithm.l
  dwFlags.l
  dwReserved.l
EndStructure

Structure WLAN_AVAILABLE_NETWORK_LIST
  dwNumberOfItems.l
  dwIndex.l
  Network.WLAN_AVAILABLE_NETWORK[1]
EndStructure


CompilerIf #WLANAPI_USE_LIB = 1
  
Import #WLAN_LIB_PATH
  WlanOpenHandle(dwClientVersion.i, *pReserved, *pdwNegotiatedVersion, *phClientHandle)
  WlanCloseHandle(hClientHandle.i, *pReserved)
  
  WlanEnumInterfaces(hClientHandle.i, *pReserved, *ppInterfaceList)
  WlanFreeMemory(*pMemory)
  
  WlanGetAvailableNetworkList(hClientHandle.i, *pInterfaceGuid, dwFlags.i, *pReserved, *ppAvailableNetworkList)
EndImport

CompilerElse

Prototype PROTO_WlanOpenHandle(dwClientVersion.i, *pReserved, *pdwNegotiatedVersion, *phClientHandle)
Prototype PROTO_WlanCloseHandle(hClientHandle.i, *pReserved)
Prototype PROTO_WlanEnumInterfaces(hClientHandle.i, *pReserved, *ppInterfaceList)
Prototype PROTO_WlanFreeMemory(*pMemory)
Prototype PROTO_WlanGetAvailableNetworkList(hClientHandle.i, *pInterfaceGuid, dwFlags.i, *pReserved, *ppAvailableNetworkList)

Global WlanOpenHandle.PROTO_WlanOpenHandle
Global WlanCloseHandle.PROTO_WlanCloseHandle
Global WlanEnumInterfaces.PROTO_WlanEnumInterfaces
Global WlanFreeMemory.PROTO_WlanFreeMemory
Global WlanGetAvailableNetworkList.PROTO_WlanGetAvailableNetworkList

CompilerEndIf
WlanTypes.pbi

Code: Select all

#DOT11_SSID_MAX_LENGTH = 32

;- ENUMS
Enumeration ;DOT11_BSS_TYPE
  #dot11_BSS_type_infrastructure = 1
  #dot11_BSS_type_independent = 2
  #dot11_BSS_type_any = 3
EndEnumeration

Enumeration ;DOT11_AUTH_ALGORITHM
  #DOT11_AUTH_ALGO_80211_OPEN = 1
  #DOT11_AUTH_ALGO_80211_SHARED_KEY = 2
  #DOT11_AUTH_ALGO_WPA = 3
  #DOT11_AUTH_ALGO_WPA_PSK = 4
  #DOT11_AUTH_ALGO_WPA_NONE = 5 ; used in NatSTA only
  #DOT11_AUTH_ALGO_RSNA = 6
  #DOT11_AUTH_ALGO_RSNA_PSK = 7
  #DOT11_AUTH_ALGO_IHV_START = $80000000
  #DOT11_AUTH_ALGO_IHV_END = $ffffffff
EndEnumeration

Enumeration ;DOT11_CIPHER_ALGORITHM
  #DOT11_CIPHER_ALGO_NONE = $00
  #DOT11_CIPHER_ALGO_WEP40 = $01
  #DOT11_CIPHER_ALGO_TKIP = $02
  #DOT11_CIPHER_ALGO_CCMP = $04
  #DOT11_CIPHER_ALGO_WEP104 = $05
  #DOT11_CIPHER_ALGO_WPA_USE_GROUP = $100
  #DOT11_CIPHER_ALGO_RSN_USE_GROUP = $100
  #DOT11_CIPHER_ALGO_WEP = $101
  #DOT11_CIPHER_ALGO_IHV_START = $80000000
  #DOT11_CIPHER_ALGO_IHV_END = $ffffffff
EndEnumeration

;- STRUCTURES
Structure DOT11_SSID
  uSSIDLength.l
  ucSSID.a[#DOT11_SSID_MAX_LENGTH]
EndStructure
windot11.pbi

Code: Select all

XIncludeFile "WlanTypes.pbi"

Enumeration ;DOT11_PHY_TYPE
  #dot11_phy_type_unknown = 0
  #dot11_phy_type_any = #dot11_phy_type_unknown
  #dot11_phy_type_fhss = 1
  #dot11_phy_type_dsss = 2
  #dot11_phy_type_irbaseband = 3
  #dot11_phy_type_ofdm = 4
  #dot11_phy_type_hrdsss = 5
  #dot11_phy_type_erp = 6
  #dot11_phy_type_ht = 7
  #dot11_phy_type_IHV_start = $80000000
  #dot11_phy_type_IHV_end = $ffffffff
EndEnumeration

Re: Scanning Wireless Networks

Posted: Sun May 06, 2012 5:59 pm
by fsw
Couldn't download the file, so I had to copy and paste it manually.
Compiled without errors and the app showed me my wireless devices and the wireless networks available.
Good job.

Thank you.

Re: Scanning Wireless Networks

Posted: Mon May 07, 2012 5:07 pm
by Mohawk70
This works but doesn't pick up on a computer-to-computer network connection from a PSP that always shows in the list of wireless connections found by Windows. Nice work though !