-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathos_det.c
365 lines (274 loc) · 8.34 KB
/
os_det.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
Packet sniffer using libpcap library
*/
#include<pcap.h>
#include<stdio.h>
#include<stdlib.h> // for exit()
#include<string.h> //for memset
#include<sys/socket.h>
#include<arpa/inet.h> // for inet_ntoa()
#include<net/ethernet.h>
#include<netinet/ip_icmp.h> //Provides declarations for icmp header
#include<netinet/udp.h> //Provides declarations for udp header
#include<netinet/tcp.h> //Provides declarations for tcp header
#include<netinet/ip.h> //Provides declarations for ip header
#include <stdbool.h>
#include <unistd.h>
#include <pthread.h>
#include <libnet.h>
#include <sys/socket.h>
void process_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
void process_ip_packet(const u_char * , int);
void print_ip_packet(const u_char * , int);
void print_tcp_packet(const u_char * , int );
void * send_http_request();
#define KNOWN_OS 10
#define MAX_LIMIT 50
// OS database
struct os_prop
{
char name[100];
float score;
};
struct os_prop os_matrix[KNOWN_OS];
struct os_prop os;
char dist_ip[20]; //="172.16.11.85";
int port__;
void initialize_os_matrix()
{
strcpy(os_matrix[0].name, "Windows");
strcpy(os_matrix[1].name, "Windows");
strcpy(os_matrix[2].name, "Windows");
strcpy(os_matrix[3].name, "Linux");
strcpy(os_matrix[4].name, "FreeBSD");
strcpy(os_matrix[5].name, "Mac OS");
strcpy(os_matrix[6].name, "Symbian");
strcpy(os_matrix[7].name, "Palm OS");
strcpy(os_matrix[8].name, "NetBSD");
strcpy(os_matrix[9].name, "Open BSD");
}//end initialize_os_matrix
//Filters
struct bpf_program filter;
pthread_t request_thread;
FILE *logfile;
struct sockaddr_in source,dest;
int tcp=0,udp=0,icmp=0,others=0,igmp=0,total=0,i,j;
int main(int argc, char **argv)
{
pcap_if_t *alldevsp , *device;
pcap_t *handle; //Handle of the device that shall be sniffed
char errbuf[100] , *devname , devs[100][100];
int count = 1 , n;
//First get the list of available devices
// printf("Finding available devices ... ");
if( pcap_findalldevs( &alldevsp , errbuf) )
{
// printf("Error finding devices : %s" , errbuf);
exit(1);
}
// printf("Done");
//Print the available devices
// printf("\nAvailable Devices are :\n");
for(device = alldevsp ; device != NULL ; device = device->next)
{
// printf("%d. %s - %s\n" , count , device->name , device->description);
if(device->name != NULL)
{
strcpy(devs[count] , device->name);
}
count++;
}
//Ask user which device to sniff
// printf("Enter the number of the device you want to sniff : ");
// scanf("%d" , &n);
n = 1;
devname = devs[n];
// printf("Enter the port for os detection : ");
// scanf("%d" , &port__);
port__=atoi(argv[2]);
char pport[20];
sprintf(pport, "%d", port__);
// char str[20] = "172.16.11.85";
// printf("Dist IP : ");
// scanf("%[^\n]%*c", str);
strcpy(dist_ip,argv[1]);
char p[MAX_LIMIT] = "src ";
strcat(p,dist_ip);
// char and[] = " and dst port ";
// strcat(p,and);
// strcat(p,pport);
// printf("Opening device %s for sniffing ... " , devname);
handle = pcap_open_live(devname , 65536 , 1 , 0 , errbuf);
if (handle == NULL)
{
fprintf(stderr, "Couldn't open device %s : %s\n" , devname , errbuf);
exit(1);
}
// printf("Done\n");
logfile=fopen("log.txt","w");
if(logfile==NULL)
{
printf("Unable to create file.");
}
bpf_u_int32 mask; /* The netmask of our sniffing device */
bpf_u_int32 net; /* The IP of our sniffing device */
if (pcap_lookupnet(devname, &net, &mask, errbuf) == -1) {
fprintf(stderr, "Can't get netmask for device %s\n", devname);
net = 0;
mask = 0;
}
if (pcap_compile(handle, &filter, p, 0, net) == -1) {
printf("Bad filter - %s\n", pcap_geterr(handle));
return 2;
}
if (pcap_setfilter(handle, &filter) == -1) {
printf("Error setting filter - %s\n", pcap_geterr(handle));
return 2;
}
initialize_os_matrix();
///////////////////////////
/////thread
pthread_create(&request_thread, NULL, send_http_request, NULL);
//Put the device in sniff loop
pcap_loop(handle , 1 , process_packet , NULL);
// printf("OS detection: \n");
float score_max=0;
int index=0;
for(int i=0;i<10;i++){
if(os_matrix[i].score>=score_max){
score_max=os_matrix[i].score;
index=i;
}else score_max=score_max;
}
printf("\nOs detected --> %s \n",os_matrix[index].name);
return 0;
}
void process_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *buffer)
{
int size = header->len;
//Get the IP Header part of this packet , excluding the ethernet header
struct iphdr *iph = (struct iphdr*)(buffer + sizeof(struct ethhdr));
++total;
switch (iph->protocol) //Check the Protocol and do accordingly...
{
case 1: //ICMP Protocol
++icmp;
//print_icmp_packet( buffer , size);
break;
case 2: //IGMP Protocol
++igmp;
break;
case 6: //TCP Protocol
++tcp;
print_tcp_packet(buffer , size);
break;
case 17: //UDP Protocol
++udp;
//print_udp_packet(buffer , size);
break;
default: //Some Other Protocol like ARP etc.
++others;
break;
}
printf("\rTCP : %d UDP : %d ICMP : %d IGMP : %d Others : %d Total : %d", tcp , udp , icmp , igmp , others , total);
}
void print_ip_header(const u_char * Buffer, int Size)
{
unsigned short iphdrlen;
struct iphdr *iph = (struct iphdr *)(Buffer + sizeof(struct ethhdr) );
iphdrlen =iph->ihl*4;
memset(&source, 0, sizeof(source));
source.sin_addr.s_addr = iph->saddr;
memset(&dest, 0, sizeof(dest));
dest.sin_addr.s_addr = iph->daddr;
unsigned int TTL=(unsigned int)iph->ttl;
if (TTL>=0 &&TTL<=64){
os_matrix[3].score+=0.5;
os_matrix[4].score+=0.5;
os_matrix[5].score+=0.5;
os_matrix[8].score+=0.5;
os_matrix[9].score+=0.5;
}else if(TTL>=64 && TTL<=128){
os_matrix[0].score+=0.5;
os_matrix[1].score+=0.5;
os_matrix[2].score+=0.5;
}else if(TTL>=128 && TTL<=255){
os_matrix[6].score+=0.5;
os_matrix[7].score+=0.5;
}
if(Size==44){
os_matrix[6].score+=1;
os_matrix[7].score+=1;
}else if(Size==48){
os_matrix[0].score+=1;
}else if(Size==52){
os_matrix[1].score+=1;
os_matrix[2].score+=1;
}else if(Size==60){
os_matrix[3].score+=1;
os_matrix[4].score+=1;
}else if(Size==64){
os_matrix[8].score+=1;
os_matrix[9].score+=1;
}
}
void print_tcp_packet(const u_char * Buffer, int Size)
{
unsigned short iphdrlen;
struct iphdr *iph = (struct iphdr *)( Buffer + sizeof(struct ethhdr) );
iphdrlen = iph->ihl*4;
struct tcphdr *tcph=(struct tcphdr*)(Buffer + iphdrlen + sizeof(struct ethhdr));
int header_size = sizeof(struct ethhdr) + iphdrlen + tcph->doff*4;
print_ip_header(Buffer,Size);
uint16_t win= ntohs(tcph->window);
if (win) {
++os_matrix[0].score;
++os_matrix[1].score;
++os_matrix[2].score;
}
if(win>=2920 && win<=584014600){
os_matrix[3].score+=1;
} else if(win==65550){
os_matrix[4].score+=1;
}
unsigned int ACK = (unsigned int)tcph->ack;
if(ACK){
os_matrix[0].score+=1;
os_matrix[1].score+=1;
os_matrix[2].score+=1;
os_matrix[3].score+=1;
os_matrix[4].score+=1;
os_matrix[9].score+=1;
}else{
os_matrix[5].score+=1;
os_matrix[6].score+=1;
os_matrix[7].score+=1;
os_matrix[8].score+=1;
}
}
void * send_http_request() {
for(int i=0;i<1;i++){
int sock = 0, valread;
struct sockaddr_in serv_addr;
char* hello = ((char*)("GET / HTTP/1.0\n\n"));
char buffer[1024] = { 0 };
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port__);
// Convert IPv4 and IPv6 addresses from text to binary
// form
if (inet_pton(AF_INET, dist_ip, &serv_addr.sin_addr) <= 0) {
printf("\nInvalid address/ Address not supported \n");
}
if (connect(sock, (struct sockaddr*)&serv_addr,sizeof(serv_addr))< 0) {
printf("\nConnection Failed \n");
}
send(sock, hello, strlen(hello), 0);
//printf("Hello message sent\n");
valread = read(sock, buffer, 1024);
//printf("%s\n", buffer);
sleep(1);
}
}