From e39ebaac230a5e3adff4a2b21ef9cb54c3396ffa Mon Sep 17 00:00:00 2001 From: Ruakij Date: Mon, 29 Nov 2021 09:23:08 +0100 Subject: [PATCH 1/8] Fixed Regex codesmell --- src/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.js b/src/main.js index 4212cf4..3726245 100644 --- a/src/main.js +++ b/src/main.js @@ -62,7 +62,7 @@ if(errorMsg){ logger.debug("Creating & Attaching streams.."); proc.stdout .setEncoding("utf8") - .pipe(new RegexBlockStream(/^[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{6}.*(\n( {4,8}|\t\t?).*){1,}\n/gm)) + .pipe(new RegexBlockStream(/^\d{2}:\d{2}:\d{2}.\d{6}.*(\n( {4,8}|\t\t?).*)+\n/gm)) .pipe(new PacketStreamFactory()) .pipe(new PacketInfluxPointFactory()) .pipe(new InfluxPointWriter(influxDb, env.INFLUX_ORG, env.INFLUX_BUCKET)); From 54eadf3bc360e7b05909116223ab75c0bdb70975 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Mon, 29 Nov 2021 09:24:18 +0100 Subject: [PATCH 2/8] Fixed Regex codesmells --- src/streamHandler/PacketStreamFactory.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/streamHandler/PacketStreamFactory.js b/src/streamHandler/PacketStreamFactory.js index ea1f5cb..8980f4b 100644 --- a/src/streamHandler/PacketStreamFactory.js +++ b/src/streamHandler/PacketStreamFactory.js @@ -65,12 +65,12 @@ class PacketStreamFactory extends Transform{ ?.forEach(match => packet.flags[FLAG_TYPE_MAP[match]] = true) // Set them to true in flags ); - packet.dataRate = Number(data.match(/(?<=^|\s)[0-9]+(\.[0-9]+)?(?=\sMb\/?s($|\s))/i)?.[0]) || null; - packet.frequency = Number(data.match(/(?<=^|\s)[0-9]{4}(?=\sMHz($|\s))/i)?.[0]) || null; + packet.dataRate = Number(data.match(/(?<=^|\s)\d+(\.\d+)?(?=\sMb\/?s($|\s))/i)?.[0]) || null; + packet.frequency = Number(data.match(/(?<=^|\s)\d{4}(?=\sMHz($|\s))/i)?.[0]) || null; - packet.durationMicros = Number(data.match(/(?<=^|\s)[0-9]{1,4}(?=us($|\s))/i)?.[0]) || null; + packet.durationMicros = Number(data.match(/(?<=^|\s)\d{1,4}(?=us($|\s))/i)?.[0]) || null; - packet.signal = Number(data.match(/(?<=^|\s)-[0-9]{2,3}(?=dBm\sSignal($|\s))/i)?.[0]) || null; + packet.signal = Number(data.match(/(?<=^|\s)-\d{2,3}(?=dBm\sSignal($|\s))/i)?.[0]) || null; let packetTypeStr = data.match(new RegExp('(?<=^|\\s)('+ PACKET_TYPES_REGEX +')(?=$|\\s)', 'i'))?.[0]; packet.packetType = packetTypeStr? PACKET_TYPE_MAP[packetTypeStr]: From 8417de5756ab921863e7a7b8e4f3ca166e2e8ac7 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Mon, 29 Nov 2021 09:35:17 +0100 Subject: [PATCH 3/8] Extract checks with if-statements --- src/streamHandler/PacketStreamFactory.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/streamHandler/PacketStreamFactory.js b/src/streamHandler/PacketStreamFactory.js index 8980f4b..2a7175d 100644 --- a/src/streamHandler/PacketStreamFactory.js +++ b/src/streamHandler/PacketStreamFactory.js @@ -73,9 +73,14 @@ class PacketStreamFactory extends Transform{ packet.signal = Number(data.match(/(?<=^|\s)-\d{2,3}(?=dBm\sSignal($|\s))/i)?.[0]) || null; let packetTypeStr = data.match(new RegExp('(?<=^|\\s)('+ PACKET_TYPES_REGEX +')(?=$|\\s)', 'i'))?.[0]; - packet.packetType = packetTypeStr? PACKET_TYPE_MAP[packetTypeStr]: - data.match(/(SA|TA|DA|RA|BSSID):.{17}\s*$/i)? PacketType.NoData: - PacketType.Unknown; + if(packetTypeStr) + packet.packetType = PACKET_TYPE_MAP[packetTypeStr]; + else if(data.match(/(SA|TA|DA|RA|BSSID):.{17}\s*$/i)){ + packet.packetType = PacketType.NoData + } + else { + packet.packetType = PacketType.Unknown; + } packet.srcMac = data.match(/(?<=(^|\s)(SA|TA):).{17}(?=$|\s)/i)?.[0] ?? null; From 227ba127f8218ea93e846d7a98cb629458301c6f Mon Sep 17 00:00:00 2001 From: Ruakij Date: Mon, 29 Nov 2021 09:36:21 +0100 Subject: [PATCH 4/8] Extract packetType handling in own function --- src/streamHandler/PacketStreamFactory.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/streamHandler/PacketStreamFactory.js b/src/streamHandler/PacketStreamFactory.js index 2a7175d..6fd6bb8 100644 --- a/src/streamHandler/PacketStreamFactory.js +++ b/src/streamHandler/PacketStreamFactory.js @@ -88,6 +88,12 @@ class PacketStreamFactory extends Transform{ packet.bssid = data.match(/(?<=(^|\s)BSSID:).{17}(?=$|\s)/i)?.[0] ?? null; + packet = _handleHeaderPacketType(packet); + + return packet; + } + + _handleHeaderPacketType(packet){ // Cover special cases with more data let newPacket; switch(packet.packetType){ @@ -126,6 +132,12 @@ class PacketStreamFactory extends Transform{ packet.payloadData = hexConv.hexToBytes(data.match(/(?<=\s)([A-F0-9]{1,4}(?=\s))/igm)?.join('') ?? ''); packet.payloadData.splice(packet.payloadData.length-4, 4); // Remove FrameCheck sequence + packet = _handlePayloadPacketType(packet, data); + + return packet; + } + + _handlePayloadPacketType(packet) { // Cover special cases with more data let newPacket; switch(packet.packetType){ From 2ae85ababbfdbdb2cd21e4a0d172dcb1712d6108 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Mon, 29 Nov 2021 09:37:28 +0100 Subject: [PATCH 5/8] Extract keyInfo reading to helper-file --- src/helper/wifiStateAnalyzer.js | 22 ++++++++++++++++++++++ src/streamHandler/PacketStreamFactory.js | 17 +++-------------- 2 files changed, 25 insertions(+), 14 deletions(-) create mode 100644 src/helper/wifiStateAnalyzer.js diff --git a/src/helper/wifiStateAnalyzer.js b/src/helper/wifiStateAnalyzer.js new file mode 100644 index 0000000..33a97ae --- /dev/null +++ b/src/helper/wifiStateAnalyzer.js @@ -0,0 +1,22 @@ + +function keyInfoFromRaw(rawKeyInfo) { + return { + "KeyDescriptorVersion": keyInfoRaw>>0 & 0b111, + "KeyType": keyInfoRaw>>3 & 0b1, + "KeyIndex": keyInfoRaw>>4 & 0b11, + "Install": keyInfoRaw>>6 & 0b1, + "KeyACK": keyInfoRaw>>7 & 0b1, + "KeyMIC": keyInfoRaw>>8 & 0b1, + "Secure": keyInfoRaw>>9 & 0b1, + "Error": keyInfoRaw>>10 & 0b1, + "Request": keyInfoRaw>>11 & 0b1, + "EncryptedKeyData": keyInfoRaw>>12 & 0b1, + "SMKMessage": keyInfoRaw>>13 & 0b1, + }; +} + + +// Specify exports +module.exports = { + keyInfoFromRaw, +}; \ No newline at end of file diff --git a/src/streamHandler/PacketStreamFactory.js b/src/streamHandler/PacketStreamFactory.js index 6fd6bb8..06f59a9 100644 --- a/src/streamHandler/PacketStreamFactory.js +++ b/src/streamHandler/PacketStreamFactory.js @@ -3,6 +3,7 @@ const { Transform } = require('stream'); const { DateTime } = require("luxon"); const { PacketType, FlagType, Packet, PacketWithSSID, BeaconPacket, ProbeRequestPacket, ProbeResponsePacket, AuthenticationPacket, AuthenticationType, AssociationResponsePacket, DisassociationPacket, HandshakePacket, HandshakeStage } = require.main.require('./dto/Packet.js'); const hexConv = require.main.require("./helper/hexConverter.js"); +const wifiStateAnalyser = require.main.require("./helper/wifiStateAnalyzer.js"); const PACKET_TYPE_MAP = { "Beacon": PacketType.Beacon, @@ -146,20 +147,8 @@ class PacketStreamFactory extends Transform{ // Read key-information const keyInfoRaw = (packet.payloadData[0x5]<<0x8) + packet.payloadData[0x6]; - const keyInfo = { - "KeyDescriptorVersion": keyInfoRaw>>0 & 0b111, - "KeyType": keyInfoRaw>>3 & 0b1, - "KeyIndex": keyInfoRaw>>4 & 0b11, - "Install": keyInfoRaw>>6 & 0b1, - "KeyACK": keyInfoRaw>>7 & 0b1, - "KeyMIC": keyInfoRaw>>8 & 0b1, - "Secure": keyInfoRaw>>9 & 0b1, - "Error": keyInfoRaw>>10 & 0b1, - "Request": keyInfoRaw>>11 & 0b1, - "EncryptedKeyData": keyInfoRaw>>12 & 0b1, - "SMKMessage": keyInfoRaw>>13 & 0b1, - }; - + const keyInfo = wifiStateAnalyser.keyInfoFromRaw(keyInfoRaw); + newPacket.handshakeStage = (!keyInfo.Install && keyInfo.KeyACK && !keyInfo.KeyMIC && !keyInfo.Secure)? HandshakeStage[1] : (!keyInfo.Install && !keyInfo.KeyACK && keyInfo.KeyMIC && !keyInfo.Secure)? HandshakeStage[2] : ( keyInfo.Install && keyInfo.KeyACK && keyInfo.KeyMIC && keyInfo.Secure)? HandshakeStage[3] : From 8ff7211f0feead55b4c4270e6b5431bfc3b9ed69 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Mon, 29 Nov 2021 09:50:14 +0100 Subject: [PATCH 6/8] Revert "Extract packetType handling in own function" This reverts commit 227ba127f8218ea93e846d7a98cb629458301c6f. --- src/streamHandler/PacketStreamFactory.js | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/streamHandler/PacketStreamFactory.js b/src/streamHandler/PacketStreamFactory.js index 06f59a9..3dbbe26 100644 --- a/src/streamHandler/PacketStreamFactory.js +++ b/src/streamHandler/PacketStreamFactory.js @@ -89,12 +89,6 @@ class PacketStreamFactory extends Transform{ packet.bssid = data.match(/(?<=(^|\s)BSSID:).{17}(?=$|\s)/i)?.[0] ?? null; - packet = _handleHeaderPacketType(packet); - - return packet; - } - - _handleHeaderPacketType(packet){ // Cover special cases with more data let newPacket; switch(packet.packetType){ @@ -133,12 +127,6 @@ class PacketStreamFactory extends Transform{ packet.payloadData = hexConv.hexToBytes(data.match(/(?<=\s)([A-F0-9]{1,4}(?=\s))/igm)?.join('') ?? ''); packet.payloadData.splice(packet.payloadData.length-4, 4); // Remove FrameCheck sequence - packet = _handlePayloadPacketType(packet, data); - - return packet; - } - - _handlePayloadPacketType(packet) { // Cover special cases with more data let newPacket; switch(packet.packetType){ From 31ab10c3e1043f2d0674ecb96d2d5db240e63827 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Mon, 29 Nov 2021 09:57:05 +0100 Subject: [PATCH 7/8] Fix wrong var-name --- src/helper/wifiStateAnalyzer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helper/wifiStateAnalyzer.js b/src/helper/wifiStateAnalyzer.js index 33a97ae..bb71f60 100644 --- a/src/helper/wifiStateAnalyzer.js +++ b/src/helper/wifiStateAnalyzer.js @@ -1,5 +1,5 @@ -function keyInfoFromRaw(rawKeyInfo) { +function keyInfoFromRaw(keyInfoRaw) { return { "KeyDescriptorVersion": keyInfoRaw>>0 & 0b111, "KeyType": keyInfoRaw>>3 & 0b1, From 91c3aca9e2eb80ca8ca0b87e8b26321b5b62aaa1 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Mon, 29 Nov 2021 09:57:33 +0100 Subject: [PATCH 8/8] Add function to get handshakeStage from keyInfo --- src/helper/wifiStateAnalyzer.js | 21 +++++++++++++++++++++ src/streamHandler/PacketStreamFactory.js | 10 +++------- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/helper/wifiStateAnalyzer.js b/src/helper/wifiStateAnalyzer.js index bb71f60..e2622de 100644 --- a/src/helper/wifiStateAnalyzer.js +++ b/src/helper/wifiStateAnalyzer.js @@ -1,3 +1,4 @@ +const { HandshakeStage } = require.main.require('./dto/Packet.js'); function keyInfoFromRaw(keyInfoRaw) { return { @@ -15,8 +16,28 @@ function keyInfoFromRaw(keyInfoRaw) { }; } +const HANDSHAKE_STAGE_KEYINFO = { + "keys": ["Install", "KeyACK", "KeyMIC", "Secure"], + "0100": HandshakeStage[1], + "0010": HandshakeStage[2], + "1111": HandshakeStage[3], + "0011": HandshakeStage[4], +}; +function handshakeStageFromKeyInfo(keyInfo){ + + // Extract compare-keys + let keyData = ""; + for (const key of HANDSHAKE_STAGE_KEYINFO['keys']) { + keyData += keyInfo[key].toString(); + } + + // Get and return stage + return HANDSHAKE_STAGE_KEYINFO[keyData]; +} + // Specify exports module.exports = { keyInfoFromRaw, + handshakeStageFromKeyInfo, }; \ No newline at end of file diff --git a/src/streamHandler/PacketStreamFactory.js b/src/streamHandler/PacketStreamFactory.js index 3dbbe26..9d7c866 100644 --- a/src/streamHandler/PacketStreamFactory.js +++ b/src/streamHandler/PacketStreamFactory.js @@ -135,13 +135,9 @@ class PacketStreamFactory extends Transform{ // Read key-information const keyInfoRaw = (packet.payloadData[0x5]<<0x8) + packet.payloadData[0x6]; - const keyInfo = wifiStateAnalyser.keyInfoFromRaw(keyInfoRaw); - - newPacket.handshakeStage = (!keyInfo.Install && keyInfo.KeyACK && !keyInfo.KeyMIC && !keyInfo.Secure)? HandshakeStage[1] : - (!keyInfo.Install && !keyInfo.KeyACK && keyInfo.KeyMIC && !keyInfo.Secure)? HandshakeStage[2] : - ( keyInfo.Install && keyInfo.KeyACK && keyInfo.KeyMIC && keyInfo.Secure)? HandshakeStage[3] : - (!keyInfo.Install && !keyInfo.KeyACK && keyInfo.KeyMIC && keyInfo.Secure)? HandshakeStage[4] : - null; + const keyInfo = wifiStateAnalyser.keyInfoFromRaw(keyInfoRaw); // Convert + + newPacket.handshakeStage = wifiStateAnalyser.handshakeStageFromKeyInfo(keyInfo); // Get stage break; } if(newPacket) packet = Object.assign(newPacket, packet);