-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
bugbounty.py
314 lines (233 loc) · 10.6 KB
/
bugbounty.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# github.com/tintinweb
#
"""
BugBounty Companion
(1) clone all **Immunefi** Repositories according highest bugbounty payout amount
$ bugbounty.py [sync|unique|clone]
(a) sync with immunefi
$ bugbounty.py sync
(b) show unique repos
$ bugbounty.py unique [minReward=100000, maxReward=200000, maxAge=100, ignore=byteball,immunefi-team]
(c) clone all repos (dryrun)
$ bugbounty.py unique clone [minReward=100000]
(d) clone all repos (actually do it)
$ bugbounty.py unique clone no-dryrun [minReward=100000]
# NOTE: default output folder is $(pwd)/bugbounty_repos/<project>
(2) run your fancy tools/code-smell rules on it
(3) profit $$
Consider donating if you win a $mm 🥳
"""
import requests
import re
import json
import os
from datetime import datetime, timedelta
from decimal import Decimal
##### CONFIG THIS
MIN_REWARD_CUTOFF = 100_000 # only check out projects with max bounty >= 100_000 $$
OUTPATH = os.path.abspath("./bugbounty_repos") # abspath to main repo checkout dir
##### NO CHANGES BEYOND THIS LINE
class BaseCompanion(object):
REX_SUB_FOR_SHELL = re.compile(r'[^a-zA-Z0-9\_\-]')
REX_DUP_USCORE = re.compile(r'_+')
def __init__(self, repofile):
self.repofile = repofile
self.data = []
def loadRepo(self):
try:
with open(self.repofile, 'r') as f:
self.data = json.load(f)
except: pass
return self
def _saveRepo(self):
with open(self.repofile, 'w') as f:
f.write(json.dumps(self.data))
return self
def gitClone(self, basedir, project, giturl):
if("/tree/" in giturl or "/blob/" in giturl): return # skip single files
project = self.REX_DUP_USCORE.sub("_", self.REX_SUB_FOR_SHELL.sub("_",project.lower()))
cmd = "mkdir -p %s/%s; cd %s/%s && git clone --depth=1 %s"%(basedir, project, basedir, project, giturl)
if "no-dryrun" in sys.argv:
os.system(cmd)
print(cmd)
def getUniqeRepos(self, repos):
return sorted(set([ "/".join(r.split("/")[0:5]) for r in repos]))
class ImmunefiCompanion(BaseCompanion):
def __init__(self, repofile="immunefi_repos.json"):
super().__init__(repofile=repofile)
def getRepos(self):
mainPage = requests.get("https://immunefi.com/explore/?filter=immunefi").text
mainMeta = re.findall('id="__NEXT_DATA__" type="application/json">(.*)</script></body></html>', mainPage)
if not mainMeta:
return
all_projects = json.loads(mainMeta[0])["props"]["pageProps"]["bounties"]
print("found %d projects"%len(all_projects))
results = self.data
for nr,pstruct in enumerate(all_projects):
p= pstruct["id"]
pmeta = {
'name': p,
'max_reward': 0,
'repos': []
}
all_links = set([])
source = requests.get("https://immunefi.com/bounty/%s/"%p).text
links = re.findall('id="__NEXT_DATA__" type="application/json">(.*)</script></body></html>', source)
pmeta['max_reward'] = int(pstruct['maximum_reward'])
print("#",nr, p, "$$", pmeta["max_reward"])
for f in links:
dataJson = json.loads(f)
if "assets_in_scope" in dataJson["props"]["pageProps"]["bounty"].keys():
for target in dataJson["props"]["pageProps"]["bounty"]["assets_in_scope"]:
if target["target"].startswith("https://github.com"):
all_links.add(target["target"])
if "mdx" in dataJson["props"]["pageProps"]["bounty"].keys():
for mdx in dataJson["props"]["pageProps"]["bounty"]["mdx"].values():
if not mdx: continue
alt = re.findall("(https?://github.com/[a-zA-Z_\-0-9\/#]+)", mdx)
all_links.update(alt)
if "assets" in dataJson["props"]["pageProps"]["bounty"].keys():
if dataJson["props"]["pageProps"]["bounty"]["assets"]:
for ass in dataJson["props"]["pageProps"]["bounty"]["assets"]:
if not ass: continue
if not "url" in ass.keys(): continue
alt = re.findall("(https?://github.com/[a-zA-Z_\-0-9\/#]+)", ass["url"])
all_links.update(alt)
if len(all_links) == 0:
# fallback, grep serialized links
alt = re.findall("(https?://github.com/[a-zA-Z_\-0-9\/#]+)", str(links))
all_links.update(alt)
# cleanup
def filterLinks(l):
if "#" in l: return False
if "audit" in l: return False
if "PublicReports" in l or "Halborn" in l: return False
if len(l.split("/",5))<5: return False
if l.split("/",5)[-1]=="": return False
return True
all_links = [l for l in all_links if filterLinks(l) ]
print("found %d repos"%len(all_links))
if len(all_links)== 0 and False: # debug
import pprint
pprint.pprint(dataJson["props"])
exit()
pmeta["repos"] = sorted(list(all_links))
results.append(pmeta)
# sort results by max bounty
results = list(reversed(sorted(results, key=lambda x: int(x['max_reward']))))
self.data = results
return self
class Code4renaCompanion(BaseCompanion):
def __init__(self, repofile="code4rena.json"):
super().__init__(repofile=repofile)
def _amountToDecimal(self, amount):
return int(Decimal(re.sub(r'[^\d.]', '', amount)))
def getRepos(self):
return self.getReposV1()
def getReposV1(self):
now = datetime.now()
mydata = requests.get("https://code4rena.com/audits").text
idx_start = mydata.find('\\"contests\\":[{\\') #"contests\":[{
#idx_end = mydata.find('}]}]}]}]\\n"]',idx_start+1)
idx_end = mydata.find(',\\"coreAppPage\\":', idx_start+1)
contests = json.loads( "{" + str.encode(mydata[idx_start:idx_end-1]).decode("unicode-escape"))['contests']
results = []
for c in contests:
end_time = datetime.strptime(c["end_time"],'%Y-%m-%dT%H:%M:%S.%fZ')
if end_time <= now:
continue
c["max_reward"] = self._amountToDecimal(c["amount"])
c["repos"] = [c["repo"]]
c["name"] = c["title"]
results.append(c)
self.data = results
return self
def getReposV0(self):
now = datetime.now()
cdata = requests.get("https://code4rena.com/page-data/index/page-data.json").json()["result"]["data"]["contests"]["edges"]
results = []
for contest in cdata:
c = contest["node"]
end_time = datetime.strptime(c["end_time"],'%Y-%m-%dT%H:%M:%S.%fZ')
if end_time <= now:
continue
c["max_reward"] = self._amountToDecimal(c["amount"])
c["repos"] = [c["repo"]]
c["name"] = c["title"]
results.append(c)
self.data = results
return self
def getMinRewardCutoffFromArgs():
for a in sys.argv:
if a.startswith("minReward="):
return int(a[len("minReward="):])
return MIN_REWARD_CUTOFF
def getMaxRewardCutoffFromArgs():
for a in sys.argv:
if a.startswith("maxReward="):
return int(a[len("maxReward="):])
return None
def getMaxAgeFromArgs():
for a in sys.argv:
if a.startswith("maxAge="):
return int(a[len("maxAge="):])
return None
def getIgnoreFromArgs():
for a in sys.argv:
if a.startswith("ignore="):
return a[len("ignore="):].split(',')
return None
if __name__ == "__main__":
import sys
programs = []
if "immunefi" in sys.argv:
programs.append(ImmunefiCompanion())
if "code4rena" in sys.argv or "codearena" in sys.argv or "code4" in sys.argv or "c4" in sys.argv:
programs.append(Code4renaCompanion())
if not len(programs):
if "noask" not in sys.argv:
print("⚠️ You are about to run this script on ALL bug bounty platforms (currently: C4 AND Immunefi)!")
yno = input("Continue? [yN] (note: use cmdline arg 'noask' to skip )")
if yno != "y":
raise Exception("abort")
programs = [Code4renaCompanion(), ImmunefiCompanion()]
arg_min_reward = getMinRewardCutoffFromArgs()
arg_max_reward = getMaxRewardCutoffFromArgs()
arg_max_age = getMaxAgeFromArgs()
arg_ignore = getIgnoreFromArgs()
for ic in programs:
if "sync" in sys.argv:
bounties = ic.getRepos().data
ic._saveRepo()
else:
bounties = ic.loadRepo().data
num_repos = 0
num_bounties_selected = 0
for bounty in reversed(sorted(bounties, key=lambda x: int(x['max_reward']))):
if arg_ignore and any(e.lower() in bounty["name"].lower() for e in arg_ignore): continue
if int(bounty["max_reward"]) < arg_min_reward: continue
if arg_max_reward and int(bounty["max_reward"]) > arg_max_reward: continue
if arg_max_age and "start_time" in bounty and datetime.strptime(bounty["start_time"],'%Y-%m-%dT%H:%M:%S.%fZ') >= datetime.now()+timedelta(days=-arg_max_age):
continue
if not len(bounty["repos"]): continue
num_bounties_selected += 1
repos = bounty["repos"]
if "unique" in sys.argv:
repos = ic.getUniqeRepos(bounty["repos"])
num_repos += len(repos)
print("%-30s : $$ %-30s" % (bounty["name"], bounty["max_reward"]))
for k in set(["/".join(r.split("/")[:5]) for r in repos]):
if "#" in k: continue
print(" ➡️ %s"%k)
if "clone" in sys.argv:
ic.gitClone(OUTPATH, bounty["name"], k) # TODO: maybe handle path exists; it will just error right now
print("=============================")
print(ic.__class__.__name__)
print("-----------------")
print("total bounties: %d"%len(bounties))
print("")
print("min_reward: %d"%arg_min_reward)
print(" - selected bounties: %d"%num_bounties_selected)
print(" - selected repos: %d"%num_repos)