More fun with SEH

Just to demonstrate what kind of fancy stuff SEH provides, I wrote this small demo code for win32/x86. Idea of this code is to show how to modify the way other frames handle their exceptions. I don't see much applications for this technology, other than explanation how SEH works. If you understand how this demo does what it does - you likely understand SEHs pretty well.
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
void handler() {
  printf("intercepted");
  getchar();
  exit(1);
}

void install(int where) {
  void\*\* head;
  __asm {
    mov eax, fs:0
    mov head, eax
  }
  int n = 0;
  while (head != (void\*\*)-1) {
    if (n++ == where) {
      \*(head+1) = &handler;
    }
    head = (void\*\*)\*head;
  }
  return;
}

void foo(int r) {
  if (r == 0) {
    install(3);
    return;
  }
  __try {
    foo(r-1);
    if (r == 3) {
      \*(char\*)0 = 0;
    }
  } __except(1) {
    printf("regular\\n");
  }
}

int _tmain(int argc, _TCHAR\* argv[])
{
  foo(10);

  getchar();

  return 0;
}
Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

nike

Search

Categories
Archives
« April 2015
SunMonTueWedThuFriSat
   
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  
       
Today