-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashcat_bot.py
183 lines (160 loc) · 7.62 KB
/
hashcat_bot.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
#Data 11/6/23
import re
import asyncio
import logging
import discord
import time
import datetime
from discord.ext import commands
# Logging Setup
logging.basicConfig(filename="app.log", level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
# Constants
INTENTS = discord.Intents.default()
INTENTS.typing = False
INTENTS.presences = False
INTENTS.messages = True
INTENTS.message_content = True
RULE_FILE = "rules/OneRuleToRuleThemStill.rule"
SUPPORTED_ALGORITHMS = {
"md5": 0, "sha1": 100, "ntlm": 1000, "netntlmv2": 5600, "mssql2000": 131, "mssql2005": 132, "asrep23": 18200, "asrep18": 19700, "bcrypt": 3200
}
HASH_PATTERNS = {
"md5": re.compile("^[a-fA-F0-9]{32}$"),
"sha1": re.compile("^[a-fA-F0-9]{40}$"),
"ntlm": re.compile("^[a-fA-F0-9]{32}$"),
"netntlmv2": re.compile("^[a-zA-Z0-9\-_$]+::[a-zA-Z0-9\-_$]*:[a-fA-F0-9]{16}:[a-fA-F0-9]+:[a-fA-F0-9]+$"),
"mssql2000": re.compile("^[a-fA-F0-9]{54}$"),
"mssql2005": re.compile("^[a-fA-F0-9]{40}$"),
"asrep23": re.compile(r"^\$krb5asrep\$[0-9]+\$[a-zA-Z0-9\-_.]+@[a-zA-Z0-9\-_.]+:.*\$[a-fA-F0-9]+$"),
"asrep18": re.compile(r"^\$krb5tgs\$18\$[a-zA-Z0-9\-./]+\$[A-Z]+\$\*[a-zA-Z0-9\-./]+\*\$[a-fA-F0-9]+\$[a-fA-F0-9]+$"),
"bcrypt": re.compile("^\$2[ayb]\$[0-9]{2}\$[a-zA-Z0-9./]{53}$")
}
bot = commands.Bot(command_prefix="!", intents=INTENTS)
bot.remove_command("help")
class HashcatManager:
def __init__(self):
self.is_processing_job = False
self.current_process = None
self.current_job_status = {"algorithm": None, "start_time": None, "progress": "0%", "timeout": None}
self.current_mode = None
def read_log_file(self, filename):
with open(filename, 'r') as file:
return file.read()
def find_password_for_hash(self, target_hash, filename="hashcat.potfile"):
target_hash = target_hash.lower()
with open(filename, 'r', encoding='utf-8', errors='ignore') as f:
for line in f:
parts = line.strip().rsplit(":", 1)
if len(parts) != 2:
continue
hash_from_file, password = parts
if hash_from_file.lower() == target_hash:
return password
return None
async def run_hashcat(self, command, timeout, job_type="rockyou"):
log_content = ""
timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
log_filename = f"hashcat_output_{timestamp}.log"
command.extend(["--status-timer=1"])
try:
with open(log_filename, 'a') as log_file:
self.current_process = await asyncio.create_subprocess_exec(*command, stdout=log_file, stderr=log_file)
self.current_job_status["start_time"] = time.time()
self.current_job_status["algorithm"] = command[2]
self.current_job_status["timeout"] = timeout
await asyncio.wait_for(self.current_process.wait(), timeout=timeout)
log_content = self.read_log_file(log_filename)
if "Exhausted" in log_content:
return log_content, "Exhausted"
elif self.current_process.returncode == 0:
self.current_job_status["progress"] = "100%"
return log_content, "Success"
else:
return log_content, "Error"
except asyncio.TimeoutError:
if self.current_process:
self.current_process.terminate()
self.is_processing_job = False
self.current_mode = None
return f"Hashcat operation timed out. Partial output in {log_filename}", "Timeout"
except Exception as e:
logging.error(f"Error running hashcat: {e}")
return str(e), "Error"
finally:
self.current_process = None
if "Success" in log_content or job_type != "brute_force":
self.is_processing_job = False
self.current_mode = None
manager = HashcatManager()
@bot.command(name="hashcat")
async def _hashcat(ctx, algorithm: str = None, hash_value: str = None):
if not algorithm or not hash_value:
help_text = ("Usage: !hashcat [algorithm] [hash_value]\n"
f"Supported algorithms: {', '.join(SUPPORTED_ALGORITHMS.keys())}\n"
"Example: !hashcat md5 5d41402abc4b2a76b9719d911017c592\n\n"
"To check the status of a running job, use: !hashcat_status\n"
"To stop a currently running job, use: !hashcat_stop")
await ctx.send(help_text)
return
if manager.is_processing_job:
await ctx.send("Hashcat is currently processing a job. Please wait.")
return
if algorithm not in SUPPORTED_ALGORITHMS:
await ctx.send("Unsupported hash algorithm.")
return
if not HASH_PATTERNS[algorithm].match(hash_value):
await ctx.send("Invalid hash format.")
return
wordlist = "rockyou.txt"
command = ["hashcat", "-m", str(SUPPORTED_ALGORITHMS[algorithm]), hash_value, wordlist, "-r", RULE_FILE, "--loopback"]
manager.is_processing_job = True
await ctx.send("Hashcat operation started using a wordlist with rule file, this may take up to an hour.")
stdout, status = await manager.run_hashcat(command, 3600, "rockyou")
if status == "Exhausted" or status == "Timeout":
manager.is_processing_job = False
manager.current_mode = None
password = manager.find_password_for_hash(hash_value)
if not password:
await ctx.send("Did not find the password using rockyou.txt with rule file. Switching to brute force mode for an additional hour.")
manager.current_mode = "brute_force"
manager.is_processing_job = True
command = ["hashcat", "-m", str(SUPPORTED_ALGORITHMS[algorithm]), hash_value, "-a", "3"]
stdout, status = await manager.run_hashcat(command, 3600, "brute_force")
if status == "Success":
password = manager.find_password_for_hash(hash_value)
if password:
await ctx.send(f"Found password: {password}")
else:
await ctx.send("Password not found in hashcat.potfile, but hashcat reported success. Please check manually.")
elif status == "Timeout" or status == "Error":
manager.is_processing_job = False
manager.current_mode = None
await ctx.send(f"Hashcat operation did not finish successfully. Status: {status}")
@bot.command(name="hashcat_stop")
async def hashcat_stop(ctx):
if not manager.is_processing_job:
await ctx.send("No Hashcat operation is currently running.")
return
if manager.current_process:
manager.current_process.terminate()
manager.is_processing_job = False
manager.current_mode = None
await ctx.send("Hashcat operation stopped.")
@bot.command(name="hashcat_status")
async def hashcat_status(ctx):
if not manager.is_processing_job:
await ctx.send("No Hashcat operation is currently running.")
return
elapsed_time = int(time.time() - manager.current_job_status["start_time"])
total_time = manager.current_job_status["timeout"]
progress_percentage = min(100, (elapsed_time / total_time) * 100)
elapsed_time_string = str(datetime.timedelta(seconds=elapsed_time))
status_message = (f"Hashcat is cracking with algorithm {manager.current_job_status['algorithm']}.\n"
f"Elapsed Time: {elapsed_time_string}\n"
f"Progress: {progress_percentage:.2f}%")
await ctx.send(status_message)
@bot.event
async def on_ready():
logging.info(f"We have logged in as {bot.user}")
if __name__ == "__main__":
bot.run("")