dev commit

This commit is contained in:
Andrei Yankovich 2019-03-21 00:34:57 +03:00
parent 15fbdb2091
commit 1a9c99d1ff
4 changed files with 181 additions and 41 deletions

View File

@ -9,22 +9,22 @@ int PE::findIndexPE(QFile &file) {
return -1;
}
int limit = 0xFF;
int currentSeack = INDEX_PE_MAGIC;
int limit = 0x400;
int currentSeeck = INDEX_PE_MAGIC;
unsigned int PE = 0x0;
while (currentSeack <= limit) {
if (!file.seek(currentSeack)) {
while (currentSeeck <= limit) {
if (!file.seek(currentSeeck)) {
return -1;
}
file.read(reinterpret_cast<char*>(&PE), sizeof (PE));
if (PE == PE_MAGIC) {
return currentSeack;
return currentSeeck;
}
currentSeack++;
currentSeeck++;
}
return -1;
@ -50,7 +50,7 @@ bool PE::fillMetaInfo(LIB_META_INFO &info, const QString &file) {
}
unsigned short mashine = 0x0;
SEEK(static_cast<unsigned int>(peAddress) + sizeof (unsigned short));
SEEK(static_cast<unsigned int>(peAddress) + sizeof (unsigned int));
f.read(reinterpret_cast<char*>(&mashine), sizeof (mashine));
@ -64,28 +64,35 @@ bool PE::fillMetaInfo(LIB_META_INFO &info, const QString &file) {
info.type = magic;
unsigned int importTableIndex = 0;
unsigned int rvaIndex = 0;
if (static_cast<RunType>(info.type) == RunType::_32bit) {
importTableIndex = static_cast<unsigned int>(peAddress) + INDEX_IMPORTS_32;
rvaIndex = static_cast<unsigned int>(peAddress) + NUMBER_RVA_AND_SIZES_32;
} else if (static_cast<RunType>(info.type) == RunType::_64bit) {
importTableIndex = static_cast<unsigned int>(peAddress) + INDEX_IMPORTS_64;
rvaIndex = static_cast<unsigned int>(peAddress) + NUMBER_RVA_AND_SIZES_64;
} else {
f.close();
return false;
}
SEEK(rvaIndex);
unsigned int NumberOfRvaAndSizes = 0;
f.read(reinterpret_cast<char*>(&NumberOfRvaAndSizes), sizeof (NumberOfRvaAndSizes));
SEEK(importTableIndex);
unsigned int impoerAddress = 0x0;
IMAGE_DATA_DIRECTORY import = {};
f.read(reinterpret_cast<char*>(&impoerAddress), sizeof (impoerAddress));
f.read(reinterpret_cast<char*>(&import), sizeof (import));
SEEK(importTableIndex + sizeof (impoerAddress));
unsigned int impoerSize = 0x0;
f.read(reinterpret_cast<char*>(&impoerSize), sizeof (impoerSize));
info.addressImports = impoerAddress;
info.sizeImportTable = impoerSize;
info.addressImports = import.VirtualAddress;
info.sizeImportTable = import.Size;
f.close();
return true;

View File

@ -5,6 +5,19 @@
#include <QString>
#include "igetlibinfo.h"
//Alghoritm of read dll file
//1. find PEMagic
//2. read magic
//3. check arhitecture (PE32 or PE32+)
//4. find on map of PE achitecture peraw addresses the address of section count
//5. find like point 4 address of begin sections headers
//6. read all sections or skip all sections and read only neaded information (rva addresses)
//7. read a size and rva address of import table and import table deley.
//8. convert rva addresses to raw addresses and jomp to begin import tables.
//9. read the all data of import tables and split it.
//10 find needed library of binary file.
struct LIB_META_INFO {
unsigned short mashine = 0x0;
unsigned short type = 0x0;
@ -12,17 +25,138 @@ struct LIB_META_INFO {
unsigned int sizeImportTable = 0x0;
};
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned int DWORD;
#define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16
#define IMAGE_SIZEOF_SHORT_NAME 8
// Directory Entries
#define IMAGE_DIRECTORY_ENTRY_EXPORT 0 // Export Directory
#define IMAGE_DIRECTORY_ENTRY_IMPORT 1 // Import Directory
#define IMAGE_DIRECTORY_ENTRY_RESOURCE 2 // Resource Directory
#define IMAGE_DIRECTORY_ENTRY_EXCEPTION 3 // Exception Directory
#define IMAGE_DIRECTORY_ENTRY_SECURITY 4 // Security Directory
#define IMAGE_DIRECTORY_ENTRY_BASERELOC 5 // Base Relocation Table
#define IMAGE_DIRECTORY_ENTRY_DEBUG 6 // Debug Directory
// IMAGE_DIRECTORY_ENTRY_COPYRIGHT 7 // (X86 usage)
#define IMAGE_DIRECTORY_ENTRY_ARCHITECTURE 7 // Architecture Specific Data
#define IMAGE_DIRECTORY_ENTRY_GLOBALPTR 8 // RVA of GP
#define IMAGE_DIRECTORY_ENTRY_TLS 9 // TLS Directory
#define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG 10 // Load Configuration Directory
#define IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT 11 // Bound Import Directory in headers
#define IMAGE_DIRECTORY_ENTRY_IAT 12 // Import Address Table
#define IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT 13 // Delay Load Import Descriptors
#define IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR 14 // COM Runtime descriptor
struct IMAGE_DATA_DIRECTORY {
DWORD VirtualAddress;
DWORD Size;
};
struct IMAGE_FILE_HEADER {
WORD Machine;
WORD NumberOfSections;
DWORD TimeDateStamp;
DWORD PointerToSymbolTable;
DWORD NumberOfSymbols;
WORD SizeOfOptionalHeader;
WORD Characteristics;
};
struct IMAGE_OPTIONAL_HEADER {
WORD Magic;
BYTE MajorLinkerVersion;
BYTE MinorLinkerVersion;
DWORD SizeOfCode;
DWORD SizeOfInitializedData;
DWORD SizeOfUninitializedData;
DWORD AddressOfEntryPoint;
DWORD BaseOfCode;
DWORD BaseOfData;
DWORD ImageBase;
DWORD SectionAlignment;
DWORD FileAlignment;
WORD MajorOperatingSystemVersion;
WORD MinorOperatingSystemVersion;
WORD MajorImageVersion;
WORD MinorImageVersion;
WORD MajorSubsystemVersion;
WORD MinorSubsystemVersion;
DWORD Win32VersionValue;
DWORD SizeOfImage;
DWORD SizeOfHeaders;
DWORD CheckSum;
WORD Subsystem;
WORD DllCharacteristics;
DWORD SizeOfStackReserve;
DWORD SizeOfStackCommit;
DWORD SizeOfHeapReserve;
DWORD SizeOfHeapCommit;
DWORD LoaderFlags;
DWORD NumberOfRvaAndSizes;
IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
};
struct IMAGE_NT_HEADERS {
DWORD Signature;
IMAGE_FILE_HEADER FileHeader;
IMAGE_OPTIONAL_HEADER OptionalHeader;
};
struct IMAGE_SECTION_HEADER {
BYTE Name[IMAGE_SIZEOF_SHORT_NAME];
union {
DWORD PhysicalAddress;
DWORD VirtualSize;
} Misc;
DWORD VirtualAddress;
DWORD SizeOfRawData;
DWORD PointerToRawData;
DWORD PointerToRelocations;
DWORD PointerToLinenumbers;
WORD NumberOfRelocations;
WORD NumberOfLinenumbers;
DWORD Characteristics;
};
class PE : public IGetLibInfo {
private:
// int defSection(DWORD rva)
// {
// for (int i = 0; i < numberOfSection; ++i)
// {
// DWORD start = sections[i].VirtualAddress;
// DWORD end = start + ALIGN_UP(sections[i].VirtualSize, sectionAligment);
// if(rva >= start && rva < end)
// return i;
// }
// return -1;
// }
// DWORD rvaToOff(DWORD rva)
// {
// int indexSection = defSection(rva);
// if(indexSection != -1)
// return rva - sections[indexSection].VirtualAddress + sections[indexSection].PointerToRawData;
// else
// return 0;
// }
int findIndexPE(QFile &file);
bool fillMetaInfo(LIB_META_INFO& info, const QString &file);
constexpr static unsigned int PE_MAGIC = 0x00004550;
constexpr static unsigned int INDEX_PE_MAGIC = 0x80;
constexpr static unsigned int INDEX_MAGIC = 0x18;
constexpr static unsigned int INDEX_IMPORTS_32 = 0x68;
constexpr static unsigned int INDEX_IMPORTS_64 = 0x78;
constexpr static unsigned int INDEX_IMPORTS_32 = 104;
constexpr static unsigned int INDEX_IMPORTS_64 = 120;
constexpr static unsigned int NUMBER_RVA_AND_SIZES_32 = 92;
constexpr static unsigned int NUMBER_RVA_AND_SIZES_64 = 108;
public:
enum class MashineTypesS: unsigned short {

View File

@ -65,6 +65,28 @@ void LibCreator::initLinux64() {
}
void LibCreator::initWin32() {
createLib(":/win32mingw.dll", {
"libEGL.dll",
"libgcc_s_dw2-1.dll",
"KERNEL32.dll",
"msvcrt.dll",
"libGLESv2.dll",
},
Platform::Win32 );
createLib(":/win32mingw.exe",{
" Qt5Core.dll",
" Qt5Gui.dll",
" Qt5Qml.dll",
" Qt5Widgets.dll",
" libgcc_s_dw2-1.dll",
" KERNEL32.dll",
" msvcrt.dll",
" SHELL32.dll",
" libstdc++-6.dll",
},
Platform::Win32
);
createLib(":/win32msvc.dll",{
"ole32.dll",
"OLEAUT32.dll",
@ -102,28 +124,6 @@ void LibCreator::initWin32() {
},
Platform::Win32);
createLib(":/win32mingw.dll", {
"libEGL.dll",
"libgcc_s_dw2-1.dll",
"KERNEL32.dll",
"msvcrt.dll",
"libGLESv2.dll",
},
Platform::Win32 );
createLib(":/win32mingw.exe",{
" Qt5Core.dll",
" Qt5Gui.dll",
" Qt5Qml.dll",
" Qt5Widgets.dll",
" libgcc_s_dw2-1.dll",
" KERNEL32.dll",
" msvcrt.dll",
" SHELL32.dll",
" libstdc++-6.dll",
},
Platform::Win32
);
}
void LibCreator::initWin64() {

View File

@ -298,7 +298,6 @@ void deploytest::testExtractLib() {
for (auto &&lib : libs) {
QVERIFY(scaner.fillLibInfo(info, lib));
QVERIFY(info.isValid());
QVERIFY(info.name == QFileInfo(lib).fileName());
QVERIFY(info.path == QFileInfo(lib).absolutePath());
QVERIFY(info.fullPath() == QFileInfo(lib).absoluteFilePath());