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/lib/rex/proto/addp.rb
Views: 11655
# -*- coding: binary -*-1module Rex2module Proto34#5# This provides constants, encoding, and decoding routines for Digi International's ADDP protocol6#7class ADDP89require "rex/socket"1011#12# See the following URLs for more information:13# - http://qbeukes.blogspot.com/2009/11/advanced-digi-discovery-protocol_21.html14# - http://www.digi.com/wiki/developer/index.php/Advanced_Device_Discovery_Protocol_%28ADDP%2915#161718MAGICS = %W{ DIGI DVKT DGDP }19ERRORS = %W{ no_response unknown success authenticaton_failed unit_has_address invalid_value invalid_data unsupported_command }20WLAN_ENC_MODES = %W{ unknown none wep40 wep128 }21WLAN_AUTH_MODES = %W{ unknown open shared_key open_shared_key }22HWTYPES = %W{23unknown ps3_desk8 ps3_desk16 ps3_desk32 ps3_rack16 ps2_desk16 ps2_rack1624lets_desk1 lets_desk2 lets_desk4 dorpia_dinrail1 nubox01 nubox02 nubox0425digione_sp digione_ia digione_em26}2728CMD_CONF_REQ = 129CMD_CONF_REP = 230CMD_SET_ADDR_REQ = 331CMD_SET_ADDR_REP = 432CMD_REBOOT_REQ = 533CMD_REBOOT_REP = 634CMD_SET_DHCP_REQ = 735CMD_SET_DHCP_REP = 836CMD_SET_WL_REQ = 937CMD_SET_WL_REP = 1038CMD_SET_WL_COUNTRIES_REQ = 1139CMD_SET_WL_COUNTRIES_REP = 1240CMD_EDP = 1341CMD_CNT = 14424344def self.encode_password(pwd="dbps")45[pwd.length].pack("C") + pwd46end4748def self.request_config(magic, dmac="\xff\xff\xff\xff\xff\xff")49mac = (dmac.length == 6) ? dmac : Rex::Socket.eth_aton(dmac)50req = magic + [ CMD_CONF_REQ, 6].pack("nn") + mac51return req52end5354def self.request_config_all(dmac="\xff\xff\xff\xff\xff\xff")55mac = (dmac.length == 6) ? dmac : Rex::Socket.eth_aton(dmac)56res = []57MAGICS.each { |m| res << self.request_config(m, dmac) }58return res59end6061def self.request_static_ip(magic, dmac, ip, mask, gw, pwd="dbps")62mac = (dmac.length == 6) ? dmac : Rex::Socket.eth_aton(dmac)63buf =64Rex::Socket.addr_aton(ip) +65Rex::Socket.addr_aton(mask) +66Rex::Socket.addr_aton(gw) +67mac +68self.encode_password(pwd)6970req = magic + [CMD_SET_ADDR_REQ, buf.length].pack("nn") + buf71return req72end7374def self.request_dhcp(magic, dmac, enabled, pwd="dbps")75mac = (dmac.length == 6) ? dmac : Rex::Socket.eth_aton(dmac)76buf =77[ enabled ? 1 : 0 ].pack("C") +78mac +79self.encode_password(pwd)8081req = magic + [CMD_SET_DHCP_REQ, buf.length].pack("nn") + buf82return req83end8485def self.request_reboot(magic, dmac, pwd="dbps")86mac = (dmac.length == 6) ? dmac : Rex::Socket.eth_aton(dmac)87buf =88mac +89self.encode_password(pwd)9091req = magic + [CMD_REBOOT_REQ, buf.length].pack("nn") + buf92return req93end9495def self.decode_reply(data)96res = {}97r_magic = data[0,4]98r_ptype = data[4,2].unpack("n").first99r_plen = data[6,2].unpack("n").first100buff = data[8, r_plen]101bidx = 0102103res[:magic] = data[0,4]104res[:cmd] = r_ptype105106while bidx < (buff.length - 2)107i_type, i_len = buff[bidx, 2].unpack("CC")108i_data = buff[bidx + 2, i_len]109110break if i_data.length != i_len111112case i_type113when 0x01114res[:mac] = Rex::Socket.eth_ntoa(i_data)115when 0x02116res[:ip] = Rex::Socket.addr_ntoa(i_data)117when 0x03118res[:mask] = Rex::Socket.addr_ntoa(i_data)119when 0x04120res[:hostname] = i_data121when 0x05122res[:domain] = i_data123when 0x06124res[:hwtype] = HWTYPES[ i_data.unpack("C").first ] || HWTYPES[ 0 ]125when 0x07126res[:hwrev] = i_data.unpack("C").first127when 0x08128res[:fwrev] = i_data129when 0x09130res[:msg] = i_data131when 0x0a132res[:result] = i_data.unpack("C").first133when 0x0b134res[:gw] = Rex::Socket.addr_ntoa(i_data)135when 0x0c136res[:advisory] = i_data.unpack("n").first137when 0x0d138res[:hwname] = i_data139when 0x0e140res[:realport] = i_data.unpack("N").first141when 0x0f142res[:dns] = Rex::Socket.addr_ntoa(i_data)143when 0x10144res[:dhcp] = (i_data.unpack("C").first == 0) ? false : true145when 0x11146res[:error] = ERRORS[ i_data.unpack("C").first ] || ERRORS[0]147when 0x12148res[:ports] = i_data.unpack("C").first149when 0x13150res[:realport_enc] = (i_data.unpack("C").first == 0) ? false : true151when 0x14152res[:version] = i_data.unpack("n").first153when 0x15154res[:vendor_guid] = i_data.unpack("H*") # GUID155when 0x16156res[:iftype] = i_data.unpack("C").first157when 0x17158res[:challenge] = i_data # Unknown format159when 0x18160res[:cap_port] = i_data.unpack("n").first161when 0x19162res[:edp_devid] = i_data.unpack("H*").first # Unknown format163when 0x1a164res[:edp_enabled] = (i_data.unpack("C").first == 0) ? false : true165when 0x1b166res[:edp_url] = i_data167when 0x1c168res[:wl_ssid] = i_data169when 0x1d170res[:wl_auto_ssid] = (i_data.unpack("n").first == 0) ? false : true171when 0x1e172res[:wl_tx_enh_power] = i_data.unpack("n").first173when 0x1f174res[:wl_auth_mode] = WLAN_AUTH_MODES[ i_data.unpack("n").first ] || WLAN_AUTH_MODES[ 0 ]175when 0x20176res[:wl_enc_mode] = WLAN_ENC_MODES[ i_data.unpack("n").first ] || WLAN_ENC_MODES[ 0 ]177when 0x21178res[:wl_enc_key] = i_data179when 0x22180res[:wl_cur_country] = i_data181when 0x23182res[:wl_country_list] = i_data183else184# Store unknown responses185res["unknown_0x#{"%.2x" % i_type}".to_sym] = i_data186end187188bidx = bidx + 2 + i_len189end190return res191end192193def self.reply_to_string(res)194str = ""195196fields = [197:hwname, :hwtype, :hwrev, :fwrev,198:mac, :ip, :mask, :gw, :hostname, :domain, :dns, :dhcp,199:msg, :result, :error,200:advisory, :ports, :realport, :realport_enc,201:version, :vendor_guid, :iftype, :challenge, :cap_port, :edp_devid, :edp_enabled,202:edp_url, :wl_ssid, :wl_auto_ssid, :wl_tx_enh_power, :wl_auth_mode, :wl_enc_mode,203:wl_enc_key, :wl_cur_country, :wl_country_list, :magic204]205206fields.each do |fname|207next unless res.has_key?(fname)208str << "#{fname}:#{res[fname]} "209end210return str211end212213end214215end216end217218219220