nesca/CheckKey_Th.cpp

96 lines
3.3 KiB
C++
Raw Normal View History

2015-02-24 14:00:19 +00:00
#include "CheckKey_Th.h"
#include "STh.h"
#include "externData.h"
#include "externFunctions.h"
2015-03-22 00:43:15 +00:00
#include "Connector.h"
#include "Utils.h"
2015-02-24 14:00:19 +00:00
2015-03-06 14:32:36 +00:00
void getSubStrEx(const char *src, char *startStr, char *endStr, char *dest, int szDest)
2015-02-24 14:00:19 +00:00
{
ZeroMemory(dest, szDest);
2015-03-02 14:27:38 +00:00
char *ptr1 = strstri((const char*)src, startStr);
2015-02-24 14:00:19 +00:00
if(ptr1 != NULL)
{
2015-03-02 14:27:38 +00:00
char *ptr2 = strstri((const char*)ptr1, endStr);
2015-02-24 14:00:19 +00:00
if(ptr2 != NULL)
{
int szStartStr = strlen(startStr);
int sz = ptr2 - ptr1 - szStartStr;
strncpy(dest, ptr1 + szStartStr, sz < szDest ? sz : szDest);
};
};
}
void getSubStr(char *src, char *startStr, char *endStr, char *dest, int szDest)
{
ZeroMemory(dest, szDest);
2015-03-02 14:27:38 +00:00
char *ptr1 = strstri((const char*)src, startStr);
2015-02-24 14:00:19 +00:00
if(ptr1 != NULL)
{
2015-03-02 14:27:38 +00:00
char *ptr2 = strstri((const char*)ptr1, endStr);
2015-02-24 14:00:19 +00:00
if(ptr2 != NULL)
{
int sz = ptr2 - ptr1;
strncpy(dest, ptr1, sz < szDest ? sz : szDest);
};
};
}
2015-03-10 14:35:50 +00:00
2015-02-24 14:00:19 +00:00
int emitIfOK = -1;
int KeyCheckerMain()
{
int kLen = strlen(trcPersKey);
if(kLen == 0)
{
stt->doEmitionRedFoundData("[Key check] Key field is empty.");
return -1;
}
else if(kLen < 32)
{
stt->doEmitionRedFoundData("[Key check] Key length is not valid.");
return -1;
};
2015-03-06 14:32:36 +00:00
std::vector<std::string> headerVector;
headerVector.push_back("X-Nescav3: True");
std::string buffer;
Connector::nConnect((std::string(trcSrv) + std::string(trcScr)).c_str(), std::stoi(trcSrvPortLine), &buffer, NULL, &headerVector);
2015-03-10 14:35:50 +00:00
int hostStringIndex = buffer.find("\r\n\r\n");
2015-03-06 14:32:36 +00:00
if(hostStringIndex != -1) {
2015-03-10 14:35:50 +00:00
int s = buffer.find("http://", hostStringIndex);
int e = buffer.find('/', s + 8);
std::string url = buffer.substr(s, e - s);
Connector::nConnect((url + std::string("/api/checkaccount?key=") + std::string(trcPersKey)).c_str(),
2015-03-06 14:32:36 +00:00
std::stoi(trcSrvPortLine), &buffer, NULL, &headerVector);
if(Utils::ci_find_substr(buffer, std::string("202 Accepted")) != -1) {
stt->doEmitionGreenFoundData("Key is valid.");
if(emitIfOK == 0) stt->doEmitionStartScanIP();
else if(emitIfOK == 1) stt->doEmitionStartScanDNS();
else if(emitIfOK == 2) stt->doEmitionStartScanImport();
return 1;
} else if(Utils::ci_find_substr(buffer, std::string("400 Bad Request")) != -1) {
QString errorDef = GetNSErrorDefinition(buffer.c_str(), "notify");
2015-03-10 14:35:50 +00:00
if(errorDef == "Invalid access key") stt->doEmitionYellowFoundData("[NS-Track] Key is unauthorized. A valid key is required.");
else stt->doEmitionYellowFoundData("[Key check] -FAIL! [400 Bad Request : " + GetNSErrorDefinition(buffer.c_str(), "notify") + "]");
2015-03-06 14:32:36 +00:00
} else if(Utils::ci_find_substr(buffer, std::string("503 Bad Gateway")) != -1) {
2015-03-10 14:35:50 +00:00
stt->doEmitionYellowFoundData("[Key check] 503 Backend not responding!");
2015-03-06 14:32:36 +00:00
} else {
char header[64] = {0};
getSubStrEx(buffer.c_str(), "http/1.1 ", "\r\n", header, 64);
2015-03-10 14:35:50 +00:00
stt->doEmitionRedFoundData("[Key check] -FAIL! An error occured. (" + QString::number(WSAGetLastError()) + ") Header: <u>" + QString::fromLocal8Bit(header) + "</u>");
2015-03-06 14:32:36 +00:00
if(gDebugMode) stt->doEmitionDebugFoundData(QString(buffer.c_str()));
};
} else {
2015-03-10 14:35:50 +00:00
stt->doEmitionRedFoundData("[Key check] Cannot acquire host string.");
2015-03-06 14:32:36 +00:00
}
2015-03-10 14:35:50 +00:00
return -1;
2015-03-06 14:32:36 +00:00
}
2015-02-24 14:00:19 +00:00
void CheckKey_Th::run()
{
KeyCheckerMain();
2015-03-06 14:32:36 +00:00
}