forked from rivsec/wordpress-rest-enum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordpress-rest-enum.py
141 lines (122 loc) · 5.75 KB
/
wordpress-rest-enum.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
# wordpress-rest-enum.py
import requests
import json
import argparse
import logging
import urllib3
urllib3.disable_warnings()
# Argument parsing
parser = argparse.ArgumentParser()
parser.add_argument("-w", "--website", help="Website to check.", action='store', type=str, required=True)
parser.add_argument("--log-level", default=logging.INFO, type=lambda x: getattr(logging, x), help="Configure the logging level.")
parser.add_argument("-m", "--media", help="Fetch media", action=argparse.BooleanOptionalAction, required=False)
parser.add_argument("-po", "--posts", help="Fetch posts", action=argparse.BooleanOptionalAction, required=False)
parser.add_argument("-pa", "--pages", help="Fetch pages", action=argparse.BooleanOptionalAction, required=False)
parser.add_argument("-u", "--users", help="Fetch users", action=argparse.BooleanOptionalAction, required=False)
parser.add_argument("-c", "--comments", help="Fetch comments", action=argparse.BooleanOptionalAction, required=False)
cliArgs = parser.parse_args()
# Logging
logging.basicConfig(level=cliArgs.log_level)
# Globals
HEADERS = {'User-Agent': 'WordPress Testing'}
def requestRESTAPIComments(website: str, fetchPage: int, timeout=10) -> json:
perPage = 100
apiRequest = f'{website}/wp-json/wp/v2/comments?per_page={perPage}&page={str(fetchPage)}'
results = []
try:
with requests.Session() as s:
download = s.get(apiRequest, headers=HEADERS, verify=False, timeout=timeout)
if download.status_code == 200:
# WordPress API returns mixed HTML and JSON in the users API endpoint.
# Remove all content to the first `[` indicating the beginning of JSON data.
content = '[' + '['.join(download.text.split('[')[1:])
comments = json.loads(content)
for comment in comments:
try:
newComment = {"name": comment['author_name'], "date":
comment['date'], "link": comment['link']}
results.append(newComment)
except Exception as err:
print(f"Unexpected {err=}, {type(err)=}")
raise
fetchPage = fetchPage + 1
if len(comments) > 0:
results += requestRESTAPIComments(website, fetchPage)
except json.JSONDecodeError as e:
pass
except urllib3.exceptions.MaxRetryError as e:
pass # Silently fail and do nothing
except requests.exceptions.ConnectionError as e:
pass # Silently fail and do nothing
return results
def requestRESTAPIUsers(website: str, fetchPage: int, timeout=10) -> json:
perPage = 100
apiRequest = f'{website}/wp-json/wp/v2/users?per_page={perPage}&page={str(fetchPage)}'
results = []
try:
with requests.Session() as s:
download = s.get(apiRequest, headers=HEADERS, verify=False, timeout=timeout)
if download.status_code == 200:
content = download.text
users = json.loads(content)
for user in users:
try:
newUser = {"name": user['name'], "username": user['slug']}
results.append(newUser)
except Exception as err:
print(f"Unexpected {err=}, {type(err)=}")
raise
fetchPage = fetchPage + 1
if len(users) > 0:
results += requestRESTAPIUsers(website, fetchPage)
except json.JSONDecodeError as e:
pass
except urllib3.exceptions.MaxRetryError as e:
pass # Silently fail and do nothing
except requests.exceptions.ConnectionError as e:
pass # Silently fail and do nothing
return results
def requestRESTAPI(type: str, website: str, fetchPage: int, timeout=10) -> list:
perPage = 100
try:
apiRequest = f'{website}/wp-json/wp/v2/{type}?per_page={perPage}&page={str(fetchPage)}'
results = []
with requests.Session() as s:
download = s.get(apiRequest, headers=HEADERS, verify=False, timeout=timeout)
if download.status_code == 200:
content = download.text
if content is not None:
apiResponse = json.loads(content)
for typeReturn in apiResponse:
try:
results.append(typeReturn['guid']['rendered'])
except Exception as err:
print(f"Unexpected {err=}, {type(err)=}")
raise
fetchPage = fetchPage + 1
if len(apiResponse) > 0:
results += requestRESTAPI(type, website, fetchPage)
except json.JSONDecodeError as e:
pass
except urllib3.exceptions.MaxRetryError as e:
pass # Silently fail and do nothing
except requests.exceptions.ConnectionError as e:
pass # Silently fail and do nothing
return results
def main():
website = cliArgs.website
fetchPage = 1
result = {}
if cliArgs.posts:
result["posts"] = requestRESTAPI("posts", website, fetchPage)
if cliArgs.pages:
result["pages"] = requestRESTAPI("pages", website, fetchPage)
if cliArgs.comments:
result["comments"] = requestRESTAPIComments(website, fetchPage)
if cliArgs.media:
result["media"] = requestRESTAPI("media", website, fetchPage)
if cliArgs.users:
result["users"] = requestRESTAPIUsers(website, fetchPage)
print(json.dumps(result))
if __name__ == '__main__':
main()