retdec
upx_stub.h
Go to the documentation of this file.
1 
7 #ifndef UNPACKERTOOL_PLUGINS_UPX_UPX_STUB_H
8 #define UNPACKERTOOL_PLUGINS_UPX_UPX_STUB_H
9 
10 #include <memory>
11 
12 #include "retdec/loader/loader.h"
13 #include "retdec/unpacker/plugin.h"
16 
17 using namespace retdec::utils;
18 
19 namespace retdec {
20 
21 // Forward declaration
22 namespace unpacker { class CompressedData; }
23 
24 namespace unpackertool {
25 namespace upx {
26 
27 class Decompressor;
28 
32 enum class UpxStubVersion
33 {
34  LZMA,
35  NRV2B,
36  NRV2D,
37  NRV2E,
38  UPXSHIT,
40  UNIVERSAL,
41  UNKNOWN
42 };
43 
50 {
51  UPX_PACKING_METHOD_NRV2B_LE32 = 0x2, // NRV2B Little Endian 32-bit parser.
52  UPX_PACKING_METHOD_NRV2B_8 = 0x3, // NRV2B 8-bit parser.
53  UPX_PACKING_METHOD_NRV2D_LE32 = 0x5, // NRV2D Little Endian 32-bit parser.
54  UPX_PACKING_METHOD_NRV2D_8 = 0x6, // NRV2D 8-bit parser.
55  UPX_PACKING_METHOD_NRV2E_LE32 = 0x8, // NRV2E Little Endian 32-bit parser.
56  UPX_PACKING_METHOD_NRV2E_8 = 0x9, // NRV2E 8-bit parser
57  UPX_PACKING_METHOD_LZMA = 0xE // LZMA - no bit parser used but has it's own value
58 };
59 
60 struct UpxStubData;
61 
63 {
64 public:
65  UpxMetadata();
66  UpxMetadata(const UpxMetadata& metadata);
67 
68  static UpxMetadata read(retdec::loader::Image* file);
69  static std::uint8_t calcChecksum(const DynamicBuffer& data);
70  static std::uint32_t getSizeOfVersion(std::uint8_t version);
71 
72  UpxStubVersion getStubVersion() const;
73 
74  bool isDefined() const { return _defined; }
75  bool usesPackingMethod() const { return _usesPackingMethod; }
76  std::uint32_t getFileOffset() const { return _fileOffset; }
77  std::uint32_t getFileSize() const { return _fileSize; }
78  UpxPackingMethod getPackingMethod() const { return static_cast<UpxPackingMethod>(_packingMethod); }
79  std::uint32_t getPackedDataSize() const { return _packedDataSize; }
80  std::uint32_t getUnpackedDataSize() const { return _unpackedDataSize; }
81  std::uint8_t getFilterId() const { return _filterId; }
82  std::uint8_t getFilterParameter() const { return _filterParam; }
83 
84 private:
85  void setDefined(bool set) { _defined = set; }
86  void setUsesPackingMethod(bool set) { _usesPackingMethod = set; }
87  void setFileOffset(std::uint32_t fileOffset) { _fileOffset = fileOffset; }
88  void setFileSize(std::uint32_t fileSize) { _fileSize = fileSize; }
89  void setPackingMethod(std::uint8_t packingMethod) { _packingMethod = packingMethod; }
90  void setPackedDataSize(std::uint32_t packedDataSize) { _packedDataSize = packedDataSize; }
91  void setUnpackedDataSize(std::uint32_t unpackedDataSize) { _unpackedDataSize = unpackedDataSize; }
92  void setFilterId(std::uint8_t filterId) { _filterId = filterId; }
93  void setFilterParameter(std::uint8_t filterParam) { _filterParam = filterParam; }
94 
95  bool _defined;
97  std::uint32_t _fileOffset;
98  std::uint32_t _fileSize;
99  std::uint8_t _packingMethod;
100  std::uint32_t _packedDataSize;
101  std::uint32_t _unpackedDataSize;
102  std::uint8_t _filterId;
103  std::uint8_t _filterParam;
104 };
105 
111 {
112 public:
113  UpxStub(retdec::loader::Image* inputFile, const UpxStubData* stubData, const DynamicBuffer& stubCapturedData,
114  std::unique_ptr<Decompressor> decompressor, const UpxMetadata& metadata);
115 
116  static std::shared_ptr<UpxStub> createStub(retdec::loader::Image* file);
117  static std::shared_ptr<UpxStub> createStub(retdec::loader::Image* file, const DynamicBuffer& stubBytes);
118 
119  UpxStubVersion getVersion() const;
120  const UpxStubData* getStubData() const;
121  const DynamicBuffer* getStubCapturedData() const;
122  Decompressor* getDecompressor() const;
123  const UpxMetadata* getUpxMetadata() const;
124  virtual std::uint32_t getRealEpAddress() const;
125 
126  void setStubData(const UpxStubData* stubData);
127  void setStubCapturedData(const DynamicBuffer& stubCapturedData);
128 
129 protected:
130  std::unique_ptr<Decompressor> decodePackingMethod(std::uint8_t packingMethod) const;
131 
134  std::unique_ptr<Decompressor> _decompressor;
136 
137 private:
138  static std::shared_ptr<UpxStub> _createStubImpl(retdec::loader::Image* file, const DynamicBuffer* stubBytes);
139 };
140 
141 } // namespace upx
142 } // namespace unpackertool
143 } // namespace retdec
144 
145 #endif
Definition: image.h:22
Definition: unpacking_stub.h:25
Definition: decompressor.h:28
std::uint32_t getPackedDataSize() const
Definition: upx_stub.h:79
std::uint32_t getFileOffset() const
Definition: upx_stub.h:76
bool usesPackingMethod() const
Definition: upx_stub.h:75
void setDefined(bool set)
Definition: upx_stub.h:85
std::uint32_t getUnpackedDataSize() const
Definition: upx_stub.h:80
std::uint32_t _unpackedDataSize
Definition: upx_stub.h:101
std::uint32_t _packedDataSize
Definition: upx_stub.h:100
std::uint32_t _fileOffset
Definition: upx_stub.h:97
std::uint8_t _packingMethod
Definition: upx_stub.h:99
void setFilterParameter(std::uint8_t filterParam)
Definition: upx_stub.h:93
void setPackingMethod(std::uint8_t packingMethod)
Definition: upx_stub.h:89
bool _defined
Definition: upx_stub.h:95
UpxPackingMethod getPackingMethod() const
Definition: upx_stub.h:78
bool _usesPackingMethod
Definition: upx_stub.h:96
std::uint8_t _filterId
Definition: upx_stub.h:102
void setUsesPackingMethod(bool set)
Definition: upx_stub.h:86
std::uint8_t _filterParam
Definition: upx_stub.h:103
void setFileSize(std::uint32_t fileSize)
Definition: upx_stub.h:88
void setPackedDataSize(std::uint32_t packedDataSize)
Definition: upx_stub.h:90
void setFilterId(std::uint8_t filterId)
Definition: upx_stub.h:92
std::uint8_t getFilterId() const
Definition: upx_stub.h:81
void setUnpackedDataSize(std::uint32_t unpackedDataSize)
Definition: upx_stub.h:91
bool isDefined() const
Definition: upx_stub.h:74
std::uint8_t getFilterParameter() const
Definition: upx_stub.h:82
void setFileOffset(std::uint32_t fileOffset)
Definition: upx_stub.h:87
std::uint32_t getFileSize() const
Definition: upx_stub.h:77
std::uint32_t _fileSize
Definition: upx_stub.h:98
Definition: upx_stub.h:111
std::unique_ptr< Decompressor > _decompressor
Decompressor associated with stub.
Definition: upx_stub.h:134
UpxMetadata _metadata
UPX metadata aka packheader.
Definition: upx_stub.h:135
DynamicBuffer _stubCapturedData
Data captured while matching signature of this stub.
Definition: upx_stub.h:133
const UpxStubData * _stubData
Additional stub information.
Definition: upx_stub.h:132
The class for dynamic buffered data manipulation taking the endianness of the data in account.
Definition: dynamic_buffer.h:36
Declaration of class for buffered data mainpulation.
Interface to loader library.
std::uint64_t fileSize(const std::string &filename)
Definition: PeLibAux.cpp:138
UpxPackingMethod
Definition: upx_stub.h:50
@ UPX_PACKING_METHOD_NRV2B_8
Definition: upx_stub.h:52
@ UPX_PACKING_METHOD_NRV2E_LE32
Definition: upx_stub.h:55
@ UPX_PACKING_METHOD_LZMA
Definition: upx_stub.h:57
@ UPX_PACKING_METHOD_NRV2E_8
Definition: upx_stub.h:56
@ UPX_PACKING_METHOD_NRV2D_8
Definition: upx_stub.h:54
@ UPX_PACKING_METHOD_NRV2B_LE32
Definition: upx_stub.h:51
@ UPX_PACKING_METHOD_NRV2D_LE32
Definition: upx_stub.h:53
UpxStubVersion
Definition: upx_stub.h:33
Definition: unpacking_stub.h:16
Definition: archive_wrapper.h:19
Supportive functionality for the generic unpacker.
Plugin class declaration which is representation of interface to the plugin library.
Definition: upx_stub_signatures.h:25
Base class for unpacking stubs that can be subclassed in unpacker plugins.