From bd2c9e01f68c580f96f1d667ffa2a0586bd39961 Mon Sep 17 00:00:00 2001 From: patrik Date: Mon, 20 Feb 2012 19:12:38 +0000 Subject: [PATCH] o [NSE] Applied patch from Andrew Orr that fixes the recent changes in the BitCoin protocol. [Patrik] --- CHANGELOG | 3 + nselib/bitcoin.lua | 190 ++++++++++++++++++++++++++++++------ scripts/bitcoin-getaddr.nse | 16 ++- 3 files changed, 174 insertions(+), 35 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 7e1503c4f..af7880d4a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,8 @@ # Nmap Changelog ($Id$); -*-text-*- +o [NSE] Applied patch from Andrew Orr that fixes the recent changes in the + BitCoin protocol. [Patrik] + o [NSE] Added a Network Data Management Protocol (ndmp) library and the scripts: + ndmp-version - retrieves version information diff --git a/nselib/bitcoin.lua b/nselib/bitcoin.lua index fe2317998..cdb414ed7 100644 --- a/nselib/bitcoin.lua +++ b/nselib/bitcoin.lua @@ -20,12 +20,17 @@ -- -- * Helper - The primary interface to scripts -- +--@author Patrik Karlsson +--@author Andrew Orr +--@copyright Same as Nmap--See http://nmap.org/book/man-legal.html -- --- Version 0.1 +-- Version 0.2 -- -- Created 11/09/2011 - v0.1 - created by Patrik Karlsson --- +-- Revised 17/02/2012 - v0.2 - fixed count parsing +-- - changed version/verack handling to support +-- February 20th 2012 bitcoin protocol switchover module(... or "bitcoin", package.seeall) @@ -114,11 +119,24 @@ Request = { local ra = NetworkAddress:new(self.host, self.port) local sa = NetworkAddress:new(self.lhost, self.lport) local nodeid = openssl.rand_bytes(8) - local subver = "\0" + local useragent = "\0" local lastblock = 0 + + -- Construct payload in order to calculate checksum for the header + local payload = bin.pack("IA12II", data) + return header + end, + }, + + + Alert = { + + type = "Alert", + -- Creates a new instance of Version based on data string + -- @param data string containing the raw response + -- @return o instance of Version + new = function(self, data) + local o = { + data = data, + } + setmetatable(o, self) + self.__index = self + o:parse() + return o + end, + + -- Parses the raw data and builds the Version instance + parse = function(self) + local pos = Response.Header.size + 1 + self.header = Response.Header.parse(self.data) + + local p_length + pos, p_length = Util.decodeVarInt(self.data, pos) + local data + pos, data = bin.unpack("A" .. p_length, self.data, pos) + + -- + -- TODO: Alert decoding goes here + -- + + return + end, + }, + + -- The version response message Version = { @@ -177,20 +269,22 @@ Response = { -- Parses the raw data and builds the Version instance parse = function(self) local pos, ra, sa - pos, self.magic, self.cmd, self.len, self.ver_raw, self.service, + + -- After 2012-02-20, version messages contain checksums + pos, self.magic, self.cmd, self.len, self.checksum, self.ver_raw, self.service, self.timestamp, ra, sa, self.nodeid, - self.subver, self.lastblock = bin.unpack(" 31402 ) then @@ -277,18 +374,14 @@ Response = { -- @return response instance of response packet if status is true -- err string containing the error message if status is false recvPacket = function(socket, version) - local status, header = socket:recv(20) + local status, header = socket:recv(24) if ( not(status) ) then return false, "Failed to read the packet header" end - local pos, magic, cmd, len = bin.unpack(" 1 ) then + return stdnse.format_output(true, tab.dump(response) ) + end end