Commit 67189f6e authored by Chu's avatar Chu

remove exception

parent f69ba366
......@@ -6,6 +6,7 @@
#include <unistd.h>
#include <cstring>
#include <sstream>
#include <stdexcept>
Elf::~Elf()
......@@ -95,5 +96,7 @@ std::size_t Elf::get_func_size(std::size_t address)
}
}
throw std::runtime_error(std::to_string(address) + " not found in " + path_);
std::ostringstream stream;
stream << "0x" << std::hex << address << " not found in " << path_;
throw std::runtime_error(stream.str());
}
#include <sys/types.h>
#include <iostream>
#include <string>
#include <tuple>
#include "elf.h"
#include "process.h"
......@@ -18,65 +15,28 @@ int main(int argc, char *argv[])
}
// process
pid_t pid = 0;
try
{
pid = std::stoi(argv[1]);
}
catch (const std::exception &)
{
std::cerr << "[!] invalid pid " << argv[1] << std::endl;
return 1;
}
Process process(pid);
Process process(std::stoi(argv[1]));
// find libc.so in process
std::string libc_path;
std::size_t libc_base;
try
{
std::tie(libc_path, libc_base) = process.find_libc();
}
catch (const std::exception &e)
{
std::cerr << "[!] " << e.what() << std::endl;
return 1;
}
auto [libc_path, libc_base] = process.find_libc();
std::cout << "[+] libc: " << libc_path << " 0x" << std::hex << libc_base << std::endl;
// find __libc_dlopen_mode in libc.so
Elf libc(libc_path);
libc.set_base(libc_base);
std::size_t libc_dlopen_mode;
try
{
libc_dlopen_mode = libc.find_symbol_address_by_name("__libc_dlopen_mode");
}
catch (const std::exception &e)
{
std::cerr << "[!] " << e.what() << std::endl;
return 1;
}
auto libc_dlopen_mode = libc.find_symbol_address_by_name("__libc_dlopen_mode");
std::cout << "[+] __libc_dlopen_mode: 0x" << libc_dlopen_mode << std::endl;
// get process' entry
std::size_t entry;
try
{
entry = process.get_entry();
}
catch (const std::exception &e)
{
std::cerr << "[!] " << e.what() << std::endl;
return 1;
}
auto entry = process.get_entry();
std::cout << "[+] entry: 0x" << entry << std::endl;
// copy shellcode to process' entry
auto call_libc_dlopen_mode_addr = reinterpret_cast<std::size_t>(&call_libc_dlopen_mode);
auto call_libc_dlopen_mode_size = Elf(argv[0]).get_func_size(call_libc_dlopen_mode_addr);
std::cout << "[+] shellcode: 0x" << call_libc_dlopen_mode_addr << " " << std::dec << call_libc_dlopen_mode_size
<< std::endl;
auto call_libc_dlopen_mode_addr = &call_libc_dlopen_mode;
auto call_libc_dlopen_mode_size =
Elf(argv[0]).get_func_size(reinterpret_cast<std::size_t>(call_libc_dlopen_mode_addr));
std::cout << "[+] shellcode: 0x" << reinterpret_cast<std::size_t>(call_libc_dlopen_mode_addr) << " " << std::dec
<< call_libc_dlopen_mode_size << std::endl;
// call shellcode
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment