mirror of
https://github.com/nmap/nmap.git
synced 2025-12-25 08:59:01 +00:00
Remove trailing whitespace in lua files
Whitespace is not significant, so this should not be a problem. https://secwiki.org/w/Nmap/Code_Standards
This commit is contained in:
106
nselib/socks.lua
106
nselib/socks.lua
@@ -18,15 +18,15 @@ AuthMethod = {
|
||||
}
|
||||
|
||||
Request = {
|
||||
|
||||
|
||||
-- Class that handles the connection request to the server
|
||||
Connect = {
|
||||
|
||||
|
||||
-- Creates a new instance of the class
|
||||
-- @param auth_method table of requested authentication methods
|
||||
-- @return o instance on success, nil on failure
|
||||
new = function(self, auth_method)
|
||||
local o = {
|
||||
local o = {
|
||||
version = 5,
|
||||
auth_method = ( "table" ~= type(auth_method) and { auth_method } or auth_method )
|
||||
}
|
||||
@@ -34,7 +34,7 @@ Request = {
|
||||
self.__index = self
|
||||
return o
|
||||
end,
|
||||
|
||||
|
||||
-- Converts the instance to string, so that it can be sent to the
|
||||
-- server.
|
||||
-- @return string containing the raw request
|
||||
@@ -42,15 +42,15 @@ Request = {
|
||||
local methods = ""
|
||||
for _, m in ipairs(self.auth_method) do
|
||||
methods = methods .. string.char(m)
|
||||
end
|
||||
end
|
||||
return bin.pack("Cp", self.version, methods)
|
||||
end,
|
||||
end,
|
||||
|
||||
},
|
||||
|
||||
|
||||
-- Class that handles the authentication request to the server
|
||||
Authenticate = {
|
||||
|
||||
|
||||
-- Creates a new instance of the class
|
||||
-- @param auth_method number with the requested authentication method
|
||||
-- @param creds method specific table of credentials
|
||||
@@ -59,7 +59,7 @@ Request = {
|
||||
-- <code>username</code> and <code>password</code>
|
||||
-- @return o instance on success, nil on failure
|
||||
new = function(self, auth_method, creds)
|
||||
local o = {
|
||||
local o = {
|
||||
auth_method = auth_method,
|
||||
creds = creds
|
||||
}
|
||||
@@ -69,7 +69,7 @@ Request = {
|
||||
return o
|
||||
end
|
||||
end,
|
||||
|
||||
|
||||
-- Converts the instance to string, so that it can be sent to the
|
||||
-- server.
|
||||
-- @return string containing the raw request
|
||||
@@ -87,16 +87,16 @@ Request = {
|
||||
return bin.pack("Cpp", version, username, password)
|
||||
end
|
||||
end,
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Response = {
|
||||
|
||||
|
||||
-- Class that handles the connection response
|
||||
Connect = {
|
||||
|
||||
|
||||
-- Creates a new instance of the class
|
||||
-- @param data string containing the data as received over the socket
|
||||
-- @return o instance on success, nil on failure
|
||||
@@ -108,7 +108,7 @@ Response = {
|
||||
return o
|
||||
end
|
||||
end,
|
||||
|
||||
|
||||
-- Parses the received data and populates member variables
|
||||
-- @return true on success, false on failure
|
||||
parse = function(self)
|
||||
@@ -116,21 +116,21 @@ Response = {
|
||||
return
|
||||
end
|
||||
local pos
|
||||
pos, self.version, self.method = bin.unpack("CC", self.data)
|
||||
pos, self.version, self.method = bin.unpack("CC", self.data)
|
||||
return true
|
||||
end
|
||||
|
||||
|
||||
},
|
||||
|
||||
|
||||
-- Class that handles the authentication response
|
||||
Authenticate = {
|
||||
|
||||
|
||||
Status = {
|
||||
SUCCESS = 0,
|
||||
-- could be anything but zero
|
||||
FAIL = 1,
|
||||
},
|
||||
|
||||
|
||||
-- Creates a new instance of the class
|
||||
-- @param data string containing the data as received over the socket
|
||||
-- @return o instance on success, nil on failure
|
||||
@@ -142,7 +142,7 @@ Response = {
|
||||
return o
|
||||
end
|
||||
end,
|
||||
|
||||
|
||||
-- Parses the received data and populates member variables
|
||||
-- @return true on success, false on failure
|
||||
parse = function(self)
|
||||
@@ -150,25 +150,25 @@ Response = {
|
||||
return
|
||||
end
|
||||
local pos
|
||||
pos, self.version, self.status = bin.unpack("CC", self.data)
|
||||
pos, self.version, self.status = bin.unpack("CC", self.data)
|
||||
return true
|
||||
end,
|
||||
|
||||
|
||||
-- checks if the authentication was successful or not
|
||||
-- @return true on success, false on failure
|
||||
isSuccess = function(self)
|
||||
return ( self.status == self.Status.SUCCESS )
|
||||
end,
|
||||
|
||||
end,
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
-- A buffered socket implementation
|
||||
Socket =
|
||||
{
|
||||
{
|
||||
retries = 3,
|
||||
|
||||
|
||||
-- Creates a new socket instance
|
||||
-- @param host table containing the host table
|
||||
-- @param port table containing the port table
|
||||
@@ -176,8 +176,8 @@ Socket =
|
||||
-- <code>timeout</code> - socket timeout in ms
|
||||
-- @return o new instance of Socket
|
||||
new = function(self, host, port, options)
|
||||
local o = {
|
||||
host = host,
|
||||
local o = {
|
||||
host = host,
|
||||
port = port,
|
||||
options = options or {}
|
||||
}
|
||||
@@ -187,7 +187,7 @@ Socket =
|
||||
o.Buffer = nil
|
||||
return o
|
||||
end,
|
||||
|
||||
|
||||
-- Connects the socket to the server
|
||||
-- @return status true on success false on failure
|
||||
-- @return err string containing error message on failure
|
||||
@@ -195,7 +195,7 @@ Socket =
|
||||
self.Socket:set_timeout(self.options.timeout or 10000)
|
||||
return self.Socket:connect( self.host, self.port )
|
||||
end,
|
||||
|
||||
|
||||
-- Closes an open connection.
|
||||
--
|
||||
-- @return Status (true or false).
|
||||
@@ -203,7 +203,7 @@ Socket =
|
||||
close = function( self )
|
||||
return self.Socket:close()
|
||||
end,
|
||||
|
||||
|
||||
-- Opposed to the <code>socket:receive_bytes</code> function, that returns
|
||||
-- at least x bytes, this function returns the amount of bytes requested.
|
||||
--
|
||||
@@ -213,9 +213,9 @@ Socket =
|
||||
-- err containing error message if status is false
|
||||
recv = function( self, count )
|
||||
local status, data
|
||||
|
||||
|
||||
self.Buffer = self.Buffer or ""
|
||||
|
||||
|
||||
if ( #self.Buffer < count ) then
|
||||
status, data = self.Socket:receive_bytes( count - #self.Buffer )
|
||||
if ( not(status) or #data < count - #self.Buffer ) then
|
||||
@@ -223,13 +223,13 @@ Socket =
|
||||
end
|
||||
self.Buffer = self.Buffer .. data
|
||||
end
|
||||
|
||||
|
||||
data = self.Buffer:sub( 1, count )
|
||||
self.Buffer = self.Buffer:sub( count + 1)
|
||||
|
||||
return true, data
|
||||
|
||||
return true, data
|
||||
end,
|
||||
|
||||
|
||||
-- Sends data over the socket
|
||||
--
|
||||
-- @return Status (true or false).
|
||||
@@ -242,7 +242,7 @@ Socket =
|
||||
|
||||
-- The main script interface
|
||||
Helper = {
|
||||
|
||||
|
||||
-- Create a new instance of the class
|
||||
-- @param host table containing the host table
|
||||
-- @param port table containing the port table
|
||||
@@ -255,7 +255,7 @@ Helper = {
|
||||
self.__index = self
|
||||
return o
|
||||
end,
|
||||
|
||||
|
||||
-- Get the authentication method name by number
|
||||
-- @param method number containing the authentication method
|
||||
-- @return string containing the method name or Unknown
|
||||
@@ -267,31 +267,31 @@ Helper = {
|
||||
}
|
||||
return methods[method] or ("Unknown method (%d)"):format(method)
|
||||
end,
|
||||
|
||||
|
||||
-- Connects to the SOCKS server
|
||||
-- @param auth_method table containing the auth. methods to request
|
||||
-- @return status true on success, false on failure
|
||||
-- @return response table containing the respons or err string on failure
|
||||
connect = function(self, auth_method)
|
||||
self.socket = Socket:new(self.host, self.port, self.options)
|
||||
self.socket = Socket:new(self.host, self.port, self.options)
|
||||
local status, err = self.socket:connect()
|
||||
if ( not(status) ) then
|
||||
return status, err
|
||||
end
|
||||
|
||||
|
||||
auth_method = auth_method or {AuthMethod.NONE, AuthMethod.GSSAPI, AuthMethod.USERPASS}
|
||||
status = self.socket:send( tostring(Request.Connect:new(auth_method)) )
|
||||
if ( not(status) ) then
|
||||
self.socket:close()
|
||||
return false, "Failed to send connection request to server"
|
||||
end
|
||||
|
||||
|
||||
local status, data = self.socket:recv(2)
|
||||
if ( not(status) ) then
|
||||
self.socket:close()
|
||||
return false, "Failed to receive connection response from server"
|
||||
end
|
||||
|
||||
|
||||
local response = Response.Connect:new(data)
|
||||
if ( not(response) ) then
|
||||
return false, "Failed to parse response from server"
|
||||
@@ -303,12 +303,12 @@ Helper = {
|
||||
if ( response.method == 0xFF ) then
|
||||
return false, "No acceptable authentication methods"
|
||||
end
|
||||
|
||||
|
||||
-- store the method so authenticate knows what to use
|
||||
self.auth_method = response.method
|
||||
return true, response
|
||||
end,
|
||||
|
||||
|
||||
-- Authenticates to the SOCKS server
|
||||
-- @param creds table containing authentication method specific fields
|
||||
-- currently only authentication method 2 (username and pass) is
|
||||
@@ -325,20 +325,20 @@ Helper = {
|
||||
if ( not(req) ) then
|
||||
return false, "Failed to create authentication request"
|
||||
end
|
||||
|
||||
|
||||
local status = self.socket:send(tostring(req))
|
||||
if ( not(status) ) then
|
||||
return false, "Failed to send authentication request"
|
||||
end
|
||||
|
||||
|
||||
if ( 2 == self.auth_method ) then
|
||||
local status, data = self.socket:recv(2)
|
||||
local auth = Response.Authenticate:new(data)
|
||||
|
||||
|
||||
if ( not(auth) ) then
|
||||
return false, "Failed to parse authentication response"
|
||||
end
|
||||
|
||||
|
||||
if ( auth:isSuccess() ) then
|
||||
return true, "Authentication was successfull"
|
||||
else
|
||||
@@ -353,7 +353,7 @@ Helper = {
|
||||
close = function(self)
|
||||
return self.socket:close()
|
||||
end,
|
||||
|
||||
|
||||
}
|
||||
|
||||
return _ENV;
|
||||
|
||||
Reference in New Issue
Block a user