forked from MrSentex/0day.today-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApiLib.py
129 lines (85 loc) · 4.8 KB
/
ApiLib.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
#############################
# 0day.today unofficial api #
# By MrSentex #
#############################
# <= Imports =>
from random import SystemRandom
from requests import Session
from codecs import ascii_encode
from unidecode import unidecode
from bs4 import BeautifulSoup as Soup
# <= Imports =>
class api_0day_today:
def __init__(self):
self.url = "http://{}.0day.today".format(self.randomSubDomain(5))
self.session_obj = Session()
self.terms = self.acceptTerms()
def acceptTerms(self):
try:
self.session_obj.post(self.url, data={"agree" : "Yes, I agree"})
return (True, None)
except Exception as err:
return (False, str(err))
def randomSubDomain(self, interactions):
string = ""
for _ in range(1, interactions):
string += SystemRandom().choice("abcdefghijklmnopqrstuvwxyz1234567890")
return string
def fixString(self, string):
string = ascii_encode(unidecode(string.replace("\n", "").replace("\t", "")))[0]
if string.find("Comments:") != -1:
string = string[:string.find("Comments: {}".format(string[string.find("Comments:")+10:string.find("Comments:")+len(string)-string.find("Comments:")-1]))]
if string.find("Rate down:") != -1:
string = string[:string.find("Rate down: {}".format(string[string.find("Rate down:")+11:string.find("Rate down:")+len(string)-string.find("Rate down:")-1]))]
if string.find("Rate up:") != -1:
string = string[:string.find("Rate up: {}".format(string[string.find("Rate up:")+9:string.find("Rate up:")+len(string)-string.find("Rate up:")-1]))]
return string
def fixPrice(self, price):
price = self.fixString(price)
if price.startswith("free"):
return "free"
btc = price[price.find("for")+4:price.find("BTC")-1]
pre_gold = price[price.find("BTC")+4:len(price)]
gold = pre_gold[pre_gold.find("for")+4:pre_gold.find("GOLD")-1]
return "{} BTC or {} GOLD".format(btc, gold)
def search(self, param):
if not self.terms[0]:
return {"status" : "fail", "exception" : "An error ocurred in the acceptTerms function | {}".format(self.terms[1])}
param = param.replace(" ", "+")
try:
response = self.session_obj.get("{}/search?search_request={}".format(self.url, param))
parser = Soup(response.text, "lxml")
result = []
tables = parser.find_all("div", attrs={"class" : "ExploitTableContent"})
for table in tables:
rows = table.find_all("div", attrs={"class" : "td"})
date = self.fixString(rows[0].getText())
desc = self.fixString(rows[1].getText())
platform = self.fixString(rows[2].getText())
price = self.fixPrice(rows[9].getText())
author = self.fixString(rows[10].getText())[0:self.fixString(rows[10].getText()).find("Exploits")]
url = rows[1].find("a", href=True)["href"].replace("/description", "")
result.append({"date" : date, "desc" : desc, "platform" : platform, "price" : price, "author" : author, "url" : "https://0day.today/"+url})
return {"status" : "success", "response" : result}
except Exception as err:
return {"status" : "fail", "exception" : str(err)}
def getIndex(self):
if not self.terms[0]:
return {"status" : "fail", "exception" : "An error ocurred in the acceptTerms function | {}".format(self.terms[1])}
try:
response = self.session_obj.get("{}".format(self.url))
parser = Soup(response.text, "lxml")
result = []
tables = parser.find_all("div", attrs={"class" : "ExploitTableContent"})
for table in tables:
rows = table.find_all("div", attrs={"class" : "td"})
date = self.fixString(rows[0].getText())
desc = self.fixString(rows[1].getText())
platform = self.fixString(rows[2].getText())
price = self.fixPrice(rows[9].getText())
author = self.fixString(rows[10].getText())[0:self.fixString(rows[10].getText()).find("Exploits")]
url = rows[1].find("a", href=True)["href"].replace("/description", "")
result.append({"date" : date, "desc" : desc, "platform" : platform, "price" : price, "author" : author, "url" : self.url+url})
return {"status" : "success", "response" : result}
except Exception as err:
return {"status" : "fail", "exception" : str(err)}