WIP: Quick and dirty python3 port

This commit is contained in:
Hector Martin
2021-03-17 23:53:31 +09:00
parent a8e5f6c0f7
commit cad274a7fb
41 changed files with 2399 additions and 2387 deletions

View File

@@ -1,11 +1,11 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys
import re
import pywii as wii
hash = wii.SHA.new(open(sys.argv[2]).read()).digest().encode("hex")
f = open(sys.argv[1], "r")
f = open(sys.argv[1], "rb")
data = f.read()
f.close()
data = re.sub('@SHA1SUM@', hash, data)
open(sys.argv[3], "w").write(data)
open(sys.argv[3], "wb").write(data)

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys, os, os.path, struct
import pywii as wii

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys, os, os.path, struct
import pywii as wii
@@ -7,20 +7,24 @@ fstb = wii.WiiFSTBuilder(0x20)
fstb.addfrom(sys.argv[2])
arc = open(sys.argv[1],"wb")
# dummy generate to get length
fstlen = len(fstb.fst.generate())
dataoff = wii.align(0x20+fstlen,0x20)
fst = fstb.fst.generate(dataoff)
try:
arc = open(sys.argv[1],"wb")
# dummy generate to get length
fstlen = len(fstb.fst.generate())
dataoff = wii.align(0x20+fstlen,0x20)
fst = fstb.fst.generate(dataoff)
hdr = struct.pack(">IIII16x",0x55AA382d,0x20,fstlen,dataoff)
arc.write(hdr)
hdr = struct.pack(">IIII16x",0x55AA382d,0x20,fstlen,dataoff)
arc.write(hdr)
arc.write(fst)
wii.falign(arc,0x20)
for f in fstb.files:
data = open(f, "rb").read()
arc.write(data)
arc.write(fst)
wii.falign(arc,0x20)
for f in fstb.files:
data = open(f, "rb").read()
arc.write(data)
wii.falign(arc,0x20)
arc.close()
arc.close()
except:
os.remove(sys.argv[1])
raise

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys, os, os.path
import pywii as wii
@@ -15,12 +15,12 @@ certfile = args.pop(0)
certs, certlist = wii.parse_certs(open(args.pop(0), "rb").read())
print "Certification file %s: " % certfile
print("Certification file %s: " % certfile)
cert = wii.WiiCert(open(certfile, "rb").read())
cert.showinfo(" ")
cert.showsig(certs," ")
print "Certificates:"
print("Certificates:")
for cert in certlist:
cert.showinfo(" - ")
cert.showsig(certs," ")

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys, os, os.path
import pywii as wii
@@ -11,7 +11,7 @@ disc.showinfo()
partitions = disc.read_partitions()
parts = range(len(partitions))
parts = list(range(len(partitions)))
try:
pnum = int(sys.argv[2])

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys, os, os.path
import pywii
@@ -17,7 +17,7 @@ if sys.argv[1] == "-cetk":
elif sys.argv[1] == "-tmd":
signed = pywii.WiiTmd(open(infile, "rb").read())
else:
print "EYOUFAILIT"
print("EYOUFAILIT")
sys.exit(1)
certs, certlist = pywii.parse_certs(open(certfile).read())
@@ -25,11 +25,11 @@ certs, certlist = pywii.parse_certs(open(certfile).read())
signed.update_issuer(issuer)
if not signed.sign(certs):
print "dpki signing failed"
print("dpki signing failed")
sys.exit(1)
open(outfile, "wb").write(signed.data)
print "successfully signed %s" % outfile
print("successfully signed %s" % outfile)
sys.exit(0)

View File

@@ -1,10 +1,10 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys
import pywii as wii
if len(sys.argv) != 3:
print "Usage: %s keyfile.[priv|pub] infile"%sys.argv[0]
print("Usage: %s keyfile.[priv|pub] infile"%sys.argv[0])
sys.exit(1)
if sys.argv[1] == "-":
@@ -12,46 +12,46 @@ if sys.argv[1] == "-":
else:
k = open(sys.argv[1],"rb").read()
if len(k) not in (30,60):
print "Failed to read key"
print("Failed to read key")
sys.exit(2)
if len(k) == 30:
print "Key is a private key, generating public key..."
print("Key is a private key, generating public key...")
q = wii.ec.priv_to_pub(k)
else:
q = k
print "Public key:"
print("Public key:")
pq = q.encode('hex')
print "X =",pq[:30]
print " ",pq[30:60]
print "Y =",pq[60:90]
print " ",pq[90:]
print
print("X =",pq[:30])
print(" ",pq[30:60])
print("Y =",pq[60:90])
print(" ",pq[90:])
print()
indata = open(sys.argv[2],"rb").read()
if len(indata) < 64 or indata[:4] != "SIG0":
print "Invalid header"
print("Invalid header")
sys.exit(3)
r = indata[4:34]
s = indata[34:64]
sha = wii.SHA.new(indata[64:]).digest()
print "SHA1: %s"%sha.encode('hex')
print("SHA1: %s"%sha.encode('hex'))
print
print "Signature:"
print "R =",r[:15].encode('hex')
print " ",r[15:].encode('hex')
print "S =",s[:15].encode('hex')
print " ",s[15:].encode('hex')
print
print()
print("Signature:")
print("R =",r[:15].encode('hex'))
print(" ",r[15:].encode('hex'))
print("S =",s[:15].encode('hex'))
print(" ",s[15:].encode('hex'))
print()
if wii.ec.check_ecdsa(q,r,s,sha):
print "Signature is VALID"
print("Signature is VALID")
else:
print "Signature is INVALID"
print("Signature is INVALID")
sys.exit(4)

View File

@@ -1,33 +1,33 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys, os
import pywii as wii
if len(sys.argv) != 2:
print "Usage: %s keyfile.priv"%sys.argv[0]
print("Usage: %s keyfile.priv"%sys.argv[0])
sys.exit(1)
print "Generating private key..."
print("Generating private key...")
k = wii.ec.gen_priv_key()
print "Private key:"
print("Private key:")
pk = k.encode('hex')
print "K =",pk[:30]
print " ",pk[30:]
print("K =",pk[:30])
print(" ",pk[30:])
print
print "Corresponding public key:"
print()
print("Corresponding public key:")
q = wii.ec.priv_to_pub(k)
pq = q.encode('hex')
print "X =",pq[:30]
print " ",pq[30:60]
print "Y =",pq[60:90]
print " ",pq[90:]
print("X =",pq[:30])
print(" ",pq[30:60])
print("Y =",pq[60:90])
print(" ",pq[90:])
fd = open(sys.argv[1],"wb")
os.fchmod(fd.fileno(), 0o600)
fd.write(k)
fd.close()
print "Saved private key to %s"%sys.argv[1]
print("Saved private key to %s"%sys.argv[1])

View File

@@ -1,10 +1,10 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys
import pywii as wii
if len(sys.argv) not in (2,3):
print "Usage: %s keyfile.priv [keyfile.pub]"%sys.argv[0]
print("Usage: %s keyfile.priv [keyfile.pub]"%sys.argv[0])
sys.exit(1)
if sys.argv[1] == "-":
@@ -12,19 +12,19 @@ if sys.argv[1] == "-":
else:
k = open(sys.argv[1],"rb").read()
if len(k) != 30:
print "Failed to read private key"
print("Failed to read private key")
sys.exit(2)
print "Public key:"
print("Public key:")
q = wii.ec.priv_to_pub(k)
pq = q.encode('hex')
print "X =",pq[:30]
print " ",pq[30:60]
print "Y =",pq[60:90]
print " ",pq[90:]
print("X =",pq[:30])
print(" ",pq[30:60])
print("Y =",pq[60:90])
print(" ",pq[90:])
if len(sys.argv) == 3:
fd = open(sys.argv[2],"wb")
fd.write(q)
fd.close()
print "Saved public key to %s"%sys.argv[2]
print("Saved public key to %s"%sys.argv[2])

View File

@@ -1,10 +1,9 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys
import pywii as wii
if len(sys.argv) != 4:
print "Usage: %s keyfile.priv infile outfile"%sys.argv[0]
print("Usage: %s keyfile.priv infile outfile"%sys.argv[0])
sys.exit(1)
if sys.argv[1] == "-":
@@ -13,20 +12,20 @@ else:
k = open(sys.argv[1],"rb").read()
if len(k) != 30:
print "Failed to read private key"
print("Failed to read private key")
sys.exit(2)
indata = open(sys.argv[2],"rb").read()
sha = wii.SHA.new(indata).digest()
print "SHA1: %s"%sha.encode('hex')
print
print "Signature:"
print("SHA1: %s"%sha.encode('hex'))
print()
print("Signature:")
r,s = wii.ec.generate_ecdsa(k,sha)
print "R =",r[:15].encode('hex')
print " ",r[15:].encode('hex')
print "S =",s[:15].encode('hex')
print " ",s[15:].encode('hex')
print("R =",r[:15].encode('hex'))
print(" ",r[15:].encode('hex'))
print("S =",s[:15].encode('hex'))
print(" ",s[15:].encode('hex'))
outdata = "SIG0" + r + s + indata

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys, os, os.path
import pywii as wii
@@ -11,8 +11,8 @@ def parseint(d):
return int(d)
if len(sys.argv) < 4 or len(sys.argv) > 7:
print "Usage:"
print " python %s <encrypted ISO> <partition number> <file to extract to> [Partition offset] [length]"%sys.argv[0]
print("Usage:")
print(" python %s <encrypted ISO> <partition number> <file to extract to> [Partition offset] [length]"%sys.argv[0])
sys.exit(1)
iso_name, partno, data_name = sys.argv[1:4]
@@ -27,19 +27,19 @@ if len(sys.argv) == 6:
copy_length = parseint(sys.argv[5])
if copy_length is not None and copy_length < 0:
print "Error: negative copy length"
print("Error: negative copy length")
sys.exit(1)
disc = wii.WiiDisc(iso_name)
disc.showinfo()
part = wii.WiiCachedPartition(disc, partno, cachesize=32, debug=False, checkhash=False)
if part_offset >= part.data_bytes:
print "Error: Offset past end of partition"
print("Error: Offset past end of partition")
sys.exit(1)
if copy_length is None:
copy_length = part.data_bytes - part_offset
if copy_length > (part.data_bytes - part_offset):
print "Error: Length too large"
print("Error: Length too large")
sys.exit(1)
dataf = open(data_name, "wb")
@@ -49,7 +49,7 @@ while left > 0:
blocklen = min(left, 4*1024*1024)
d = part.read(offset, blocklen)
if len(d) != blocklen:
print "Part EOF reached!"
print("Part EOF reached!")
sys.exit(1)
dataf.write(d)
offset += blocklen

View File

@@ -1,4 +1,4 @@
#!/usr/bin/python2
#!/usr/bin/python3
import sys, os, os.path
sys.path.append(os.path.realpath(os.path.dirname(sys.argv[0]))+"/../Common")
@@ -7,8 +7,8 @@ import pywii as wii
wii.loadkeys(os.environ["HOME"]+os.sep+".wii")
if len(sys.argv) != 4:
print "Usage:"
print " python %s <encrypted ISO> <partition number> <dol output>"%sys.argv[0]
print("Usage:")
print(" python %s <encrypted ISO> <partition number> <dol output>"%sys.argv[0])
sys.exit(1)
iso_name, partno, dol_name = sys.argv[1:4]

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys, os, os.path
import pywii as wii
@@ -11,8 +11,8 @@ def parseint(d):
return int(d)
if len(sys.argv) < 4 or len(sys.argv) > 7:
print "Usage:"
print " python %s <encrypted ISO> <partition number> <root path to extract to> "%sys.argv[0]
print("Usage:")
print(" python %s <encrypted ISO> <partition number> <root path to extract to> "%sys.argv[0])
sys.exit(1)
iso_name, partno, data_name = sys.argv[1:4]
@@ -27,19 +27,19 @@ if len(sys.argv) == 6:
copy_length = parseint(sys.argv[5])
if copy_length is not None and copy_length < 0:
print "Error: negative copy length"
print("Error: negative copy length")
sys.exit(1)
disc = wii.WiiDisc(iso_name)
disc.showinfo()
part = wii.WiiCachedPartition(disc, partno, cachesize=32, debug=False, checkhash=False)
if part_offset >= part.data_bytes:
print "Error: Offset past end of partition"
print("Error: Offset past end of partition")
sys.exit(1)
if copy_length is None:
copy_length = part.data_bytes - part_offset
if copy_length > (part.data_bytes - part_offset):
print "Error: Length too large"
print("Error: Length too large")
sys.exit(1)
dataf = open(data_name, "wb")
@@ -49,7 +49,7 @@ while left > 0:
blocklen = min(left, 4*1024*1024)
d = part.read(offset, blocklen)
if len(d) != blocklen:
print "Part EOF reached!"
print("Part EOF reached!")
sys.exit(1)
dataf.write(d)
offset += blocklen

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys, os, os.path
import pywii as wii
@@ -6,8 +6,8 @@ import pywii as wii
wii.loadkeys()
if len(sys.argv) != 5:
print "Usage:"
print " python %s <encrypted ISO> <partition number> <apploader text> <apploader trailer>"%sys.argv[0]
print("Usage:")
print(" python %s <encrypted ISO> <partition number> <apploader text> <apploader trailer>"%sys.argv[0])
sys.exit(1)
iso_name, partno, app_name, trail_name = sys.argv[1:5]

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys, os, os.path
import pywii as wii
@@ -11,8 +11,8 @@ def parseint(d):
return int(d)
if len(sys.argv) < 4 or len(sys.argv) > 7:
print "Usage:"
print " python %s <encrypted ISO> <partition number> <file to inject> [Partition offset] [data offset] [length]"%sys.argv[0]
print("Usage:")
print(" python %s <encrypted ISO> <partition number> <file to inject> [Partition offset] [data offset] [length]"%sys.argv[0])
sys.exit(1)
iso_name, partno, data_name = sys.argv[1:4]
@@ -34,10 +34,10 @@ if copy_length == None:
copy_length = data_len - data_offset
copy_end = data_offset + copy_length
if copy_length < 0:
print "Error: negative copy length"
print("Error: negative copy length")
sys.exit(1)
if copy_end > data_len:
print "Error: data file is too small"
print("Error: data file is too small")
sys.exit(1)
disc = wii.WiiDisc(iso_name)
@@ -52,7 +52,7 @@ while left > 0:
blocklen = min(left, 4*1024*1024)
d = dataf.read(blocklen)
if len(d) != blocklen:
print "File EOF reached!"
print("File EOF reached!")
sys.exit(1)
part.write(offset, d)
offset += blocklen

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys, os, os.path
import pywii as wii
@@ -6,8 +6,8 @@ import pywii as wii
wii.loadkeys()
if len(sys.argv) != 4:
print "Usage:"
print " python %s <encrypted ISO> <partition number> <dol to inject>"%sys.argv[0]
print("Usage:")
print(" python %s <encrypted ISO> <partition number> <dol to inject>"%sys.argv[0])
sys.exit(1)
iso_name, partno, dol_name = sys.argv[1:4]

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys, os, os.path
import pywii as wii
@@ -6,9 +6,9 @@ import pywii as wii
wii.loadkeys()
if len(sys.argv) != 4:
print "Usage:"
print " python %s <encrypted ISO> <partition number> <IOS version>"%sys.argv[0]
print " IOS version should be just the minor number (16, 33, etc) in decimal"
print("Usage:")
print(" python %s <encrypted ISO> <partition number> <IOS version>"%sys.argv[0])
print(" IOS version should be just the minor number (16, 33, etc) in decimal")
sys.exit(1)
iso_name, partno, ios = sys.argv[1:4]

View File

@@ -1,10 +1,10 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys, os, os.path
import pywii as wii
def hexdump(s):
return ' '.join(map(lambda x: "%02x"%x,map(ord,s)))
return ' '.join(["%02x"%x for x in list(map(ord,s))])
isofile = sys.argv[1]
disc = WiiDisc(isofile)

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys, os, os.path
import pywii as wii

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys, os, os.path
import pywii as wii
@@ -6,7 +6,7 @@ import pywii as wii
wii.loadkeys()
tikfile = sys.argv[1]
print "fixing Tik file %s " % tikfile
print("fixing Tik file %s " % tikfile)
tik = wii.WiiTik(open(tikfile, "rb").read())
tik.null_signature()
tik.brute_sha()

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys, os, os.path
import pywii as wii
@@ -17,12 +17,12 @@ certs = None
if len(args) > 0:
certs, certlist = wii.parse_certs(open(args.pop(0), "rb").read())
print "ETicket file %s:"%tikfile
print("ETicket file %s:"%tikfile)
tik = wii.WiiTik(open(tikfile, "rb").read())
tik.showinfo(" ")
if certs is not None:
tik.showsig(certs," ")
print "Certificates:"
print("Certificates:")
for cert in certlist:
cert.showinfo(" - ")
cert.showsig(certs," ")

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys, os, os.path
import pywii as wii
@@ -6,7 +6,7 @@ import pywii as wii
wii.loadkeys()
tmdfile = sys.argv[1]
print "TMD file %s:"%tmdfile
print("TMD file %s:"%tmdfile)
tmd = wii.WiiTmd(open(tmdfile, "rb").read())
tmd.null_signature()
tmd.brute_sha()

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys, os, os.path
import pywii as wii
@@ -17,12 +17,12 @@ certs = None
if len(args) > 0:
certs, certlist = wii.parse_certs(open(args.pop(0), "rb").read())
print "TMD file %s:"%tmdfile
print("TMD file %s:"%tmdfile)
tmd = wii.WiiTmd(open(tmdfile, "rb").read())
tmd.showinfo(" ")
if certs is not None:
tmd.showsig(certs," ")
print "Certificates:"
print("Certificates:")
for cert in certlist:
cert.showinfo(" - ")
cert.showsig(certs," ")

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys, os, os.path
import pywii as wii
@@ -14,7 +14,7 @@ args = sys.argv[1:]
tmdfile = args.pop(0)
indir = args.pop(0)
print "updating content records of TMD file %s" % tmdfile
print("updating content records of TMD file %s" % tmdfile)
tmd = wii.WiiTmd(open(tmdfile, "rb").read())
for i, cr in enumerate(tmd.get_content_records()):

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys, os, os.path
import pywii as wii
@@ -14,7 +14,7 @@ if len(args) == 2:
else:
newvers = int(args.pop(0), 16)
print "setting version of TMD file %s to 0x%04x" % (tmdfile, newvers)
print("setting version of TMD file %s to 0x%04x" % (tmdfile, newvers))
tmd = wii.WiiTmd(open(tmdfile, "rb").read())
tmd.title_version = newvers
tmd.update()

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys, os, os.path
import pywii as wii

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys, os, os.path
import pywii as wii

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys, os, os.path
import pywii as wii

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys, os, os.path
import pywii as wii