Skip to content

Commit a64c9e8

Browse files
committed
Linux Kernel exploitation Tutorial.
1 parent 6a490f9 commit a64c9e8

File tree

90 files changed

+3695
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+3695
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
obj-m += sample.o
2+
3+
all:
4+
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
5+
6+
clean:
7+
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <linux/module.h> /* Needed by all modules */
2+
#include <linux/kernel.h> /* Needed for KERN_INFO */
3+
4+
int init_module(void) {
5+
printk(KERN_INFO "Hello world - Lazenca0x0.\n");
6+
return 0;
7+
}
8+
9+
void cleanup_module(void) {
10+
printk(KERN_INFO "Goodbye world - Lazenca0x0\n.");
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
obj-m := chardev.o
2+
3+
all:
4+
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
5+
clean:
6+
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <linux/init.h>
2+
#include <linux/module.h>
3+
#include <linux/types.h>
4+
#include <linux/kernel.h>
5+
#include <linux/fs.h>
6+
#include <linux/cdev.h>
7+
#include <linux/sched.h>
8+
#include <linux/device.h>
9+
#include <linux/slab.h>
10+
#include <asm/current.h>
11+
#include <linux/uaccess.h>
12+
13+
#define DEVICE_NAME "chardev"
14+
#define DEVICE_FILE_NAME "chardev"
15+
#define MAJOR_NUM 100
16+
17+
static int chardev_open(struct inode *inode, struct file *file)
18+
{
19+
printk("chardev_open");
20+
return 0;
21+
}
22+
23+
struct file_operations chardev_fops = {
24+
.open = chardev_open,
25+
};
26+
27+
static int chardev_init(void)
28+
{
29+
int ret_val;
30+
ret_val = register_chrdev(MAJOR_NUM, DEVICE_NAME, &chardev_fops);
31+
32+
if (ret_val < 0) {
33+
printk(KERN_ALERT "%s failed with %d\n",
34+
"Sorry, registering the character device ", ret_val);
35+
return ret_val;
36+
}
37+
38+
printk(KERN_INFO "%s The major device number is %d.\n",
39+
"Registeration is a success", MAJOR_NUM);
40+
printk(KERN_INFO "If you want to talk to the device driver,\n");
41+
printk(KERN_INFO "you'll have to create a device file. \n");
42+
printk(KERN_INFO "We suggest you use:\n");
43+
printk(KERN_INFO "mknod %s c %d 0\n", DEVICE_FILE_NAME, MAJOR_NUM);
44+
printk(KERN_INFO "The device file name is important, because\n");
45+
printk(KERN_INFO "the ioctl program assumes that's the\n");
46+
printk(KERN_INFO "file you'll use.\n");
47+
48+
return 0;
49+
}
50+
51+
static void chardev_exit(void)
52+
{
53+
unregister_chrdev(MAJOR_NUM, DEVICE_NAME);
54+
}
55+
56+
module_init(chardev_init);
57+
module_exit(chardev_exit);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
obj-m := chardev.o
2+
3+
all:
4+
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
5+
clean:
6+
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#include <linux/init.h>
2+
#include <linux/module.h>
3+
#include <linux/types.h>
4+
#include <linux/kernel.h>
5+
#include <linux/fs.h>
6+
#include <linux/cdev.h>
7+
#include <linux/sched.h>
8+
#include <linux/device.h>
9+
#include <linux/slab.h>
10+
#include <asm/current.h>
11+
#include <linux/uaccess.h>
12+
13+
MODULE_LICENSE("Dual BSD/GPL");
14+
15+
#define DRIVER_NAME "chardev"
16+
#define BUFFER_SIZE 256
17+
18+
static const unsigned int MINOR_BASE = 0;
19+
static const unsigned int MINOR_NUM = 2;
20+
static unsigned int chardev_major;
21+
static struct cdev chardev_cdev;
22+
static struct class *chardev_class = NULL;
23+
24+
static int chardev_open(struct inode *, struct file *);
25+
static int chardev_release(struct inode *, struct file *);
26+
static ssize_t chardev_read(struct file *, char *, size_t, loff_t *);
27+
static ssize_t chardev_write(struct file *, const char *, size_t, loff_t *);
28+
29+
struct file_operations chardev_fops = {
30+
.open = chardev_open,
31+
.release = chardev_release,
32+
.read = chardev_read,
33+
.write = chardev_write,
34+
};
35+
36+
struct data {
37+
unsigned char buffer[BUFFER_SIZE];
38+
};
39+
40+
static int chardev_init(void)
41+
{
42+
int alloc_ret = 0;
43+
int cdev_err = 0;
44+
int minor;
45+
dev_t dev;
46+
47+
printk("The chardev_init() function has been called.");
48+
49+
alloc_ret = alloc_chrdev_region(&dev, MINOR_BASE, MINOR_NUM, DRIVER_NAME);
50+
if (alloc_ret != 0) {
51+
printk(KERN_ERR "alloc_chrdev_region = %d\n", alloc_ret);
52+
return -1;
53+
}
54+
//Get the major number value in dev.
55+
chardev_major = MAJOR(dev);
56+
dev = MKDEV(chardev_major, MINOR_BASE);
57+
58+
//initialize a cdev structure
59+
cdev_init(&chardev_cdev, &chardev_fops);
60+
chardev_cdev.owner = THIS_MODULE;
61+
62+
//add a char device to the system
63+
cdev_err = cdev_add(&chardev_cdev, dev, MINOR_NUM);
64+
if (cdev_err != 0) {
65+
printk(KERN_ERR "cdev_add = %d\n", alloc_ret);
66+
unregister_chrdev_region(dev, MINOR_NUM);
67+
return -1;
68+
}
69+
70+
chardev_class = class_create(THIS_MODULE, "chardev");
71+
if (IS_ERR(chardev_class)) {
72+
printk(KERN_ERR "class_create\n");
73+
cdev_del(&chardev_cdev);
74+
unregister_chrdev_region(dev, MINOR_NUM);
75+
return -1;
76+
}
77+
78+
for (minor = MINOR_BASE; minor < MINOR_BASE + MINOR_NUM; minor++) {
79+
device_create(chardev_class, NULL, MKDEV(chardev_major, minor), NULL, "chardev%d", minor);
80+
}
81+
82+
return 0;
83+
}
84+
85+
static void chardev_exit(void)
86+
{
87+
int minor;
88+
dev_t dev = MKDEV(chardev_major, MINOR_BASE);
89+
90+
printk("The chardev_exit() function has been called.");
91+
92+
for (minor = MINOR_BASE; minor < MINOR_BASE + MINOR_NUM; minor++) {
93+
device_destroy(chardev_class, MKDEV(chardev_major, minor));
94+
}
95+
96+
class_destroy(chardev_class);
97+
cdev_del(&chardev_cdev);
98+
unregister_chrdev_region(dev, MINOR_NUM);
99+
}
100+
101+
static int chardev_open(struct inode *inode, struct file *file)
102+
{
103+
char *str = "helloworld";
104+
int ret;
105+
106+
struct data *p = kmalloc(sizeof(struct data), GFP_KERNEL);
107+
108+
printk("The chardev_open() function has been called.");
109+
110+
if (p == NULL) {
111+
printk(KERN_ERR "kmalloc - Null");
112+
return -ENOMEM;
113+
}
114+
115+
ret = strlcpy(p->buffer, str, sizeof(p->buffer));
116+
if(ret > strlen(str)){
117+
printk(KERN_ERR "strlcpy - too long (%d)",ret);
118+
}
119+
120+
file->private_data = p;
121+
return 0;
122+
}
123+
124+
static int chardev_release(struct inode *inode, struct file *file)
125+
{
126+
printk("The chardev_release() function has been called.");
127+
if (file->private_data) {
128+
kfree(file->private_data);
129+
file->private_data = NULL;
130+
}
131+
return 0;
132+
}
133+
134+
static ssize_t chardev_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)
135+
{
136+
struct data *p = filp->private_data;
137+
138+
printk("The chardev_write() function has been called.");
139+
printk("Before calling the copy_to_user() function : %p, %s",p->buffer,p->buffer);
140+
if (copy_from_user(p->buffer, buf, count) != 0) {
141+
return -EFAULT;
142+
}
143+
printk("After calling the copy_to_user() function : %p, %s",p->buffer,p->buffer);
144+
return count;
145+
}
146+
147+
static ssize_t chardev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
148+
{
149+
struct data *p = filp->private_data;
150+
151+
printk("The chardev_read() function has been called.");
152+
153+
if(count > BUFFER_SIZE){
154+
count = BUFFER_SIZE;
155+
}
156+
157+
if (copy_to_user(buf, p->buffer, count) != 0) {
158+
return -EFAULT;
159+
}
160+
161+
return count;
162+
}
163+
164+
module_init(chardev_init);
165+
module_exit(chardev_exit);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <fcntl.h>
4+
#include <unistd.h>
5+
#include <errno.h>
6+
7+
int main()
8+
{
9+
static char buff[256];
10+
int fd0_A, fd0_B, fd1_A;
11+
12+
if ((fd0_A = open("/dev/chardev0", O_RDWR)) < 0) perror("open");
13+
if ((fd0_B = open("/dev/chardev0", O_RDWR)) < 0) perror("open");
14+
if ((fd1_A = open("/dev/chardev1", O_RDWR)) < 0) perror("open");
15+
16+
if (write(fd0_A, "0_A", 4) < 0) perror("write");
17+
if (write(fd0_B, "0_B", 4) < 0) perror("write");
18+
if (write(fd1_A, "1_A", 4) < 0) perror("write");
19+
20+
if (read(fd0_A, buff, 4) < 0) perror("read");
21+
printf("%s\n", buff);
22+
if (read(fd0_B, buff, 4) < 0) perror("read");
23+
printf("%s\n", buff);
24+
if (read(fd1_A, buff, 4) < 0) perror("read");
25+
printf("%s\n", buff);
26+
27+
if (close(fd0_A) != 0) perror("close");
28+
if (close(fd0_B) != 0) perror("close");
29+
if (close(fd1_A) != 0) perror("close");
30+
31+
return 0;
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
obj-m := chardev.o
2+
MY_CFLAGS += -g -DDEBUG
3+
ccflags-y += ${MY_CFLAGS}
4+
CC += ${MY_CFLAGS}
5+
6+
all:
7+
make -C /lib/modules/4.18.0/build M=$(shell pwd) modules
8+
9+
debug:
10+
make -C /lib/modules/4.18.0/build M=$(PWD) modules EXTRA_CFLAGS="$(MY_CFLAGS)"
11+
12+
clean:
13+
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean
14+

0 commit comments

Comments
 (0)