forked from ajinabraham/nodejsscan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
239 lines (196 loc) · 6.4 KB
/
Copy pathutils.py
File metadata and controls
239 lines (196 loc) · 6.4 KB
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
#!/usr/bin/env python
# -*- coding: utf_8 -*-
"""
Utils
"""
import zipfile
import string
import random
import sys
import linecache
import datetime
import hashlib
import ntpath
import hmac
import time
import json
import os
import os.path as osp
import re
import ast
import unicodedata
import subprocess
import core.settings as settings
_punctuation_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')
class Color(object):
"""Coloring Terminal/Bash Output"""
GREEN = '\033[92m'
ORANGE = '\033[33m'
RED = '\033[91m'
BOLD = '\033[1m'
END = '\033[0m'
def print_exception(msg):
"""Print and Log Exception"""
_, exc_obj, tb_f = sys.exc_info()
f_obj = tb_f.tb_frame
lineno = tb_f.tb_lineno
filename = f_obj.f_code.co_filename
linecache.checkcache(filename)
line = linecache.getline(filename, lineno, f_obj.f_globals)
timestamp = time.time()
stringtime = datetime.datetime.fromtimestamp(
timestamp).strftime('%Y-%m-%d %H:%M:%S')
dat = '\n[' + stringtime + ']\n' + msg + \
'\nFILE: {0}\nLINE: {1} - {2}\nDESC: {3}'.format(
filename, lineno, line.strip(), exc_obj)
print(Color.BOLD + Color.RED + dat + Color.END)
with open(settings.LOG_FILE, 'a') as file_ptr:
file_ptr.write(dat)
def unzip(app_path, ext_path):
"""Unzip Files to a Given Path and Returns the list of files extracted"""
print("\n[INFO] Unzipping from Zip File")
try:
if not os.path.exists(ext_path):
os.makedirs(ext_path)
files = []
with zipfile.ZipFile(app_path, "r") as ptr:
ptr.extractall(ext_path)
files = ptr.namelist()
return files
except:
print_exception("[ERROR] Unzipping from Zip File with Python")
print("\n[INFO] Using the Default OS Unzip Utility.")
try:
subprocess.call(['unzip', '-o', '-q', app_path, '-d', ext_path])
dat = subprocess.check_output(['unzip', '-qq', '-l', app_path])
dat = dat.split('\n')
dat = ['Length Date Time Name'] + dat
return dat
except:
print_exception("[ERROR] Unzipping from Zip File")
def read_file(file_path):
"""Unicode Safe File Read, Returns data"""
with open(file_path, "rb") as file_ptr:
return file_ptr.read().decode('utf-8', 'replace')
def write_file(file_path, data):
"""Unicode Safe File Write"""
with open(file_path, "w") as file_ptr:
file_ptr.write(data)
def sha2_match_regex(data):
"""SHA2 String Match"""
return re.match('^[0-9a-f]{64}$', data)
def is_number(strn):
"""Checks if given string is a number"""
try:
float(strn)
return True
except ValueError:
pass
try:
unicodedata.numeric(strn)
return True
except (TypeError, ValueError):
pass
return False
def python_list(value):
"""Convert a list like string to python list"""
if not value:
value = []
if isinstance(value, list):
return value
return ast.literal_eval(value)
def python_dict(value):
"""Convert a dict like string to python dict"""
if not value:
value = {}
if isinstance(value, dict):
return value
return ast.literal_eval(value)
def gen_random_hmac_sha256(msg):
"""Generate HMAC SHA256 of a message with random key"""
key = hashlib.sha256(random.SystemRandom().random()).hexdigest()[:5]
hashed = hmac.new(key, msg, hashlib.sha256)
return hashed.hexdigest()
def gen_fixed_hmac_sha256(msg):
"""Generate HMAC SHA256 of a message with fixed key"""
key = settings.HMAC_KEY
hashed = hmac.new(key, msg, hashlib.sha256)
return hashed.hexdigest()
def gen_hmac_sha1(key, msg):
"""Generate HMAC SHA1 of a message"""
hashed = hmac.new(key, msg, hashlib.sha1)
return hashed.hexdigest()
def gen_sha256_hash(msg):
"""Generate SHA 256 Hash of the message"""
hash_object = hashlib.sha256(msg.encode('utf-8'))
return hash_object.hexdigest()
def gen_sha256_file(path):
"""Generate SHA 256 Hash of the file"""
blocksize = 64 * 1024
sha = hashlib.sha256()
with open(path, 'rb') as fptr:
while True:
data = fptr.read(blocksize)
if not data:
break
sha.update(data)
return sha.hexdigest()
def gen_sha256_files_n_dir(dirc):
"""Get SHA 256 Hash of entire directory"""
sha2 = {}
sha2_files = {}
for root, _, files in os.walk(dirc):
for fpath in [osp.join(root, f) for f in files]:
sha256_hash = gen_sha256_file(fpath)
sha2_files[fpath] = sha256_hash
sha2['dir_sha256'] = gen_sha256_hash(json.dumps(sha2_files))
sha2['files_sha256'] = sha2_files
return sha2
def gen_hashes(locations):
"""Generate File and Dir SHA256 Hashes"""
sha2_hashes = []
for loc in locations:
sha2 = gen_sha256_files_n_dir(loc)
sha2_hashes.append(sha2)
scan_hash = gen_sha256_hash(''.join(locations))
hash_of_sha2 = gen_sha256_hash(json.dumps(sha2_hashes))
return scan_hash, sha2_hashes, hash_of_sha2
def get_filename(path):
"""Get Filename from Path"""
head, tail = ntpath.split(path)
return tail or ntpath.basename(head)
def year():
"""Get Current Year"""
now = datetime.datetime.now()
return now.year
def random_string(size=6, chars=string.ascii_lowercase):
"""Random String Generator (Contains lowercase a-z and 0-9)"""
return ''.join(random.SystemRandom().choice(chars) for _ in range(size))
def slugify(text, delim=u'-'):
"""Generates an slightly worse ASCII-only slug."""
"""
Generate an ASCII-only slug.
"""
result = []
for word in _punctuation_re.split(text.lower()):
word = unicodedata.normalize('NFKD', word) \
.encode('ascii', 'ignore') \
.decode('utf-8')
if word:
result.append(word)
return delim.join(result)
def js_escape(value):
"""JS XSS Escape"""
return (value.replace('<', "\\u003c").
replace('>', "\\u003e").
replace('"', "\\u0022").
replace("'", "\\u0027").
replace("`", "\\u0060").
replace("(", "\\u0028").
replace(")", "\\u0029").
replace("{", "\\u007b").
replace("}", "\\u007d").
replace("-", "\\u002d").
replace("+", "\\u007d").
replace("$", "\\u0024").
replace("/", "\\u002f"))