/
udpBlocking.c
170 lines (161 loc) · 6.91 KB
/
udpBlocking.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
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
167
168
169
170
/*
Copyright (c) 2013, 2014
Authors: Ryan K.L. Ko, Alan Y.S. Tan, Ting Gao
CROW - Cybersecurity Researchers of Waikato
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the organization nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ANTHONY M. BLAKE BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/kprobes.h>
#include <linux/socket.h>
#include <linux/netlink.h>
#include <linux/skbuff.h>
#include <net/sock.h>
#include <linux/kallsyms.h>
#define NETLINK_USER 31
struct sock *nl_sk_udp = NULL;
int pid=0;
bool gotPid=false;
extern void letItResumeUdp(bool destory);
extern void gotUserOrNotUdp(bool found);
static void printmsg(struct msghdr *msg){
int i;
printk("start printing msg\n");
for (i=0;i<(msg->msg_iov[0]).iov_len;i=i+1){
printk("%c\n",((char *)(msg->msg_iov[0]).iov_base)[i]);
}
printk("finish printing msg\n");
}
static int my_udp_sendmsg(struct kiocb *iocb,
struct sock *sk,
struct msghdr *msg,
size_t len)
{
struct sk_buff *skb_out;
struct nlmsghdr *nlh;
int length;
int res=-1;
printk("udp_sendmsg from %s,%d\n", current->comm,(msg->msg_iov[0]).iov_len);
length = (int)(msg->msg_iov[0].iov_len);
skb_out = nlmsg_new(length,0);
if(!skb_out)
{
printk(KERN_ERR "Failed to allocate new skb\n");
jprobe_return();
}
nlh=nlmsg_put(skb_out,0,0,NLMSG_DONE,length,0);
NETLINK_CB(skb_out).dst_group = 0; /* not in mcast group */
memcpy(nlmsg_data(nlh),(msg->msg_iov[0]).iov_base,length);
if (gotPid){
res=nlmsg_unicast(nl_sk_udp,skb_out,pid);
if(res<0){
printk(KERN_INFO "Error while sending bak to user\n");
gotPid=false;
gotUserOrNotUdp(false);
}else {
printk("send to user\n");
}
}else {
printk("no pid got\n");
gotUserOrNotUdp(false);
}
jprobe_return();
return 0;
}
static struct jprobe my_jprobe_udp = {
.entry = (kprobe_opcode_t *) my_udp_sendmsg
};
static void nl_recv_msg_udp(struct sk_buff *skb)
{
struct nlmsghdr *nlh;
struct sk_buff *skb_out;
int msg_size;
char *msg="Hello from kernel";
int res;
printk(KERN_INFO "Entering: %s\n", __FUNCTION__);
msg_size=strlen(msg);
nlh=(struct nlmsghdr*)skb->data;
if (!gotPid) {
pid = nlh->nlmsg_pid; /*pid of sending process */
gotPid=true;
gotUserOrNotUdp(true);
skb_out = nlmsg_new(msg_size,0);
printk(KERN_INFO "Netlink received msg payload: %s\n",(char*)nlmsg_data(nlh));
if(!skb_out)
{
printk(KERN_ERR "Failed to allocate new skb\n");
return;
}
nlh = nlmsg_put(skb_out,0,0,NLMSG_DONE,msg_size,0);
NETLINK_CB(skb_out).dst_group = 0;
strncpy(nlmsg_data(nlh),msg,msg_size);
res=nlmsg_unicast(nl_sk_udp,skb_out,pid);
if (res<0){
printk(KERN_INFO "Error while sending back to user\n");
gotPid=false;
gotUserOrNotUdp(false);
} else {
}
}else {
printk(KERN_INFO "Netlink received msg payload: %s\n",(char*)nlmsg_data(nlh));
skb_out = nlmsg_new(msg_size,0);
if(!skb_out)
{
printk(KERN_ERR "Failed to allocate new skb\n");
return;
}
if (((char*)nlmsg_data(nlh))[0] == 'y'){
letItResumeUdp(false);
}else{
letItResumeUdp(true);
}
}
}
int init_module(void)
{
int ret;
printk("Entering: %s\n",__FUNCTION__);
nl_sk_udp=netlink_kernel_create(&init_net, NETLINK_USER, 0, nl_recv_msg_udp, NULL, THIS_MODULE);
my_jprobe_udp.kp.addr =
(kprobe_opcode_t *) kallsyms_lookup_name("udp_sendmsg");
if (!my_jprobe_udp.kp.addr) {
printk("Couldn't find %s to plant jprobe\n", "udp_sendmsg");
return -1;
}
if ((ret = register_jprobe(&my_jprobe_udp)) <0) {
printk("register_jprobe failed, returned %d\n", ret);
return -1;
}
printk("Planted jprobe at %p, handler addr %p\n",
my_jprobe_udp.kp.addr, my_jprobe_udp.entry);
return 0;
}
void cleanup_module(void)
{
unregister_jprobe(&my_jprobe_udp);
netlink_kernel_release(nl_sk_udp);
printk("jprobe unregistered\n");
}
MODULE_LICENSE("GPL");