Skip to content

Commit

Permalink
simplified File class and fixed small bug in offsets for gadgets
Browse files Browse the repository at this point in the history
  • Loading branch information
user committed May 9, 2024
1 parent 885f2dd commit 210ebc3
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 30 deletions.
19 changes: 6 additions & 13 deletions src/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,15 @@ size_t File::GetSize(std::string filePath) {
return file.GetSize();
}

FileContents File::Read(std::string filePath) {
std::vector<char> File::Read(std::string filePath) {
File file(std::move(filePath));
return file.Read();
}

FileContents File::Read() {
size_t fileSize = GetSize();
FileContents file = {
.size = fileSize,
.contents = reinterpret_cast<char*>(calloc(fileSize, 1))
};
fileStream_.read(file.contents, file.size);
if (!file.contents)
{
file.size = 0;
}
std::vector<char> File::Read() {
std::vector<char> fileContents(GetSize());

fileStream_.read(fileContents.data(), static_cast<long long>(fileContents.size()));

return file;
return fileContents;
}
10 changes: 2 additions & 8 deletions src/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,13 @@
#ifndef GADGIFY_FILE_H
#define GADGIFY_FILE_H

struct FileContents
{
size_t size;
char* contents;
};

class File {
public:
explicit File(std::string filePath);
explicit File(std::string filePath, std::ios_base::openmode openMode);
virtual ~File();
static FileContents Read(std::string filePath);
FileContents Read();
static std::vector<char> Read(std::string filePath);
std::vector<char> Read();
static bool Delete(std::string filePath);
static size_t GetSize(std::string filePath);
bool Delete();
Expand Down
7 changes: 5 additions & 2 deletions src/Gadgify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ bool Gadgify::SearchGadgets(const std::function<void(uint64_t, std::string)> &ca
csh handle;
cs_insn *insn;
size_t count;

if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK)
{
return false;
Expand Down Expand Up @@ -141,6 +140,10 @@ bool Gadgify::SearchGadgets(const std::function<void(uint64_t, std::string)> &ca
matches = 0;
gadget.clear();
}
if (gadget.size() == 1)
{
firstOffset = insn[j].address;
}
if (regexes.size() == matches)
{
std::string gadgetString;
Expand All @@ -149,7 +152,7 @@ bool Gadgify::SearchGadgets(const std::function<void(uint64_t, std::string)> &ca
gadgetString.append(i);
gadgetString.append("; ");
}
callback(insn[j].address + bytecode.virtualAddress, gadgetString);
callback(firstOffset + bytecode.virtualAddress, gadgetString);
gapCounter = 0;
matches = 0;
gadget.clear();
Expand Down
7 changes: 2 additions & 5 deletions src/PEFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

PEFile::PEFile(std::string filePath)
{
FileContents file = File::Read(std::move(filePath));
peContents_ = std::vector<char>(file.size);
memcpy(peContents_.data(), file.contents, file.size);
free(file.contents);
peSize_ = file.size;
peContents_ = File::Read(std::move(filePath));
peSize_ = peContents_.size();
peBufferAddr_ = reinterpret_cast<uintptr_t>(peContents_.data());
ParseHeadersAndValidate();
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ int main(int argc, char *argv[]) {

if (isRaw)
{
FileContents file = File::Read(program.get<std::string>("binaryPath"));
std::vector<char> fileContents(File::Read(program.get<std::string>("binaryPath")));
Gadgify::GetGadgets([&results](uint64_t offset, const std::string &gadget)
{
results << "0x" << std::hex << std::setfill('0') << std::setw(8) << offset << ": " << gadget << std::endl;
},
std::vector<char>(file.contents, file.contents+file.size),
fileContents,
program.get<std::string>("--pattern"),
program.get<uint32_t>("--gap")
);
Expand Down

0 comments on commit 210ebc3

Please sign in to comment.