Skip to content

Commit a564c45

Browse files
committed
better Aes handling
1 parent 284f0ee commit a564c45

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

Private/ZenTools.cpp

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ INT32_MAIN_INT32_ARGC_TCHAR_ARGV()
3434
return Result;
3535
}
3636

37-
bool FIOStoreTools::ExtractPackagesFromContainers( const FString& ContainerDirPath, const FString& OutputDirPath, const FString& EncryptionKeysFile,
37+
bool FIOStoreTools::ExtractPackagesFromContainers( const FString& ContainerDirPath, const FString& OutputDirPath, const FString& MainAesKey, const FString& EncryptionKeysFile,
3838
EZenPackageVersion DefaultZenPackageVersion, const FString& PackageFilter, const FString& Filter, bool SkipBulkData )
3939
{
4040
TMap<FGuid, FAES::FAESKey> EncryptionKeys;
@@ -65,7 +65,11 @@ bool FIOStoreTools::ExtractPackagesFromContainers( const FString& ContainerDirPa
6565
FGuid KeyGuid;
6666
if ( FGuid::Parse( EntryPair.Key, KeyGuid ) )
6767
{
68-
const FString EncryptionKeyHex = EntryPair.Value->AsString();
68+
FString EncryptionKeyHex = EntryPair.Value->AsString();
69+
if (EncryptionKeyHex.StartsWith("0x"))
70+
{
71+
EncryptionKeyHex = EncryptionKeyHex.RightChop(2);
72+
}
6973

7074
TArray<uint8> HexToBytesBuffer;
7175
HexToBytesBuffer.AddZeroed( EncryptionKeyHex.Len() + 1 );
@@ -84,7 +88,22 @@ bool FIOStoreTools::ExtractPackagesFromContainers( const FString& ContainerDirPa
8488
}
8589
}
8690
}
87-
91+
92+
if (!MainAesKey.IsEmpty())
93+
{
94+
TArray<uint8> HexToBytesBuffer;
95+
HexToBytesBuffer.AddZeroed(MainAesKey.Len() + 1);
96+
HexToBytesBuffer.SetNumZeroed(HexToBytes(MainAesKey, HexToBytesBuffer.GetData()));
97+
if (HexToBytesBuffer.Num() != FAES::FAESKey::KeySize)
98+
{
99+
UE_LOG(LogIoStoreTools, Warning, TEXT("Ignoring Main AES Key because it has invalid size (%d bytes vs %d expected)"), HexToBytesBuffer.Num(), FAES::FAESKey::KeySize);
100+
}
101+
else
102+
{
103+
FMemory::Memcpy(EncryptionKeys.FindOrAdd(FGuid()).Key, HexToBytesBuffer.GetData(), HexToBytesBuffer.Num());
104+
}
105+
}
106+
88107
TArray<FString> FilterStrings;
89108
if (!Filter.IsEmpty())
90109
{
@@ -193,12 +212,14 @@ bool FIOStoreTools::ExecuteIOStoreTools(const TCHAR* Cmd)
193212
{
194213
EncryptionKeysFile = FPaths::ConvertRelativePathToFull( EncryptionKeysFile );
195214
}
196-
// Legacy argument support, it should not have a forward slash
197-
else if ( FParse::Value( Cmd, TEXT("-EncryptionKeys="), EncryptionKeysFile ) )
215+
216+
FString MainAesKey;
217+
218+
if (FParse::Value(Cmd, TEXT("AES="), MainAesKey) && MainAesKey.StartsWith("0x"))
198219
{
199-
EncryptionKeysFile = FPaths::ConvertRelativePathToFull( EncryptionKeysFile );
220+
MainAesKey = MainAesKey.RightChop(2);
200221
}
201-
222+
202223
ContainerFolderPath = FPaths::ConvertRelativePathToFull( ContainerFolderPath );
203224
ExtractFolderRootPath = FPaths::ConvertRelativePathToFull( ExtractFolderRootPath );
204225

@@ -250,7 +271,7 @@ bool FIOStoreTools::ExecuteIOStoreTools(const TCHAR* Cmd)
250271
FString PotentialPackageFilter;
251272
FParse::Value( Cmd, TEXT("PackageFilter="), PotentialPackageFilter );
252273

253-
return ExtractPackagesFromContainers( ContainerFolderPath, ExtractFolderRootPath, EncryptionKeysFile, DefaultZenPackageVersion, PotentialPackageFilter, FilterFilePath, SkipBulkData);
274+
return ExtractPackagesFromContainers( ContainerFolderPath, ExtractFolderRootPath, MainAesKey,EncryptionKeysFile, DefaultZenPackageVersion, PotentialPackageFilter, FilterFilePath, SkipBulkData);
254275
}
255276

256277
UE_LOG( LogIoStoreTools, Display, TEXT("Unknown command. Available commands: ") );

Private/ZenTools.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ class ZENTOOLS_API FIOStoreTools
1212
{
1313
public:
1414
static bool ExecuteIOStoreTools( const TCHAR* Cmd );
15-
static bool ExtractPackagesFromContainers( const FString& ContainerDirPath, const FString& OutputDirPath, const FString& EncryptionKeysFile,
15+
static bool ExtractPackagesFromContainers( const FString& ContainerDirPath, const FString& OutputDirPath, const FString& MainAesKey, const FString& EncryptionKeysFile,
1616
EZenPackageVersion DefaultZenPackageVersion, const FString& PackageFiler, const FString& Filter, bool SkipBulkData );
1717
};

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ And `FFileHelper::SaveArrayToFile` signature to supports TArrayView64
3030

3131
## Usage:
3232

33-
`ZenTools ExtractPackages <ContainerFolderPath> <ExtractionDir> [-EncryptionKeys=<KeyFile>] [-ZenPackageVersion=<Initial/DataResourceTable/Latest>] [-SkipBulkData] [-PackageFilter=<Package/Path/Filter>] [-Filter=<FilterFile>]`
33+
`ZenTools ExtractPackages <ContainerFolderPath> <ExtractionDir> [-Aes=<MainAesKey>] [-EncryptionKeys=<KeyFile>] [-ZenPackageVersion=<Initial/DataResourceTable/Latest>] [-SkipBulkData] [-PackageFilter=<Package/Path/Filter>] [-Filter=<FilterFile>]`
3434

3535
- `ContainerFolderPath` - Path to the folder containing the container files (.ucas/.utoc + .pak).
3636
- `ExtractionDir` - Path to the folder where the extracted packages will be saved.
37+
- `MainAesKey` - Main AES key for the game.
3738
- `KeyFile` - Path to the file containing encryption keys.
3839
- `ZenPackageVersion` - Version of the ZenPackage format to use. Default is `Latest`. `Initial` for UE5.1
3940
- `SkipBulkData` - Skip extracting bulk data.
@@ -63,3 +64,4 @@ Since the game in the above example needs an AES key, this is the following `key
6364
"00000000-0000-0000-0000-000000000000": "DEADBEEFCAFEDEADBEEFCAFEDEADBEEFCAFEDEADBEEFCAFEDEADBEEFCAFEDEAD"
6465
}
6566
```
67+
or use `-AES=DEADBEEFCAFEDEADBEEFCAFEDEADBEEFCAFEDEADBEEFCAFEDEADBEEFCAFEDEAD` instead.

0 commit comments

Comments
 (0)