Skip to content

Commit 78b06c4

Browse files
committed
feat: script hash verify
1 parent 5ad1d12 commit 78b06c4

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

craft.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22
import os
33
import json
4+
from hashlib import sha256
45
from base64 import b64encode
56

67
def read_file_return_base64ed_content(filename):
@@ -28,7 +29,19 @@ def create_value_map(filepath:str):
2829
vscode_path = ".vscode"
2930

3031
template_head = """#!/bin/zsh
31-
createhack () {
32+
# __ __ __ ______ ____ _
33+
# \ \ / /__ __ _ _ __ ___ _ _\ \ / / ___| / ___|___ __| | ___
34+
# \ \ /\ / / _ \/ _` | '_ \ / _ \| '_ \ \ / /\___ \| | / _ \ / _` |/ _ \\
35+
# \ V V / __/ (_| | |_) | (_) | | | \ V / ___) | |__| (_) | (_| | __/
36+
# \_/\_/ \___|\__,_| .__/ \___/|_| |_|\_/ |____/ \____\___/ \__,_|\___|
37+
# |_|
38+
#
39+
# Author: Esonhugh <[email protected]>
40+
# Path: createhackenv.sh
41+
# Usage: createhackenv <foldername>
42+
# Description: Create a hacking project with vscode based environment
43+
44+
weapon_vscode () {
3245
echo "Creating VSCode Hack Environment"
3346
echo "Usage: $0 <foldername>"
3447
if [ -z "$1" ]
@@ -48,6 +61,12 @@ def create_value_map(filepath:str):
4861
echo "Generate Success" "Launched VSCode"
4962
code $1
5063
}
64+
65+
# Create Alias for weapon_vscode
66+
alias createhackenv=weapon_vscode
67+
alias createhack=weapon_vscode
68+
69+
# Script Hash: __HASH__
5170
"""
5271

5372

@@ -75,10 +94,13 @@ def test():
7594
print(filename_to_variable_name("./vscode/source.zsh"))
7695
print(generate_bash_script())
7796

97+
def content_hash(content):
98+
return sha256(content.encode('utf-8')).hexdigest()
7899

79100
def main():
80101
print("reGenerating bash script")
81102
content = generate_bash_script()
103+
content = content.replace("__HASH__", content_hash(content))
82104
print("Content: ")
83105
print(content)
84106
print("Writing to createhackenv.sh file")

createhackenv.sh

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
#!/bin/zsh
2-
createhack () {
2+
# __ __ __ ______ ____ _
3+
# \ \ / /__ __ _ _ __ ___ _ _\ \ / / ___| / ___|___ __| | ___
4+
# \ \ /\ / / _ \/ _` | '_ \ / _ \| '_ \ \ / /\___ \| | / _ \ / _` |/ _ \
5+
# \ V V / __/ (_| | |_) | (_) | | | \ V / ___) | |__| (_) | (_| | __/
6+
# \_/\_/ \___|\__,_| .__/ \___/|_| |_|\_/ |____/ \____\___/ \__,_|\___|
7+
# |_|
8+
#
9+
# Author: Esonhugh <[email protected]>
10+
# Path: createhackenv.sh
11+
# Usage: createhackenv <foldername>
12+
# Description: Create a hacking project with vscode based environment
13+
14+
weapon_vscode () {
315
echo "Creating VSCode Hack Environment"
416
echo "Usage: $0 <foldername>"
517
if [ -z "$1" ]
@@ -45,3 +57,9 @@ createhack () {
4557
echo "Generate Success" "Launched VSCode"
4658
code $1
4759
}
60+
61+
# Create Alias for weapon_vscode
62+
alias createhackenv=weapon_vscode
63+
alias createhack=weapon_vscode
64+
65+
# Script Hash: 2ac1b5c9ab4545d52df6fa05c64da92a2e8bfe442be8433c80fad25f3a1331aa

verify.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python3
2+
3+
from hashlib import sha256
4+
import sys
5+
6+
def content_hash(content:str):
7+
return sha256(content.encode('utf-8')).hexdigest()
8+
9+
def read_file(filename):
10+
with open(filename, 'r') as f:
11+
data = f.readlines()
12+
return data
13+
14+
def regenerate_hash(filename):
15+
data = read_file("createhackenv.sh")
16+
script_hash_line = data[-1]
17+
# print(f"Script hash line: {script_hash_line}")
18+
raw_script_hash = script_hash_line.split(" ")[3].strip()
19+
# print(f"Script hash in Script: {raw_script_hash}")
20+
content = "".join(data)
21+
content = content.replace(raw_script_hash, "__HASH__")
22+
calc_script_hash = content_hash(content)
23+
# print(f"Script reCalc hash: {calc_script_hash}")
24+
# print(f"Script hash: {raw_script_hash}")
25+
return calc_script_hash, raw_script_hash
26+
27+
def verify_hash(calc_script_hash, raw_script_hash):
28+
print("Verifying script hash...")
29+
print(f"Script reCalc hash: {calc_script_hash}")
30+
print(f"Script hash wait for check: {raw_script_hash}")
31+
if calc_script_hash == raw_script_hash:
32+
print("Script hash is correct")
33+
else:
34+
print("Script hash is incorrect!")
35+
exit(114)
36+
37+
def main():
38+
if len(sys.argv) == 2 :
39+
regen_hash,raw_hash = regenerate_hash(sys.argv[1])
40+
verify_hash(regen_hash, raw_hash)
41+
elif len(sys.argv) == 3:
42+
user_hash = sys.argv[2]
43+
regen_hash,raw_hash = regenerate_hash(sys.argv[1])
44+
verify_hash(regen_hash, user_hash)
45+
else:
46+
print("Usage: python3 verify.py <filename> [<check_hash>]")
47+
exit(1)
48+
49+
if __name__ == "__main__":
50+
main()

0 commit comments

Comments
 (0)