Hacking Discussion Info on SHA-256 hashes on FS patches

mrdude

Developer
Developer
Joined
Dec 11, 2015
Messages
3,071
Trophies
1
Age
56
XP
8,227
I looped on all files inside the folder until i get the nca with title id 0100000000000033.
remaining would be finding the addresses.

Code:
import os
import sys
import subprocess

if len(sys.argv) == 1:
   print("no argv")
   sys.exit(1)

ES_NCA = ""
FIRMWARE_DIR = sys.argv[1]

print("Checking files in " + FIRMWARE_DIR + " folder.")
for filename in os.listdir(FIRMWARE_DIR):
   if filename.endswith(".nca"):
 
       outlines = subprocess.check_output(['hactool', '--disablekeywarns', FIRMWARE_DIR + '/' + filename])

       for line in outlines.splitlines():
           line = line.decode('ascii').replace(" ","")
           if line.startswith("TitleID:0100000000000033") and not filename.endswith(".cnmt.nca"):
               print("Found! Filename : " + filename)
               ES_NCA = filename
               break
       if ES_NCA:
           print("Using hactool to extract exefsdir")
           subprocess.run(["hactool","-t nca","--exefsdir=.","--disablekeywarns", FIRMWARE_DIR + '/' + filename], stdout=subprocess.DEVNULL)
           if os.path.exists("main"):
               outlines = subprocess.check_output(['hactool','--keyset=prod.keys','--intype=nso','--disablekeywarns','--uncompressed=main_dec','main'])
               print("Using hactool to uncompress main")
               for line in outlines.splitlines():
           
                   line = line.decode('ascii').replace(" ","")
                   if line.startswith("BuildId:"):
                       print("Found Build ID : " + line.replace("BuildId:","")[0:40])
           break
I can't get this script to work, for some reason it's not finding my keys "keys.dat" file, I edited your script and replaced prod.keys with keys.dat, but it was still saying: Invalid NCA header! Are keys correct?
 

crckd

Member
OP
Newcomer
Joined
Dec 3, 2020
Messages
14
Trophies
0
Age
43
XP
291
Country
Philippines
I can't get this script to work, for some reason it's not finding my keys "keys.dat" file, I edited your script and replaced prod.keys with keys.dat, but it was still saying: Invalid NCA header! Are keys correct?

I had the same issue.
just change "-k keys.txt" to "--keyset=keys.txt". there should be no spaces on the argument.

049pEVT.png
 
Last edited by crckd,
  • Like
Reactions: LyuboA

mrdude

Developer
Developer
Joined
Dec 11, 2015
Messages
3,071
Trophies
1
Age
56
XP
8,227
I had the same issue.
just change "-k keys.txt" to "--keyset=keys.txt". there should be no spaces on the argument.

049pEVT.png
I'm still getting an error - I have hactool and the keys in the same folder as I am using the py script in, and it's not finding the keys, which is weird as it obviously finds and runs hactool. It's a mystery.
 

crckd

Member
OP
Newcomer
Joined
Dec 3, 2020
Messages
14
Trophies
0
Age
43
XP
291
Country
Philippines
I'm still getting an error - I have hactool and the keys in the same folder as I am using the py script in, and it's not finding the keys, which is weird as it obviously finds and runs hactool. It's a mystery.

At which part do you get the nca header error?
 

mrdude

Developer
Developer
Joined
Dec 11, 2015
Messages
3,071
Trophies
1
Age
56
XP
8,227
At which part do you get the nca header error?

This is what I m doing - see pictures for folder content, command line, and output + modded py script.

NNO9MVr.png

1d5Tv22.png


Ignore the double quotes round the keyset line - it's the same error using single quotes.
 
Last edited by mrdude,

crckd

Member
OP
Newcomer
Joined
Dec 3, 2020
Messages
14
Trophies
0
Age
43
XP
291
Country
Philippines
This is what I m doing - see pictures for folder content, command line, and output + modded py script.
try to replace line 25 with this and use single quotes
subprocess.run(['hactool','--intype=nca','--exefsdir=.','--disablekeywarns', FIRMWARE_DIR + '/' + filename], stdout=subprocess.DEVNULL)
 
Last edited by crckd,

mrdude

Developer
Developer
Joined
Dec 11, 2015
Messages
3,071
Trophies
1
Age
56
XP
8,227
try to replace line 25 with this
subprocess.run(['hactool','--intype=nca','--exefsdir=.','--disablekeywarns', FIRMWARE_DIR + '/' + filename], stdout=subprocess.DEVNULL)

Same error. (reason it's only showing 1 error - is because I just have the one file you posted above in the firmware folder, to speed things up).
 
Last edited by mrdude,

mrdude

Developer
Developer
Joined
Dec 11, 2015
Messages
3,071
Trophies
1
Age
56
XP
8,227
how about at line 16? did you add
your keys there?

i have my prod.keys at %USERPROFILE%\.switch so i don't need to add it as an argument on hactool.
i forgot to add it also on some lines of the script

Yep I tried that before, I think it was line 25 which was causing the issues - and the keyfile being missing from the original script. Here the modded script which is now working. (use keys.dat).

7HZI7KE.png
 

Attachments

  • es.zip
    702 bytes · Views: 161
Last edited by mrdude,

mrdude

Developer
Developer
Joined
Dec 11, 2015
Messages
3,071
Trophies
1
Age
56
XP
8,227
Slightly modded the script to add another argument as people are using different named keyfiles.

Usage: python es.py firmware prod.keys

Firmware: is the name of the folder the firmware files are in.
prod.keys can be changed to whatever key file you are using - example: python es.py 11.0.0 keys.txt

Batch file code to dump + cleanup
Code:
rem SET VAR="C:\python3.9\python"
cls
mkdir dumped
%VAR% es.py firmware keys.dat
del main.* /Q
move main_dec dumped
)

@crckd
Your script doesn't seem find the files on firmware Firmware 10.1.0 or Firmware 10.0.4, firmware newer is ok though (10.2.0 & 11.x.x) Maybe this will affect newer firmware's in future. Do you know what's different in these older firmware's and why the script isn't finding the anything in those?

EDIT: OK, found it - change line to this: (this then finds the build id in nca files as well)
Code:
if line.startswith("TitleID:0100000000000033") and not filename.endswith("*.nca"):

Although this is what happens on 10.0.4:
Checking files in firmware folder.
Found! Filename : b5757088d76f42f7222f141d3082d02d.nca
Using hactool to extract exefsdir

There's no extraction of files.......even though it has the correct title id:
C6R6pc8.png


So removing that file, and running the script again - it finds this also has the same id
Code:
e006abf2423022702fa4839be9825ebc.nca

and this is the output: (which seems correct)
Found! Filename : e006abf2423022702fa4839be9825ebc.nca
Using hactool to extract exefsdir
Using hactool to uncompress main
Found Build ID : 03E4EB5556B98B327D1353E8AA2C7ADF2C544470
Running this:
Code:
hactool --keyset=keys.dat --intype=nca --exefsdir=dumped --disablekeywarns firmware/e006abf2423022702fa4839be9825ebc.nca
Dumps main + main.npdm - so maybe we need to put a check as well to check for:
"ContentType: Program" because the other file's content type is "meta" (no space before program - but forum is turning it into a smiley)
 

Attachments

  • es.zip
    733 bytes · Views: 167
Last edited by mrdude,

mrdude

Developer
Developer
Joined
Dec 11, 2015
Messages
3,071
Trophies
1
Age
56
XP
8,227
I'm looking at the 3rd patch of 11.0.0. It would be much simpler if we can patch previous versions like the 11.0.0

mw0lxnI.png
We should be able to find the hex patterns if we use some wildcards, it shouldn't be a problem to do that - I just checked the first patch on each firmware, can can't see an issue doing it that way.
 

crckd

Member
OP
Newcomer
Joined
Dec 3, 2020
Messages
14
Trophies
0
Age
43
XP
291
Country
Philippines
We should be able to find the hex patterns if we use some wildcards, it shouldn't be a problem to do that - I just checked the first patch on each firmware, can can't see an issue doing it that way.

patch 3 pattern for 11.0.0 below can be used also.
but 11.0.0 we cannot be sure if it will change on new updates.

on patch 1, we can use "patch = int((0x14 << 24) | ((inst >> 5) & 0x7FFFF))" to convert that cbz to b (credit to anon user)

bR4ZmhB.png
 
  • Like
Reactions: LyuboA and mrdude

mrdude

Developer
Developer
Joined
Dec 11, 2015
Messages
3,071
Trophies
1
Age
56
XP
8,227
Not sure if this is any help but for patch 3 search for this text in ida: "escertificate"
u3Ih9l2.png

See pic 1, scroll to the top of the function where you see the branch:
JoX4NlL.png

Click on that so you can go to the branch address, You will then be in the function that needs patched.
gNp4vds.png


Now I know that's not ideal - and it may need done manually - but it's better than nothing :-)

Patch 3 in older firmware was doing a NOP, now it's doing a branch to address:
68Etej3.png


It could be possible that we can just (change the patch) nop the call to branch to address as shown in the 2nd picture - I didn't try this, but it might work and make patch 3 easier to automatically do.
 
Last edited by mrdude,

crckd

Member
OP
Newcomer
Joined
Dec 3, 2020
Messages
14
Trophies
0
Age
43
XP
291
Country
Philippines
I was thinking the same thing. cbz-to-b on patch 3. It would change the existing es patches of versions below 11 but it would be much easier and more consistent.

off-topic:
Which OS/python/IDA version do you use? i'll try to install IDA to see the disassemble code easier. last debugger i used was ollydbg and it was 8 years ago.
 
  • Like
Reactions: ShadowOne333

mrdude

Developer
Developer
Joined
Dec 11, 2015
Messages
3,071
Trophies
1
Age
56
XP
8,227
I was thinking the same thing. cbz-to-b on patch 3. It would change the existing es patches of versions below 11 but it would be much easier and more consistent.

off-topic:
Which OS/python/IDA version do you use? i'll try to install IDA to see the disassemble code easier. last debugger i used was ollydbg and it was 8 years ago.
Check your pm's, I'm using python 3.9.1/Windows 10 (64bit), IDA 7.5 (64 bit).
 
Last edited by mrdude,
  • Like
Reactions: ShadowOne333

mrdude

Developer
Developer
Joined
Dec 11, 2015
Messages
3,071
Trophies
1
Age
56
XP
8,227
@crckd, as you probably have never used IDA, it's a bit of a learning curve - but here's how to work out the patch for patch 3.

The pictures should explain better than me drivelling on.
View from IDA of unpatched file..
9CvkBHK.png


Arm2hex website, how to get the branch address patch:
EuxEWts.png


Now using that info for: Alternative Branch Patches
Firmware 9.1.0 - patch 3 (tested - works fine)
Alternative Patch would look like this: offset: 2d3a0 - patch: 04000014

Firmware 10.0.4 - patch 3 (tested - works fine)
Alternative Patch would look like this: offset: 2dc50 - patch: 09000014

Firmware 10.1.0/10.2.0 - patch 3 (tested - works fine)
Alternative Patch would look like this: offset: 2dc64 - patch: 09000014

Firmware 11.x.x - patch 3 (tested - works fine)
offset: 2d0f4 - patch: 06000014
 

Attachments

  • alt-es_patches.zip
    956 bytes · Views: 190
Last edited by mrdude,

LyuboA

Unknown Entity
Member
Joined
Jun 1, 2018
Messages
530
Trophies
0
XP
919
Country
Bulgaria
@mrdude i tested the patches for 10.2.0 they work
now that you know how to make them what does this mean for script
or a guide so everyone can make them
 
Last edited by LyuboA,

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    BakerMan @ BakerMan: @RedColoredStars My condolences, was that your cat? +1