-
Notifications
You must be signed in to change notification settings - Fork 0
/
win_scan_engine.cpp
executable file
·140 lines (125 loc) · 2.93 KB
/
win_scan_engine.cpp
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
#ifndef SCAN_ENGINE_H
#include "scan_engine.h"
#endif
#define close closesocket
inline int udp_scan(int port)
{
int success = 0;
return success;
}
inline int tcp_scan(int port)
{
int sock = 0;
int is_opened = 0;
struct sockaddr_in tcp_dest;
int success = -1;
if((sock = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
printf("Couldn't make socket!\n");
exit(-1);
}
tcp_dest.sin_family = AF_INET;
tcp_dest.sin_port = htons(port);
tcp_dest.sin_addr = *((struct in_addr *)host->h_addr);
memset(&(tcp_dest.sin_zero), '\0', 8);
success = connect(sock , (struct sockaddr *)&tcp_dest, sizeof(struct sockaddr));
if (success != -1)
{
is_opened = TRUE;
}
else
{
is_opened = FALSE;
}
close(sock);
return is_opened;
}
#if (defined WINSOCK1 && defined WIN32)
inline int raw_scan(int port, int scan_type)
{
printf("You cannot call this function\n");
return 0;
}
void set_sniffer(int scan_type)
{
printf("You cannot call this function\n");
}
#endif
#ifndef WINSOCK1
inline int raw_scan(int port, int scan_type)
{
int success = 0;
/*TODO*/
return success;
}
void set_sniffer(int scan_type)
{
int sock;
char temp[MAX_HOSTNAME_LAN];
char buffer[sizeof(struct iphdr) + sizeof(struct tcphdr)];
struct iphdr *ip = (struct iphdr *)(buffer + sizeof(iphdr));
struct tcphdr *tcp;
struct hostent *h;
DWORD dwBytesRet;
int optval = 1;
SOCKADDR_IN sa;
gethostname(temp, MAX_HOSTNAME_LAN);
h = gethostbyname(temp);
sa.sin_family = AF_INET;
sa.sin_port = htons(0);
memcpy(&sa.sin_addr.S_un.S_addr, h->h_addr_list[0], h->h_length);
sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP);
//setsockopt(sock,IPPROTO_TCP,IP_HDRINCL, (char*)&optval, sizeof(optval));
bind(sock, (SOCKADDR *)&sa, sizeof(sa));
WSAIoctl(sock, SIO_RCVALL, &optval, sizeof(optval), NULL, 0, &dwBytesRet, NULL, NULL);
printf("starting sniffer\n");
STARTED_SNIFFER = 1;
while(!STOP_SNIFFER)
{
memset(buffer, 0, sizeof(buffer));
recvfrom(sock, buffer, sizeof(buffer), 0, NULL, NULL);
tcp = (struct tcphdr *)(buffer + (ip->tot_len << 2));
unsigned int x;
for (x = 0; x <= sizeof(buffer); x++)
printf("%x2", buffer[x]);
printf("\n\n");
if (scan_type == FIN || scan_type == XMAS || scan_type == NULL_S)
{
if (tcp->th_flags & TH_RST)
closed_raw_port(ntohs(tcp->th_sport));
}
if (scan_type == SYN)
{
if (tcp->th_flags & TH_SYN){
if (tcp->th_flags &TH_ACK){
opened_raw_port(ntohs(tcp->th_sport));}}
}
}
closesocket(sock);
STARTED_SNIFFER = 0;
}
#endif
void get_ip()
{
char temp[MAX_HOSTNAME_LAN];
struct hostent *h;
gethostname(temp, MAX_HOSTNAME_LAN);
h = gethostbyname(temp);
local_ip = inet_ntoa(*((struct in_addr *)h->h_addr));
}
USHORT in_cksum(USHORT *buffer, int size)
{
unsigned long cksum=0;
while (size > 1)
{
cksum += *buffer++;
size -= sizeof(USHORT);
}
if (size)
{
cksum += *(UCHAR*)buffer;
}
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >>16);
return (USHORT)(~cksum);
}