-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
170 lines (141 loc) · 6.5 KB
/
utils.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
import paramiko
import scp as sp
import yaml
import datetime
import pytz
import string
import random
import hashlib
# IPV4SEG = r'(?:^| )(?P<address>((?:[1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}(?:[1-9]?\d|1\d{2}|2[0-4]\d|25[0-5]))(?P<slash>/)?(?(slash)(?P<cidr>[0-9]|[12][0-9]|3[0-2]))(?:$| )'
IPV4ADDR = r'^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
# from http://vernon.mauery.com/content/projects/linux/ipv6_regex
IPV6ADDR = r'(\A([0-9a-f]{1,4}:){1,1}(:[0-9a-f]{1,4}){1,6}\Z)|' \
r'(\A([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,5}\Z)|' \
r'(\A([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,4}\Z)|' \
r'(\A([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,3}\Z)|' \
r'(\A([0-9a-f]{1,4}:){1,5}(:[0-9a-f]{1,4}){1,2}\Z)|' \
r'(\A([0-9a-f]{1,4}:){1,6}(:[0-9a-f]{1,4}){1,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-5]|2[0-4]\d|[0-1]?\d?\d)){3}\Z)'
URLS = r'^(ftps?|https?|chrome):\/\/[^\s$.?#].[^\s]*'
def ssh_execute(host, port, user, password, commands, noprompt=False, logout=True):
result = None
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(host, port=port, username=user, password=password)
if type(commands) == type(str()):
stdin, stdout, stderr = ssh.exec_command(commands, timeout=10)
if noprompt == False:
result = "".join(stdout.readlines())
if type(commands) == type(list()):
result = {}
for command in commands:
stdin, stdout, stderr = ssh.exec_command(command, timeout=10)
result[command] = "".join(stdout.readlines())
if logout:
ssh.close()
else:
return ssh # Return SSH object to logout later
except Exception as e:
print("[+] Error in ssh_execute: %s" % (e,))
return result
def ssh_prompt(host, port, user, password):
ssh = ssh_execute(host, port, user, password, "uname -a", noprompt=False, logout=False)
while True:
cmd = input(">>")
if cmd == "exit_ssh":
ssh.close()
break
stdin, stdout, stderr = ssh.exec_command(cmd, timeout=10)
print("".join(stdout.readlines()))
def sftp(host, port, user, password, src_file, dst_file):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(host, port=port, username=user, password=password)
sftp = ssh.open_sftp()
sftp.put(src_file, dst_file)
except Exception as e:
print ("[+] Error in scp: %s" % (e,))
def scp(host, port, user, password, src_file, dest_file, dir=True):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(host, port=port, username=user, password=password)
scp_obj = sp.SCPClient(ssh.get_transport())
scp_obj.put(src_file, recursive=dir,remote_path=dest_file)
scp_obj.close()
except Exception as e:
print("[+] Error in scp: %s" % (e,))
# load a yaml file and return the values
def get_config(file):
cfg = None
with open(file) as ymlfile:
cfg = yaml.load(ymlfile)
return cfg
def get_rand_string(length, codef=string.ascii_letters):
"""
Random string generator
:param length: Length of the string
:param codef: Codification to use, by default the ASCII is being used
:return: random string
"""
return ''.join(random.choice(codef) for i in range(length))
def sha256_checksum(filename, block_size=65536):
sha256 = hashlib.sha256()
with open(filename, 'rb') as f:
for block in iter(lambda: f.read(block_size), b''):
sha256.update(block)
return sha256.hexdigest()
def entropy(string):
"""
Measures the entropy of the a string and returns a value
:param string:
:return:
"""
import math
log2 = lambda x: math.log(x) / math.log(2)
exr = {}
t_len = len(string)
infoc = 0
for each in string:
try:
exr[each] += 1
except:
exr[each] = 1
freq = 0
for k, v in exr.items():
freq = 1.0 * v / t_len
infoc += freq * log2(freq)
infoc *= -1
return infoc
# Static function get QEMU command, this one can be used by any one or any class, since
# it has been defined to establish the scope of the program, it should only be changed
# if a major change is done to the architecture
#TODO: Needs to be improved and we need to add more ARCHs in a correct way
def get_qemu_command(arch, cfg):
sim = {"arm": "qemu-system-arm",
"mips": "qemu-system-mips",
"x86": "qemu-system-i386"}
result = "%s -M malta -kernel %s\
-hda %s \
-net user,hostfwd=tcp::10022-:22\
-net nic\
-monitor stdio \
-append \"root=/dev/sda1 console=tty0\"" % (sim[arch], cfg["images"]["debian"]["kernel"], cfg["images"]["debian"]["image"])
return result
# From date to miliseconds since epoch
def from_date_to_msepoch(year, month, day, hour, minute, seconds, microseconds=None):
if microseconds is not None:
return int((datetime.datetime(year, month, day, hour, minute, microsecond=microseconds,tzinfo=pytz.UTC) - datetime.datetime(1970, 1, 1,tzinfo=pytz.UTC)).total_seconds()*1000)
return int((datetime.datetime(year, month, day, hour, minute, second=seconds,tzinfo=pytz.UTC) - datetime.datetime(1970, 1, 1,tzinfo=pytz.UTC)).total_seconds()*1000)