-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
executable file
·356 lines (298 loc) · 11.3 KB
/
Copy pathsetup.py
File metadata and controls
executable file
·356 lines (298 loc) · 11.3 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
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
#!/usr/bin/env python3
"""
AttackSim Setup — Python-based dependency installation and verification
This script handles cross-platform dependency installation for:
- Ubuntu/Debian (apt)
- Fedora/RedHat/CentOS (dnf/yum)
- Alpine (apk)
- Arch (pacman)
- openSUSE (zypper)
- macOS (homebrew)
Usage:
python3 setup.py # Interactive setup
python3 setup.py --auto # Auto-detect distro
python3 setup.py --verify-only # Check existing installation
"""
import os
import sys
import subprocess
import platform
import shutil
from pathlib import Path
from typing import List, Tuple, Optional
# Colors for terminal output
class Colors:
RED = '\033[0;31m'
GREEN = '\033[0;32m'
YELLOW = '\033[1;33m'
BLUE = '\033[0;34m'
CYAN = '\033[0;36m'
NC = '\033[0m' # No Color
def log_info(msg: str):
"""Log information message"""
print(f"{Colors.BLUE}[INFO]{Colors.NC} {msg}")
def log_success(msg: str):
"""Log success message"""
print(f"{Colors.GREEN}[✓]{Colors.NC} {msg}")
def log_warn(msg: str):
"""Log warning message"""
print(f"{Colors.YELLOW}[!]{Colors.NC} {msg}")
def log_error(msg: str):
"""Log error message and exit"""
print(f"{Colors.RED}[✗]{Colors.NC} {msg}", file=sys.stderr)
sys.exit(1)
class PlatformDetector:
"""Detect OS and package manager"""
def __init__(self):
self.system = platform.system()
self.distro = self.detect_distro()
self.pkg_manager = self.detect_package_manager()
def detect_distro(self) -> str:
"""Detect Linux distribution"""
if self.system == "Darwin":
return "macos"
if self.system != "Linux":
return "unknown"
# Try /etc/os-release first (modern systems)
os_release = Path("/etc/os-release")
if os_release.exists():
for line in os_release.read_text().split('\n'):
if line.startswith('ID='):
return line.split('=')[1].strip('"').lower()
# Fallback to /etc/issue
issue = Path("/etc/issue")
if issue.exists():
first_line = issue.read_text().split('\n')[0].lower()
for name in ['ubuntu', 'debian', 'fedora', 'rhel', 'centos', 'alpine', 'arch', 'suse']:
if name in first_line:
return name
return "unknown"
def detect_package_manager(self) -> Tuple[str, List[str]]:
"""Detect package manager and return (name, install_command)"""
managers = {
'apt': ['apt', 'install', '-y'],
'dnf': ['dnf', 'install', '-y'],
'yum': ['yum', 'install', '-y'],
'apk': ['apk', 'add', '--no-cache'],
'pacman': ['pacman', '-S', '--noconfirm'],
'zypper': ['zypper', 'install', '-y'],
'brew': ['brew', 'install'],
}
for mgr, cmd in managers.items():
if shutil.which(mgr):
return mgr, cmd
return None, None
class DependencyInstaller:
"""Install system and Python dependencies"""
PYTHON_PACKAGES = [
'bcrypt>=4.0',
'passlib>=1.7',
'psutil',
'pyyaml',
'dnspython',
'requests',
]
SYSTEM_PACKAGES = {
'apt': {
'python': ['python3', 'python3-pip', 'python3-venv', 'python3-dev'],
'tools': ['git', 'curl', 'wget', 'nmap', 'dnsutils', 'net-tools'],
},
'dnf': {
'python': ['python3', 'python3-pip', 'python3-devel'],
'tools': ['git', 'curl', 'wget', 'nmap', 'bind-utils', 'net-tools'],
},
'yum': {
'python': ['python3', 'python3-pip', 'python3-devel'],
'tools': ['git', 'curl', 'wget', 'nmap', 'bind-utils', 'net-tools'],
},
'apk': {
'python': ['python3', 'py3-pip', 'py3-virtualenv'],
'tools': ['git', 'curl', 'wget', 'nmap', 'bind-tools', 'iputils'],
},
'pacman': {
'python': ['python', 'python-pip'],
'tools': ['git', 'curl', 'wget', 'nmap', 'bind-tools', 'net-tools'],
},
'zypper': {
'python': ['python3', 'python3-pip', 'python3-devel'],
'tools': ['git', 'curl', 'wget', 'nmap', 'bind-tools', 'net-tools'],
},
'brew': {
'python': ['python3'],
'tools': ['git', 'curl', 'wget', 'nmap'],
},
}
def __init__(self, detector: PlatformDetector, dev_mode: bool = False):
self.detector = detector
self.dev_mode = dev_mode
def install_system_deps(self):
"""Install system-level dependencies"""
if self.detector.pkg_manager is None:
log_warn("Could not detect package manager - skipping system dependencies")
return
mgr_name, cmd = self.detector.pkg_manager
packages_dict = self.SYSTEM_PACKAGES.get(mgr_name, {})
if not packages_dict:
log_warn(f"No system packages defined for {mgr_name}")
return
log_info(f"Installing system dependencies via {mgr_name}...")
# Install Python
python_pkgs = packages_dict.get('python', [])
if python_pkgs:
log_info(f"Installing Python packages: {', '.join(python_pkgs)}")
self._run_install(cmd, python_pkgs)
# Install tools
tools_pkgs = packages_dict.get('tools', [])
if tools_pkgs:
log_info(f"Installing system tools: {', '.join(tools_pkgs)}")
self._run_install(cmd, tools_pkgs, ignore_errors=True)
def install_python_deps(self):
"""Install Python packages via pip"""
log_info("Installing Python packages...")
# Ensure pip is up to date
try:
subprocess.run(
[sys.executable, '-m', 'pip', 'install', '--upgrade', 'pip'],
capture_output=True,
check=False
)
except Exception as e:
log_warn(f"Failed to upgrade pip: {e}")
# Install packages
for package in self.PYTHON_PACKAGES:
try:
subprocess.run(
[sys.executable, '-m', 'pip', 'install', '--quiet', package],
check=True,
capture_output=True
)
log_success(f"Installed {package}")
except subprocess.CalledProcessError:
log_warn(f"Failed to install {package} (optional)")
# Install dev dependencies if requested
if self.dev_mode:
dev_packages = ['pytest>=7.0', 'ruff>=0.4', 'mypy>=1.8']
for package in dev_packages:
try:
subprocess.run(
[sys.executable, '-m', 'pip', 'install', '--quiet', package],
check=True,
capture_output=True
)
log_success(f"Installed {package}")
except subprocess.CalledProcessError:
log_warn(f"Failed to install {package}")
def _run_install(self, cmd: List[str], packages: List[str], ignore_errors: bool = False):
"""Run package manager install command"""
try:
full_cmd = cmd + packages
# Use sudo if not already root
if os.getuid() != 0 and self.detector.system == "Linux":
full_cmd = ['sudo'] + full_cmd
subprocess.run(full_cmd, check=True)
log_success(f"Installed: {', '.join(packages)}")
except subprocess.CalledProcessError as e:
if not ignore_errors:
log_error(f"Failed to install packages: {e}")
else:
log_warn(f"Some packages failed to install (optional)")
except PermissionError:
log_error("Permission denied - try running with sudo")
class DependencyVerifier:
"""Verify installed dependencies"""
@staticmethod
def check_python_version() -> bool:
"""Check Python version (3.9+)"""
log_info(f"Python version: {sys.version}")
if sys.version_info < (3, 9):
log_error(f"Python 3.9+ required (found {sys.version_info.major}.{sys.version_info.minor})")
log_success("Python version OK")
return True
@staticmethod
def check_python_module(module: str) -> bool:
"""Check if Python module is installed"""
try:
__import__(module)
return True
except ImportError:
return False
@staticmethod
def verify_all() -> bool:
"""Verify all dependencies"""
log_info("Verifying dependencies...")
# Check Python version
DependencyVerifier.check_python_version()
# Check Python modules
modules = ['bcrypt', 'passlib', 'psutil', 'yaml', 'dns', 'requests']
missing = []
for module in modules:
if DependencyVerifier.check_python_module(module):
log_success(f"Module '{module}' found")
else:
log_warn(f"Module '{module}' not found")
missing.append(module)
# Check system tools
tools = ['git', 'curl', 'python3']
for tool in tools:
if shutil.which(tool):
log_success(f"Tool '{tool}' found")
else:
log_warn(f"Tool '{tool}' not found")
if missing:
log_warn(f"Missing optional modules: {', '.join(missing)}")
return False
log_success("All dependencies verified")
return True
def main():
"""Main setup function"""
print("")
print(f"{Colors.BLUE}{'='*50}{Colors.NC}")
print(f"{Colors.CYAN} AttackSim Setup & Dependency Installer{Colors.NC}")
print(f"{Colors.BLUE}{'='*50}{Colors.NC}")
print("")
# Parse arguments
auto_mode = '--auto' in sys.argv
verify_only = '--verify-only' in sys.argv
dev_mode = '--dev' in sys.argv
# Detect platform
detector = PlatformDetector()
log_info(f"Detected OS: {detector.system}")
log_info(f"Detected distro: {detector.distro}")
if detector.pkg_manager:
log_info(f"Package manager: {detector.pkg_manager[0]}")
else:
log_warn("Could not detect package manager")
print("")
# Only verify if requested
if verify_only:
success = DependencyVerifier.verify_all()
sys.exit(0 if success else 1)
# Interactive mode
if not auto_mode and len(sys.argv) == 1:
print("Installation options:")
print(" 1. Install all dependencies")
print(" 2. Verify existing installation")
print(" 3. Exit")
choice = input("\nSelect option [1]: ").strip() or "1"
if choice == "2":
DependencyVerifier.verify_all()
return
elif choice == "3":
return
elif choice != "1":
log_error("Invalid choice")
# Install dependencies
print("")
installer = DependencyInstaller(detector, dev_mode=dev_mode)
if detector.system == "Linux":
installer.install_system_deps()
elif detector.system == "Darwin":
log_info("macOS detected - using Homebrew")
log_info("Please ensure Homebrew is installed: https://brew.sh")
print("")
installer.install_python_deps()
print("")
print("Installation complete!")
print(f"\nYou can now run: {Colors.CYAN}./run_all.sh https://target.com{Colors.NC}")
if __name__ == '__main__':
main()