2015-03-22 00:43:15 +00:00
# include "Connector.h"
2015-03-23 17:11:00 +00:00
# include "SSHAuth.h"
2015-04-18 13:05:35 +00:00
# include "Filter.h"
2015-03-05 14:29:05 +00:00
2015-03-06 14:32:36 +00:00
# if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
2015-03-22 00:43:15 +00:00
int _pingMyTarget ( const char * ip )
2015-03-06 14:32:36 +00:00
{
HANDLE hIcmpFile ;
unsigned long ipaddr = INADDR_NONE ;
DWORD dwRetVal = 0 ;
char SendData [ 32 ] = " Data Buffer " ;
LPVOID ReplyBuffer = NULL ;
DWORD ReplySize = 0 ;
ipaddr = inet_addr ( ip ) ;
if ( ipaddr = = INADDR_NONE )
{
stt - > doEmitionRedFoundData ( " [Pinger] INADDR_NONE! [ " + QString ( ip ) + " ] " ) ;
return 0 ;
}
hIcmpFile = IcmpCreateFile ( ) ;
if ( hIcmpFile = = INVALID_HANDLE_VALUE )
{
stt - > doEmitionRedFoundData ( " [Pinger] Unable to open handle. [ " + QString : : number ( GetLastError ( ) ) + " ] " ) ;
return 0 ;
}
ReplySize = sizeof ( ICMP_ECHO_REPLY ) + sizeof ( SendData ) ;
ReplyBuffer = ( VOID * ) malloc ( ReplySize ) ;
if ( ReplyBuffer = = NULL )
{
stt - > doEmitionRedFoundData ( " [Pinger] Unable to allocate memory. " ) ;
return 0 ;
}
dwRetVal = IcmpSendEcho ( hIcmpFile , ipaddr , SendData , sizeof ( SendData ) ,
NULL , ReplyBuffer , ReplySize , gPingTimeout * 1000 ) ;
if ( dwRetVal ! = 0 ) {
PICMP_ECHO_REPLY pEchoReply = ( PICMP_ECHO_REPLY ) ReplyBuffer ;
struct in_addr ReplyAddr ;
ReplyAddr . S_un . S_addr = pEchoReply - > Address ;
printf ( " \t Sent icmp message to %s \n " , " 127.0.0.1 " ) ;
if ( dwRetVal > 1 )
{
if ( gDebugMode ) stt - > doEmitionYellowFoundData ( " [Pinger] Received " + QString : : number ( dwRetVal ) + " icmp message responses. " ) ;
}
else
{
if ( gDebugMode ) stt - > doEmitionYellowFoundData ( " [Pinger] Received " + QString : : number ( dwRetVal ) + " icmp message responses. " ) ;
}
if ( gDebugMode ) stt - > doEmitionYellowFoundData ( " [Pinger] Received from: " + QString ( inet_ntoa ( ReplyAddr ) ) + " ; Status = " + QString : : number ( pEchoReply - > Status ) + " ; Roundtrip time = " + QString : : number ( pEchoReply - > RoundTripTime ) + " ms. " ) ;
return 1 ;
}
else
{
printf ( " \t Call to IcmpSendEcho failed. \n " ) ;
printf ( " \t IcmpSendEcho returned error: %ld \n " , GetLastError ( ) ) ;
if ( gDebugMode ) stt - > doEmitionRedFoundData ( " [Pinger] Call to IcmpSendEcho failed. IcmpSendEcho returned error: " + QString : : number ( GetLastError ( ) ) ) ;
return 0 ;
} ;
}
# else
2015-03-20 14:28:51 +00:00
int _pingMyTarget ( const char * ip )
2015-03-06 14:32:36 +00:00
{
FILE * pipe = popen ( ( " ping -w " + std : : to_string ( gPingTimeout ) + " " + ip ) . c_str ( ) , " r " ) ;
if ( ! pipe ) {
stt - > doEmitionRedFoundData ( " Ping pipe failed: cannot open pipe. " ) ;
perror ( " pipe " ) ;
return 0 ;
}
char buffer [ 128 ] = { 0 } ;
std : : string result ;
2015-03-05 14:29:05 +00:00
2015-03-06 14:32:36 +00:00
while ( ! feof ( pipe ) ) {
if ( fgets ( buffer , 128 , pipe ) ! = NULL ) {
result + = buffer ;
}
}
pclose ( pipe ) ;
2015-03-05 14:29:05 +00:00
2015-03-06 14:32:36 +00:00
if ( strstr ( ( char * ) result . c_str ( ) , " 100% packet loss " ) ! = NULL ) return 0 ;
return 1 ;
}
# endif
struct data {
char trace_ascii ; /* 1 or 0 */
} ;
static
int my_trace ( CURL * handle , curl_infotype type ,
char * data , size_t size ,
void * userp )
{
2015-03-17 14:30:53 +00:00
if ( type = = CURLINFO_HEADER_OUT ) {
2015-03-22 00:43:15 +00:00
data [ size ] = ' \0 ' ;
Activity + = strlen ( data ) ;
stt - > doEmitionAddOutData ( QString ( data ) ) ;
2015-03-06 14:32:36 +00:00
}
return 0 ;
}
2015-04-01 12:39:14 +00:00
size_t nWriteCallback ( void * contents , size_t size , size_t nmemb , void * userp )
2015-03-05 14:29:05 +00:00
{
( ( std : : string * ) userp ) - > append ( ( char * ) contents , size * nmemb ) ;
2015-04-01 12:39:14 +00:00
Activity + = nmemb ;
int ssz = ( ( std : : string * ) userp ) - > size ( ) ;
if ( ssz > 180000 ) {
return - 1 ;
}
2015-03-05 14:29:05 +00:00
return size * nmemb ;
}
2015-03-06 14:32:36 +00:00
2015-03-20 14:28:51 +00:00
int Connector : : nConnect ( const char * ip , const int port , std : : string * buffer ,
2015-03-06 14:32:36 +00:00
const char * postData ,
2015-03-16 14:29:34 +00:00
const std : : vector < std : : string > * customHeaders ,
const std : : string * lpString ) {
buffer - > clear ( ) ;
2015-03-05 14:29:05 +00:00
CURL * curl = curl_easy_init ( ) ;
if ( curl )
{
2015-04-01 12:39:14 +00:00
curl_easy_setopt ( curl , CURLOPT_NOSIGNAL , 0L ) ;
2015-03-19 14:34:35 +00:00
if ( MapWidgetOpened ) {
struct data config ;
config . trace_ascii = 1 ; /* enable ascii tracing */
2015-03-16 14:29:34 +00:00
curl_easy_setopt ( curl , CURLOPT_DEBUGFUNCTION , my_trace ) ;
curl_easy_setopt ( curl , CURLOPT_DEBUGDATA , & config ) ;
curl_easy_setopt ( curl , CURLOPT_VERBOSE , 1L ) ;
2015-03-19 14:34:35 +00:00
}
2015-03-05 14:29:05 +00:00
curl_easy_setopt ( curl , CURLOPT_URL , ip ) ;
curl_easy_setopt ( curl , CURLOPT_PORT , port ) ;
2015-03-16 14:29:34 +00:00
curl_easy_setopt ( curl , CURLOPT_USERAGENT ,
" Mozilla/5.0 (X11; Linux x86_64; rv:35.0) Gecko/20100101 Firefox/35.0 " ) ;
2015-03-05 14:29:05 +00:00
curl_easy_setopt ( curl , CURLOPT_HEADER , 1L ) ;
curl_easy_setopt ( curl , CURLOPT_AUTOREFERER , 1L ) ;
curl_easy_setopt ( curl , CURLOPT_SSL_VERIFYPEER , 0L ) ;
curl_easy_setopt ( curl , CURLOPT_SSL_VERIFYHOST , 0L ) ;
curl_easy_setopt ( curl , CURLOPT_WRITEFUNCTION , nWriteCallback ) ;
curl_easy_setopt ( curl , CURLOPT_WRITEDATA , buffer ) ;
2015-03-06 14:32:36 +00:00
int proxyPort = std : : atoi ( gProxyPort ) ;
if ( strlen ( gProxyIP ) ! = 0 & & ( proxyPort > 0 & & proxyPort < 65535 ) ) {
curl_easy_setopt ( curl , CURLOPT_PROXY , gProxyIP ) ;
curl_easy_setopt ( curl , CURLOPT_PROXYPORT , proxyPort ) ;
2015-03-19 14:34:35 +00:00
} else {
curl_easy_setopt ( curl , CURLOPT_PROXY , " " ) ;
2015-03-06 14:32:36 +00:00
}
2015-03-05 14:29:05 +00:00
curl_easy_setopt ( curl , CURLOPT_FOLLOWLOCATION , 1L ) ;
curl_easy_setopt ( curl , CURLOPT_CONNECTTIMEOUT , gTimeOut ) ;
2015-04-02 10:56:03 +00:00
curl_easy_setopt ( curl , CURLOPT_TIMEOUT , gTimeOut + 3 ) ;
2015-03-05 14:29:05 +00:00
if ( postData ! = NULL ) {
curl_easy_setopt ( curl , CURLOPT_POSTFIELDS , postData ) ;
} ;
if ( customHeaders ! = NULL ) {
struct curl_slist * chunk = NULL ;
2015-03-06 14:32:36 +00:00
for ( auto & ch : * customHeaders ) {
2015-03-05 14:29:05 +00:00
2015-03-06 14:32:36 +00:00
chunk = curl_slist_append ( chunk , ch . c_str ( ) ) ;
2015-03-05 14:29:05 +00:00
}
curl_easy_setopt ( curl , CURLOPT_HTTPHEADER , chunk ) ;
}
2015-03-23 13:54:40 +00:00
if ( lpString ! = NULL ) {
2015-04-18 13:05:35 +00:00
curl_easy_setopt ( curl , CURLOPT_HTTPAUTH , ( long ) CURLAUTH_ANY ) ;
2015-03-23 13:54:40 +00:00
curl_easy_setopt ( curl , CURLOPT_UNRESTRICTED_AUTH , 1L ) ;
2015-03-23 07:51:58 +00:00
curl_easy_setopt ( curl , CURLOPT_FTPLISTONLY , 1L ) ;
2015-03-22 00:43:15 +00:00
curl_easy_setopt ( curl , CURLOPT_USERPWD , lpString - > c_str ( ) ) ;
} ;
int res = curl_easy_perform ( curl ) ;
2015-04-18 13:05:35 +00:00
if ( port ! = 21 & & lpString ! = NULL ) {
int pos = Utils : : ustrstr ( * buffer , " \r \n \r \n " ) ;
if ( pos ! = - 1 ) {
* buffer = buffer - > substr ( pos + 4 ) ;
}
}
2015-03-22 00:43:15 +00:00
curl_easy_cleanup ( curl ) ;
if ( res = = CURLE_OK | |
( port = = 21 & & buffer - > size ( ) > 0 ) ) {
if ( MapWidgetOpened ) stt - > doEmitionAddIncData ( QString ( ip ) , QString ( buffer - > c_str ( ) ) ) ;
Activity + = buffer - > size ( ) ;
2015-04-01 12:39:14 +00:00
return buffer - > size ( ) ;
2015-03-22 00:43:15 +00:00
} else {
2015-04-04 07:24:31 +00:00
if ( res = = 6 ) return - 2 ;
else if ( res ! = 28 & &
2015-03-22 00:43:15 +00:00
res ! = 7 & &
res ! = 67 & &
res ! = 52 & &
res ! = 55 & &
2015-04-01 16:58:12 +00:00
res ! = 56 & &
res ! = 35 & &
2015-04-01 17:01:06 +00:00
res ! = 19 & &
2015-04-01 12:39:14 +00:00
res ! = 23 ) {
2015-03-22 00:43:15 +00:00
if ( res = = 5 ) {
stt - > doEmitionRedFoundData ( " Couldn't resolve proxy. The given proxy host could not be resolved. " ) ;
return - 2 ;
}
else if ( res = = 13 ) {
2015-03-22 11:05:58 +00:00
stt - > doEmitionFoundData ( " Unknown ftp. ( " + QString : : number ( res ) + " ) " +
2015-03-22 00:43:15 +00:00
QString ( ip ) + " : " + QString : : number ( port ) ) ;
return - 2 ;
} else if ( res = = 8 ) {
2015-03-23 16:32:33 +00:00
stt - > doEmitionFoundData ( " Strange ftp reply. ( " +
2015-03-22 00:43:15 +00:00
QString : : number ( res ) + " ) " + QString ( ip ) +
" : " + QString : : number ( port ) ) ;
return - 2 ;
}
2015-03-22 11:05:58 +00:00
else if ( res = = 18 ) {
stt - > doEmitionFoundData ( " Inappropriate file size. ( " +
QString : : number ( res ) + " ) " + QString ( ip ) +
" : " + QString : : number ( port ) ) ;
return - 2 ;
2015-03-22 00:43:15 +00:00
}
else stt - > doEmitionRedFoundData ( " CURL error: ( " + QString : : number ( res ) + " ) " +
QString ( ip ) + " : " + QString : : number ( port ) ) ;
}
2015-04-01 12:39:14 +00:00
if ( res = = 23 & & buffer - > size ( ) > 0 ) {
if ( MapWidgetOpened ) stt - > doEmitionAddIncData ( QString ( ip ) , QString ( " [OVERFLOW] " ) ) ;
return buffer - > size ( ) ;
2015-04-04 07:24:31 +00:00
} else return - 1 ;
2015-03-22 00:43:15 +00:00
}
2015-03-05 14:29:05 +00:00
} else {
stt - > doEmitionRedFoundData ( " Curl error. " ) ;
return - 1 ;
2015-03-22 00:43:15 +00:00
} ;
2015-03-05 14:29:05 +00:00
2015-04-01 12:39:14 +00:00
if ( MapWidgetOpened ) stt - > doEmitionAddIncData ( QString ( ip ) , QString ( buffer - > c_str ( ) ) ) ;
2015-03-22 00:43:15 +00:00
return buffer - > size ( ) ;
2015-03-05 14:29:05 +00:00
}
2015-03-17 14:30:53 +00:00
2015-04-18 13:05:35 +00:00
int Connector : : connectToPort ( char * ip , int port )
2015-03-05 14:29:05 +00:00
{
if ( gPingNScan )
{
2015-04-16 11:51:51 +00:00
if ( _pingMyTarget ( ip ) = = 0 ) return - 2 ;
2015-03-05 14:29:05 +00:00
} ;
std : : string buffer ;
int size = 0 ;
2015-04-01 12:39:14 +00:00
if ( port = = 22 ) size = SSHAuth : : SSHLobby ( ip , port , & buffer ) ;
else size = nConnect ( ip , port , & buffer ) ;
2015-03-05 14:29:05 +00:00
2015-03-22 00:43:15 +00:00
if ( size > 0 )
{
2015-04-04 08:47:27 +00:00
+ + Alive ; //ME2
+ + found ; //PieStat
2015-03-22 00:43:15 +00:00
Lexems lx ;
2015-04-18 13:05:35 +00:00
lx . filler ( ip , port , & buffer , size , & lx ) ;
2015-04-03 18:50:54 +00:00
}
else if ( size = = - 2 ) return - 2 ;
2015-03-05 14:29:05 +00:00
return 0 ;
}