Skip to content

Commit

Permalink
webMAN MOD 1.47.45 (Update) (#997)
Browse files Browse the repository at this point in the history
* Added support for encrypted ISOs

Will copy the key if it is available from external NTFS/exFAT HDD with the same name as the ISO to decrypt it on-the-fly with Cobra when the ISO is mounted

* Removed duplicated code

* Removed spaces in code

* webMAN MOD 1.47.45 (Update)

[prepISO] - Added support for encrypted ISO '.key' file (Will copy it to /dev_hdd0/tmp/wmtmp)

[prepISO] - Added support for encrypted ISO '.dkey' file, will transform it to '.key' format and will copy it to /dev_hdd0/tmp/wmtmp

[prepISO] - Fixed issue while copying '.key' files to internal HDD
  • Loading branch information
Evilnat authored Jan 1, 2024
1 parent 078ac66 commit 0ab6535
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 25 deletions.
67 changes: 48 additions & 19 deletions _Projects_/prepISO/include/exfat.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,35 @@ void addlog(char *msg1, char *msg2)
*/
static int s_mode; // 0 = png/jpg/sfo, 1=iso/bin/img/mdf

int copy_exfat(char *src_file, char *out_file, u64 size)
static void convert_dkey_to_key(uint8_t disckey[0x10], char dkey[0x20])
{
for (int i = 0; i < 0x10; i++)
{
char byte[3];
strncpy(byte, &dkey[i * 2], 2);
byte[2] = 0;
disckey[i] = strtol(byte, NULL, 16);
}
}

static int readfile_exfat(char *file, char *data, u64 size)
{
if(size == 0) return FAILED;

FIL fd;
UINT re;

if (!f_open(&fd, file, FA_READ))
{
f_read(&fd, data, size, &re);
f_close(&fd);
return SUCCESS;
}

return FAILED;
}

static int copy_exfat(char *src_file, char *out_file, u64 size)
{
if(size == 0) return FAILED;

Expand Down Expand Up @@ -107,29 +135,30 @@ static int dir_read (char *dpath)
}
//---------------

char output[256], filename[256];
uint8_t disckey[0x10];
char dkey[0x20];
strcpy(filename, fno.fname);
snprintf(fn, 255, "%s/%s", dpath, fno.fname);
snprintf(output, 255, "/dev_hdd0/tmp/wmtmp/%s", fno.fname);

// If the key exists, copy it to "/dev_hdd0/tmp/wmtmp" to
// decrypt on-the-fly with Cobra when the ISO is mounted (By Evilnat)
if(strcasestr(ext, ".key"))
{
FIL fd;
char output[256];
snprintf (fn, 255, "%s/%s", dpath, fno.fname);
snprintf(output, 255, "/dev_hdd0/tmp/wmtmp/%s", fno.fname);

if (!f_open(&fd, fn, FA_READ))
copy_exfat(fn, output, 0x10);

// If the dkey exists, we convert it to disckey and copy it to "/dev_hdd0/tmp/wmtmp"
// to decrypt on-the-fly with Cobra when the ISO is mounted (By Evilnat)
if(strcasestr(fno.fname + flen - 5, ".dkey"))
{
if(!readfile_exfat(fn, dkey, 0x20))
{
UINT re;
uint8_t data[0x10];
f_read(&fd, data, 0x10, &re);
f_close (&fd);
filename[strlen(filename) - 5] = '\0';
snprintf(output, 255, "/dev_hdd0/tmp/wmtmp/%s.key", filename);

int fda = ps3ntfs_open(output, O_WRONLY | O_CREAT | O_TRUNC, 0777);
if(fda >= 0)
{
ps3ntfs_write(fda, (void *)data, 0x10);
ps3ntfs_close(fda);
}
}
convert_dkey_to_key(disckey, dkey);
SaveFile(output, (char *)disckey, 0x10);
}
}

//--- is ISO?
Expand Down
16 changes: 16 additions & 0 deletions _Projects_/prepISO/include/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ u64 get_filesize(char *path)
return st.st_size;
}

int readfile_ntfs(char *file, char *data, u64 size)
{
if(size==0) return FAILED;

int fd = ps3ntfs_open(file, O_RDONLY, 0);
if(fd >= 0)
{
int re = ps3ntfs_read(fd, (void *)data, size);
ps3ntfs_close(fd);

return (re == size) ? SUCCESS : FAILED;
}

return FAILED;
}

int copy_file(char *src_file, char *out_file)
{
u64 size=get_filesize(src_file);
Expand Down
39 changes: 33 additions & 6 deletions _Projects_/prepISO/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ uint8_t plugin_args[PLUGIN_ARGS_SIZE];

#define MAX_PATH_LEN 0x420

static char key_path[MAX_PATH_LEN];
static char output[MAX_PATH_LEN];
static char path[MAX_PATH_LEN];
static char full_path[MAX_PATH_LEN];
static char wm_path[MAX_PATH_LEN];
static char image_file[MAX_PATH_LEN];

Expand Down Expand Up @@ -127,6 +130,7 @@ int main(int argc, const char* argv[])
char path0[MAX_PATH_LEN], subpath[MAX_PATH_LEN];
char direntry[MAX_PATH_LEN];
char filename[MAX_PATH_LEN];
char filekey[MAX_PATH_LEN];
bool has_dirs, is_iso = false;
char *ext;
u16 flen;
Expand Down Expand Up @@ -221,13 +225,15 @@ int main(int argc, const char* argv[])
has_dirs = false;

snprintf(path, sizeof(path), "%s:/%s%s%s", mounts[i].name, prefix[p], c_path[m], SUFIX(profile));
strcpy(full_path, path);

pdir = ps3ntfs_diropen(path);
if(pdir != NULL)
{
while(ps3ntfs_dirnext(pdir, dir.d_name, &st) == 0)
{
flen = sprintf(filename, "%s", dir.d_name);
strcpy(filekey, filename);

ext_len = 4;
if(flen < ext_len) continue; ext = filename + flen - ext_len;
Expand Down Expand Up @@ -268,14 +274,35 @@ int main(int argc, const char* argv[])
}
//---------------

// If the key exists, copy it to "/dev_hdd0/tmp/wmtmp" to
// decrypt on-the-fly with Cobra when the ISO is mounted (By Evilnat)
if(strcasestr(ext, ".key"))
{
char output[256];
/* By Evilnat
If disckey ('.key' file) exists, copy it to "/dev_hdd0/tmp/wmtmp" to
decrypt on-the-fly with Cobra when the ISO is mounted
If dkey ('.dkey' file) exists, we will transform it to disckey and
copy it to "/dev_hdd0/tmp/wmtmp"
*/
if(strcasestr(ext, ".key") || strcasestr(filename + flen - 5, ".dkey"))
{
snprintf(output, 255, "/dev_hdd0/tmp/wmtmp/%s", dir.d_name);
sprintf(key_path, "%s/%s", full_path, filename);

copy_file(filename, output);
if(strcasestr(filename + flen - 5, ".dkey"))
{
uint8_t disckey[0x10];
char dkey[0x20];

if(!readfile_ntfs(key_path, dkey, 0x20))
{
filekey[strlen(filekey) - 5] = '\0';
snprintf(output, 255, "/dev_hdd0/tmp/wmtmp/%s.key", filekey);

convert_dkey_to_key(disckey, dkey);
SaveFile(output, (char *)disckey, 0x10);
}
}
else
copy_file(key_path, output);
}

//--- is ISO?
Expand Down

0 comments on commit 0ab6535

Please sign in to comment.