Skip to content

Commit

Permalink
Code Clean Up
Browse files Browse the repository at this point in the history
  • Loading branch information
rem0obb committed Dec 28, 2021
1 parent 6720907 commit 629a19e
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 145 deletions.
7 changes: 2 additions & 5 deletions src/abstract_elfheader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@
#include <cstring>
#include <stdexcept>

#ifdef __APPLE__
#include "endian.h"
#elif WINDOWS
#include <WinSock2.h>
#include "endian.h"
#if WINDOWS || __APPLE__
#include "endian.hpp"
#else
#include <arpa/inet.h>
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/abstract_programheader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <iostream>

#if __APPLE__ || WINDOWS
#include "endian.h"
#include "endian.hpp"
#else
#include <arpa/inet.h>
#endif
Expand Down
6 changes: 2 additions & 4 deletions src/abstract_sectionheader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
#include <stdexcept>
#include <boost/foreach.hpp>

#ifdef __APPLE__
#include "endian.h"
#elif WINDOWS
#include "endian.h"
#if WINDOWS || __APPLE__
#include "endian.hpp"
#else
#include <arpa/inet.h>
#endif
Expand Down
175 changes: 90 additions & 85 deletions src/abstract_symbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,84 +5,79 @@
#include <boost/lexical_cast.hpp>
#include <sstream>

#ifdef __APPLE__
#include "endian.h"
#elif WINDOWS
#include "endian.h"
#if WINDOWS || __APPLE__
#include "endian.hpp"
#else
#include <arpa/inet.h>
#endif

namespace
std::string getSymBinding(boost::uint8_t p_info)
{
std::string getSymBinding(boost::uint8_t p_info)
std::string str;
switch ((p_info >> 4) & 0x0f)
{
switch ((p_info >> 4) & 0x0f)
{
case elf::symbol::k_local:
return "STB_LOCAL";
case elf::symbol::k_global:
return "STB_GLOBAL";
case elf::symbol::k_weak:
return "STB_WEAK";
default:
return boost::lexical_cast<std::string>((p_info >> 4) & 0x0f);
}
case elf::symbol::k_local:
str = "STB_LOCAL";
case elf::symbol::k_global:
str = "STB_GLOBAL";
case elf::symbol::k_weak:
str = "STB_WEAK";
default:
str = boost::lexical_cast<std::string>((p_info >> 4) & 0x0f);
}
return str;
}

std::string getSymType(boost::uint8_t p_info)
std::string getSymType(boost::uint8_t p_info)
{
std::string str;
switch (p_info & 0x0f)
{
switch (p_info & 0x0f)
{
case elf::symbol::k_notype:
return "STT_NOTYPE";
case elf::symbol::k_object:
return "STT_OBJECT";
case elf::symbol::k_function:
return "STT_FUNC";
case elf::symbol::k_section:
return "STT_SECTION";
case elf::symbol::k_file:
return "STT_FILE";
case elf::symbol::k_common:
return "STT_COMMON";
case elf::symbol::k_tls:
return "STT_TLS";
default:
return boost::lexical_cast<std::string>(p_info & 0x0f);
}
case elf::symbol::k_notype:
str = "STT_NOTYPE";
case elf::symbol::k_object:
str = "STT_OBJECT";
case elf::symbol::k_function:
str = "STT_FUNC";
case elf::symbol::k_section:
str = "STT_SECTION";
case elf::symbol::k_file:
str = "STT_FILE";
case elf::symbol::k_common:
str = "STT_COMMON";
case elf::symbol::k_tls:
str = "STT_TLS";
default:
str = boost::lexical_cast<std::string>(p_info & 0x0f);
}

return str;
}

AbstractSymbol::AbstractSymbol(const char* p_data, boost::uint32_t p_offset,
bool p_is64, bool p_isLE) :
m_symbol32(),
m_symbol64(),
m_name(),
m_is64(p_is64),
m_isLE(p_isLE)
AbstractSymbol::AbstractSymbol(const char *p_data, boost::uint32_t p_offset,
bool p_is64, bool p_isLE) : m_symbol32(),
m_symbol64(),
m_name(),
m_is64(p_is64),
m_isLE(p_isLE)
{
if (m_is64)
{
m_symbol64 = reinterpret_cast<const elf::symbol::symtable_entry64*>(p_data + p_offset);
}
m_symbol64 = reinterpret_cast<const elf::symbol::symtable_entry64 *>(p_data + p_offset);
else
{
m_symbol32 = reinterpret_cast<const elf::symbol::symtable_entry32*>(p_data + p_offset);
}
m_symbol32 = reinterpret_cast<const elf::symbol::symtable_entry32 *>(p_data + p_offset);

std::stringstream value;
value << "0x" << std::hex << getValue();
m_name.assign(value.str());
}

AbstractSymbol::AbstractSymbol(const AbstractSymbol& p_rhs) :
m_symbol32(p_rhs.m_symbol32),
m_symbol64(p_rhs.m_symbol64),
m_name(p_rhs.m_name),
m_is64(p_rhs.m_is64),
m_isLE(p_rhs.m_isLE)
AbstractSymbol::AbstractSymbol(const AbstractSymbol &p_rhs)
{
m_symbol32 = p_rhs.m_symbol32;
m_symbol64 = p_rhs.m_symbol64;
m_name = p_rhs.m_name;
m_is64 = p_rhs.m_is64;
m_isLE = p_rhs.m_isLE;
}

AbstractSymbol::~AbstractSymbol()
Expand All @@ -91,29 +86,35 @@ AbstractSymbol::~AbstractSymbol()

boost::uint32_t AbstractSymbol::getStructSize() const
{
boost::uint32_t size;
if (m_is64)
{
return sizeof(elf::symbol::symtable_entry64);
}
return sizeof(elf::symbol::symtable_entry32);
size = sizeof(elf::symbol::symtable_entry64);
else
size = sizeof(elf::symbol::symtable_entry32);

return size;
}

boost::uint8_t AbstractSymbol::getType() const
{
boost::uint8_t type;
if (m_is64)
{
return m_symbol64->m_info & 0x0f;
}
return m_symbol32->m_info & 0x0f;
type = m_symbol64->m_info & 0x0f;
else
type = m_symbol32->m_info & 0x0f;

return type;
}

boost::uint8_t AbstractSymbol::getInfo() const
{
boost::uint8_t info;
if (m_is64)
{
return m_symbol64->m_info;
}
return m_symbol32->m_info;
info = m_symbol64->m_info;
else
info = m_symbol32->m_info;

return info;
}

std::string AbstractSymbol::getTypeName() const
Expand All @@ -128,40 +129,44 @@ std::string AbstractSymbol::getBinding() const

boost::uint64_t AbstractSymbol::getValue() const
{
boost::uint64_t value;
if (m_is64)
{
return m_isLE ? m_symbol64->m_address : htobe64(m_symbol64->m_address);
}
return m_isLE ? m_symbol32->m_address : ntohl(m_symbol32->m_address);
value = m_isLE ? m_symbol64->m_address : htobe64(m_symbol64->m_address);
else
value = m_isLE ? m_symbol32->m_address : ntohl(m_symbol32->m_address);

return value;
}

boost::uint32_t AbstractSymbol::getNameIndex() const
{
boost::uint32_t name;
if (m_is64)
{
return m_isLE ? m_symbol64->m_name : ntohl(m_symbol64->m_name);
}
return m_isLE ? m_symbol32->m_name : ntohl(m_symbol32->m_name);
name = m_isLE ? m_symbol64->m_name : ntohl(m_symbol64->m_name);
else
name = m_isLE ? m_symbol32->m_name : ntohl(m_symbol32->m_name);

return name;
}

boost::uint16_t AbstractSymbol::getSectionIndex() const
{
boost::uint16_t index;
if (m_is64)
{
return m_isLE ? m_symbol64->m_shndx : ntohs(m_symbol64->m_shndx);
}
return m_isLE ? m_symbol32->m_shndx : ntohs(m_symbol32->m_shndx);
index = m_isLE ? m_symbol64->m_shndx : ntohs(m_symbol64->m_shndx);
else
index = m_isLE ? m_symbol32->m_shndx : ntohs(m_symbol32->m_shndx);

return index;
}

const std::string& AbstractSymbol::getName() const
const std::string &AbstractSymbol::getName() const
{
return m_name;
}

void AbstractSymbol::setName(const std::string& p_name)
void AbstractSymbol::setName(const std::string &p_name)
{
if (!p_name.empty())
{
m_name.assign(p_name);
}
}
}
6 changes: 2 additions & 4 deletions src/dynamicsection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
#include <boost/foreach.hpp>
#include <boost/assign.hpp>

#ifdef __APPLE__
#include "endian.h"
#elif WINDOWS
#include "endian.h"
#if WINDOWS || __APPLE__
#include "endian.hpp"
#else
#include <arpa/inet.h>
#endif
Expand Down
7 changes: 3 additions & 4 deletions src/elfparser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
#include "elfparser.hpp"
#include "structures/elfheader.hpp"
#include "datastructures/search_value.hpp"
#include "../lib/hash-lib/md5.h"
#include "../lib/hash-lib/sha256.h"
#include "../lib/hash-lib/md5.h"
#include "../lib/hash-lib/sha1.h"
#include "../lib/hash-lib/md5.hpp"
#include "../lib/hash-lib/sha256.hpp"
#include "../lib/hash-lib/sha1.hpp"

#include <map>
#include <set>
Expand Down
8 changes: 3 additions & 5 deletions src/initarray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
#include <sstream>
#include <boost/foreach.hpp>

#ifdef __APPLE__
#include "endian.h"
#elif WINDOWS
#include "endian.h"
#if WINDOWS || __APPLE__
#include "endian.hpp"
#else
#include <arpa/inet.h>
#endif
Expand Down Expand Up @@ -73,4 +71,4 @@ std::string InitArray::printToStd() const
}

return returnValue.str();
}
}
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void do_parsing(const std::string &p_fileName, bool p_printReasons,
if (!parser.getFamily().empty())
{
std::cout <<"[Family: " << parser.getFamily() << "]" << std::endl << std::endl;
std::cout <<"[SHA56: " << std::hex << parser.getSha256() << " ]" << std::endl;
std::cout <<"[SHA256: " << std::hex << parser.getSha256() << " ]" << std::endl;
std::cout <<"[SHA1: " << std::hex << parser.getSha1() << " ]" << std::endl;
std::cout <<"[MD5: " << std::hex << parser.getMD5() << " ]" << std::endl << std::endl;
}
Expand Down
23 changes: 7 additions & 16 deletions src/programheaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
#include <sstream>

ProgramHeaders::ProgramHeaders() : m_programHeaders()
{ }
{
}

ProgramHeaders::~ProgramHeaders()
{ }
{
}

void ProgramHeaders::setHeaders(const char *p_data, boost::uint16_t p_count,
boost::uint16_t p_size, bool p_is64, bool p_isLE)
Expand Down Expand Up @@ -59,28 +61,17 @@ void ProgramHeaders::evaluate(std::vector<std::pair<boost::int32_t, std::string>

BOOST_FOREACH (const AbstractProgramHeader &header, m_programHeaders)
{
switch (header.getType())
if (header.getType() == elf::k_pload)
{
case elf::k_pload:
++load_count;
found_load = true;
break;
default:
break;
}
++entry_count;
}

if (load_count > 2)
{
p_reasons.push_back(std::make_pair(30, std::string("Found 2+ PT_LOAD. Possible post-compilation addition of code (cryptor or packer)")));
}

if (entry_count > 0)
{
if (!found_load)
{
p_reasons.push_back(std::make_pair(5, std::string("Didn't find PT_LOAD in the program headers")));
}
}
if (entry_count > 0 && !found_load)
p_reasons.push_back(std::make_pair(5, std::string("Didn't find PT_LOAD in the program headers")));
}
Loading

0 comments on commit 629a19e

Please sign in to comment.