PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/nselib/sip.lua

https://github.com/prakashgamit/nmap
Lua | 836 lines | 459 code | 140 blank | 237 comment | 62 complexity | d00c2e065e727c7446ac7a43464d0c72 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-2.0, LGPL-2.1
  1. --- A SIP library supporting a limited subset of SIP commands and methods
  2. --
  3. -- The library currently supports the following methods:
  4. -- * REGISTER, INVITE & OPTIONS
  5. --
  6. -- Overview
  7. -- --------
  8. -- The library consists of the following classes:
  9. --
  10. -- o SessionData
  11. -- - Holds session data for the SIP session
  12. --
  13. -- o Session
  14. -- - Contains application functionality related to the implemented
  15. -- SIP methods.
  16. --
  17. -- o Connection
  18. -- - A class containing code related to socket communication.
  19. --
  20. -- o Response
  21. -- - A class containing code for handling SIP responses
  22. --
  23. -- o Request
  24. -- - A class containing code for handling SIP requests
  25. --
  26. -- o Util
  27. -- - A class containing static utility functions
  28. --
  29. -- o SIPAuth
  30. -- - A class containing code related to SIP Authentication
  31. --
  32. -- o Helper
  33. -- - A class containing code used as a primary interface by scripts
  34. --
  35. --
  36. -- @author "Patrik Karlsson <patrik@cqure.net>"
  37. -- @copyright Same as Nmap--See http://nmap.org/book/man-legal.html
  38. --
  39. -- @args sip.timeout - specifies the session (socket) timeout in seconds
  40. -- Version 0.1
  41. -- Created 2011/03/30 - v0.1 - created by Patrik Karlsson <patrik@cqure.net>
  42. local bin = require "bin"
  43. local math = require "math"
  44. local nmap = require "nmap"
  45. local os = require "os"
  46. local stdnse = require "stdnse"
  47. local openssl = stdnse.silent_require "openssl"
  48. local string = require "string"
  49. local table = require "table"
  50. _ENV = stdnse.module("sip", stdnse.seeall)
  51. -- Method constants
  52. Method = {
  53. ACK = "ACK",
  54. INVITE = "INVITE",
  55. OPTIONS = "OPTIONS",
  56. REGISTER = "REGISTER",
  57. }
  58. -- Error constants
  59. Error = {
  60. TRYING = 100,
  61. RING = 180,
  62. TIMEOUT = 408,
  63. BUSY = 486,
  64. DECLINE = 603,
  65. OK = 200,
  66. UNAUTHORIZED = 401,
  67. FORBIDDEN = 403,
  68. NOTFOUND = 404,
  69. PROXY_AUTH_REQUIRED = 407,
  70. }
  71. -- The SessionData class
  72. SessionData = {
  73. --- Creates a new instance of sessiondata
  74. -- @return o an instance of SessionData
  75. new = function(self, o)
  76. local o = o or {}
  77. setmetatable(o, self)
  78. self.__index = self
  79. o.user = "user"
  80. return o
  81. end,
  82. --- Sets the session username
  83. -- @param user string containing the username
  84. setUsername = function(self, user) self.user = user end,
  85. --- Sets the session password
  86. -- @param pass string containing the password
  87. setPassword = function(self, pass) self.pass = pass end,
  88. --- Sets the SIP domain
  89. -- @param domain string containing the SIP domain
  90. setDomain = function(self, domain) self.domain = domain end,
  91. --- Sets the ip and port of the remote server
  92. -- @param host string containing the ip of the remote server
  93. -- @param port number containing the port of the remote server
  94. setServer = function(self, host, port) self.server = { host = host, port = port } end,
  95. --- Sets the ip and port of the client
  96. -- @param host string containing the ip of the client
  97. -- @param port number containing the port of the client
  98. setClient = function(self, host, port) self.client = { host = host, port = port } end,
  99. --- Sets the SIP users Full Name
  100. -- @param name string containing the full name of the user
  101. setName = function(self, name) self.name = name end,
  102. --- Retrieves the username
  103. -- @return user string containing the sessions username
  104. getUsername = function(self) return self.user end,
  105. --- Retrieves the session password
  106. -- @return pass string containing the session password
  107. getPassword = function(self) return self.pass end,
  108. --- Retrieves the SIP domain
  109. -- @return domain string containing the SIP domain
  110. getDomain = function(self) return self.domain end,
  111. --- Retrieves the client IP and port
  112. -- @return host string containing the client IP
  113. -- @return port number containing the client port
  114. getClient = function(self) return self.client.host, self.client.port end,
  115. --- Retrieves the server IP and port
  116. -- @return host string containing the server IP
  117. -- @return port number containing the server port
  118. getServer = function(self) return self.server.host, self.server.port end,
  119. --- Retrieves the SIP users full name
  120. -- @return name string containing the users full name
  121. getName = function(self) return self.name or "Nmap NSE" end,
  122. }
  123. -- The session class holds the code necessary to register a SIP session
  124. Session = {
  125. --- Creates a new session instance
  126. -- @param host table containing the remote host to connect to
  127. -- @param port table containing the remote port to connect to
  128. -- @param sessdata instance of SessionData
  129. -- @param options table containing zero or more of the following options
  130. -- <code>expires</code> - the expire value in seconds
  131. -- <code>timeout</code> - the socket timeout in seconds
  132. -- @return o containing a new instance of the Session class
  133. new = function(self, host, port, sessdata, options)
  134. local o = {}
  135. setmetatable(o, self)
  136. self.__index = self
  137. o.protocol = port.protocol:upper()
  138. o.expires = (options and options.expires) or 300
  139. o.conn = Connection:new(host,port)
  140. o.cseq = (options and options.cseq) or 1234
  141. local timeout = ( ( options and options.timeout ) and
  142. options.timeout * 1000 ) or 5000
  143. o.conn.socket:set_timeout( timeout )
  144. o.sessdata = sessdata or SessionData:new()
  145. return o
  146. end,
  147. --- Connect the session
  148. -- @return true on success, false on failure
  149. -- @return err string containing error message
  150. connect = function(self)
  151. local status, err = self.conn:connect()
  152. if (not(status)) then
  153. return false, "ERROR: Failed to connect to server"
  154. end
  155. local status, lhost, lport, rhost, rport = self.conn.socket:get_info()
  156. if ( not(status) ) then
  157. return false, "Failed to retreive socket information"
  158. end
  159. self.sessdata:setClient(lhost, lport)
  160. self.sessdata:setServer(rhost, rport)
  161. return true
  162. end,
  163. --- Closes the session
  164. -- TODO: We should probably send some "closing" packets here
  165. -- @return true on success, false on failure
  166. close = function(self) return self.conn:close() end,
  167. --- Sends and SIP invite
  168. -- @param uri
  169. invite = function(self, uri)
  170. local request = Request:new(Method.INVITE, self.protocol)
  171. local lhost, _ = self.sessdata:getClient()
  172. local tm = os.time()
  173. local uri = (uri and uri:match("^sip:.*@.*")) or
  174. ("sip:%s@%s"):format(uri, self.sessdata:getDomain())
  175. request:setUri(uri)
  176. request:setSessionData(self.sessdata)
  177. local data = {}
  178. table.insert(data, "v=0")
  179. table.insert(data, ("o=- %s %s IN IP4 %s"):format(tm, tm, lhost))
  180. table.insert(data, "s=-")
  181. table.insert(data, ("c=IN IP4 %s"):format(lhost))
  182. table.insert(data, "t=0 0")
  183. table.insert(data, "m=audio 49174 RTP/AVP 0")
  184. table.insert(data, "a=rtpmap:0 PCMU/8000")
  185. request:setContent(stdnse.strjoin("\r\n", data))
  186. request:setContentType("application/sdp")
  187. local status, response = self:exch(request)
  188. if ( not(status) ) then return false, response end
  189. local errcode = response:getErrorCode()
  190. if ( Error.PROXY_AUTH_REQUIRED == errcode or
  191. Error.UNAUTHORIZED == errcode ) then
  192. -- Send an ACK to the server
  193. request:setMethod(Method.ACK)
  194. local status, err = self.conn:send( tostring(request) )
  195. if ( not(status) ) then return status, "ERROR: Failed to send request" end
  196. -- Send an authenticated INVITE to the server
  197. request:setMethod(Method.INVITE)
  198. self.cseq = self.cseq + 1
  199. status, data = self:authenticate(request, response)
  200. if ( not(status) ) then return false, "SIP Authentication failed" end
  201. response = Response:new(data)
  202. -- read a bunch of 180 Ringing and 100 Trying requests, until we get a 200 OK
  203. while ( response:getErrorCode() ~= Error.OK ) do
  204. status, data = self.conn:recv()
  205. if ( not(status) ) then return status, "ERROR: Failed to receive response" end
  206. response = Response:new(data)
  207. end
  208. end
  209. return true
  210. end,
  211. --- Prepares and sends the challenge response authentication to the server
  212. -- @param request instance of the request object requiring authentication
  213. -- @param authdata string containing authentication data
  214. -- @return status true on success false on failure
  215. -- @return err string containing an error message if status is false
  216. authenticate = function(self, request, response)
  217. local rhost, _ = self.sessdata:getServer()
  218. local auth_header, auth_data = response:getAuthData()
  219. local auth = SipAuth:new(auth_data)
  220. auth:setUsername(self.sessdata:getUsername())
  221. auth:setPassword(self.sessdata:getPassword())
  222. auth:setMethod(request.method)
  223. auth:setUri(("sip:%s"):format(rhost))
  224. if ( auth_header == "WWW-Authenticate" ) then
  225. request:setWWWAuth(auth:createResponse())
  226. else
  227. request:setProxyAuth(auth:createResponse())
  228. end
  229. request:setCseq(self.cseq)
  230. local status, err = self.conn:send( tostring(request) )
  231. if ( not(status) ) then return status, "ERROR: Failed to send request" end
  232. local data
  233. status, data = self.conn:recv()
  234. if ( not(status) and data ~= "TIMEOUT" ) then
  235. return status, "ERROR: Failed to receive response"
  236. end
  237. return status, data
  238. end,
  239. --- Sends a SIP Request and receives the Response
  240. -- @param request instance of Request
  241. -- @return status true on success, false on failure
  242. -- @return resp containing a new Response instance
  243. -- err containing error message if status is false
  244. exch = function(self, request)
  245. request:setCseq(self.cseq)
  246. local status, err = self.conn:send( tostring(request) )
  247. if ( not(status) ) then return status, "ERROR: Failed to send request" end
  248. local status, data = self.conn:recv()
  249. if ( not(status) ) then return status, "ERROR: Failed to receive response" end
  250. return true, Response:new(data)
  251. end,
  252. --- Sends a register request to the server
  253. -- @return status true on success, false on failure
  254. -- @return msg string containing the error message (if status is false)
  255. register = function(self)
  256. local request = Request:new(Method.REGISTER, self.protocol)
  257. request:setUri("sip:" .. self.sessdata:getServer())
  258. request:setSessionData(self.sessdata)
  259. request:setExpires(self.expires)
  260. local status, response = self:exch(request)
  261. if (not(status)) then return false, response end
  262. local errcode = response:getErrorCode()
  263. if ( status and errcode == Error.OK ) then
  264. return true, response
  265. elseif ( Error.PROXY_AUTH_REQUIRED == errcode or Error.UNAUTHORIZED == errcode ) then
  266. local data
  267. self.cseq = self.cseq + 1
  268. status, data = self:authenticate(request, response)
  269. response = Response:new(data)
  270. errcode = response:getErrorCode()
  271. if ( not(status) or ( errcode and errcode ~= Error.OK ) ) then
  272. return false, "ERROR: Failed to authenticate"
  273. end
  274. elseif ( Error.FORBIDDEN == errcode ) then
  275. return false, "Authentication forbidden"
  276. else
  277. return false, ("Unhandled error: %d"):format(errcode)
  278. end
  279. return true
  280. end,
  281. --- Sends an option request to the server and handles the response
  282. -- @return status true on success, false on failure
  283. -- @return response if status is true, nil else.
  284. options = function(self)
  285. local req = Request:new(Method.OPTIONS, self.protocol)
  286. req:setUri("sip:" .. self.sessdata:getServer())
  287. req:setSessionData(self.sessdata)
  288. req:setExpires(self.expires)
  289. req:addHeader("Accept", "application/sdp")
  290. local status, response = self:exch(req)
  291. if status then return true, response end
  292. return false, nil
  293. end,
  294. }
  295. -- The connection class contains basic communication code
  296. Connection = {
  297. --- Creates a new SIP Connection
  298. -- @param host table containing the host to connect to
  299. -- @param port table containing the port to connect to
  300. -- @return o containing a new Connection instance
  301. new = function(self, host, port)
  302. local o = {}
  303. setmetatable(o, self)
  304. self.__index = self
  305. o.host = host
  306. o.port = port
  307. o.socket = nmap.new_socket()
  308. return o
  309. end,
  310. --- Connects to the server
  311. -- @return status containing true on success and false on failure
  312. -- @return err containing the error message (if status is false)
  313. connect = function(self)
  314. local status, err = self.socket:connect(self.host, self.port)
  315. if ( status ) then
  316. local status, lhost, lport, _, _ = self.socket:get_info()
  317. if ( status ) then
  318. self.lhost = lhost
  319. self.lport = lport
  320. end
  321. end
  322. return status, err
  323. end,
  324. --- Sends the data over the socket
  325. -- @return status true on success, false on failure
  326. send = function(self, data)
  327. return self.socket:send(data)
  328. end,
  329. --- Receives data from the socket
  330. -- @return status true on success, false on failure
  331. recv = function(self)
  332. return self.socket:receive()
  333. end,
  334. --- Closes the communication channel (socket)
  335. -- @return true on success false on failure
  336. close = function(self)
  337. return self.socket:close()
  338. end,
  339. --- Retrieves the client ip and port
  340. -- @return lhost string containing the local ip
  341. -- @return lport number containing the local port
  342. getClient = function(self) return self.lhost, self.lport end,
  343. --- Retrieves the server ip and port
  344. -- @return rhost string containing the server ip
  345. -- @return rport number containing the server port
  346. getServer = function(self) return ( self.host.ip or self.host ), ( self.port.number or self.port ) end,
  347. }
  348. -- The response class holds the necessary methods and parameters to parse a response
  349. Response = {
  350. --- Creates a new Response instance
  351. -- @param str containing the data as received over the socket
  352. -- @return o table containing a new Response instance
  353. new = function(self, str)
  354. local o = {}
  355. setmetatable(o, self)
  356. self.__index = self
  357. o.tbl = stdnse.strsplit("\r\n", str)
  358. return o
  359. end,
  360. --- Retrieves a given header value from the response
  361. -- @param name string containing the name of the header
  362. -- @return value string containing the header value
  363. getHeader = function(self,name)
  364. for _, line in ipairs(self.tbl) do
  365. local header, value = line:match("^(.-): (.*)$")
  366. if ( header and header:lower() == name:lower() ) then
  367. return value
  368. end
  369. end
  370. end,
  371. --- Returns the error code from the SIP response
  372. -- @return err number containing the error code
  373. getErrorCode = function(self)
  374. return tonumber(self.tbl[1]:match("SIP/%d%.%d (%d+)"))
  375. end,
  376. --- Returns the error message returned by the server
  377. -- @return errmsg string containing the error message
  378. getErrorMessage = function(self)
  379. return self.tbl[1]:match("^SIP/%d%.%d %d+ (.+)$")
  380. end,
  381. --- Returns the message method
  382. -- @return method string containing the method
  383. getMethod = function(self)
  384. return self.tbl[1]:match("^(.-)%s.*SIP/2%.0$")
  385. end,
  386. --- Returns the authentication data from the SIP response
  387. -- @return auth string containing the raw authentication data
  388. getAuthData = function(self)
  389. local auth = self:getHeader("WWW-Authenticate") or self:getHeader("Proxy-Authenticate")
  390. if ( auth ) then
  391. return ( self:getHeader("WWW-Authenticate") and
  392. "WWW-Authenticate" or
  393. "Proxy-Authenticate"), auth
  394. end
  395. end,
  396. --- Retrieves the current sequence number
  397. -- @return cseq number containing the current sequence number
  398. getCSeq = function(self)
  399. local cseq = self:getHeader("CSeq")
  400. cseq = (cseq and cseq:match("^(%d+)"))
  401. return (cseq and tonumber(cseq))
  402. end,
  403. }
  404. -- The request class holds the necessary functions and parameters for a basic SIP request
  405. Request = {
  406. --- Creates a new Request instance
  407. -- @param method string containing the request method to use
  408. -- @param proto Used protocol, could be "UDP" or "TCP"
  409. -- @return o containing a new Request instance
  410. new = function(self, method, proto)
  411. local o = {}
  412. setmetatable(o, self)
  413. self.__index = self
  414. o.ua = "Nmap NSE"
  415. o.protocol = proto or "UDP"
  416. o.expires = 0
  417. o.allow = "PRACK, INVITE ,ACK, BYE, CANCEL, UPDATE, SUBSCRIBE"
  418. .. ",NOTIFY, REFER, MESSAGE, OPTIONS"
  419. o.maxfwd = 70
  420. o.method = method
  421. o.length = 0
  422. o.cid = Util.get_random_string(60)
  423. return o
  424. end,
  425. --- Sets the sessiondata so that session information may be fetched
  426. -- @param data instance of SessionData
  427. setSessionData = function(self, data) self.sessdata = data end,
  428. --- Adds a custom header to the request
  429. -- @param name string containing the header name
  430. -- @param value string containing the header value
  431. addHeader = function(self, name, value)
  432. self.headers = self.headers or {}
  433. table.insert(self.headers, ("%s: %s"):format(name, value))
  434. end,
  435. --- Sets the SIP uri
  436. -- @param uri string containing the SIP uri
  437. setUri = function(self, uri) self.uri = uri end,
  438. --- Sets an error
  439. -- @param code number containing the error code
  440. -- @param msg string containing the error message
  441. setError = function(self, code, msg) self.error = { code = code, msg = msg } end,
  442. --- Sets the request method
  443. -- @param method string containing a valid SIP method (@see Method constant)
  444. setMethod = function(self, method) self.method = method end,
  445. --- Sets the sequence number
  446. -- @param seq number containing the sequence number to set
  447. setCseq = function(self, seq) self.cseq = seq end,
  448. --- Sets the allow header
  449. -- @param allow table containing all of the allowed SIP methods
  450. setAllow = function(self, allow) self.allow = stdnse.strjoin(", ", allow) end,
  451. --- Sets the request content data
  452. -- @param string containing the content data
  453. setContent = function(self, content) self.content = content end,
  454. --- Sets the requests' content type
  455. -- @param t string containing the content type
  456. setContentType = function(self, t) self.content_type = t end,
  457. --- Sets the supported SIP methods
  458. -- @param supported string containing the supported methods
  459. setSupported = function(self, supported) self.supported = supported end,
  460. --- Sets the content-length of the SIP request
  461. -- @param len number containing the length of the actual request
  462. setContentLength = function(self, len) self.length = len end,
  463. --- Sets the expires header of the SIP request
  464. -- @param expires number containing the expire value
  465. setExpires = function(self, expires) self.expires = expires end,
  466. --- Sets the User Agent being used to connect to the SIP server
  467. -- @param ua string containing the User-Agent name (defaults to Nmap NSE)
  468. setUA = function(self, ua) self.ua = ua end,
  469. --- Sets the caller ID information of the SIP request
  470. -- @param cid string containing the callers id
  471. setCallId = function(self, cid) self.cid = cid end,
  472. --- Sets the maximum forwards allowed of this request
  473. -- @param maxfwd number containing the maximum allowed forwards
  474. setForwards = function(self, maxfwd) self.maxfwd = maxfwd end,
  475. --- Sets the proxy authentication data
  476. -- @param auth string containing properly formatted proxy authentication data
  477. setProxyAuth = function(self, auth) self.proxyauth = auth end,
  478. --- Sets the www authentication data
  479. -- @param auth string containing properly formatted proxy authentication data
  480. setWWWAuth = function(self, auth) self.wwwauth = auth end,
  481. --- Specifies the network protocol being used
  482. -- @param proto should be either "UDP" or "TCP"
  483. setProtocol = function(self, proto)
  484. assert( proto == "UDP" or proto == "TCP", ("Unsupported protocol %s"):format(proto))
  485. self.protocol = proto
  486. end,
  487. --- Converts the request to a String suitable to be sent over the socket
  488. -- @return ret string containing the complete request for sending over the socket
  489. __tostring = function(self)
  490. local data = {}
  491. local branch = "z9hG4bK" .. Util.get_random_string(25)
  492. -- must be at least 32-bit unique
  493. self.from_tag = self.from_tag or Util.get_random_string(20)
  494. local sessdata = self.sessdata
  495. local lhost, lport = sessdata:getClient()
  496. local rhost, rport = sessdata:getServer()
  497. local name, user, domain = sessdata:getName(), sessdata:getUsername(), sessdata:getDomain()
  498. assert(self.method, "No method specified")
  499. assert(self.maxfwd, "Max forward not set")
  500. -- if no domain was specified use the remote host instead
  501. domain = domain or rhost
  502. if ( self.error ) then
  503. table.insert(data, ("SIP/2.0 %s %d"):format(self.error.msg, self.error.code))
  504. else
  505. if ( self.method == Method.ACK ) then
  506. table.insert(data, ("%s %s:%d SIP/2.0"):format(self.method, self.uri, rport))
  507. else
  508. table.insert(data, ("%s %s SIP/2.0"):format(self.method, self.uri))
  509. end
  510. end
  511. table.insert(data, ("Via: SIP/2.0/%s %s:%d;rport;branch=%s"):format(self.protocol, lhost, lport, branch))
  512. table.insert(data, ("Max-Forwards: %d"):format(self.maxfwd))
  513. table.insert(data, ("From: \"%s\" <sip:%s@%s>;tag=%s"):format(name, user, domain, self.from_tag))
  514. if ( self.method == Method.INVITE ) then
  515. table.insert(data, ("To: <sip:%s@%s>"):format(user, domain))
  516. else
  517. table.insert(data, ("To: \"%s\" <sip:%s@%s>"):format(name, user, domain))
  518. end
  519. table.insert(data, ("Call-ID: %s"):format(self.cid))
  520. if ( self.error and self.error.code == Error.OK ) then
  521. table.insert(data, ("CSeq: %d OPTIONS"):format(self.cseq))
  522. else
  523. table.insert(data, ("CSeq: %d %s"):format(self.cseq, self.method))
  524. end
  525. if ( self.method ~= Method.ACK ) then
  526. table.insert(data, ("User-Agent: %s"):format(self.ua))
  527. table.insert(data, ("Contact: \"%s\" <sip:%s@%s:%d>"):format(name, user, lhost, lport))
  528. if ( self.expires ) then
  529. table.insert(data, ("Expires: %d"):format(self.expires))
  530. end
  531. if ( self.allow ) then
  532. table.insert(data, ("Allow: %s"):format(self.allow))
  533. end
  534. if ( self.supported ) then
  535. table.insert(data, ("Supported: %s"):format(self.supported))
  536. end
  537. if ( not(self.error) ) then
  538. if ( self.proxyauth ) then
  539. table.insert(data, ("Proxy-Authorization: %s"):format(self.proxyauth))
  540. end
  541. if ( self.wwwauth ) then
  542. table.insert(data, ("Authorization: %s"):format(self.wwwauth))
  543. end
  544. end
  545. self.length = (self.content and #self.content +2 or 0)
  546. if ( self.headers ) then
  547. for _, val in ipairs(self.headers) do
  548. table.insert(data, val)
  549. end
  550. end
  551. if ( self.content_type ) then
  552. table.insert(data, ("Content-Type: %s"):format(self.content_type))
  553. end
  554. table.insert(data, ("Content-Length: %d"):format(self.length))
  555. table.insert(data, "")
  556. if ( self.content ) then table.insert(data, self.content) end
  557. table.insert(data, "")
  558. else
  559. self.length = (self.content and #self.content +2 or 0)
  560. table.insert(data, ("Content-Length: %d"):format(self.length))
  561. table.insert(data, "")
  562. end
  563. return stdnse.strjoin("\r\n", data)
  564. end,
  565. }
  566. -- A minimal Util class with supporting functions
  567. Util = {
  568. --- Generates a random string of the requested length.
  569. -- @param length (optional) The length of the string to return. Default: 8.
  570. -- @param set (optional) The set of letters to choose from. Default: upper, lower, numbers, and underscore.
  571. -- @return The random string.
  572. get_random_string = function(length, set)
  573. if(length == nil) then
  574. length = 8
  575. end
  576. if(set == nil) then
  577. set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_"
  578. end
  579. local str = ""
  580. for i = 1, length, 1 do
  581. local random = math.random(#set)
  582. str = str .. string.sub(set, random, random)
  583. end
  584. return str
  585. end
  586. }
  587. -- The SIP authentication class, supporting MD5 digest authentication
  588. SipAuth = {
  589. --- Creates a new SipAuth instance
  590. -- @param auth string containing the auth data as received from the server
  591. new = function(self, auth)
  592. local o = {}
  593. setmetatable(o, self)
  594. self.__index = self
  595. o.auth = auth
  596. return o
  597. end,
  598. --- Sets the username used for authentication
  599. -- @param username string containing the name of the user
  600. setUsername = function(self, username) self.username = username end,
  601. --- Sets the password used for authentication
  602. -- @param password string containing the password of the user
  603. setPassword = function(self, password) self.password = password end,
  604. --- Sets the method used for authentication
  605. -- @param method string containing the method (Usually REGISTER)
  606. setMethod = function(self, method) self.method = method end,
  607. --- Sets the uri used for authentication
  608. -- @param uri string containing the uri (Usually sip:<ip>)
  609. setUri = function(self, uri) self.uri = uri end,
  610. --- Processes and parses a challenge as received from the server
  611. parseChallenge = function(self)
  612. if ( not(self.auth) ) then return end
  613. self.nonce = self.auth:match("nonce=[\"]([^,]-)[\"]")
  614. self.algorithm = self.auth:match("algorithm=[\"]*(.-)[\"]*,")
  615. self.realm = self.auth:match("realm=[\"]([^,]-)[\"]")
  616. assert(self.algorithm:upper() == "MD5",
  617. ("Unsupported algorithm detected in authentication challenge (%s)"):format(self.algorithm:upper()))
  618. end,
  619. --- Calculates the authentication response
  620. -- @return reponse string containing the authentication response
  621. calculateResponse = function(self)
  622. if ( not(self.nonce) or not(self.algorithm) or not(self.realm) ) then
  623. self:parseChallenge()
  624. end
  625. assert(self.username, "SipAuth: No username specified")
  626. assert(self.password, "SipAuth: No password specified")
  627. assert(self.method, "SipAuth: No method specified")
  628. assert(self.uri, "SipAuth: No uri specified")
  629. local result
  630. if ( self.algorithm == "MD5" ) then
  631. local HA1 = select(2, bin.unpack("H16", openssl.md5(self.username .. ":" .. self.realm .. ":" .. self.password)))
  632. local HA2 = select(2, bin.unpack("H16", openssl.md5(self.method .. ":" .. self.uri)))
  633. result = openssl.md5(HA1:lower() .. ":" .. self.nonce ..":" .. HA2:lower())
  634. end
  635. return select(2, bin.unpack("H16", result)):lower()
  636. end,
  637. --- Creates the complete authentication response
  638. -- @return auth string containing the complete authentication digest
  639. createResponse = function(self)
  640. local response = self:calculateResponse()
  641. return ("Digest username=\"%s\", realm=\"%s\", nonce=\"%s\"," ..
  642. " uri=\"%s\", response=\"%s\", algorithm=%s"):format(self.username, self.realm,
  643. self.nonce, self.uri, response, self.algorithm)
  644. end,
  645. }
  646. -- The Helper class used as main script interface
  647. Helper = {
  648. --- Creates a new instance of the Helper class
  649. -- @param host table containing the remote host
  650. -- @param port table containing the remote port
  651. -- @param options table containing any options to pass along to the
  652. -- session (@see Session:new for more details)
  653. -- @return o containing a new instance of the Helper class
  654. new = function(self, host, port, options)
  655. local o = {}
  656. setmetatable(o, self)
  657. self.__index = self
  658. local timeout = stdnse.get_script_args("sip.timeout")
  659. if ( timeout ) then options.timeout = timeout end
  660. o.sessdata = SessionData:new()
  661. o.session = Session:new(host, port, o.sessdata, options)
  662. return o
  663. end,
  664. --- Connects the helper instance
  665. connect = function(self) return self.session:connect() end,
  666. --- Disconnects and closes the helper instance
  667. close = function(self) return self.session:close() end,
  668. --- Sets the credentials used when performing authentication
  669. -- @param username string containing the username to use for authentication
  670. -- @param password string containing the password to use for authentication
  671. setCredentials = function(self, username, password)
  672. self.sessdata:setUsername(username)
  673. self.sessdata:setPassword(password)
  674. end,
  675. --- Sets the SIP domain
  676. -- @param domain string containing the domain name
  677. setDomain = function(self, domain) self.sessdata:setDomain(domain) end,
  678. --- Register the UAC with the server
  679. -- @param options table containing zero or more options
  680. -- (@see Session:register for more details)
  681. -- @return status true on success, false on failure
  682. -- @return msg containing the error message if status is false
  683. register = function(self, options)
  684. local status, response = self.session:register(options)
  685. if ( not(status) ) then return false, response end
  686. return true
  687. end,
  688. options = function(self) return self.session:options() end,
  689. --- Attempts to INVITE the user at uri to a call
  690. -- @param uri string containing the sip uri
  691. -- @return status true on success, false on failure
  692. invite = function(self, uri)
  693. return self.session:invite(uri)
  694. end,
  695. }
  696. return _ENV;