mirror of
https://gitlab.com/BinaryHardening/cfgrip
synced 2026-07-26 12:41:08 +00:00
cfgrip: standalone CFG extraction tool from PE/ELF binaries
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
#include "types.hpp"
|
||||
#include "loader/binary.hpp"
|
||||
#include "cfg/builder.hpp"
|
||||
#include "disasm/engine.hpp"
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
if (argc < 2)
|
||||
{
|
||||
cerr << "usage: cfgrip <binary>" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
string path = argv[1];
|
||||
|
||||
auto binary = Binary::create(path);
|
||||
if (!binary)
|
||||
{
|
||||
cerr << "failed to load binary: " << path << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
cout << "format: ";
|
||||
switch (binary->getFormat())
|
||||
{
|
||||
case BinaryFormat::PE: cout << "PE"; break;
|
||||
case BinaryFormat::ELF: cout << "ELF"; break;
|
||||
default: cout << "unknown";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
cout << "arch: ";
|
||||
switch (binary->getArch())
|
||||
{
|
||||
case Arch::X86: cout << "x86"; break;
|
||||
case Arch::X64: cout << "x86-64"; break;
|
||||
default: cout << "unknown";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
cout << "entry: 0x" << hex << binary->getEntryPoint() << dec << endl;
|
||||
cout << "imports: " << binary->getImportedFunctions().size() << endl;
|
||||
|
||||
for (const auto& imp : binary->getImportedFunctions())
|
||||
{
|
||||
cout << " 0x" << hex << imp.address << " " << imp.name;
|
||||
if (!imp.library.empty()) cout << " (" << imp.library << ")";
|
||||
cout << dec << endl;
|
||||
}
|
||||
|
||||
Disassembler dis;
|
||||
if (!dis.init(binary->getArch()))
|
||||
{
|
||||
cerr << "failed to init capstone" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
CFGBuilder builder(binary.get(), &dis);
|
||||
CFG cfg = builder.build();
|
||||
|
||||
cout << "functions: " << cfg.functions.size() << endl;
|
||||
cout << "indirect targets: " << cfg.indirect_targets.size() << endl;
|
||||
|
||||
string out = path + ".cfg";
|
||||
ofstream f(out);
|
||||
if (f)
|
||||
{
|
||||
f << cfg.toJSON();
|
||||
f.close();
|
||||
cout << "cfg written to: " << out << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user