-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwp-scanner.py
193 lines (167 loc) · 6.08 KB
/
wp-scanner.py
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
#!/usr/bin/python
# WordPress Version Scanner
# v1.2
# Scan paths for outdated WordPress installations
# If MySQLdb available then wp-config.php credentials will be checked for valid installations
#
# URL: www.admingeekz.com
# Contact: [email protected]
#
#
# Copyright (c) 2013, AdminGeekZ Ltd
# All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2, as
# published by the Free Software Foundation.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
import sys
#Check for version 2.3 or greater
if int(sys.version_info[1] <= 2):
print "We need python v2.3 or above"
sys.exit(1)
import urllib
import re
import os
import datetime
debug="no" #disable debugging
path=['/home', '/var/www', '/www'] #Paths to scan
#If MySQLdb exists enable fetching blog of url and verifying of DB credentials
try:
import MySQLdb
checksiteurl="yes"
except:
checksiteurl="no"
#Override path if cPanel exists, this reduces scan time to only valid accounts and skips virtfs
if os.path.exists('/usr/local/cpanel/version'):
path=[]
for base, dir, files in os.walk("/var/cpanel/users"):
for f in files:
#Only accept numbers, letters and under score for usernames
if re.search("^(\w+)$", f):
path.append("/home/"+f+"/public_html")
#Sort paths in alphabetical order
path.sort()
#original function based on wp-upgrade.py by stevenbrown.ca
def get_latest_wp_version(wp_latest_url="http://wordpress.org/latest.tar.gz"):
"""Returns a tuple containing the version string of the latest version of
wordpress and the full filename.
'wp_latest_url' is a link to the latest tar.gz release."""
# get version of latest available
f = urllib.urlopen(wp_latest_url)
filename = f.info()["content-disposition"]
m = re.search(".*filename=(\S+).*", filename)
filename = m.group(1)
m = re.search(".*?([\.\d]+)\..*", filename)
version = m.group(1)
f.close()
return version, filename
def get_wp_version(wp_directory="wordpress"):
"""Returns the version string of wordpress in wp_directory.
'wp_directory' is the path to the wordpress folder to check."""
vfilename = os.path.join(wp_directory, "wp-includes/version.php")
try:
vfile = open(vfilename)
# extract version string from "$wp_version = 'the_string';" in version.php
pattern = re.compile("\s*\$wp_version\s*=\s*.([\.\d]+).\s*;?")
version = "unknown"
for l in vfile:
m = re.search(pattern, l)
if m:
version = m.group(1)
break
return version
except:
return "unknown"
def wpconfig(path):
"""Extract the configuration variables from wp-config.php
'path' is the path to the wp-config file"""
db_params = {}
db_params['table_prefix'] = 'wp_'
mapping = {
'DB_NAME' :'db',
'DB_USER' :'user',
'DB_PASSWORD':'passwd',
'DB_HOST' :'host',
}
vars = re.compile(
r"^\s*define\s*\(\s*'(?P<key>DB_[A-Z]+)'\s*,\s*'(?P<val>.*)'\s*\)\s*;")
TablePrefix = re.compile(
r"^\s*\$table_prefix\s*=\s*'(\w+)'\s*;")
checkpath=os.path.join(path, "wp-config.php")
if not os.path.exists(checkpath):
checkpath=os.path.join(path, "../wp-config.php")
checksettings=os.path.join(path, "../wp-settings.php")
if os.path.exists(checksettings):
return "invalid"
if not os.path.exists(checkpath):
return "invalid"
f = file(checkpath)
while(True):
line = f.readline()
if not line:
break
match = vars.match(line)
if match:
key = match.group('key')
if key in mapping:
db_params[ mapping[key] ] = match.group('val')
match = TablePrefix.match(line)
if match:
db_params['table_prefix'] = match.group(1)
return db_params
def getsiteurl(dbinfo):
"""Obtain the siteurl value from the wp_options table
'dbinfo' the database information and table prefix"""
try:
db = MySQLdb.connect(dbinfo['host'],dbinfo['user'],dbinfo['passwd'],dbinfo['db'])
cursor = db.cursor()
query = "SELECT option_value from " + dbinfo['table_prefix'] + "options WHERE option_name='siteurl' LIMIT 1"
cursor.execute(query)
out=cursor.fetchone()
db.close()
return out[0]
except:
return "invalid"
def find_wp_installs(path="/",wpname="wp-cron.php"):
"""Scans directory for wordpress installations.
'paths' is the path to scan.
'wpname' is the name of the file to scan for."""
results = []
for num, p in enumerate(path):
if debug == "yes": print "%s DEBUG: processing %s (%s/%s)" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), p, num, len(path))
for base, dir, files in os.walk(p):
files = [f for f in files if f == wpname]
if files:
results.append(base)
return results
if __name__ == "__main__":
try:
url=""
wplatest, newest_filename = get_latest_wp_version()
for wpinstall in find_wp_installs(path):
configvars = wpconfig(wpinstall)
#If the directory doesn't have a valid wp-config.php, common for test installs/fresh installs then ignore
if configvars == "invalid":
continue
if checksiteurl == "yes":
#Check the credentials in wp-config.php are valid otherwise ignore
url=getsiteurl(configvars)
if url == "invalid":
continue
outdated=""
curver = get_wp_version(wpinstall)
if curver < wplatest:
outdated="- OUTDATED "
print "%s - %s %s- %s - %s" % (url, wpinstall, outdated, curver, wplatest)
except Exception,e:
print "An Exception occurred. Scan not performed."
if debug == "yes": print e
sys.exit(1)