-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.py
206 lines (179 loc) · 6.78 KB
/
install.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
# -*- coding: utf-8 -*-
# Octowire Framework
# Copyright (c) ImmunIT - Jordan Ovrè / Paul Duncan
# License: Apache 2.0
# Paul Duncan / Eresse <[email protected]>
# Jordan Ovrè / Ghecko <[email protected]>
import ctypes
import os
import pkg_resources
import platform
import re
import requests
import subprocess
import sys
import tarfile
import tempfile
bitbucket_base_url = 'https://api.bitbucket.org/2.0'
bitbucket_download_url = "https://bitbucket.org/octowire/{}/get/{}.tar.gz"
python_path = sys.executable
# Common Colors
class Colors:
if "Windows" in platform.system() and "WT_SESSION" not in os.environ:
import colorama
from colorama import Fore, Style
colorama.init()
HEADER = Fore.LIGHTMAGENTA_EX
OKBLUE = Fore.LIGHTCYAN_EX
OKGREEN = Fore.LIGHTGREEN_EX
WARNING = Fore.YELLOW
FAIL = Fore.LIGHTRED_EX
ENDC = Fore.RESET
BOLD = Style.BRIGHT
MAGENTA = Fore.LIGHTMAGENTA_EX
UNDERLINE = ""
else:
HEADER = '\x1b[95m'
OKBLUE = '\x1b[94m'
OKGREEN = '\x1b[92m'
WARNING = '\x1b[93m'
FAIL = '\x1b[91m'
ENDC = '\x1b[0m'
BOLD = '\x1b[1m'
MAGENTA = '\x1b[95m'
UNDERLINE = '\x1b[4m'
def is_venv():
return hasattr(sys, 'real_prefix') or sys.base_prefix != sys.prefix
def get_filename_from_cd(cd):
"""
Get filename from content-disposition.
:param cd: Content-Disposition HTTP header.
:return: filename from Content-Disposition or None.
"""
if not cd:
return None
fname = re.findall('filename=(.+)', cd)
if len(fname) == 0:
return None
return fname[0]
def download_release(tarball_url):
"""
Download the latest release of the Octowire framework.
:param tarball_url: release tarball url.
:return: Filename.
"""
print("Downloading the latest octowire-framework release...")
resp = requests.get(tarball_url, stream=True)
if resp.status_code == 200:
tmpdir = tempfile.gettempdir()
filename = tmpdir + f"/{get_filename_from_cd(resp.headers.get('content-disposition'))}"
with open(filename, "wb") as f:
f.write(resp.content)
return filename
else:
print(f"{Colors.FAIL}[X]{Colors.ENDC} Failed to downlaod the latest framework released. - "
"HTTP response code: {resp.status_code}")
exit(-1)
def extract_tarball(filename):
"""
Extract the specified tarball.
:param filename: The tarball file path.
:return: Directory path of the extracted archive.
"""
tar = tarfile.open(filename)
path = re.sub(r'\.tar.gz$', '', filename)
setup_dir = '{}/{}'.format(path, tar.getmembers()[0].name)
tar.extractall(path)
tar.close()
return setup_dir
def _get_latest_framework_version():
"""
Return the latest release version of the Octowire framework.
:return: String
"""
module_release_url = bitbucket_base_url + '/repositories/octowire/octowire-framework/' \
'refs/tags?sort=-name&pagelen=1'
resp = requests.get(module_release_url)
if resp.status_code == 200:
latest_release = resp.json()["values"]
if latest_release:
return latest_release[0]["name"]
else:
print(f'{Colors.FAIL}[X]{Colors.ENDC} No release found for the Octowire framework')
return None
else:
print(f"{Colors.FAIL}[X]{Colors.ENDC} Unable to retrieve latest release URL. Check your network connection "
f"and try again.")
return None
def install_modules():
"""
Run the owfupdate command to download and install released modules.
"""
subprocess.run(['owfupdate', '-m'], stdout=sys.stdout, stderr=sys.stderr)
def install_library():
"""
Install the Octowire library (octowire-lib)
"""
print("[*] Installing the octowire-lib package...")
pipes = subprocess.Popen([python_path, '-m', 'pip', 'install', '--upgrade', "octowire-lib"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = pipes.communicate()
if pipes.returncode != 0:
print("{}[X]{} Error while installing the octowire-lib package: {}\n"
.format(Colors.FAIL, Colors.ENDC, stderr.strip()))
exit(-1)
else:
print("{}[V]{} The octowire-lib was successfully installed.\n"
.format(Colors.OKGREEN, Colors.ENDC))
def manage_install():
"""
Manage the installation of the octowire-framework.
"""
# Check if the framework is already installed
try:
current_version = pkg_resources.parse_version(
pkg_resources.get_distribution('octowire-framework').version)
except pkg_resources.DistributionNotFound:
current_version = ''
# Install the Octowire library package
install_library()
# Framework already installed
if current_version != '':
print("{}[!] The octowire-framework is already installed. Please run 'owfupdate' to update it.{}"
.format(Colors.WARNING, Colors.ENDC))
exit(0)
# Framework not installed on the system
else:
release_tarball_url = f"{bitbucket_download_url.format('octowire-framework', _get_latest_framework_version())}"
tarball_filename = download_release(release_tarball_url)
print(tarball_filename)
if tarball_filename:
print("[*] Extracting the tarball archive...")
setup_dir = extract_tarball(tarball_filename)
package_dir = setup_dir.split("/")[-1]
setup_dir = '/'.join(setup_dir.split("/")[:-1])
print("[*] Installing the octowire-framework package...")
pipes = subprocess.Popen([python_path, '-m', 'pip', 'install', '--upgrade', f"./{package_dir}"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=setup_dir)
stdout, stderr = pipes.communicate()
if pipes.returncode != 0:
print("{}[X]{} Error while installing the octowire-framework package: {}\n"
.format(Colors.FAIL, Colors.ENDC, stderr.strip()))
exit(-1)
else:
print("{}[V]{} The octowire-framework was successfully installed.\n"
.format(Colors.OKGREEN, Colors.ENDC))
if __name__ == '__main__':
# Check root/admin permission
try:
is_admin = os.getuid() == 0
except AttributeError:
is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0
if not is_admin and not is_venv():
print("{}[X]{} Please run the installation script as root or use a virtualenv. Exiting..."
.format(Colors.FAIL, Colors.ENDC))
exit(-1)
# Run the installation of the framework
manage_install()
# Run the update process
install_modules()