Skip to content

Instantly share code, notes, and snippets.

@ntddk
Last active October 10, 2015 22:38
Show Gist options
  • Save ntddk/3761740 to your computer and use it in GitHub Desktop.
Save ntddk/3761740 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <ucontext.h>
char preserve;
int hoge()
{
printf("HOGEHOGE\n");
}
void my_sa(int sig, siginfo_t *si, void *arg)
{
char *targ;
ucontext_t *uc = arg;
mcontext_t *mc = &uc->uc_mcontext;
printf("TRAPED\n");
targ = hoge;
*targ = preserve;
printf("%x %x %x %x\n",hoge, mc->mc_eip, mc->mc_esp, &targ);
mc->mc_eip--;
}
int main()
{
struct sigaction sa;
int oprot;
stack_t st;
char *targ;
sa.sa_sigaction = my_sa;
sa.sa_flags = SA_SIGINFO;
bzero(&sa.sa_mask , sizeof(sigset_t));
sigaction(SIGTRAP, &sa, NULL);
targ = hoge;
// メモリ保護設定変更
mprotect(hoge, 4, PROT_READ|PROT_WRITE|PROT_EXEC);
targ = (char *) hoge;
preserve = *targ;
printf("%x\n", preserve);
*targ = 0xcc;
// ブレークポイントを挿入してhoge
// mprotect(hoge, 4, PROT_READ|PROT_EXEC);
hoge();
// FIXME: セグメンテーション違反回避
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment