Skip to content

Commit

Permalink
MongoDB wire protocol is using signed int32. Fixes #1802
Browse files Browse the repository at this point in the history
Unlike the old bin library, Lua string.pack does not support silent conversions:
Negative integers cannot be "I" packed and 0xFFFFFFFF cannot be packed with "i4"
  • Loading branch information
nnposter committed Nov 17, 2019
1 parent 45994bf commit 67fe6bb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Expand Up @@ -10,6 +10,9 @@ o [Windows] Add support for the new loopback behavior in Npcap 0.9983. This
Adapter to be installed, which was a source of problems for some users.
[Daniel Miller]

o [NSE][GH1802] The MongoDB library was causing errors when assembling protocol
payloads. [nnposter]

o [NSE][GH#1781][GH#1796] The RTSP library was not correctly generating request
strings. [nnposter]

Expand Down
20 changes: 10 additions & 10 deletions nselib/mongodb.lua
Expand Up @@ -373,10 +373,10 @@ MongoData ={
return o
end
}
--Adds unsigned int32 to the message body
--Adds signed int32 to the message body
--@param value the value to add
function MongoData:addUnsignedInt32(value)
self.valueString = self.valueString..string.pack("<I4",value)
function MongoData:addInt32(value)
self.valueString = self.valueString..string.pack("<i4",value)
end
-- Adds a string to the message body
--@param value the string to add
Expand All @@ -402,10 +402,10 @@ end
-- This method creates necessary header information and puts it with the body
function MongoData:data()
local header = MongoData:new()
header:addUnsignedInt32( self.valueString:len()+4+4+4+4)
header:addUnsignedInt32( self.requestID)
header:addUnsignedInt32( self.responseTo or 0xFFFFFFFF)
header:addUnsignedInt32( self.opCode)
header:addInt32( self.valueString:len()+4+4+4+4)
header:addInt32( self.requestID)
header:addInt32( self.responseTo or -1)
header:addInt32( self.opCode)
return header.valueString .. self.valueString
end
-- Creates a query
Expand All @@ -415,10 +415,10 @@ end
--@return packet data OR error message
local function createQuery(collectionName, query)
local packet = MongoData:new({opCode=MongoData.OP.QUERY})
packet:addUnsignedInt32(0); -- options
packet:addInt32(0); -- options
packet:addString(collectionName);
packet:addUnsignedInt32(0) -- number to skip
packet:addUnsignedInt32(-1) -- number to return : no limit
packet:addInt32(0) -- number to skip
packet:addInt32(-1) -- number to return : no limit
local status, error = packet:addBSON(query)
if not status then
Expand Down

0 comments on commit 67fe6bb

Please sign in to comment.