From 4ef8fcc363673157b70dbf996cf7531510734c86 Mon Sep 17 00:00:00 2001 From: dmiller Date: Fri, 9 Mar 2018 06:26:45 +0000 Subject: [PATCH] Simplify packing/unpacking command names for bitcoin (no counting nulls) --- nselib/bitcoin.lua | 58 +++++++++++++++++++++++++------------ scripts/bitcoin-getaddr.nse | 4 +-- 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/nselib/bitcoin.lua b/nselib/bitcoin.lua index 6034b4ff1..650b083af 100644 --- a/nselib/bitcoin.lua +++ b/nselib/bitcoin.lua @@ -36,6 +36,7 @@ local match = require "match" local nmap = require "nmap" local os = require "os" local stdnse = require "stdnse" +local string = require "string" local table = require "table" local openssl = stdnse.silent_require('openssl') _ENV = stdnse.module("bitcoin", stdnse.seeall) @@ -110,11 +111,13 @@ Request = { -- @return data as string __tostring = function(self) local magic = 0xD9B4BEF9 - local cmd = "version\0\0\0\0\0" + local cmd = "version" local len = 85 -- ver: 0.4.0 local ver = 0x9c40 + cmd = cmd .. ('\0'):rep(12 - #cmd) + -- NODE_NETWORK = 1 local services = 1 local timestamp = os.time() @@ -167,9 +170,10 @@ Request = { -- @return data as string __tostring = function(self) local magic = 0xD9B4BEF9 - local cmd = "getaddr\0\0\0\0\0" + local cmd = "getaddr" local len = 0 local chksum = 0xe2e0f65d + cmd = cmd .. ('\0'):rep(12 - #cmd) return bin.pack("IA12II", data) + local cmd + pos, header.magic, cmd, header.length, header.checksum = bin.unpack(">IA12II", data) + header.cmd = string.unpack("z", cmd) return header end, }, @@ -292,10 +301,12 @@ Response = { parse = function(self) local pos, ra, sa + local cmd -- After 2012-02-20, version messages contain checksums - pos, self.magic, self.cmd, self.len, self.checksum, self.ver_raw, self.service, + pos, self.magic, cmd, self.len, self.checksum, self.ver_raw, self.service, self.timestamp, ra, sa, self.nodeid, self.subver, self.lastblock = bin.unpack(" 29000 ) then @@ -554,7 +574,7 @@ Helper = { local status, err = self.socket:send(tostring(req)) if ( not(status) ) then - return false, "Failed to send \"Version\" request to server" + return false, "Failed to send \"GetAddr\" request to server" end -- take care of any alerts that may be incoming diff --git a/scripts/bitcoin-getaddr.nse b/scripts/bitcoin-getaddr.nse index dbed70507..6138f6e1e 100644 --- a/scripts/bitcoin-getaddr.nse +++ b/scripts/bitcoin-getaddr.nse @@ -51,12 +51,12 @@ action = function(host, port) local status, ver = bcoin:exchVersion() if ( not(status) ) then - return fail("Failed to extract version information") + return fail("Failed to extract version information: " .. ver) end local status, nodes = bcoin:getNodes() if ( not(status) ) then - return fail("Failed to extract address information") + return fail("Failed to extract address information" .. nodes) end bcoin:close()