-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.cpp
More file actions
160 lines (146 loc) · 4.39 KB
/
service.cpp
File metadata and controls
160 lines (146 loc) · 4.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
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
#include "service.h"
#include<pthread.h>
using namespace std;
//Connects to live host<ipToScan> that
//showed port<port> was open in the scan
int connectToHost(string ipToScan, int port){
struct sockaddr_in sa;
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(port);
sa.sin_addr.s_addr = inet_addr(ipToScan.c_str());
int clientSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(clientSock < 0){
cout<<"Error creating socket to get service."<<endl;
pthread_exit(NULL);
// exit(EXIT_FAILURE);
}
const struct sockaddr* sAddr = (struct sockaddr *)&sa;
if(connect(clientSock, sAddr, sizeof(sa)) < 0){
return -1;
}
return clientSock;
}
string httpCheck(int clientSock){
char getRequest[100];
strcpy(getRequest,"GET / HTTP/1.1\r\nHOST: 129.79.247.86\r\n\r\n");
//sendto(int socket, char data, int dataLength, flags, destinationAddress, int destinationStructureLength)
int bytes_sent = send(clientSock, getRequest, strlen(getRequest), 0);
char rMsg[1024];
int msgLen;
while ((msgLen = recv(clientSock, rMsg, 1000, 0)) > 0) {
string recvMsg(rMsg);
if (recvMsg.find("HTTP/1.1") != string::npos) {
return "HTTP 1.1 in use";
} else if (recvMsg.find("HTTP/1.0") != string::npos) {
return "HTTP 1.0 in use";
} else {
return "HTTP not running";
}
}
}
string smtpCheck(int clientSock, string ipToScan){
char rMsg[1024];
memset(rMsg, 0, sizeof(rMsg));
int msgLen = recv(clientSock, rMsg, 1000, 0);
rMsg[msgLen] = '\0';
char getRequest[25];
strcpy(getRequest,"EHLO\n\n");
int requestLen = 5 + strlen(ipToScan.c_str());
int bytes_sent = send(clientSock, getRequest, strlen(getRequest), 0);
msgLen = recv(clientSock, rMsg, 1000, 0);
string recvMsg(rMsg);
if (recvMsg.find("250") != string::npos) {
return "ESMTP in use";
} else if (recvMsg.find("500") != string::npos) {
return "SMTP in use";
}
}
string sshCheck(int clientSock){
char rMsg[1024];
memset(rMsg, 0, sizeof(rMsg));
int msgLen = recv(clientSock, rMsg, 1024, 0);
return string(rMsg);
}
string popCheck(int clientSock){
char rMsg[1024];
memset(rMsg, 0, sizeof(rMsg));
int msgLen = recv(clientSock, rMsg, 1024, 0);
cout<<rMsg<<endl;
if(msgLen > 0){
return "POP in use";
} else {
return string();
}
}
string imapCheck(int clientSock){
char rMsg[1024];
memset(rMsg, 0, sizeof(rMsg));
int msgLen = recv(clientSock, rMsg, 1024, 0);
if (msgLen < 0){
cout<<"Service Detection: Error while recieving."<<endl;
}
string resMsg(rMsg);
size_t pos = resMsg.find("IMAP");
if(pos != string::npos){
return resMsg.substr(pos, 10);
} else {
return string();
}
}
string whoCheck(int clientSock, string ipToScan){
char getRequest[20];
char rMsg[1024];
strcpy(getRequest,"google.com\n\n");
// strcat(getRequest, ipToScan.c_str());
int requestLen = 5 + strlen(ipToScan.c_str());
int bytes_sent = send(clientSock, &getRequest, 4, 0);
int msgLen = recv(clientSock, rMsg, 1000, 0);
if(msgLen > 0){
return "WHOIS running";
} else {
return string();
}
}
string privCheck(int clientSock){
char rMsg[1024];
memset(rMsg, 0, sizeof(rMsg));
int msgLen = recv(clientSock, rMsg, 1024, 0);
string service(rMsg);
if(service.find("mailserver") != string::npos){
return "Private mail sys in use";
} else {
return string();
}
}
string servChk(string ipToScan, unsigned short port){
string result;
int clientSock = connectToHost(ipToScan, port);
if(clientSock < 0){
return string("Unknown");
}
switch(port){
case 80:
return httpCheck(clientSock);
case 22:
return sshCheck(clientSock);
case 24:
return privCheck(clientSock);
case 25:
case 587:
return smtpCheck(clientSock, ipToScan);
case 43:
return whoCheck(clientSock, ipToScan);
case 110:
return popCheck(clientSock);
case 143:
return imapCheck(clientSock);
default:
return "Unknown";
}
}
/*
int main(int argc, char* argv[]){
unsigned short port = atoi(argv[1]);
cout<<servChk("129.79.247.87", port)<<endl;
}*/