-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathh4xtools.py
More file actions
executable file
·302 lines (245 loc) · 9.35 KB
/
Copy pathh4xtools.py
File metadata and controls
executable file
·302 lines (245 loc) · 9.35 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
#!/usr/bin/env python3
"""
Copyright (c) 2023-2026. Vili and contributors.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import argparse
import socket
import subprocess
import time
from colorama import Fore, Style
from helper import config, printer, proxymanager
from tools import BaseTool, discover_tools
QUIT_COMMANDS = {"quit", "exit", "q", "kill"}
HELP_COMMANDS = {"?", "help"}
def _get_git_ref() -> str:
"""Returns the SHORT commit hash of the latest commit"""
try:
result = subprocess.run(
["git", "rev-parse", "--short", "HEAD"],
capture_output=True,
text=True,
check=True,
)
return result.stdout.strip()
except (subprocess.CalledProcessError, FileNotFoundError):
# Fallback if git fails (e.g., not a git repo) or git is not installed
printer.verbose(
"Failed to fetch the build hash! Make sure you are running this in the parent folder!"
)
return "26"
def _internet_check() -> None:
"""
Check if the user is connected to the internet by
creating a socket connection to a known host and port.
"""
try:
socket.setdefaulttimeout(3)
socket.create_connection(("gnu.org", 80))
printer.success("Internet Connection is Available..!")
except socket.error as sock_error:
printer.warning(
"Internet Connection is Unavailable or some other problem occurred..!\n{}".format(
sock_error
)
)
# Hash as the "version" string
VERSION = _get_git_ref()
def _print_banner() -> None:
print(
Fore.LIGHTBLACK_EX
+ f"""
▄ .▄▐▄• ▄ ▄▄▄▄▄ ▄▄▌ .▄▄ ·
██▪▐█ █▌█▌▪•██ ▪ ▪ ██• ▐█ ▀.
██▀▐█ ·██· ▐█.▪ ▄█▀▄ ▄█▀▄ ██▪ ▄▀▀▀█▄
██▌▐▀▪▐█·█▌ ▐█▌·▐█▌.▐▌▐█▌.▐▌▐█▌▐▌▐█▄▪▐█
▀▀▀ ·•▀▀ ▀▀ ▀▀▀ ▀█▄▀▪ ▀█▄▀▪.▀▀▀ ▀▀▀▀
{Style.RESET_ALL}Build {VERSION} / Vili (@vil) / https://vili.dev
"""
)
def _display_help(tools: tuple[BaseTool, ...]) -> None:
print(Fore.LIGHTCYAN_EX)
print(
"H4X-Tools build: {} - A modular, terminal-based toolkit for OSINT, reconnaissance, and scraping - built in Python, runs on Linux and Windows.".format(
VERSION
)
)
print("Repository link: https://github.com/vil/h4x-tools")
print("\nMade in Finland, with love.\n")
print("Available Tools:")
print("------------------")
for index, tool in enumerate(tools, start=1):
aliases = ", ".join(tool.aliases)
print(f"* {str(index).zfill(2)}. {tool.name} ({aliases}) - {tool.description}")
print("\nAdding Tools:")
print("------------------")
print(
"Create a Python file in tools/, subclass tools.base.BaseTool, set id/name/description/order/aliases/arguments, "
"and implement run(). H4X-Tools discovers it automatically at startup."
)
print("\nClosing the Toolkit:")
print("----------------------")
print("You can close the toolkit using the following commands:")
for command in sorted(QUIT_COMMANDS):
print(f"* {command}")
print("\nLicense and Credits:")
print("---------------------")
print(
"H4X-Tools is under the GNU General Public License v3, made by Vili (@vil).\n"
"This toolkit is for educational and authorised security research purposes only."
)
def _print_menu(tools: tuple[BaseTool, ...]) -> None:
max_option_length = max(len(tool.name) for tool in tools)
for index, tool in enumerate(tools, start=1):
key = str(index)
print(
f"{Fore.LIGHTGREEN_EX}[{key.zfill(2)}]{Style.RESET_ALL} {tool.name.ljust(max_option_length)}",
end="",
)
if index % 2 == 0:
print()
else:
print(" " * 4, end="")
print("\n")
if proxymanager.is_enabled():
proxy_list = proxymanager.list_proxies()
count = len(proxy_list)
mode = "rotating" if proxymanager.is_rotating() else "fixed"
if count:
print(
f"{Fore.LIGHTGREEN_EX}[P]{Style.RESET_ALL} "
f"Proxy routing active / {count} {'proxy' if count == 1 else 'proxies'} ({mode})"
)
else:
print(
f"{Fore.LIGHTYELLOW_EX}[P]{Style.RESET_ALL} "
"Proxy routing enabled but no proxies configured."
)
print(f"Type {Style.BRIGHT}?{Style.RESET_ALL} for help.")
print(f"Type {Style.BRIGHT}exit{Style.RESET_ALL} to close the toolkit...")
def _build_parser(tools: tuple[BaseTool, ...]) -> argparse.ArgumentParser:
"""
Build the H4X-Tools command-line parser from discovered tool metadata.
:return: Configured argument parser.
"""
parser = argparse.ArgumentParser(
prog="h4xtools",
description="H4X-Tools - modular OSINT, reconnaissance, and scraping toolkit.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("--version", action="version", version=f"H4X-Tools v{VERSION}")
parser.add_argument(
"-v",
"--verbose",
action="count",
default=0,
help="Enable verbose output. Repeat for more verbosity.",
)
parser.add_argument(
"--debug",
action="store_true",
help="Enable debug output. Implies verbose output.",
)
parser.add_argument(
"--list-tools",
action="store_true",
help="Print the full tool help and exit.",
)
parser.add_argument(
"--no-internet-check",
action="store_true",
help="Skip the startup internet connectivity check.",
)
tool_group = parser.add_argument_group(
"Tool shortcuts",
"Pass one or more tool flags to run them directly without opening the menu. "
"Flags with optional values will prompt if the value is omitted.",
)
for tool in tools:
tool.add_cli_arguments(tool_group)
return parser
def _cli_tool_selected(args: argparse.Namespace, tools: tuple[BaseTool, ...]) -> bool:
"""
Determine whether any direct-run tool flag was provided.
:param args: Parsed CLI args.
:param tools: Discovered tools.
:return: ``True`` if at least one tool should run directly.
"""
return any(tool.selected(args) for tool in tools)
def _run_cli_tools(args: argparse.Namespace, tools: tuple[BaseTool, ...]) -> None:
"""
Execute tool flags directly and exit without opening the menu.
Tools run in menu order when multiple flags are provided.
:param args: Parsed CLI args.
:param tools: Discovered tools.
"""
for tool in tools:
if not tool.selected(args):
continue
try:
printer.verbose(f"Running {tool.name}")
tool.run_from_cli(args)
except KeyboardInterrupt:
printer.warning("Cancelled..!")
break
def main(args: argparse.Namespace | None = None) -> None:
tools = discover_tools()
if args is None:
args = _build_parser(tools).parse_args()
printer.set_verbosity(verbose=args.verbose > 0, debug_enabled=args.debug)
config.init_config()
if args.list_tools:
_display_help(tools)
return
if not args.no_internet_check:
_internet_check()
time.sleep(0.5)
printer.debug("DEBUG IS ON.")
if _cli_tool_selected(args, tools):
printer.set_pause_after_tool(False)
_run_cli_tools(args, tools)
return
printer.set_pause_after_tool(True)
menu_options = {str(index): tool for index, tool in enumerate(tools, start=1)}
while True:
_print_banner()
_print_menu(tools)
user_input = printer.user_input("Tool to execute : \t")
normalized_input = user_input.lower()
if normalized_input in QUIT_COMMANDS:
printer.warning("Quitting... Goodbye!")
print(Style.RESET_ALL)
time.sleep(0.5)
break
if user_input in menu_options:
try:
menu_options[user_input].run()
except KeyboardInterrupt:
printer.warning("Cancelled..!")
elif normalized_input in HELP_COMMANDS:
_display_help(tools)
printer.user_input("Done reading? Press the Enter key.")
else:
printer.error("Invalid option!")
time.sleep(0.5)
if __name__ == "__main__":
while True:
try:
main()
break
except ValueError:
printer.error("Invalid value inputted..!")
except KeyboardInterrupt:
print("\n")
printer.warning("Quitting... Goodbye!")
print(Style.RESET_ALL)
exit(0)