Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hashcat
GitHub Repository: hashcat/hashcat
Path: blob/master/Python/generic_hash_sp.py
6628 views
1
#!/bin/bash python3
2
3
import sys
4
import struct
5
import hashlib
6
import hcshared
7
import hcsp
8
9
ST_HASH = "33522b0fd9812aa68586f66dba7c17a8ce64344137f9c7d8b11f32a6921c22de*9348746780603343"
10
ST_PASS = "hashcat"
11
12
# In theory, you only have to implement this function...
13
def calc_hash(password: bytes, salt: dict) -> str:
14
salt_buf = hcshared.get_salt_buf(salt)
15
hash = hashlib.sha256(salt_buf + password) # the salt is prepended to the password as is (not as hex-bytes but as ASCII)
16
for i in range(10000):
17
hash = hashlib.sha256(hash.digest())
18
return hash.hexdigest()
19
20
# ...except when using an esalt. The esalt void* structure is both dynamic and specific to a hash mode.
21
# If you use an esalt, you must convert its contents into Python datatypes.
22
# If you don't use esalt, just return []
23
# For this example hash-mode, we kept it very general and pushed all salt data in a generic format of generic sizes
24
# As such, it has to go into esalt
25
def extract_esalts(esalts_buf):
26
esalts=[]
27
for hash_buf, hash_len, salt_buf, salt_len in struct.iter_unpack("1024s I 1024s I", esalts_buf):
28
hash_buf = hash_buf[0:hash_len]
29
salt_buf = salt_buf[0:salt_len]
30
esalts.append({ "hash_buf": hash_buf, "salt_buf": salt_buf })
31
return esalts
32
33
# From here you really can leave things as they are
34
# The init function is good for converting the hashcat data type because it is only called once
35
def kernel_loop(ctx,passwords,salt_id,is_selftest):
36
return hcsp.handle_queue(ctx,passwords,salt_id,is_selftest)
37
38
def init(ctx):
39
# Uncomment this line below to dump the hashcat ctx for your salted hash
40
# hcshared.dump_hashcat_ctx(ctx, source=__name__) #enable this to dump the ctx from hashcat
41
hcsp.init(ctx,extract_esalts)
42
43
def term(ctx):
44
hcsp.term(ctx)
45
46
47
if __name__ == '__main__':
48
# Main is only run when debugging this python script and never when -m 72000 is called directly from hashcat cli
49
50
hcshared.add_hashcat_path_to_environment()
51
# Load hashcat ctx from a file dumped when running -m 73000 . Optional argument is a Path() object to ctx file.
52
ctx = hcshared.load_ctx(ST_HASH)
53
54
init(ctx)
55
hashcat_passwords = 256
56
passwords = []
57
for line in sys.stdin:
58
passwords.append(bytes(line.rstrip(), 'utf-8'))
59
if len(passwords) == hashcat_passwords:
60
hashes = kernel_loop(ctx,passwords,0,False)
61
passwords.clear()
62
hashes = kernel_loop(ctx,passwords,0,False) # remaining entries
63
if hashes:
64
print(hashes[-1])
65
term(ctx)
66
67