-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
85 lines (67 loc) · 2.39 KB
/
main.cpp
File metadata and controls
85 lines (67 loc) · 2.39 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
// g++ -std=c++11 -fPIC -m32 -shared -o main.so main.cpp
#include <iostream>
#include <sstream>
#include <iomanip>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <dlfcn.h>
typedef ssize_t (*sendto_ptr)(int, const void*, size_t, int, const struct sockaddr*, socklen_t);
typedef ssize_t (*recvfrom_ptr)(int, void*, size_t, int, struct sockaddr*, socklen_t*);
static void init() __attribute__((constructor));
static sendto_ptr orig_sendto = nullptr;
static recvfrom_ptr orig_recvfrom = nullptr;
void locate_functions()
{
char *error;
dlerror();
orig_sendto = (sendto_ptr)dlsym(RTLD_NEXT, "sendto");
if ((error = dlerror()) != NULL)
{
std::cout << error << std::endl;
}
orig_recvfrom = (recvfrom_ptr)dlsym(RTLD_NEXT, "recvfrom");
if ((error = dlerror()) != NULL)
{
std::cout << error << std::endl;
}
}
void init()
{
locate_functions();
}
std::string bytes_to_hex(const unsigned char *bytes, size_t len)
{
std::ostringstream os;
os << std::hex << std::setfill('0');
for (size_t i = 0; i < len; ++i)
{
os << std::setw(2) << (int)bytes[i] << " ";
}
return os.str();
}
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen)
{
const struct sockaddr_in *addr4 = (struct sockaddr_in*) dest_addr;
const unsigned char *cbuf = (const unsigned char *) buf;
char *ip = inet_ntoa(addr4->sin_addr);
const unsigned short &port = addr4->sin_port;
std::cout << "send " << ip << ":" << port << std::endl;
std::cout << " " << bytes_to_hex(cbuf, len) << std::endl;
return orig_sendto(sockfd, buf, len, flags, dest_addr, addrlen);
}
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
struct sockaddr *from_addr, socklen_t *addrlen)
{
ssize_t bytes_read = orig_recvfrom(sockfd, buf, len, flags, from_addr, addrlen);
const struct sockaddr_in *addr4 = (struct sockaddr_in*) from_addr;
unsigned char *cbuf = (unsigned char *) buf;
char *ip = inet_ntoa(addr4->sin_addr);
const unsigned short &port = addr4->sin_port;
std::cout << "recv " << ip << ":" << port << std::endl;
std::cout << " " << bytes_to_hex(cbuf, len) << std::endl;
return bytes_read;
}