Skip to content

Commit 8db5214

Browse files
James ConlanJames Conlan
authored andcommitted
Fixed issue with loading default config
1 parent bf671f4 commit 8db5214

File tree

1 file changed

+39
-17
lines changed

1 file changed

+39
-17
lines changed

nlist.nse

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,41 @@ license = "GNU General Public License v3.0 -- See https://www.gnu.org/licenses"
3939
categories = {"default", "safe"}
4040

4141

42-
getDefaultConfPath = function(pathSeparator)
43-
local confPath = nil
44-
local scriptPath = debug.getinfo(1,"S").source:sub(2)
45-
local scriptPathSplit = {}
46-
for i in scriptPath:gmatch("[^" .. pathSeparator .. "]+") do
47-
table.insert(scriptPathSplit, i)
48-
end
49-
table.remove(scriptPathSplit)
50-
if #scriptPathSplit == 0 then
51-
table.insert(scriptPathSplit, ".")
42+
exists = function(path)
43+
local ok, err, code = os.rename(path, path)
44+
if not ok then
45+
if code == 13 then
46+
-- Permission denied, but it exists
47+
return true
48+
else
49+
return false
50+
end
51+
else
52+
return true
5253
end
53-
for _, v in pairs(scriptPathSplit) do
54-
if confPath == nil then
55-
confPath = v
54+
end
55+
56+
getDefaultConfPath = function(pathSeparator)
57+
if pathSeparator == "/" then
58+
if exists("/usr/share/nmap") then
59+
return "/usr/share/nmap/scripts/nlist.conf"
60+
elseif exists("/usr/local/share/nmap") then
61+
return "/usr/local/share/nmap/scripts/nlist.conf"
62+
else
63+
stdnse.debug(1, "Scripts directory location unknown")
64+
end
65+
elseif pathSeparator == "\\" then
66+
if exists("C:\\Program Files (x86)\\Nmap") then
67+
return "C:\\Program Files (x86)\\Nmap\\scripts\\nlist.conf"
68+
elseif exists("C:\\Program Files\\Nmap") then
69+
return "C:\\Program Files\\Nmap\\scripts\\nlist.conf"
5670
else
57-
confPath = confPath .. pathSeparator .. v
71+
stdnse.debug(1, "Scripts directory location unknown")
5872
end
73+
else
74+
stdnse.debug(1, "Unknown OS")
5975
end
60-
return confPath .. pathSeparator .. "nlist.conf"
76+
return nil
6177
end
6278

6379
getHomeConfPath = function(pathSeparator)
@@ -72,7 +88,7 @@ getHomeConfPath = function(pathSeparator)
7288
end
7389
end
7490

75-
parseConfFile = function(confFile)
91+
parseConfFile = function(confFile, confPath)
7692
if not confFile then
7793
stdnse.debug(1, "Could not open config file: '%s'", confPath)
7894
return false, string.format("Config file '%s' could not be opened", confPath)
@@ -118,10 +134,13 @@ action = function(host, port)
118134
if not confFile then
119135
stdnse.debug(1, "No '.nlist' file found in home directory")
120136
confPath = getDefaultConfPath(pathSeparator)
137+
if not confPath then
138+
return "Could not open config file"
139+
end
121140
confFile = io.open(confPath, "r")
122141
end
123142
stdnse.debug(1, "Using config file: '%s'", confPath)
124-
local status, config = parseConfFile(confFile)
143+
local status, config = parseConfFile(confFile, confPath)
125144
if not status then
126145
return config
127146
end
@@ -153,6 +172,9 @@ action = function(host, port)
153172
if config["use_default_rules"] then
154173
stdnse.debug(1, "Loading default rules", confPath)
155174
local defConfPath = getDefaultConfPath(pathSeparator)
175+
if not defConfPath then
176+
return "Could not open default config file"
177+
end
156178
local confFile = io.open(defConfPath, "r")
157179
local status, conf = parseConfFile(confFile)
158180
defConfig = conf

0 commit comments

Comments
 (0)