I tried to dump all data sent by a specific process on Linux by hooking a handler to the Kernel's function sock_sendmsg()
defined in linux/socket.c
.
I could do that by writing a systemtap probe handler for probe kernel.function("sock_sendmsg@net/socket.c")
that dumps all data blocks passed with the 2nd argument struct msghdr *msg
.
Here's the excerpt from net/socket.c
:
int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
{
struct kiocb iocb;
struct sock_iocb siocb;
int ret;
init_sync_kiocb(&iocb, NULL);
iocb.private = &siocb;
ret = __sock_sendmsg(&iocb, sock, msg, size);
if (-EIOCBQUEUED == ret)
ret = wait_on_sync_kiocb(&iocb);
return ret;
}
I tested my systemtap script hook_sendmsg.stp
. First I ran hook_sendmsg.stp
in one terminal. Then I opened another terminal and invoked telnet
command to connect to stackoverflow.com
and typed HEAD / HTTP/1.0<Enter twice>
in the terminal. I got the following output from hook_sendmsg.stp
:
root@debian:~# stap -g hook_sendmsg.stp
message block [0]; type=3(raw); state=1(unconnected)
14 00 00 00 16 00 01 03 ec 95 f4 52 00 00 00 00 |...........R....|
00 00 00 00 |................|
message block [0]; type=3(raw); state=1(unconnected)
14 00 00 00 16 00 01 03 ec 95 f4 52 00 00 00 00 |...........R....|
00 00 00 00 |................|
message block [0]; type=2(udp); state=1(unconnected)
4d 0d 01 00 00 01 00 00 00 00 00 00 0d 73 74 61 |M............sta|
63 6b 6f 76 65 72 66 6c 6f 77 03 63 6f 6d 00 00 |ckoverflow.com..|
01 00 01 |................|
message block [0]; type=2(udp); state=1(unconnected)
0f 1e 01 00 00 01 00 00 00 00 00 00 0d 73 74 61 |.............sta|
63 6b 6f 76 65 72 66 6c 6f 77 03 63 6f 6d 00 00 |ckoverflow.com..|
1c 00 01 |................|
message block [0]; type=1(tcp); state=3(connected)
48 45 41 44 20 2f 20 48 54 54 50 2f 31 2e 30 0d |HEAD / HTTP/1.0.|
0a |................|
message block [0]; type=1(tcp); state=3(connected)
0d 0a |................|
This shows that totally sock_sendmsg()
were called 6 times in the context of telnet
. Obviously the 3rd and 4th are DNS queries to Google's public DNS servers 8.8.8.8
and 8.8.4.4
. The 5th and 6th are the two lines of HTTP request sent from telnet
. But what were the 1st and 2nd called for? Did they called internally by the Kernel?
Thanks in advance.