@@ -145,6 +145,7 @@ local stringaux = require "stringaux"
145
145
local table = require " table"
146
146
local tableaux = require " tableaux"
147
147
local url = require " url"
148
+ local ascii_hostname = url .ascii_hostname
148
149
local smbauth = require " smbauth"
149
150
local unicode = require " unicode"
150
151
@@ -187,8 +188,9 @@ local function get_host_field(host, port, scheme)
187
188
if host_header then return host_header end
188
189
-- If there's no host, we can't invent a name.
189
190
if not host then return nil end
191
+ local hostname = ascii_hostname (host )
190
192
-- If there's no port, just return hostname.
191
- if not port then return stdnse . get_hostname ( host ) end
193
+ if not port then return hostname end
192
194
if type (port ) == " string" then
193
195
port = tonumber (port )
194
196
assert (port , " Invalid port: not a number or table" )
@@ -200,7 +202,7 @@ local function get_host_field(host, port, scheme)
200
202
if scheme then
201
203
-- Caller provided scheme. If it's default, return just the hostname.
202
204
if number == get_default_port (scheme ) then
203
- return stdnse . get_hostname ( host )
205
+ return hostname
204
206
end
205
207
else
206
208
scheme = url .get_default_scheme (port )
@@ -210,12 +212,12 @@ local function get_host_field(host, port, scheme)
210
212
if (ssl_port and scheme == ' https' ) or
211
213
(not ssl_port and scheme == ' http' ) then
212
214
-- If it's SSL and https, or if it's plaintext and http, return just the hostname.
213
- return stdnse . get_hostname ( host )
215
+ return hostname
214
216
end
215
217
end
216
218
end
217
219
-- No special cases matched, so include the port number in the host header
218
- return stdnse . get_hostname ( host ) .. " :" .. number
220
+ return hostname .. " :" .. number
219
221
end
220
222
221
223
-- Skip *( SP | HT ) starting at offset. See RFC 2616, section 2.2.
@@ -1076,7 +1078,7 @@ local function lookup_cache (method, host, port, path, options)
1076
1078
1077
1079
if type (port ) == " table" then port = port .number end
1078
1080
1079
- local key = stdnse . get_hostname (host ).. " :" .. port .. " :" .. path ;
1081
+ local key = ascii_hostname (host ).. " :" .. port .. " :" .. path ;
1080
1082
local mutex = nmap .mutex (tostring (lookup_cache ).. key );
1081
1083
1082
1084
local state = {
@@ -1615,7 +1617,7 @@ local redirect_ok_rules = {
1615
1617
-- * ccTLDs are not treated as such. The rule will not stop a redirect
1616
1618
-- from foo.co.uk to bar.co.uk even though it logically should.
1617
1619
function (url , host , port )
1618
- local hostname = stdnse . get_hostname (host )
1620
+ local hostname = ascii_hostname (host )
1619
1621
if hostname == host .ip then
1620
1622
return url .host == hostname
1621
1623
end
@@ -1700,7 +1702,7 @@ function parse_redirect(host, port, path, response)
1700
1702
local u = url .parse (response .header .location )
1701
1703
if ( not (u .host ) ) then
1702
1704
-- we're dealing with a relative url
1703
- u .host = stdnse . get_hostname (host )
1705
+ u .host = ascii_hostname (host )
1704
1706
end
1705
1707
-- do port fixup
1706
1708
u .port = u .port or get_default_port (u .scheme ) or port .number
@@ -1811,7 +1813,7 @@ function get_url( u, options )
1811
1813
path = path .. " ?" .. parsed .query
1812
1814
end
1813
1815
1814
- return get ( parsed .host , port , path , options )
1816
+ return get ( parsed .ascii_host or parsed . host , port , path , options )
1815
1817
end
1816
1818
1817
1819
--- Fetches a resource with a HEAD request.
@@ -2857,7 +2859,7 @@ end
2857
2859
-- @param contenttype [optional] The content-type value for the path, if it's known.
2858
2860
function save_path (host , port , path , status , links_to , linked_from , contenttype )
2859
2861
-- Make sure we have a proper hostname and port
2860
- host = stdnse . get_hostname (host )
2862
+ host = ascii_hostname (host )
2861
2863
if (type (port ) == ' table' ) then
2862
2864
port = port [' number' ]
2863
2865
end
@@ -2888,42 +2890,50 @@ function save_path(host, port, path, status, links_to, linked_from, contenttype)
2888
2890
end
2889
2891
end
2890
2892
2893
+ if parsed .host then
2894
+ host = parsed .ascii_host or parsed .host
2895
+ end
2896
+
2897
+ if parsed .port then
2898
+ port = parsed .port
2899
+ end
2900
+
2891
2901
-- Add to the 'all_pages' key
2892
- stdnse .registry_add_array ({parsed [ ' host' ] or host , ' www' , parsed [ ' port ' ] or port , ' all_pages' }, parsed [' path' ])
2902
+ stdnse .registry_add_array ({host , ' www' , port , ' all_pages' }, parsed [' path' ])
2893
2903
2894
2904
-- Add the URL with querystring to all_pages_full_query
2895
- stdnse .registry_add_array ({parsed [ ' host' ] or host , ' www' , parsed [ ' port ' ] or port , ' all_pages_full_query' }, parsed [' path_query' ])
2905
+ stdnse .registry_add_array ({host , ' www' , port , ' all_pages_full_query' }, parsed [' path_query' ])
2896
2906
2897
2907
-- Add the URL to a key matching the response code
2898
2908
if (status ) then
2899
- stdnse .registry_add_array ({parsed [ ' host' ] or host , ' www' , parsed [ ' port ' ] or port , ' status_codes' , status }, parsed [' path' ])
2909
+ stdnse .registry_add_array ({host , ' www' , port , ' status_codes' , status }, parsed [' path' ])
2900
2910
end
2901
2911
2902
2912
-- If it's a directory, add it to the directories list; otherwise, add it to the files list
2903
2913
if (parsed [' is_folder' ]) then
2904
- stdnse .registry_add_array ({parsed [ ' host' ] or host , ' www' , parsed [ ' port ' ] or port , ' directories' }, parsed [' path' ])
2914
+ stdnse .registry_add_array ({host , ' www' , port , ' directories' }, parsed [' path' ])
2905
2915
else
2906
- stdnse .registry_add_array ({parsed [ ' host' ] or host , ' www' , parsed [ ' port ' ] or port , ' files' }, parsed [' path' ])
2916
+ stdnse .registry_add_array ({host , ' www' , port , ' files' }, parsed [' path' ])
2907
2917
end
2908
2918
2909
2919
2910
2920
-- If we have an extension, add it to the extensions key
2911
2921
if (parsed [' extension' ]) then
2912
- stdnse .registry_add_array ({parsed [ ' host' ] or host , ' www' , parsed [ ' port ' ] or port , ' extensions' , parsed [' extension' ]}, parsed [' path' ])
2922
+ stdnse .registry_add_array ({host , ' www' , port , ' extensions' , parsed [' extension' ]}, parsed [' path' ])
2913
2923
end
2914
2924
2915
2925
-- Add an entry for the page and its arguments
2916
2926
if (parsed [' querystring' ]) then
2917
2927
-- Add all scripts with a querystring to the 'cgi' and 'cgi_full_query' keys
2918
- stdnse .registry_add_array ({parsed [ ' host' ] or host , ' www' , parsed [ ' port ' ] or port , ' cgi' }, parsed [' path' ])
2919
- stdnse .registry_add_array ({parsed [ ' host' ] or host , ' www' , parsed [ ' port ' ] or port , ' cgi_full_query' }, parsed [' path_query' ])
2928
+ stdnse .registry_add_array ({host , ' www' , port , ' cgi' }, parsed [' path' ])
2929
+ stdnse .registry_add_array ({host , ' www' , port , ' cgi_full_query' }, parsed [' path_query' ])
2920
2930
2921
2931
-- Add the query string alone to the registry (probably not necessary)
2922
- stdnse .registry_add_array ({parsed [ ' host' ] or host , ' www' , parsed [ ' port ' ] or port , ' cgi_querystring' , parsed [' path' ] }, parsed [' raw_querystring' ])
2932
+ stdnse .registry_add_array ({host , ' www' , port , ' cgi_querystring' , parsed [' path' ] }, parsed [' raw_querystring' ])
2923
2933
2924
2934
-- Add the individual arguments for the page, along with their values
2925
2935
for key , value in pairs (parsed [' querystring' ]) do
2926
- stdnse .registry_add_array ({parsed [ ' host' ] or host , ' www' , parsed [ ' port ' ] or port , ' cgi_args' , parsed [' path' ]}, parsed [' querystring' ])
2936
+ stdnse .registry_add_array ({host , ' www' , port , ' cgi_args' , parsed [' path' ]}, parsed [' querystring' ])
2927
2937
end
2928
2938
end
2929
2939
@@ -2934,7 +2944,7 @@ function save_path(host, port, path, status, links_to, linked_from, contenttype)
2934
2944
end
2935
2945
2936
2946
for _ , v in ipairs (links_to ) do
2937
- stdnse .registry_add_array ({parsed [ ' host' ] or host , ' www' , parsed [ ' port ' ] or port , ' links_to' , parsed [' path_query' ]}, v )
2947
+ stdnse .registry_add_array ({host , ' www' , port , ' links_to' , parsed [' path_query' ]}, v )
2938
2948
end
2939
2949
end
2940
2950
@@ -2945,13 +2955,13 @@ function save_path(host, port, path, status, links_to, linked_from, contenttype)
2945
2955
end
2946
2956
2947
2957
for _ , v in ipairs (linked_from ) do
2948
- stdnse .registry_add_array ({parsed [ ' host' ] or host , ' www' , parsed [ ' port ' ] or port , ' links_to' , v }, parsed [' path_query' ])
2958
+ stdnse .registry_add_array ({host , ' www' , port , ' links_to' , v }, parsed [' path_query' ])
2949
2959
end
2950
2960
end
2951
2961
2952
2962
-- Save it as a content-type, if we have one
2953
2963
if (contenttype ) then
2954
- stdnse .registry_add_array ({parsed [ ' host' ] or host , ' www' , parsed [ ' port ' ] or port , ' content-type' , contenttype }, parsed [' path_query' ])
2964
+ stdnse .registry_add_array ({host , ' www' , port , ' content-type' , contenttype }, parsed [' path_query' ])
2955
2965
end
2956
2966
end
2957
2967
0 commit comments