summaryrefslogtreecommitdiffstats
path: root/scripts/socks-open-proxy.nse
blob: f5fcc0cb7a6a4be494ff97b757ab2634a1657cb5 (plain)
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
local proxy = require "proxy"
local shortport = require "shortport"
local stdnse = require "stdnse"
local string = require "string"
local url = require "url"

description=[[
Checks if an open socks proxy is running on the target.

The script attempts to connect to a proxy server and send socks4 and
socks5 payloads. It is considered an open proxy if the script receives
a Request Granted response from the target port.

The payloads try to open a connection to www.google.com port 80.  A
different test host can be passed as <code>proxy.url</code>
argument.
]]
---
--@output
-- PORT     STATE  SERVICE
-- 1080/tcp open   socks
-- |  socks-open-proxy:
-- |   status: open
-- |   versions:
-- |     socks4
-- |_    socks5
--
--@xmloutput
--<elem key="status">open</elem>
--<table key="versions">
--  <elem>socks4</elem>
--  <elem>socks5</elem>
--</table>
--@usage
-- nmap --script=socks-open-proxy \
--    --script-args proxy.url=<host>,proxy.pattern=<pattern>

author = "Joao Correa"
license = "Same as Nmap--See https://nmap.org/book/man-legal.html"
categories = {"default", "discovery", "external", "safe"}


--- Performs the custom test, with user's arguments
-- @param host The host table
-- @param port The port table
-- @param test_url The url to request
-- @param pattern The pattern to check for valid result
-- @return status If any request succeeded
-- @return response Table with supported methods
local function custom_test(host, port, test_url, pattern)
  local status4, status5, fstatus, cstatus4, cstatus5
  local get_r4, get_r5
  local methods
  local response = {}

  -- strip hostname
  if not string.match(test_url, "^http://.*") then
    test_url = "http://" .. test_url
    stdnse.debug1("URL missing scheme. URL concatenated to http://")
  end
  local url_table = url.parse(test_url)
  local hostname = url_table.host
  test_url = url_table.path

  -- make requests
  status4, get_r4, cstatus4 = proxy.test_get(host, port, "socks4", test_url, hostname, pattern)
  status5, get_r5, cstatus5 = proxy.test_get(host, port, "socks5", test_url, hostname, pattern)

  fstatus = status4 or status5
  if(cstatus4) then response[#response+1]="socks4" end
  if(cstatus5) then response[#response+1]="socks5" end
  if(fstatus) then return fstatus, response end

  -- Nothing works...
  if not (cstatus4 or cstatus5) then
    return false, nil
  else
    return "pattern not matched", response
  end
end

--- Performs the default test
-- First: Default google request and checks for Server: gws
-- Second: Request to wikipedia.org and checks for wikimedia pattern
-- Third: Request to computerhistory.org and checks for museum pattern
--
-- If any of the requests is successful, the proxy is considered open.
-- If all requests return the same result, the user is alerted that
-- the proxy might be redirecting his requests (very common on wi-fi
-- connections at airports, cafes, etc.)
--
-- @param host The host table
-- @param port The port table
-- @return status If any request succeeded
-- @return response Table with supported methods
local function default_test(host, port)
  local status4, status5, fstatus
  local cstatus4, cstatus5
  local get_r4, get_r5
  local methods
  local response = {}

  local test_url = "/"
  local hostname = "www.google.com"
  local pattern = "^server: gws"
  status4, get_r4, cstatus4 = proxy.test_get(host, port, "socks4", test_url, hostname, pattern)
  status5, get_r5, cstatus5 = proxy.test_get(host, port, "socks5", test_url, hostname, pattern)

  fstatus = status4 or status5
  if(cstatus4) then response[#response+1]="socks4" end
  if(cstatus5) then response[#response+1]="socks5" end
  if(fstatus) then return fstatus, response end

  -- if we receive a invalid response, but with a valid
  -- response code, we should make a next attempt.
  -- if we do not receive any valid status code,
  -- there is no reason to keep testing... the proxy is probably not open
  if not (cstatus4 or cstatus5) then return false, nil end
  stdnse.debug1("Test 1 - Google Web Server: Received valid status codes, but pattern does not match")

  test_url = "/"
  hostname = "www.wikipedia.org"
  pattern  = "wikimedia"
  status4, get_r4, cstatus4 = proxy.test_get(host, port, "socks4", test_url, hostname, pattern)
  status5, get_r5, cstatus5 = proxy.test_get(host, port, "socks5", test_url, hostname, pattern)

  if(status4) then fstatus = true; response[#response+1]="socks4" end
  if(status5) then fstatus = true; response[#response+1]="socks5" end
  if(fstatus) then return fstatus, response end

  if not (cstatus4 or cstatus5) then return false, nil end
  stdnse.debug1("Test 2 - Wikipedia.org: Received valid status codes, but pattern does not match")

  local redir_check_get = get_r4 or get_r5

  test_url = "/"
  hostname = "www.computerhistory.org"
  pattern  = "museum"
  status4, get_r4, cstatus4 = proxy.test_get(host, port, "socks4", test_url, hostname, pattern)
  status5, get_r5, cstatus5 = proxy.test_get(host, port, "socks5", test_url, hostname, pattern)

  if(status4) then fstatus = true; response[#response+1]="socks4" end
  if(status5) then fstatus = true; response[#response+1]="socks5" end
  if(fstatus) then return fstatus, response end

  if not (cstatus4 or cstatus5) then return false, nil end
  stdnse.debug1("Test 3 - Computer History: Received valid status codes, but pattern does not match")

  -- Check if GET is being redirected
  if proxy.redirectCheck(get_r4 or get_r5, redir_check_get) then
    return "redirecting", response
  end

  -- Protocol works, but nothing matches
  return "pattern not matched", response

end

portrule = shortport.port_or_service({1080, 9050},
  {"socks", "socks4", "socks5", "tor-socks"})

action = function(host, port)
  local supported_versions
  local fstatus = false
  local pattern, test_url
  local def_test = true
  local hostname
  local retval = stdnse.output_table()

  test_url, pattern = proxy.return_args()

  if(test_url) then def_test = false end
  if(pattern) then pattern = ".*" .. pattern .. ".*" end

  if def_test
    then fstatus, supported_versions = default_test(host, port)
    else fstatus, supported_versions = custom_test(host, port, test_url, pattern)
  end

  -- If any of the tests were OK, then the proxy is potentially open
  if fstatus == true then
    retval["status"] = "open"
    retval["versions"] = supported_versions
    return retval
  elseif fstatus and supported_versions then
    retval["status"] = fstatus
    retval["versions"] = supported_versions
    return retval
  end

end