From 2646596f0f6d8f12f61dca9c5230fa4b8e25363d Mon Sep 17 00:00:00 2001 From: nnposter Date: Sun, 25 Aug 2024 17:43:17 +0000 Subject: [PATCH] Resolves ether_type incompatibility in packet.Frame Frame:new() was populating the value as a 16-bit integer while Frame:build_ether_frame() was expectng a two-byte string Ethertype constants from various locations have been migrated to packet.lua --- CHANGELOG | 3 +++ nselib/eap.lua | 6 ++---- nselib/packet.lua | 12 ++++++++---- nselib/pppoe.lua | 9 ++------- scripts/broadcast-ataoe-discover.nse | 4 ++-- scripts/broadcast-dhcp-discover.nse | 3 +-- 6 files changed, 18 insertions(+), 19 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 34258f06f..e7383ea98 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -20,6 +20,9 @@ o [GH#2901, GH#2744, GH#2745] Arbitrary separator in stdnse.tohex() is now supported. Script smb-protocols now reports SMB dialects correctly. [nnposter] +o [NSE] ether_type inconsistency in packet.Frame has been resolved. Bothi + Frame:new() and Frame:build_ether_frame() now use an integer. [nnposter] + o [GH#2900, GH#2896, GH#2897] Nmap is now able to scan IP protocol 255. [nnposter] diff --git a/nselib/eap.lua b/nselib/eap.lua index e549d9097..b89c04c10 100644 --- a/nselib/eap.lua +++ b/nselib/eap.lua @@ -41,8 +41,6 @@ _ENV = stdnse.module("eap", stdnse.seeall) -- Created 02/23/2012 - v0.1 local ETHER_BROADCAST = "01:80:c2:00:00:03" -local ETHER_TYPE_EAPOL_N = 0x888E -local ETHER_TYPE_EAPOL = string.pack(">I2",ETHER_TYPE_EAPOL_N) local ETHER_HEADER_SIZE = 14 local EAPOL_HEADER_SIZE = 4 local EAP_HEADER_SIZE = 5 @@ -162,7 +160,7 @@ local make_eapol = function (arg) local p = packet.Frame:new() p.mac_src = arg.src p.mac_dst = packet.mactobin(ETHER_BROADCAST) - p.ether_type = ETHER_TYPE_EAPOL + p.ether_type = packet.ETHER_TYPE_EAPOL p.buf = string.pack(">BBs2", arg.version, arg.type, arg.payload) p:build_ether_frame() @@ -202,7 +200,7 @@ parse = function (pkt) stdnse.debug1("mac_src: %s, mac_dest: %s, ether_type: 0x%X", tb.mac_src_str, tb.mac_dst_str, tb.ether_type) - if tb.ether_type ~= ETHER_TYPE_EAPOL_N then return nil, "not an eapol packet" end + if tb.ether_type ~= packet.ETHER_TYPE_EAPOL then return nil, "not an eapol packet" end stdnse.debug2("version: %X, type: %s, length: 0x%X", tb.version, eapol_str[tb.type] or "unknown", diff --git a/nselib/packet.lua b/nselib/packet.lua index cb27c3aa2..94ff2b38b 100644 --- a/nselib/packet.lua +++ b/nselib/packet.lua @@ -135,8 +135,12 @@ ND_OPT_MTU = 5 ND_OPT_RTR_ADV_INTERVAL = 7 ND_OPT_HOME_AGENT_INFO = 8 -ETHER_TYPE_IPV4 = "\x08\x00" -ETHER_TYPE_IPV6 = "\x86\xdd" +ETHER_TYPE_IPV4 = 0x0800 +ETHER_TYPE_IPV6 = 0x86dd +ETHER_TYPE_PPPOE_DISCOVERY = 0x8863 +ETHER_TYPE_PPPOE_SESSION = 0x8864 +ETHER_TYPE_EAPOL = 0x888e +ETHER_TYPE_ATAOE = 0x88a2 ---------------------------------------------------------------------------------------------------------------- -- Frame is a class @@ -160,7 +164,7 @@ end --- Build an Ethernet frame. -- @param mac_dst six-byte string of the destination MAC address. -- @param mac_src six-byte string of the source MAC address. --- @param ether_type two-byte string of the type. +-- @param ether_type IEEE 802 ethertype as a 16-bit integer (0x0800 for IPv4) -- @param packet string of the payload. -- @return frame string of the Ether frame. function Frame:build_ether_frame(mac_dst, mac_src, ether_type, packet) @@ -171,7 +175,7 @@ function Frame:build_ether_frame(mac_dst, mac_src, ether_type, packet) if not self.ether_type then return nil, "Unknown packet type." end - self.frame_buf = self.mac_dst..self.mac_src..self.ether_type..self.buf + self.frame_buf = self.mac_dst..self.mac_src..(">I2"):pack(self.ether_type)..self.buf end --- Parse an Ethernet frame. -- @param frame string of the Ether frame. diff --git a/nselib/pppoe.lua b/nselib/pppoe.lua index b092781e9..4a2203606 100644 --- a/nselib/pppoe.lua +++ b/nselib/pppoe.lua @@ -30,11 +30,6 @@ local table = require "table" _ENV = stdnse.module("pppoe", stdnse.seeall) -EtherType = { - PPPOE_DISCOVERY = 0x8863, - PPPOE_SESSION = 0x8864, -} - -- A Class to handle the Link Control Protocol LCP LCP = { @@ -709,7 +704,7 @@ Comm = { local p = packet.Frame:new(l2..l3) -- there's probably a more elegant way of doing this - if ( EtherType.PPPOE_DISCOVERY == p.ether_type ) then + if ( packet.ETHER_TYPE_PPPOE_DISCOVERY == p.ether_type ) then if ( header.code == PPPoE.Code.PADO ) then local pado = PPPoE.PADO.parse(l3) pado.mac_srv = p.mac_src @@ -721,7 +716,7 @@ Comm = { local pads = PPPoE.PADT.parse(l3) return true, pads end - elseif ( EtherType.PPPOE_SESSION == p.ether_type ) then + elseif ( packet.ETHER_TYPE_PPPOE_SESSION == p.ether_type ) then return true, PPPoE.SessionData.parse(l3) end return false, ("Received unsupported response, can't decode code (%d)"):format(header.code) diff --git a/scripts/broadcast-ataoe-discover.nse b/scripts/broadcast-ataoe-discover.nse index 8726f4723..de956741f 100644 --- a/scripts/broadcast-ataoe-discover.nse +++ b/scripts/broadcast-ataoe-discover.nse @@ -105,14 +105,14 @@ ATAoE = { -- Send a Config Info Request to the ethernet broadcast address -- @param iface table as returned by nmap.get_interface_info() local function sendConfigInfoRequest(iface) - local ETHER_BROADCAST, P_ATAOE = "ff:ff:ff:ff:ff:ff", 0x88a2 + local ETHER_BROADCAST = "ff:ff:ff:ff:ff:ff" local req = ATAoE.ConfigInfoRequest:new() local tag = req.tag local p = packet.Frame:new() p.mac_src = iface.mac p.mac_dst = packet.mactobin(ETHER_BROADCAST) - p.ether_type = string.pack(">I2", P_ATAOE) + p.ether_type = packet.ETHER_TYPE_ATAOE p.buf = tostring(req) p:build_ether_frame() diff --git a/scripts/broadcast-dhcp-discover.nse b/scripts/broadcast-dhcp-discover.nse index 00bab90e8..1ae334433 100644 --- a/scripts/broadcast-dhcp-discover.nse +++ b/scripts/broadcast-dhcp-discover.nse @@ -147,8 +147,7 @@ local function dhcp_listener(sock, iface, macaddr, options, timeout, xid, result -- Add the Ethernet header frame:build_ether_frame( "\xff\xff\xff\xff\xff\xff", - iface.mac, -- can't use macaddr or we won't see response - packet.ETHER_TYPE_IPV4) + iface.mac) -- can't use macaddr or we won't see response local dnet = nmap.new_dnet() dnet:ethernet_open(iface.device)