Skip to content

Commit 7e52a73

Browse files
authored
Merge pull request #31 from Add3r/karthick-dev
Code change from Procedural to ObjectOriented
2 parents 778ded8 + 433fcdd commit 7e52a73

1 file changed

Lines changed: 162 additions & 0 deletions

File tree

V2-User-Agent-Dict.py

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
import requests
2+
from bs4 import BeautifulSoup
3+
import json
4+
5+
# Color Codes for Printing
6+
GREEN = "\033[32m"
7+
YELLOW = "\033[33m"
8+
BLUE = "\033[34m"
9+
RED = "\033[31m"
10+
RESET = "\033[0m"
11+
12+
class UserAgentFuzzLib:
13+
def __init__(self):
14+
self.url = "https://www.useragentstring.com/pages/useragentstring.php?name=All"
15+
self.user_agents = []
16+
self.sequence = 0
17+
self.seen_combinations = set()
18+
self.mobile_detected = False
19+
20+
def parse_user_agents(self):
21+
try:
22+
response = requests.get(self.url)
23+
response.raise_for_status() # Raise an exception if there's an HTTP error
24+
soup = BeautifulSoup(response.content, 'html.parser')
25+
26+
for tag in soup.find_all(True):
27+
if tag.name == 'h3':
28+
group_text = tag.get_text(strip=True)
29+
if group_text == "MOBILE BROWSERS":
30+
host = "Mobile"
31+
else:
32+
host = "General"
33+
group = group_text
34+
35+
elif tag.name == 'h4':
36+
title = tag.get_text(strip=True)
37+
38+
ul_tag = tag.find_next('ul')
39+
a_tags = ul_tag.find_all('a')
40+
41+
for a_tag in a_tags:
42+
href = a_tag['href']
43+
user_agent = a_tag.text
44+
user_agent = user_agent.replace("-->>", "")
45+
46+
if "Opera/9.80 (J2ME/MIDP; Opera Mini/4.2.14912Mod.By.www.9jamusic.cz.cc/22.387; U; en)" in user_agent:
47+
continue
48+
49+
host = "Mobile" if self.mobile_detected else "General"
50+
51+
if (user_agent, title) in self.seen_combinations:
52+
continue
53+
54+
self.sequence += 1
55+
ua_dict = {
56+
"title": title if title != "Opera/9.80 (J2ME/MIDP; Opera Mini/4.2.14912Mod.By.www.9jamusic.cz.cc/22.387; U; en)" else "",
57+
"group": group if group != "Opera/9.80 (J2ME/MIDP; Opera Mini/4.2.14912Mod.By.www.9jamusic.cz.cc/22.387; U; en)" else "",
58+
"id": f"ua-{self.sequence}",
59+
"user-agent": user_agent if user_agent != "Opera/9.80 (J2ME/MIDP; Opera Mini/4.2.14912Mod.By.www.9jamusic.cz.cc/22.387; U; en)" else "",
60+
"Host": host
61+
}
62+
63+
if "Xenu Link Sleuth/1.3.7" in user_agent:
64+
self.mobile_detected = True
65+
66+
self.user_agents.append(ua_dict)
67+
self.seen_combinations.add((user_agent, title))
68+
69+
except requests.exceptions.RequestException as e:
70+
print(f"{RED}[ERROR]{RESET} Unable to retrieve data from the URL.")
71+
except (AttributeError, ValueError, BeautifulSoup.exceptions.BeautifulSoupError) as e:
72+
print(f"{RED}[ERROR]{RESET} Error while parsing HTML:", e)
73+
except KeyboardInterrupt:
74+
print(f"\n{RED}[ERROR]{RESET} Program interrupted by user.")
75+
exit(1)
76+
77+
def print_user_agents(self):
78+
try:
79+
while True:
80+
print_data = input("Do you want to print the data on the screen? (yes/no): ").lower()
81+
if print_data in ["yes", "no"]:
82+
break
83+
else:
84+
print(f"{RED}[ERROR]{RESET} Please enter 'yes' or 'no.'")
85+
86+
if print_data == "yes":
87+
print(json.dumps(self.user_agents, indent=4))
88+
else:
89+
print(f"{YELLOW}[!]{RESET} Data was not printed on the screen.")
90+
except KeyboardInterrupt:
91+
print(f"\n{RED}[ERROR]{RESET} Program interrupted by user.")
92+
exit(1)
93+
94+
def update_json_file(self):
95+
try:
96+
while True:
97+
update_json = input("Do you want to update the JSON file? (yes/no): ").lower()
98+
if update_json in ["yes", "no"]:
99+
break
100+
else:
101+
print(f"{RED}[ERROR]{RESET} Please enter 'yes' or 'no.'")
102+
103+
if update_json == "yes":
104+
json_file_path = "user_agents.json"
105+
106+
try:
107+
with open(json_file_path, "w") as json_file:
108+
json.dump(self.user_agents, json_file, indent=4)
109+
print(GREEN + "[+]" + RESET + " JSON file updated successfully.")
110+
111+
self.print_new_user_agents()
112+
113+
except (FileNotFoundError, PermissionError, json.JSONDecodeError) as e:
114+
print(f"{RED}[ERROR]{RESET} Error while updating JSON file:", e)
115+
else:
116+
print(RED + "[x]" + RESET + " JSON file was not updated.")
117+
self.print_new_user_agents()
118+
119+
except KeyboardInterrupt:
120+
print(f"\n{RED}[ERROR]{RESET} Program interrupted by user.")
121+
exit(1)
122+
123+
# Method to check if new user agents found
124+
def print_new_user_agents(self):
125+
total_user_agents_before = len(self.user_agents)
126+
127+
if len(self.user_agents) > total_user_agents_before:
128+
new_user_agents = self.user_agents[total_user_agents_before:]
129+
print(GREEN + '[+]' + RESET + " New User Agents Identified: " + BLUE + str(len(new_user_agents)) + RESET)
130+
for ua in new_user_agents:
131+
ua_dict = {
132+
"title": ua[0],
133+
"group": ua[1],
134+
"id": ua[2],
135+
"user-agent": ua[3],
136+
"Host": ua[4]
137+
}
138+
print(json.dumps(ua_dict, indent=4))
139+
140+
input("Press Enter to acknowledge...")
141+
else:
142+
print(f"{YELLOW}[!]{RESET} No new user-agents found.")
143+
144+
def print_updates(self):
145+
try:
146+
mobile_user_agents = sum(1 for ua in self.user_agents if ua["Host"] == "Mobile")
147+
general_user_agents = sum(1 for ua in self.user_agents if ua["Host"] == "General")
148+
total_user_agents_after = len(self.user_agents)
149+
150+
print(GREEN + "[+]" + RESET + " General User Agents: " + BLUE + str(general_user_agents) + RESET)
151+
print(GREEN + "[+]" + RESET + " Mobile User Agents: " + BLUE + str(mobile_user_agents) + RESET)
152+
print(GREEN + "[+]" + RESET + " Total User Agents: " + BLUE + str(total_user_agents_after) + RESET)
153+
except KeyboardInterrupt:
154+
print(f"\n{RED}[ERROR]{RESET} Program interrupted by user.")
155+
exit(1)
156+
157+
if __name__ == "__main__":
158+
fuzzlib = UserAgentFuzzLib()
159+
fuzzlib.parse_user_agents()
160+
fuzzlib.print_user_agents()
161+
fuzzlib.update_json_file()
162+
fuzzlib.print_updates()

0 commit comments

Comments
 (0)