-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalternate.py
59 lines (51 loc) · 2.61 KB
/
alternate.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
import os
import sys
import pymysql
import re
# Function to extract database configuration from wp-config.php
def extract_db_config(wp_config_path):
db_config = {}
with open(wp_config_path, 'r') as f:
for line in f:
match = re.match(r"define\('DB_(NAME|USER|PASSWORD|HOST)',\s*'(.*)'\);", line)
if match:
db_config[match.group(1).lower()] = match.group(2)
return db_config
# Function to search for URLs within WordPress content and metadata
def search_for_urls_in_wordpress(sites_directory, url_pattern):
for site in os.listdir(sites_directory):
site_path = os.path.join(sites_directory, site)
if os.path.isdir(site_path):
wp_config_path = os.path.join(site_path, 'wp-config.php')
if os.path.isfile(wp_config_path):
db_config = extract_db_config(wp_config_path)
if db_config:
try:
connection = pymysql.connect(**db_config)
cursor = connection.cursor()
# Search for URLs in post content
cursor.execute("SELECT ID, post_content FROM wp_posts WHERE post_content REGEXP %s", (url_pattern,))
posts = cursor.fetchall()
for post_id, post_content in posts:
if re.search(url_pattern, post_content):
print(f"Found URL in post content for site: {site}")
print(f"Post ID: {post_id}")
# Search for URLs in post meta values
cursor.execute("SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE meta_value REGEXP %s", (url_pattern,))
metas = cursor.fetchall()
for post_id, meta_key, meta_value in metas:
if re.search(url_pattern, meta_value):
print(f"Found URL in post meta for site: {site}")
print(f"Post ID: {post_id}, Meta Key: {meta_key}")
cursor.close()
connection.close()
except pymysql.Error as e:
print(f"Error searching for URLs for site: {site}")
print(f"Error details: {e}")
if __name__ == '__main__':
if len(sys.argv) < 3:
print("Usage: python search_wordpress_urls.py <sites_directory> <url_pattern>")
sys.exit(1)
sites_directory = sys.argv[1]
url_pattern = sys.argv[2]
search_for_urls_in_wordpress(sites_directory, url_pattern)