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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
local coroutine = require "coroutine"
local http = require "http"
local io = require "io"
local shortport = require "shortport"
local stdnse = require "stdnse"
local string = require "string"
local table = require "table"
local url = require "url"
description = [[
Checks for backups and swap files of common content management system
and web server configuration files.
When web server files are edited in place, the text editor can leave
backup or swap files in a place where the web server can serve them. The
script checks for these files:
* <code>wp-config.php</code>: WordPress
* <code>config.php</code>: phpBB, ExpressionEngine
* <code>configuration.php</code>: Joomla
* <code>LocalSettings.php</code>: MediaWiki
* <code>/mediawiki/LocalSettings.php</code>: MediaWiki
* <code>mt-config.cgi</code>: Movable Type
* <code>mt-static/mt-config.cgi</code>: Movable Type
* <code>settings.php</code>: Drupal
* <code>.htaccess</code>: Apache
And for each of these file applies the following transformations (using
<code>config.php</code> as an example):
* <code>config.bak</code>: Generic backup.
* <code>config.php.bak</code>: Generic backup.
* <code>config.php~</code>: Vim, Gedit.
* <code>#config.php#</code>: Emacs.
* <code>config copy.php</code>: Mac OS copy.
* <code>Copy of config.php</code>: Windows copy.
* <code>config.php.save</code>: GNU Nano.
* <code>.config.php.swp</code>: Vim swap.
* <code>config.php.swp</code>: Vim swap.
* <code>config.php.old</code>: Generic backup.
This script is inspired by the CMSploit program by Feross Aboukhadijeh:
http://www.feross.org/cmsploit/.
]];
---
-- @usage
-- nmap --script=http-config-backup <target>
--
-- @output
-- PORT STATE SERVICE REASON
-- 80/tcp open http syn-ack
-- | http-config-backup:
-- | /%23wp-config.php%23 HTTP/1.1 200 OK
-- |_ /config.php~ HTTP/1.1 200 OK
--
-- @args http-config-backup.path the path where the CMS is installed
-- @args http-config-backup.save directory to save all the valid config files found
--
author = "Riccardo Cecolin";
license = "Same as Nmap--See https://nmap.org/book/man-legal.html";
categories = { "auth", "intrusive" };
portrule = shortport.http;
local function make_grep(pattern)
return function(s)
return string.match(s, pattern)
end
end
local grep_php = make_grep("<%?php");
local grep_cgipath = make_grep("CGIPath");
local function check_htaccess(s)
return string.match("<Files") or string.match(s, "RewriteRule")
end
local CONFIGS = {
{ filename = "wp-config.php", check = grep_php }, -- WordPress
{ filename = "config.php", check = grep_php }, -- phpBB, ExpressionEngine
{ filename = "configuration.php", check = grep_php }, -- Joomla
{ filename = "LocalSettings.php", check = grep_php }, -- MediaWiki
{ filename = "/mediawiki/LocalSettings.php", check = grep_php }, -- MediaWiki
{ filename = "mt-config.cgi", check = grep_cgipath }, -- Movable Type
{ filename = "mt-static/mt-config.cgi", check = grep_cgipath }, -- Movable Type
{ filename = "settings.php", check = grep_php }, -- Drupal
{ filename = ".htaccess", check = check_htaccess }, -- Apache
};
-- Return directory, filename pair. directory may be empty.
local function splitdir(path)
local dir, filename
dir, filename = string.match(path, "^(.*/)(.*)$")
if not dir then
dir = ""
filename = path
end
return dir, filename
end
-- Return basename, extension pair. extension may be empty.
local function splitext(filename)
local base, ext;
base, ext = string.match(filename, "^(.+)(%..+)")
if not base then
base = filename
ext = ""
end
return base, ext
end
-- Functions mangling filenames.
local TRANSFORMS = {
function(fn)
local base, ext = splitext(fn);
if ext ~= "" then
return base .. ".bak" -- generic bak file
end
end,
function(fn) return fn .. ".bak" end,
function(fn) return fn .. "~" end, -- vim, gedit
function(fn) return "#" .. fn .. "#" end, -- Emacs
function(fn)
local base, ext = splitext(fn);
return base .. " copy" .. ext -- mac copy
end,
function(fn) return "Copy of " .. fn end, -- windows copy
function(fn) return fn .. ".save" end, -- nano
function(fn) if string.sub(fn, 1, 1) ~= "." then return "." .. fn .. ".swp" end end, -- vim swap
function(fn) return fn .. ".swp" end, -- vim swap
function(fn) return fn .. ".old" end, -- generic backup
};
---
--Creates combinations of backup names for a given filename
--Taken from: http-backup-finder.nse
local function backupNames (filename)
local dir, basename;
dir, basename = splitdir(filename);
return coroutine.wrap(function()
for _, transform in ipairs(TRANSFORMS) do
local result = transform(basename);
if result == nil then
elseif type(result) == "string" then
coroutine.yield(dir .. result);
result = {result}
elseif type(result) == "table" then
for _, r in ipairs(result) do
coroutine.yield(dir .. r);
end
end
end
end)
end
---
--Writes string to file
--Taken from: hostmap.nse
-- @param filename Filename to write
-- @param contents Content of file
-- @return True if file was written successfully
local function write_file (filename, contents)
local f, err = io.open(filename, "w");
if not f then
return f, err;
end
f:write(contents);
f:close();
return true;
end
action = function (host, port)
local path = stdnse.get_script_args("http-config-backup.path") or "/";
local save = stdnse.get_script_args("http-config-backup.save");
local backups = {};
if not path:match("/$") then
path = path .. "/";
end
if not path:match("^/") then
path = "/" .. path;
end
if (save and not(save:match("/$") ) ) then
save = save .. "/";
end
local status_404, result_404, known_404 = http.identify_404(host, port)
if not status_404 then
stdnse.debug1("Can't distinguish 404 response. Quitting.")
return stdnse.format_output(false, "Can't determine file existence")
end
-- for each config file
for _, cfg in ipairs(CONFIGS) do
-- for each alteration of the filename
for entry in backupNames(cfg.filename) do
local url_path
url_path = url.build({path = path .. entry});
-- http request
local response = http.get(host, port, url_path);
-- if it's not 200, don't bother. If it is, check that it's not a false 404
if response.status == 200 and http.page_exists(response, result_404, known_404, url_path) then
-- check it if is valid before inserting
if cfg.check(response.body) then
local filename = stdnse.escape_filename((host.targetname or host.ip) .. url_path)
-- save the content
if save then
local status, err = write_file(save .. filename, response.body);
if status then
stdnse.debug1("%s saved", filename);
else
stdnse.debug1("error saving %s", err);
end
end
table.insert(backups, url_path .. " " .. response["status-line"]);
else
stdnse.debug1("%s: found but not matching: %s",
host.targetname or host.ip, url_path);
end
end
end
end
return stdnse.format_output(true, backups);
end;
|