-
Notifications
You must be signed in to change notification settings - Fork 10
/
XCI_Decryptor.go
193 lines (155 loc) · 5.17 KB
/
XCI_Decryptor.go
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"encoding/binary"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"github.com/fatih/color"
)
type myFile struct {
Size int64
Name string
}
func printHeader() {
const header string = `██╗ ██╗ ██████╗██╗ ██████╗ ██████╗ ███╗ ██╗██╗ ██╗███████╗██████╗ ████████╗███████╗██████╗
╚██╗██╔╝██╔════╝██║ ██╔════╝██╔═══██╗████╗ ██║██║ ██║██╔════╝██╔══██╗╚══██╔══╝██╔════╝██╔══██╗
╚███╔╝ ██║ ██║ ██║ ██║ ██║██╔██╗ ██║██║ ██║█████╗ ██████╔╝ ██║ █████╗ ██████╔╝
██╔██╗ ██║ ██║ ██║ ██║ ██║██║╚██╗██║╚██╗ ██╔╝██╔══╝ ██╔══██╗ ██║ ██╔══╝ ██╔══██╗
██╔╝ ██╗╚██████╗██║███████╗╚██████╗╚██████╔╝██║ ╚████║ ╚████╔╝ ███████╗██║ ██║ ██║ ███████╗██║ ██║
╚═╝ ╚═╝ ╚═════╝╚═╝╚══════╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═══╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚══════╝╚═╝ ╚═╝
`
color.Green(header)
}
func printUsage() {
color.Red("Usage : ./XCI_Converter [.XCI FILE] [TITLE ID]")
}
func getBiggestNCA(titleIdName string, xciPath string) (file string) {
var (
nca myFile
cmd *exec.Cmd
)
color.Green("Decrypting .xci's NCA files and finding the biggest NCA...")
if runtime.GOOS == "windows" {
cmd = exec.Command("./hactool.exe", "-k", "keys.ini", "-txci", "--securedir="+titleIdName, xciPath)
} else {
cmd = exec.Command("./hactool", "-k", "keys.ini", "-txci", "--securedir="+titleIdName, xciPath)
}
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
files, err := ioutil.ReadDir(titleIdName)
if err != nil {
log.Fatal(err)
}
for _, file := range files {
if file.Size() > nca.Size {
nca.Size = file.Size()
nca.Name = file.Name()
}
}
return nca.Name
}
func decryptNCA(ncaFile string, titleIdName string) {
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
cmd = exec.Command("./hactool.exe", "-k", "keys.ini", "--romfs="+titleIdName+"/romfs.bin", "--exefsdir="+titleIdName+"/exefs", titleIdName+"/"+ncaFile)
} else {
cmd = exec.Command("./hactool", "-k", "keys.ini", "--romfs="+titleIdName+"/romfs.bin", "--exefsdir="+titleIdName+"/exefs", titleIdName+"/"+ncaFile)
}
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
files, err := ioutil.ReadDir(titleIdName)
if err != nil {
log.Fatal(err)
}
// delete *.nca
for _, file := range files {
if filepath.Ext(file.Name()) == ".nca" {
err := os.Remove(titleIdName + "/" + file.Name())
if err != nil {
log.Fatal(err)
}
}
}
}
func patchMainNPDM(titleIdName string) {
color.Green("Patching main.npdm :)")
fd, err := os.OpenFile(titleIdName+"/exefs/main.npdm", os.O_RDWR, 0000)
if err != nil {
log.Fatal(err)
}
defer fd.Close()
uint64TitleName, err := strconv.ParseUint("0x"+titleIdName, 0, 64)
if err != nil {
log.Fatalln(err)
}
_, err = fd.Seek(0x440, os.SEEK_CUR)
if err != nil {
log.Fatal(err)
}
err = binary.Write(fd, binary.LittleEndian, uint64TitleName)
if err != nil {
log.Fatal(err)
}
}
func isHex(hexa string) bool {
_, err := strconv.ParseUint(hexa, 16, 64)
if err != nil {
return false
}
return true
}
func isValidFile(path string) bool {
file, err := os.Stat(path)
if err != nil {
return false
}
// Check is regular file
if file.Mode().IsRegular() == false {
return false
}
// Check permission (at least rw)
mode := file.Mode().Perm()
if (mode >> 7) != 3 {
return false
}
return true
}
func isValidArgs(titleIDName string, xciPath string) bool {
if isHex(titleIDName) == false {
log.Fatal("Please set a valid titleID")
return false
} else if isValidFile(xciPath) == false {
log.Fatal("Path invalid or insufficient permission")
return false
}
return true
}
func main() {
var titleIdName string
var xciPath string
printHeader()
if len(os.Args) != 3 {
printUsage()
return
} else {
titleIdName = os.Args[2]
xciPath = os.Args[1]
titleIdName = strings.TrimSpace(titleIdName)
xciPath = strings.TrimSpace(xciPath)
}
if isValidArgs(titleIdName, xciPath) == false {
log.Fatal("Please set a valid titleID")
}
ncaFile := getBiggestNCA(titleIdName, xciPath)
decryptNCA(ncaFile, titleIdName)
patchMainNPDM(titleIdName)
color.Green("DONE! You should have a folder: " + titleIdName)
color.Green(titleIdName + " should contain an exefs folder and a romfs.bin. It should NOT contain anything else.")
}