-
Notifications
You must be signed in to change notification settings - Fork 0
/
TEMPLATE.py
424 lines (350 loc) · 14 KB
/
TEMPLATE.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import random
import requests
import logging
import argparse
import time
import json
import sys
import re
import asyncio
from aiohttp import ClientSession
# from urllib3.exceptions import InsecureRequestWarning
from bs4 import BeautifulSoup
from tqdm import tqdm, trange
# Create and configure logger
LOG_FORMAT = "%(levelname)s %(asctime)s - %(message)s"
# Suppress only the single warning from urllib3 needed.
# requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning)
def main():
log = logging.getLogger(__name__)
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
description="Description:\nThis is a template/skeleton script to avoid typing a lot of basic stuff.\n" +
"IMPORTANT: Make sure to update this as this script does not do anything.\n" +
"Also, delete the functions below once done as they are just tips and reminders.\n" +
"Message me if you have any suggestions to improve this template/skeleton script.\n" +
"Example commands:\n" +
"$ python3 " + sys.argv[0] + " --ip 1.2.3.4 --range-start-variable 30\n")
parser.add_argument("-v", dest="verbose", action="store_true",
default=False, help="Verbose/Debug mode")
parser.add_argument(
"--ip", dest="ip", required=True, type=str, help="The subnet:\n Example: XXX.XXX.XXX.46")
parser.add_argument(
"--range-start-variable", dest="range-start-variable", required=False, type=int, default=1, help="Range start. Default = 1")
args = parser.parse_args()
# Configure Logging Verbosity
if args.verbose:
logging.basicConfig(filename="./python-debug.log", format=LOG_FORMAT, level=logging.DEBUG)
else:
logging.basicConfig(filename="./python-info.log", format=LOG_FORMAT, level=logging.INFO)
log.debug("DEBUG: Args: %s", args)
log.info("INFO: Args: % s" % args.ip)
# Requests
proxies = {'http': 'http://127.0.0.1:8080', 'https': 'http://127.0.0.1:8080'}
headers = {'User-Agent': 'Mozilla'}
data = "id=1"
r = requests.get('https://google.com/', headers=headers, verify=False, proxies=proxies)
# r = requests.post('https://google.com/', headers=headers, verify=False, proxies=proxies, data=data)
# Providing the custom cert to requests
# When using proxy (i.e. Burp), rather than using `verify=False` in the `request.get` or `request.post`, convert the Burp certificate cacert.der instead to cacert.pem then use in on python requests
# Converting cacert.der to cacert.pem
# openssl x509 -in cacert.der -inform DER -outform PEM -out cacert.pem
# r = session.post(burp0_url, headers=burp0_headers, data=burp0_data,proxies=proxies,verify="cacert.pem")
print('Status_code is: ', r.status_code)
print('Headers are: ', r.headers)
print('Cookies are: ', r.cookies)
print('Text is: ', r.text)
if __name__ == "__main__":
start_time = time.perf_counter()
main()
ellapsed_time = round(time.perf_counter() - start_time, 2)
print(f'Finished in {ellapsed_time} seconds(s)')
def general_Tips_And_Tricks():
""" Tips and Tricks, mostly from https://www.youtube.com/watch?v=C-gEQdGVXbk"""
# - Context manager
try:
with open('test.txt', 'r') as f:
text_file = f.read()
print(text_file)
except Exception:
pass
# - Enumerate
names = ['test', 'teser']
for index, name in enumerate(names, start=1):
print(index, name)
# - Zip
names = ['test', 'teser']
namez = ['ztest', 'zteser']
for name, namz in zip(names, namez):
print(name, namz)
# - Unpack
for name, _ in zip(names, namez):
print(name)
a, b, *c = (1, 2, 3, 4, 5)
print(a, b, c)
a, b, *c, d = (1, 2, 3, 4, 5)
print(a, b, c, d)
a, b, *_, d = (1, 2, 3, 4, 5)
print(a, b, d)
# Setattr/Getattr
class Person():
pass
person = Person()
person_info = {'first': 'Corey', 'last': 'Schafer'}
for key, value in person_info.items():
setattr(person, key, value)
for key in person_info.keys():
print(getattr(person, key))
# Get sensitive information from CLI(getpass)
from getpass import getpass
password = getpass('Type in password: ')
# python3 -m (module)
# python3 -m venv my_env
# help/dir
# import smtpd
# help(smtpd)
# from datetime import datetime
# dir(datetime)
# datetime.today
# datetime.today()
# Progress bar (tqdm)
with tqdm(desc="Sample description", leave=False, unit=" Requests", total=50) as pbar: # Manual updating
for i in range(50):
r = requests.get(url)
pbar.update()
# or someFunction(pbar) then pbar.update() there
if int(r.headers['Content-Length']) == 200:
pbar.close()
return r.text
for i in trange(50): # Might not have leave feature
print("Do something)
for i in tqdm(range(50),desc="Sample description", leave=False, unit=" Requests"): # Might not have leave feature
print("Do something)
def concurrent_threading_tips():
"""
Tips from @CoreyShafer https://www.youtube.com/watch?v=IEEhzQoKtQU
Rule:
threads for IO bound
multiprocess for CPU bound
"""
# Synchronous sample
import time
start = time.perf_counter()
general_Tips_And_Tricks()
finish = time.perf_counter()
print('Finished in %s seconds(s)' % round(finish-start, 2))
# Sample thread - Synchronous sample - Manual
import time
import threading
start = time.perf_counter()
t1 = threading.Thread(target=general_Tips_And_Tricks)
t2 = threading.Thread(target=general_Tips_And_Tricks)
t1.start()
t2.start()
t1.join() # To wait for t1 to end before continuing
t2.join() # To wait for t2 to end before continuing
finish = time.perf_counter()
print('Finished in %s seconds(s)' % round(finish-start, 2))
# Loop - Manual
threads = []
for _ in range(10):
t = threading.Thread(target=general_Tips_And_Tricks)
# t = threading.Thread(target=general_Tips_And_Tricks, args=[<value>])
t.start()
threads.append(t)
for thread in threads:
thread.join()
# Threadpool executor
import concurrent.futures
with concurrent.futures.ThreadPoolExecutor() as executor:
# f1 = executor.submit(general_Tips_And_Tricks,<arg_value>)
f1 = executor.submit(general_Tips_And_Tricks, 1)
f2 = executor.submit(general_Tips_And_Tricks, 1)
print(f1.result())
print(f2.result())
# Threadpool executor - Loop
import concurrent.futures
with concurrent.futures.ThreadPoolExecutor() as executor:
results = [executor.submit(general_Tips_And_Tricks, 1) for _ in range(10)]
for f in concurrent.futures.as_completed(results):
print(f.result())
# Threadpool executor - Loop - List - Map
import concurrent.futures
with concurrent.futures.ThreadPoolExecutor() as executor:
secs = [5, 4, 3, 2, 1]
results = executor.map(general_Tips_And_Tricks, secs)
for result in results:
print(result)
# Real world example. Download images: https://github.com/CoreyMSchafer/code_snippets/blob/master/Python/Threading/download-images.py
def concurrent_multiprocessing_tips():
"""
Multiprocessing tips from https://www.youtube.com/watch?v=fKl2JW_qrso.
Nearly the same syntax as threading
Here it makes use of mutliprocessing.Process() instead of threading.Thread()
Even makes use of concurrent.futures
Makes use of concurrent.futures.ProcessPoolExecutor() instead of concurrent.futures.ThreadPoolExecutor()
Rule:
threads for IO bound
multiprocess for CPU bound
"""
# Sample multiprocessing - Synchronous sample - Manual
import multiprocessing
start = time.perf_counter()
p1 = multiprocessing.Process(target=general_Tips_And_Tricks)
p2 = multiprocessing.Process(target=general_Tips_And_Tricks)
p1.start()
p2.start()
p1.join()
p2.join()
finish = time.perf_counter()
print('Finished in %s seconds(s)' % round(finish-start, 2))
# Processpool executor - Loop
import concurrent.futures.ProcessPoolExecutor
with concurrent.futures.ProcessPoolExecutor() as executor:
results = [executor.submit(general_Tips_And_Tricks, 1) for _ in range(10)]
for f in concurrent.futures.as_completed(results):
print(f.result())
# Processpool executor - Loop - List - Map
import concurrent.futures
with concurrent.futures.ProcessPoolExecutor() as executor:
secs = [5, 4, 3, 2, 1]
results = executor.map(general_Tips_And_Tricks, secs)
for result in results:
print(result)
# Demo https://github.com/CoreyMSchafer/code_snippets/blob/master/Python/MultiProcessing/process-images.py
async def myCoroutine(id: int):
"""
Asyncio function 1
"""
process_time = random.randint(1, 5)
await asyncio.sleep(process_time)
print("Coroutine: {}, has successfully completed after {} seconds".format(id, process_time))
async def async_main():
"""
Main function in Asyncio Demo
taken from:
https://www.youtube.com/watch?v=L3RyxVOLjz8
https://www.youtube.com/watch?v=6ow7xloFy5s
"""
tasks = []
for i in range(10):
tasks.append(asyncio.ensure_future(myCoroutine(i)))
await asyncio.gather(*tasks)
# OR
tasks = []
for i in range(10):
tasks.append(
asyncio.create_task(
myCoroutine(i)
)
)
await asyncio.gather(*tasks)
# OR
await asyncio.wait([
myCoroutine(100),
myCoroutine(10),
myCoroutine(1)
])
# Three lines below should be indented left
# To run the async_main() function using event loop
loop = asyncio.get_event_loop()
loop.run_until_complete(async_main())
loop.close()
# OR a simpler way
asyncio.run(async_main())
async def async_main_http():
"""
Asyncio web scraping
taken from:
https://www.youtube.com/watch?v=6ow7xloFy5s
Sample code:
https://github.com/codingforentrepreneurs/30-Days-of-Python/blob/master/tutorial-reference/Day%2027/ascrape_sema.py
"""
# START of Asyncio POC code
# Updated to cleaner code from this: https://stackoverflow.com/questions/54303367/python-aiohttp-cancel-async-execution-on-met-condition
async def sqli_request(sem, session, url, headers, data, proxies,done):
"""
HTTP Request function to send SQLi
"""
async with sem, session.post(url, headers=headers, data=data, proxy=proxies['http'], allow_redirects=False) as response:
html_body = await response.text()
# if(response.status == 302):
# if response.headers['Content-Length'] != 2874:
if re.search("There is an SQL Error", html_body):
done.set()
done.run_answer = data
# return {"statusCode": response.status, "headers": response.headers, "body": html_body, "cookies": response.cookies}
async def sqli_main():
# Insert argparse and logging section HERE
# Variables
proxies = {'http': 'http://127.0.0.1:8080', 'https': 'http://127.0.0.1:8080'}
headers = {'User-Agent': 'Mozilla', 'Content-Type': 'application/json'}
protocol = 'http://'
path = '/someApp/someFile.php'
data = "id="+args.injection + "&sort=True"
url = protocol+args.host+path
sem = asyncio.Semaphore(10)
done = asyncio.Event()
async with ClientSession() as session:
tasks = []
for i in range(10):
tasks.append(
asyncio.create_task(
sqli_request(sem, session, url, headers, data, proxies,done)
)
)
await done.wait()
print(f"Possible SQLi using data:\n{done.run_answer}")
for t in tasks:
t.cancel()
# responses = await asyncio.gather(*tasks)
# for r in responses:
# print('Status_code is: ', r.get("statusCode"))
# print('Headers are: ', r.get("headers"))
# print('Cookies are: ', r.get("cookies"))
# print('Text is: ', r.get("body"))
# errors = re.search("There is an SQL Error", r.get("body"))
# if errors:
# print("There are errors!!! Take a closer look.")
# else:
# print("Nothing to see here.")
if "__SQLI__name__" == "__main__":
start_time = time.perf_counter()
asyncio.run(sqli_main())
ellapsed_time = round(time.perf_counter() - start_time, 2)
print(f'Finished in {ellapsed_time} seconds(s)')
# END of Asyncio POC code
def regex_tips_python():
"""
regex tips from https://www.youtube.com/watch?v=sa-TUpSx1JA
Cheatsheet for general regex
Snippets taken from https://github.com/CoreyMSchafer/code_snippets/blob/master/Regular-Expressions/snippets.txt
. - Any Character Except New Line
\d - Digit (0-9)
\D - Not a Digit (0-9)
\w - Word Character (a-z, A-Z, 0-9, _)
\W - Not a Word Character
\s - Whitespace (space, tab, newline)
\S - Not Whitespace (space, tab, newline)
\b - Word Boundary
\B - Not a Word Boundary
^ - Beginning of a String
$ - End of a String
[] - Matches Characters in brackets
[^ ] - Matches Characters NOT in brackets
| - Either Or
( ) - Group
Quantifiers:
* - 0 or More
+ - 1 or More
? - 0 or One
{3} - Exact Number
{3,4} - Range of Numbers (Minimum, Maximum)
#### Sample Regexs ####
Email regex:
[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+
URL regex:
Basic: https?://(www\.)?\w+\.\w+
Grouping: https?://(www\.)?(\w+)(\.\w+)
Replace: $1$2$3\n$0
"""