forked from oshrat/csd162ass2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KBlockerUM.c
133 lines (113 loc) · 3.16 KB
/
KBlockerUM.c
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
/*
Source: http://stackoverflow.com/questions/3299386/how-to-use-netlink-socket-to-communicate-with-a-kernel-module/3334782#3334782
*/
#include <sys/socket.h>
#include <linux/netlink.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/sha.h>
#include <sys/stat.h>
#define NETLINK_USER 31
#define MAX_PAYLOAD 1024 /* maximum payload size*/
struct sockaddr_nl src_addr, dest_addr;
struct nlmsghdr *nlh = NULL;
struct iovec iov;
int sock_fd;
struct msghdr msg;
char message[MAX_PAYLOAD] = {0};
char* file_content(FILE* file, int size)
{
int c, i = 0;
char* buffer = malloc(size);
while((c = fgetc(file)) != EOF)
{
buffer[i] = c;
i++;
}
return buffer;
}
void calculate_sha256(char *string, char outputBuffer[65], int size)
{
unsigned char hash[32];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, string, size);
SHA256_Final(hash, &sha256);
int i = 0;
for(i = 0; i < 32; i++)
{
sprintf(outputBuffer + (i * 2), "%02x", (unsigned char)hash[i]);
}
outputBuffer[64] = 0;
}
void sha_file()
{
FILE *fp;
char *str;
int i = 0;
fp = fopen(message,"r");
if(fp == NULL)
{
printf("Error in opening file\n");
return;
}
struct stat st;
stat(message, &st);
int size = st.st_size;
str = file_content(fp, size);
fclose(fp);
//call sha
static unsigned char buffer[65];
calculate_sha256(str, buffer, size);
printf("sha in user = %s\n", buffer);
for( i = 0 ; i < (int)strlen(message) ; ++i)
{
message[i] = '\0';
}
strcpy(NLMSG_DATA(nlh), message); // this is for putting 0 in all nlh buffer
strcpy(NLMSG_DATA(nlh), buffer);
}
int main()
{
sock_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_USER);
if (sock_fd < 0)
{
printf("ERROR: Can not connect to kernel.\n");
return -1;
}
memset(&src_addr, 0, sizeof(src_addr));
src_addr.nl_family = AF_NETLINK;
src_addr.nl_pid = getpid(); // self pid
bind(sock_fd, (struct sockaddr *)&src_addr, sizeof(src_addr));
memset(&dest_addr, 0, sizeof(dest_addr));
memset(&dest_addr, 0, sizeof(dest_addr));
dest_addr.nl_family = AF_NETLINK;
dest_addr.nl_pid = 0; // For Linux Kernel
dest_addr.nl_groups = 0; // unicast
nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));
memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));
nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
nlh->nlmsg_pid = getpid();
nlh->nlmsg_flags = 0;
strcpy(NLMSG_DATA(nlh), "Hello from user");
iov.iov_base = (void *)nlh;
iov.iov_len = nlh->nlmsg_len;
msg.msg_name = (void *)&dest_addr;
msg.msg_namelen = sizeof(dest_addr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
// first time calling the kernel the kernel to give pid
sendmsg(sock_fd, &msg, 0);
while(1)
{
recvmsg(sock_fd, &msg, 0);
strcpy(message, NLMSG_DATA(nlh));
printf("USER GOT:%s\n", message);
if(strncmp(message, "./unload.sh", (int)strlen("./unload.sh")) == 0)
break;
sha_file();
sendmsg(sock_fd, &msg, 0);
}
close(sock_fd);
}