A tool for deriving PKG packet encryption keys for ps4 written in c++
| 1 | #pragma once |
| 2 | |
| 3 | #include <cstdint> |
| 4 | #include <string> |
| 5 | #include <vector> |
| 6 | |
| 7 | namespace ShadPKG::Patcher { |
| 8 | |
| 9 | // Standard ELF64 Constants and Structures |
| 10 | // We redefine them here to be independent of SymbolAnalysis private structs |
| 11 | |
| 12 | struct Elf64_Ehdr { |
| 13 | unsigned char e_ident[16]; |
| 14 | uint16_t e_type; |
| 15 | uint16_t e_machine; |
| 16 | uint32_t e_version; |
| 17 | uint64_t e_entry; |
| 18 | uint64_t e_phoff; |
| 19 | uint64_t e_shoff; |
| 20 | uint32_t e_flags; |
| 21 | uint16_t e_ehsize; |
| 22 | uint16_t e_phentsize; |
| 23 | uint16_t e_phnum; |
| 24 | uint16_t e_shentsize; |
| 25 | uint16_t e_shnum; |
| 26 | uint16_t e_shstrndx; |
| 27 | }; |
| 28 | |
| 29 | struct Elf64_Phdr { |
| 30 | uint32_t p_type; |
| 31 | uint32_t p_flags; |
| 32 | uint64_t p_offset; |
| 33 | uint64_t p_vaddr; |
| 34 | uint64_t p_paddr; |
| 35 | uint64_t p_filesz; |
| 36 | uint64_t p_memsz; |
| 37 | uint64_t p_align; |
| 38 | }; |
| 39 | |
| 40 | // PS4 SELF Header structures (Reverse engineered) |
| 41 | struct SelfHeader { |
| 42 | uint32_t magic; // 0x1D3D154F |
| 43 | uint32_t version; |
| 44 | uint16_t mode; |
| 45 | uint16_t endian; |
| 46 | uint32_t attr; |
| 47 | uint32_t header_size; |
| 48 | uint64_t metadata_offset; |
| 49 | uint64_t header_len; |
| 50 | uint64_t file_len; |
| 51 | uint16_t segment_count; |
| 52 | uint16_t section_count; |
| 53 | // ... more fields if needed |
| 54 | }; |
| 55 | |
| 56 | struct SelfSegment { |
| 57 | uint64_t flags; |
| 58 | uint64_t offset; |
| 59 | uint64_t encrypted_file_size; |
| 60 | uint64_t file_size; |
| 61 | uint64_t memory_size; |
| 62 | uint64_t vaddr; |
| 63 | uint64_t align; |
| 64 | }; |
| 65 | |
| 66 | class ElfReconstructor { |
| 67 | public: |
| 68 | ElfReconstructor(); |
| 69 | |
| 70 | // Load file data (ELF, SELF, or FSELF) |
| 71 | bool loadFile(const std::string &path); |
| 72 | bool loadData(const std::vector<uint8_t> &data); |
| 73 | |
| 74 | // Check likely format |
| 75 | bool isSelf() const; |
| 76 | bool isElf() const; |
| 77 | |
| 78 | // Convert loaded SELF to ELF64 |
| 79 | // If output is provided, saves to file. Otherwise keeps in memory. |
| 80 | bool reconstructElf(const std::string &outputPath = ""); |
| 81 | |
| 82 | // Get the resulting ELF data |
| 83 | const std::vector<uint8_t> &getElfData() const { return elfData_; } |
| 84 | |
| 85 | private: |
| 86 | std::vector<uint8_t> rawData_; |
| 87 | std::vector<uint8_t> elfData_; |
| 88 | bool isLoaded_ = false; |
| 89 | |
| 90 | // Helper functions |
| 91 | bool parseSelf(); |
| 92 | }; |
| 93 | |
| 94 | } // namespace ShadPKG::Patcher |
| 95 |