简体   繁体   English

缓冲区溢出攻击后如何“干净”地终止程序

[英]How to “cleanly” terminate the program after buffer overflow attack

I'm studying buffer overflow, and I'm trying to jump to the function 'confused' and then print out "done" at the end of main by performing buffer overflow. 我正在研究缓冲区溢出,并且试图跳到“ confused”功能,然后通过执行缓冲区溢出在main的末尾打印“完成”。

#include<stdio.h>
#include<stdlib.h>

int i, n;
void confused(int i) {
  printf("**Who called me? Why am I here?? *** %x\n ", i);
  ;
}

void shell_call(char *c) {
  printf(" ***Now calling \"%s\" shell command *** \n", c);
  system(c);
}

void victim_func(){
  int a[4];
  printf("\nEnter n:  ");  scanf("%d",&n);
  printf("~~~~~~~~~~~~~ values and address of n locations ~~~~~~~~~~");
  for (i = 0;i <n ;i++)
    printf ("\n a[%d] = %x, address = %x", i, a[i], &a[i]);
  printf("\nEnter %d HEX Values \n", n);

  // Buffer Overflow vulnerability HERE!

  for (i=0;i<n;i++)  scanf("%x",&a[i]);
    printf("Done reading junk numbers\n")
}

int main() {
  printf("\n ~~~~~~~~~~~~~~~~~ Info Menu ~~~~~~~~~~~~");
  printf("\n addrss of main %x", main);
  printf("\n addrss of shell_cal %x", shell_call);
  printf("\n addrss of confused %x", confused);
  victim_func();
  printf("\n done");
  return 0;
}

What I did is I put 7 for n, and for 6th hex value I inserted the address of confused and for 7th the address of printf in main. 我所做的是,我在n中放入7,在第16个十六进制值中我插入了混淆的地址,在第7个中插入了printf的地址。 It successfully prints out "done" after the confused function, but the program goes back to the start of main. 混淆功能后,它成功打印出“完成”,但是程序返回到main的开头。 I thought the program would terminate after printing out "done". 我认为该程序将在打印出“完成”后终止。

I just wonder if I did something wrong, or it is the way it should do. 我只是想知道我做错了什么,还是应该这样做。

You can always call exit() in your shell code to terminate the program. 您始终可以在外壳程序代码中调用exit()来终止程序。 However, you can't do it using system(), because system() will create a child process which always ultimately return to it parent. 但是,您不能使用system()来完成此操作,因为system()将创建一个子进程,该子进程最终最终将返回其父进程。 You need to directly call exit() using assembly. 您需要使用程序集直接调用exit()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM