diff --git a/nselib/ajp.lua b/nselib/ajp.lua index 0e1d4a432..1d0bfbf7d 100644 --- a/nselib/ajp.lua +++ b/nselib/ajp.lua @@ -531,7 +531,7 @@ Helper = { -- should only work against 127.0.0.1 shutdownContainer = function(self) - self.comm:send(bin.pack("H", "1234000107")) + self.comm:send("\x12\x34\x00\x01\x07") self.comm:receive() end, diff --git a/nselib/asn1.lua b/nselib/asn1.lua index 0aa81fb7d..0a2053e9e 100644 --- a/nselib/asn1.lua +++ b/nselib/asn1.lua @@ -61,12 +61,8 @@ ASN1Decoder = { -- Boolean self.decoder["01"] = function( self, encStr, elen, pos ) - local pos, val = bin.unpack("H", encStr, pos) - if val ~= "FF" then - return pos, true - else - return pos, false - end + local val = string.byte(encStr, pos) + return pos + 1, val ~= 0xFF end -- Integer @@ -136,7 +132,8 @@ ASN1Decoder = { local etype, elen local newpos = pos - newpos, etype = bin.unpack("H1", encStr, newpos) + etype, newpos = string.unpack("c1", encStr, newpos) + etype = stdnse.tohex(etype) newpos, elen = self.decodeLength(encStr, newpos) if self.decoder[etype] then @@ -250,12 +247,7 @@ ASN1Decoder = { -- @return The position after decoding. -- @return The decoded integer. decodeInt = function(encStr, len, pos) - local hexStr - pos, hexStr = bin.unpack("H" .. len, encStr, pos) - local value = tonumber(hexStr, 16) - if (value >= (256^len)/2) then - value = value - 256^len - end + local value, pos = string.unpack(">i" .. len, encStr, pos) return pos, value end, @@ -281,7 +273,7 @@ ASN1Encoder = { encodeSeq = function(self, seqData) -- 0x30 = 00110000 = 00 1 10000 -- hex binary Universal Constructed value Data Type = SEQUENCE (16) - return bin.pack('HAA' , '30', self.encodeLength(#seqData), seqData) + return "\x30" .. self.encodeLength(#seqData) .. seqData end, --- @@ -341,9 +333,9 @@ ASN1Encoder = { -- Boolean encoder self.encoder['boolean'] = function( self, val ) if val then - return bin.pack('H','01 01 FF') + return '\x01\x01\xFF' else - return bin.pack('H', '01 01 00') + return '\x01\x01\x00' end end @@ -352,25 +344,25 @@ ASN1Encoder = { assert('table' == type(val), "val is not a table") assert(#val.type > 0, "Table is missing the type field") assert(val.value ~= nil, "Table is missing the value field") - return bin.pack("HAA", val.type, self.encodeLength(#val.value), val.value) + return stdnse.fromhex(val.type) .. self.encodeLength(#val.value) .. val.value end -- Integer encoder self.encoder['number'] = function( self, val ) local ival = self.encodeInt(val) local len = self.encodeLength(#ival) - return bin.pack('HAA', '02', len, ival) + return "\x02" .. len .. ival end -- Octet String encoder self.encoder['string'] = function( self, val ) local len = self.encodeLength(#val) - return bin.pack('HAA', '04', len, val) + return "\x04" .. len .. val end -- Null encoder self.encoder['nil'] = function( self, val ) - return bin.pack('H', '05 00') + return '\x05\x00' end end, diff --git a/nselib/bittorrent.lua b/nselib/bittorrent.lua index 1ce6f4b3c..c5b6f9983 100644 --- a/nselib/bittorrent.lua +++ b/nselib/bittorrent.lua @@ -325,9 +325,7 @@ local dht_ping_thread = function(pnt, timeout) local peer_ip, peer_info = next(pnt.peers_dht_ping) --transaction ids are 2 bytes long - local t_ID_hex = stdnse.tohex(transaction_id % 0xffff) - t_ID_hex = string.rep("0",4-#t_ID_hex)..t_ID_hex - peer_info.transaction_id = bin.pack("H",t_ID_hex) + peer_info.transaction_id = string.pack(">I2",transaction_id % 0xffff) -- mark it as received so we can distinguish from the others and -- successfully iterate while receiving @@ -608,7 +606,7 @@ Torrent = if not info_hash_hex then return false, "Erroneous magnet link" end - self.info_hash = bin.pack("H",info_hash_hex) + self.info_hash = stdnse.fromhex(info_hash_hex) local pos = #info_hash_hex + 21 local name = magnet:sub(pos,#magnet):match("^&dn=(.-)&") @@ -1140,23 +1138,23 @@ Torrent = -- The initial connection parameters' variables have hello_ prefixed names local hello_transaction_id = openssl.rand_bytes(4) - local hello_action = "00 00 00 00" -- 0 for a connection request - local hello_connection_id = "00 00 04 17 27 10 19 80" -- identification of the protocol - local hello_packet = bin.pack("HHA", hello_connection_id, hello_action, hello_transaction_id) + local hello_packet = "\0\0\x04\x17\x27\x10\x19\x80" -- identification of the protocol + .. "\0\0\0\0" -- 0 for a connection request + .. hello_transaction_id local status, msg = socket:sendto(host, port, hello_packet) if not status then return false, msg end status, msg = socket:receive() if not status then return false, "Could not connect to tracker:"..tracker.." reason:"..msg end - local _, r_action, r_transaction_id, r_connection_id =bin.unpack("H4A4A8",msg) + local r_action, r_transaction_id, r_connection_id =string.unpack(">I4c4c8",msg) if not (r_transaction_id == hello_transaction_id) then return false, "Received transaction ID not equivalent to sent transaction ID" end -- the action in the response has to be 0 too - if r_action ~= "00000000" then + if r_action ~= 0 then return false, "Wrong action field, usually caused by an erroneous request" end @@ -1164,24 +1162,24 @@ Torrent = -- response holds the peers -- the announce connection parameters' variables are prefixed with a_ - local a_action = "00 00 00 01" -- 1 for announce + local a_action = 1 -- 1 for announce local a_transaction_id = openssl.rand_bytes(4) local a_info_hash = self.info_hash -- info_hash of the torrent local a_peer_id = self:generate_peer_id() - local a_downloaded = "00 00 00 00 00 00 00 00" -- 0 bytes downloaded + local a_downloaded = 0 -- 0 bytes downloaded - local a_left = stdnse.tohex(self.size) -- bytes left to download is the size of torrent - a_left = string.rep("0", 16-#a_left) .. a_left + local a_left = self.size -- bytes left to download is the size of torrent - local a_uploaded = "00 00 00 00 00 00 00 00" -- 0 bytes uploaded - local a_event = "00 00 00 02" -- value of 2 for started torrent - local a_ip = "00 00 00 00" -- not necessary to specify our ip since it's resolved + local a_uploaded = 0 -- 0 bytes uploaded + local a_event = 2 -- value of 2 for started torrent + local a_ip = 0 -- not necessary to specify our ip since it's resolved -- by tracker automatically local a_key = openssl.rand_bytes(4) - local a_num_want = "FF FF FF FF" -- request for many many peers - local a_port = "1A E1" -- 6881 the port "we are listening on" - local a_extensions = "00 00" -- client recognizes no extensions of the bittorrent proto - local announce_packet = bin.pack("AHAAAHHHHHAHHH", r_connection_id, a_action, a_transaction_id, + local a_num_want = 0xFFFFFFFF -- request for many many peers + local a_port = 6881 -- the port "we are listening on" + local a_extensions = 0 -- client recognizes no extensions of the bittorrent proto + local announce_packet = string.pack(">c8 I4 c4 c40 c20 I8 I8 I8 I4 I4 c4 I4 I2 I2", + r_connection_id, a_action, a_transaction_id, a_info_hash, a_peer_id, a_downloaded, a_left, a_uploaded, a_event, a_ip, a_key, a_num_want, a_port, a_extensions) @@ -1194,10 +1192,10 @@ Torrent = if not status then return false, "Didn't receive response to announce message, reason: "..msg end - local pos, p_action, p_transaction_id, p_interval, p_leechers, p_seeders = bin.unpack("H4A4H4H4H4",msg) + local p_action, p_transaction_id, p_interval, p_leechers, p_seeders, pos = string.unpack(">I4 c4 I4 I4 I4",msg) -- the action field in the response has to be 1 (like the sent response) - if not (p_action == "00000001") then + if not (p_action == 1) then return false, "Action in response to announce erroneous" end if not (p_transaction_id == a_transaction_id) then