1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
---
-- This library implements the fundamentals needed to communicate with the
-- WinPcap Remote Capture Daemon. It currently supports authenticating to
-- the service using either NULL-, or Password-based authentication.
-- In addition it has the capabilities to list the interfaces that may be
-- used for sniffing.
--
-- The library consist of classes handling <code>Request</code> and classes
-- handling <code>Response</code>. The communication with the service is
-- handled by the <code>Comm</code> class, and the main interface for script
-- writers is kept under the <code>Helper</code> class.
--
-- The following code snippet illustrates how to connect to the service and
-- extract information about network interfaces:
-- <code>
-- local helper = rpcap.Helper:new(host, port)
-- helper:connect()
-- helper:login()
-- helper:findAllInterfaces()
-- helper:close()
-- </code>
--
-- For a more complete example, consult the rpcap-info.nse script.
--
-- @author Patrik Karlsson <patrik@cqure.net>
local ipOps = require "ipOps"
local match = require "match"
local nmap = require "nmap"
local stdnse = require "stdnse"
local string = require "string"
local table = require "table"
_ENV = stdnse.module("rpcap", stdnse.seeall)
RPCAP = {
MessageType = {
ERROR = 1,
FIND_ALL_INTERFACES = 2,
AUTH_REQUEST = 8,
},
-- Holds the two supported authentication mechanisms PWD and NULL
Authentication = {
PWD = {
new = function(self, username, password)
local o = {
type = 1,
username = username,
password = password,
}
setmetatable(o, self)
self.__index = self
return o
end,
__tostring = function(self)
local DUMMY = 0
return string.pack(">I2I2I2I2", self.type, DUMMY, #self.username, #self.password) .. self.username .. self.password
end,
},
NULL = {
new = function(self)
local o = {
type = 0,
}
setmetatable(o, self)
self.__index = self
return o
end,
__tostring = function(self)
local DUMMY = 0
return string.pack(">I2I2I2I2", self.type, DUMMY, 0, 0)
end,
}
},
-- The common request and response header
Header = {
size = 8,
new = function(self, type, value, length)
local o = {
version = 0,
type = type,
value= value or 0,
length = length or 0
}
setmetatable(o, self)
self.__index = self
return o
end,
parse = function(data)
local header = RPCAP.Header:new()
header.version, header.type, header.value, header.length = string.unpack(">BBI2I4", data)
return header
end,
__tostring = function(self)
return string.pack(">BBI2I4", self.version, self.type, self.value, self.length)
end,
},
-- The implemented request types are kept here
Request = {
Authentication = {
new = function(self, data)
local o = {
header = RPCAP.Header:new(RPCAP.MessageType.AUTH_REQUEST, nil, #data),
data = data,
}
setmetatable(o, self)
self.__index = self
return o
end,
__tostring = function(self)
return tostring(self.header) .. tostring(self.data)
end,
},
FindAllInterfaces = {
new = function(self)
local o = {
header = RPCAP.Header:new(RPCAP.MessageType.FIND_ALL_INTERFACES)
}
setmetatable(o, self)
self.__index = self
return o
end,
__tostring = function(self)
return tostring(self.header)
end,
}
},
-- Parsers for responses are kept here
Response = {
Authentication = {
new = function(self)
local o = { }
setmetatable(o, self)
self.__index = self
return o
end,
parse = function(data)
local resp = RPCAP.Response.Authentication:new()
local pos = RPCAP.Header.size + 1
resp.header = RPCAP.Header.parse(data)
return resp
end
},
Error = {
new = function(self)
local o = { }
setmetatable(o, self)
self.__index = self
return o
end,
parse = function(data)
local err = RPCAP.Response.Error:new()
local pos = RPCAP.Header.size + 1
err.header = RPCAP.Header.parse(data)
err.error, pos = string.unpack("c" .. err.header.length, data, pos)
return err
end
},
FindAllInterfaces = {
new = function(self)
local o = { }
setmetatable(o, self)
self.__index = self
return o
end,
parse = function(data)
-- Each address is made up of 4 128 byte fields, this function
-- parses these fields and return the response, if it
-- understands it. Otherwise it simply increases the pos by the
-- correct offset, to get us to the next field.
local function parseField(data, pos)
local offset = pos
local family, port
family, port, pos = string.unpack(">I2I2", data, pos)
if ( family == 0x0017 ) then
-- not sure why...
pos = pos + 4
local ipv6 = ipOps.str_to_ip(data:sub(pos, pos + 16 - 1))
return offset + 128, ipv6
elseif ( family == 0x0002 ) then
local ipv4 = ipOps.str_to_ip(data:sub(pos, pos + 4 - 1))
return offset + 128, ipv4
end
return offset + 128, nil
end
-- Parses one of X addresses returned for an interface
local function parseAddress(data, pos)
local fields = {"ip", "netmask", "bcast", "p2p"}
local addr = {}
for _, f in ipairs(fields) do
pos, addr[f] = parseField(data, pos)
end
return pos, addr
end
local resp = RPCAP.Response.FindAllInterfaces:new()
local pos = RPCAP.Header.size + 1
resp.header = RPCAP.Header.parse(data)
resp.ifaces = {}
for i=1, resp.header.value do
local name_len, desc_len, iface_flags, addr_count, dummy
name_len, desc_len, iface_flags, addr_count, dummy, pos = string.unpack(">I2I2I4I2I2", data, pos)
local name, desc
name, desc, pos = string.unpack("c" .. name_len .. "c" .. desc_len, data, pos)
local addrs = {}
for j=1, addr_count do
local addr
pos, addr = parseAddress(data, pos)
local cidr
if ( addr.netmask ) then
table.insert(addrs, addr.ip .. ipOps.subnet_to_cidr(addr.netmask))
else
table.insert(addrs, addr.ip)
end
end
table.insert(resp.ifaces, { name = name, desc = desc, addrs = addrs })
end
return resp
end,
}
}
}
-- Maps packet types to classes
RPCAP.TypeToClass = {
[1] = RPCAP.Response.Error,
[130] = RPCAP.Response.FindAllInterfaces,
[136] = RPCAP.Response.Authentication,
}
-- The communication class
Comm = {
-- Creates a new instance of the Comm class
-- @param host table
-- @param port table
-- @return o instance of Comm
new = function(self, host, port, socket)
local o = { host = host, port = port, socket = socket or nmap.new_socket() }
setmetatable(o, self)
self.__index = self
return o
end,
-- Connects the socket to the server
connect = function(self)
return self.socket:connect(self.host, self.port)
end,
-- Sends an instance of the request class to the server
-- @param req class instance
-- @return status true on success, false on failure
-- @return err string containing error message if status is false
send = function(self, req)
return self.socket:send(req)
end,
-- receives a packet and attempts to parse it if it has a supported parser
-- in RPCAP.TypeToClass
-- @return status true on success, false on failure
-- @return resp instance of a Response class or
-- err string containing the error message
recv = function(self)
local status, hdr_data = self.socket:receive_buf(match.numbytes(RPCAP.Header.size), true)
if ( not(status) ) then
return status, hdr_data
end
local header = RPCAP.Header.parse(hdr_data)
if ( not(header) ) then
return false, "rpcap: Failed to parse header"
end
local status, data = self.socket:receive_buf(match.numbytes(header.length), true)
if ( not(status) ) then
return false, "rpcap: Failed to read packet data"
end
if ( RPCAP.TypeToClass[header.type] ) then
local resp = RPCAP.TypeToClass[header.type].parse(hdr_data .. data)
if ( resp ) then
return true, resp
end
end
return false, "Failed to receive response from server"
end,
-- Sends and request and receives the response
-- @param req the instance of the Request class to send
-- @return status true on success, false on failure
-- @return resp instance of a Response class or
-- err string containing the error message
exch = function(self, req)
local status, data = self:send(tostring(req))
if ( not(status) ) then
return status, data
end
return self:recv()
end,
-- closes the socket
close = function(self)
return self.socket:close()
end,
}
Helper = {
-- Creates a new instance of the Helper class
-- @param host table
-- @param port table
-- @return o instance of Helper
new = function(self, host, port)
local o = {
host = host,
port = port,
comm = Comm:new(host, port)
}
setmetatable(o, self)
self.__index = self
return o
end,
-- Connects to the server
connect = function(self)
return self.comm:connect(self.host, self.port)
end,
-- Authenticates to the service, in case no username or password is given
-- NULL authentication is assumed.
-- @param username [optional]
-- @param password [optional]
-- @return status true on success, false on failure
-- @return err string containing error message on failure
login = function(self, username, password)
local auth
if ( username and password ) then
auth = RPCAP.Authentication.PWD:new(username, password)
else
auth = RPCAP.Authentication.NULL:new()
end
local req = RPCAP.Request.Authentication:new(tostring(auth))
local status, resp = self.comm:exch(req)
if ( not(status) ) then
return false, resp
end
if ( status and resp.error ) then
return false, resp.error
end
return true
end,
-- Requests a list of all interfaces
-- @return table containing interfaces and addresses
findAllInterfaces = function(self)
local req = RPCAP.Request.FindAllInterfaces:new()
local status, resp = self.comm:exch(req)
if ( not(status) ) then
return false, resp
end
local results = {}
for _, iface in ipairs(resp.ifaces) do
local entry = {}
entry.name = iface.name
table.insert(entry, iface.desc)
table.insert(entry, { name = "Addresses", iface.addrs })
table.insert(results, entry)
end
return true, results
end,
-- Closes the connection to the server
close = function(self)
return self.comm:close()
end,
}
return _ENV;
|