Skip to content

Commit

Permalink
Documentation Added
Browse files Browse the repository at this point in the history
  • Loading branch information
pinwhell committed Sep 15, 2023
1 parent a6e71f1 commit 9dbfb5c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
86 changes: 86 additions & 0 deletions src/jni/ElfUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,108 @@
#include <elf.h>
#include <string>

/**
* @brief Holds the ELF File Mapping
*/
union ElfPack {
/**
* @brief Pointer to ELF Header
*/
Elf32_Ehdr* header;

/**
* @brief Pointer to ELF64 Header
*/
Elf64_Ehdr* header64;

/**
* @brief ELF File mapping base address
*/
uintptr_t base;

/**
* @brief Pointer to the ELF Mapping
*/
void* baseV;

/**
* @brief Lazy int version of the Mapping Pointer, to easily do checks
*/
int res;
};

/**
* @brief Initializes the ELF Library, Notify Callback, Cleanup, Frees the ELF library
* @returns true if all the operations was sucessfully, false otherwise
*/
bool ElfOpen(const std::string& fullModulePath, std::function<void(ElfPack libMap)> callback);

/**
* @brief Check if ELF file is 64 bits.
* @returns true if ELF File is 64 bits, false otherwise
*/
bool ElfPeekIs64(const std::string& fullModulePath, bool& outResult);

/**
* @brief Get a ELF Section by its given Index.
* @param sectionIdx: the given section index
* @returns a pointer to a section header if valid, nullptr otherwise
*/
Elf32_Shdr* ElfSectionByIndex(ElfPack libMap, unsigned int sectionIdx);

/**
* @brief Traverses all sections within the ELF File
* @param callback: will be reported, all the given sections
*/
void ElfForEachSection(ElfPack libMap, std::function<bool(Elf32_Shdr* pCurrentSection)> callback);

/**
* @brief Lookup an ELF Section by its given type
* @param sectionType: ELF Section Type
* @returns A pointer to the section if it exists; nullptr otherwise.
*/
Elf32_Shdr* ElfLookupSectionByType(ElfPack libMap, uint32_t sectionType);

/**
* @brief Retrieve the ELF Section Headers Name Blob (shstr) Entry.
* @returns A pointer to the char blob entry if exist; nullptr otherwise
*/
const char* ElfGetSectionHeadersStringBlob(ElfPack libMap);

/**
* @brief Retrieve ELF Section name
* @param sectionHdr: Pointer to ELF Section Header
* @returns A Pointer to the section name if exist; nullptr otherwise.
*/
const char* ElfGetSectionName(ElfPack libMap, Elf32_Shdr* sectionHdr);

/**
* @brief Lookup a ELF Header by its name
* @param sectionName: Name of the section (ex: ".rodata", ".text" ...)
* @returns A Pointer to the ELF Section if found; nullptr otherwise.
*/
Elf32_Shdr* ElfLookupSectionByName(ElfPack libMap, const std::string& sectionName);

/**
* @brief Retrieve any available Symbol Table ELF Section.
* @returns A pointer to the Symbol Table ELF Section if exist; nullptr otherwise;
* @note This function first searches for an SHT_SYMTAB type, and if none is found,
* it searches for an SHT_DYNSYM type.
*/
Elf32_Shdr* ElfGetSymbolSection(ElfPack libMap);

/**
* @brief Traverse the symbol table.
* @param callback: A callback, for each symbol found, this callback will be invocated with the actual symbol & its name.
* @returns true if a symbol table to traverse was found, nullptr otherwise.
*/
bool ElfForEachSymbol(ElfPack libMap, std::function<bool(Elf32_Sym* pCurrentSym, const char* pCurrSymName)> callback);

/**
* @brief Lookup a symbol by its name.
* @param symbolName: The Name of the symbol to look for.
* @param outSymbolOff: (optional) A Pointer to variable where resulting relative displacement of the symbol will be saved if found.
* @returns true if the symbol was found, false otherwise.
* @note Symbol lookup may fail for various reasons, such as the absence of a symbol table or the symbol not being present in the symbol table.
*/
bool ElfLookupSymbol(ElfPack libMap, const std::string& symbolName, uint64_t* outSymbolOff = nullptr);
2 changes: 1 addition & 1 deletion src/jni/LinuxProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ bool LinuxProcess::LoadToMemoryAndHook(uintptr_t targetSrc, void* targetDst, uin
if(!localDstSize)
return false;

DstAddrinTargetMemory = FindCodeCave(localDstSize, EXECUTE_READ);
DstAddrinTargetMemory = FindCodeCave(localDstSize, PROT_READ);
if(!DstAddrinTargetMemory)
return false;

Expand Down
10 changes: 8 additions & 2 deletions src/jni/LinuxProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include <vector>
#include <sys/mman.h>

/**
* @brief Describes a memory segment (ex. a row from the proc/self/maps)
*/
struct SegmentInfo{
/**
* @brief full segment name, if is present
Expand Down Expand Up @@ -33,6 +36,9 @@ struct SegmentInfo{
uintptr_t size;
};

/**
* @brief Describes a linux process
*/
class LinuxProcess
{
private:
Expand Down Expand Up @@ -72,7 +78,7 @@ class LinuxProcess
static int FindPid(const char* procName);

/**
* @brief read the memory in target address
* @brief read a T item from target address
*
* @tparam T target object type in target memory
* @param addr it the target address
Expand All @@ -91,7 +97,7 @@ class LinuxProcess
uintptr_t FindDMAddy(uintptr_t base, std::vector<uintptr_t> offsets);

/**
* @brief write the memory in target address
* @brief write a T item to given addr in target memory
*
* @tparam T target object type to be writed in target process memory
* @param addr target address in process target memory
Expand Down

0 comments on commit 9dbfb5c

Please sign in to comment.