This repository was archived by the owner on Jul 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwc24encrypt.py
106 lines (84 loc) · 3.1 KB
/
wc24encrypt.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
import argparse
import os
import rsa
import struct
from binascii import unhexlify
from Crypto.Cipher import AES
from nlzss import encode_file
def u8(data):
return struct.pack(">B", data)
def u16(data):
return struct.pack(">H", data)
def u32(data):
return struct.pack(">I", data)
parser = argparse.ArgumentParser(description="Sign / Encrypt WiiConnect24 files.")
parser.add_argument("-t", "--type",
type=str, nargs="+",
help="Type of file. Set either enc for encrypted file, or dec for non-encrypted file.")
parser.add_argument("-in", "--input",
type=str, nargs="+",
help="Input file.")
parser.add_argument("-out", "--output",
type=str, nargs="+",
help="Output file.")
parser.add_argument("-c", "--compress",
type=str, nargs="+",
help="If set, this will compress the file before signing with LZ10.")
parser.add_argument("-key", "--aes-key",
type=str, nargs="+",
help="AES key in hex or a path.")
parser.add_argument("-iv", "--iv-key",
type=str, nargs="+",
help="AES IV in hex or a path.")
parser.add_argument("-rsa", "--rsa-key-path",
type=str, nargs="+",
help="RSA private key path. If not specified, it will use the private key in Private.pem if it exists.")
args = parser.parse_args()
if args.compress is not None:
encode_file(in_path=args.input[0], out_path="temp")
filename = "temp"
else:
filename = args.input[0]
with open(filename, "rb") as f:
data = f.read()
if args.rsa_key_path is not None:
rsa_key_path = args.rsa_key_path[0]
else:
rsa_key_path = "Private.pem"
with open(rsa_key_path, "rb") as source_file:
private_key_data = source_file.read()
private_key = rsa.PrivateKey.load_pkcs1(private_key_data, "PEM")
signature = rsa.sign(data, private_key, "SHA-1")
if args.type[0] == "enc":
if args.iv_key is not None:
try:
iv = unhexlify(args.iv_key[0])
except:
iv = open(args.iv_key[0], "rb").read()
else:
iv = os.urandom(16)
try:
key = unhexlify(args.aes_key[0])
except:
key = open(args.aes_key[0], "rb").read()
aes = AES.new(key, AES.MODE_OFB, iv=iv)
processed = aes.encrypt(data)
elif args.type[0] == "dec":
processed = data
content = {"magic": b"WC24" if args.type[0] == "enc" else u32(0)}
content["version"] = u32(1) if args.type[0] == "enc" else u32(0)
content["filler"] = u32(0)
content["crypt_type"] = u8(1) if args.type[0] == "enc" else u8(0)
content["pad"] = u8(0) * 3
content["reserved"] = u8(0) * 32
content["iv"] = iv if args.type[0] == "enc" else u8(0) * 16
content["signature"] = signature
content["data"] = processed
if os.path.exists(args.output[0]):
os.remove(args.output[0])
if args.type[0] == "dec":
os.remove("temp")
for values in content.values():
with open(args.output[0], "ab+") as f:
f.write(values)
print("Completed Successfully")