HyperDbg/hyperdbg/libhyperdbg/code/debugger/commands/meta-commands/connect.cpp

199 lines
4.5 KiB
C++
Raw Permalink Normal View History

/**
* @file connect.cpp
2022-01-18 22:38:56 +03:30
* @author Sina Karvandi (sina@hyperdbg.org)
2020-12-19 15:09:14 -08:00
* @brief .connect command
* @details
* @version 0.1
* @date 2020-05-27
*
* @copyright This project is released under the GNU Public License v3.
*
*/
#include "pch.h"
2020-05-27 12:09:57 -07:00
//
2022-01-06 23:24:05 +03:30
// Global Variables
2020-05-27 12:09:57 -07:00
//
2020-08-28 04:03:12 -07:00
extern BOOLEAN g_IsConnectedToHyperDbgLocally;
extern BOOLEAN g_IsConnectedToRemoteDebuggee;
extern BOOLEAN g_IsConnectedToRemoteDebugger;
extern BOOLEAN g_IsSerialConnectedToRemoteDebuggee;
extern BOOLEAN g_IsSerialConnectedToRemoteDebugger;
2021-03-22 18:19:39 +04:30
extern string g_ServerPort;
extern string g_ServerIp;
2020-08-28 04:03:12 -07:00
/**
2023-07-13 16:05:42 +09:00
* @brief help of the .connect command
*
* @return VOID
2020-08-28 04:03:12 -07:00
*/
2021-03-22 18:19:39 +04:30
VOID
CommandConnectHelp()
{
ShowMessages(".connect : connects to a remote or local machine to start "
"debugging.\n\n");
2022-02-09 02:16:58 +03:30
ShowMessages("syntax : \t.connect [local]\n");
ShowMessages("syntax : \t.connect [Ip (string)] [Port (decimal)]\n");
ShowMessages("\n");
2021-03-22 18:19:39 +04:30
ShowMessages("\t\te.g : .connect local\n");
2022-02-09 02:16:58 +03:30
ShowMessages("\t\te.g : .connect 192.168.1.5 50000\n");
}
2020-05-27 14:06:27 -07:00
2024-06-24 21:43:01 +09:00
/**
* @brief Connect to local debugger
*
* @return VOID
*/
VOID
ConnectLocalDebugger()
{
g_IsConnectedToHyperDbgLocally = TRUE;
}
/**
* @brief Connect to remote debugger
*
* @return BOOLEAN
*/
BOOLEAN
ConnectRemoteDebugger(const CHAR * Ip, const CHAR * Port)
{
//
// Validate IP and Port
if (!ValidateIP(Ip))
{
return FALSE;
}
if (Port != NULL)
{
if (!IsNumber(Port) || stoi(Port) > 65535 || stoi(Port) < 0)
{
return FALSE;
}
//
// connect to remote debugger
//
g_ServerIp = Ip;
g_ServerPort = Port;
RemoteConnectionConnect(Ip, Port);
}
else
{
//
// connect to remote debugger (default port)
//
g_ServerIp = Ip;
g_ServerPort = DEFAULT_PORT;
RemoteConnectionConnect(Ip, DEFAULT_PORT);
}
return TRUE;
}
2020-08-28 04:03:12 -07:00
/**
* @brief .connect command handler
*
2024-07-30 15:07:17 +09:00
* @param CommandTokens
* @param Command
2024-07-30 15:07:17 +09:00
*
* @return VOID
2020-08-28 04:03:12 -07:00
*/
2021-03-22 18:19:39 +04:30
VOID
CommandConnect(vector<CommandToken> CommandTokens, string Command)
2021-03-22 18:19:39 +04:30
{
2024-07-30 15:07:17 +09:00
string Ip;
string Port;
2021-03-22 18:19:39 +04:30
if (g_IsConnectedToHyperDbgLocally || g_IsConnectedToRemoteDebuggee ||
g_IsConnectedToRemoteDebugger)
{
ShowMessages("you're connected to a debugger, please use '.disconnect' "
2021-04-18 23:22:28 +04:30
"command\n");
2021-03-22 18:19:39 +04:30
return;
2020-08-28 04:03:12 -07:00
}
2021-03-22 18:19:39 +04:30
if (g_IsSerialConnectedToRemoteDebuggee ||
g_IsSerialConnectedToRemoteDebugger)
{
ShowMessages("you're connected to a an instance of HyperDbg, please use "
2021-04-18 23:22:28 +04:30
"'.debug close' command\n");
2021-03-22 18:19:39 +04:30
return;
}
2024-07-30 15:07:17 +09:00
if (CommandTokens.size() == 1)
2021-03-22 18:19:39 +04:30
{
//
// Means that user entered just a connect so we have to
// ask to connect to what ?
//
ShowMessages("incorrect use of the '%s'\n\n",
GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
2021-03-22 18:19:39 +04:30
CommandConnectHelp();
return;
}
2024-07-30 15:07:17 +09:00
else if (CompareLowerCaseStrings(CommandTokens.at(1), "local") && CommandTokens.size() == 2)
2021-03-22 18:19:39 +04:30
{
//
// connect to local debugger
//
2021-04-24 16:50:12 +04:30
ShowMessages("local debugging (vmi-mode)\n");
2024-07-30 15:07:17 +09:00
2024-06-24 21:43:01 +09:00
ConnectLocalDebugger();
2024-07-30 15:07:17 +09:00
2021-03-22 18:19:39 +04:30
return;
}
2024-07-30 15:07:17 +09:00
else if (CommandTokens.size() == 3 || CommandTokens.size() == 2)
2021-03-22 18:19:39 +04:30
{
2024-07-30 15:07:17 +09:00
Ip = GetCaseSensitiveStringFromCommandToken(CommandTokens.at(1));
2021-03-22 18:19:39 +04:30
2024-07-30 15:07:17 +09:00
if (CommandTokens.size() == 3)
2021-03-22 18:19:39 +04:30
{
2024-07-30 15:07:17 +09:00
Port = GetCaseSensitiveStringFromCommandToken(CommandTokens.at(2));
2021-03-22 18:19:39 +04:30
}
//
// means that probably wants to connect to a remote
// system, let's first check the if the parameters are
// valid
//
2024-07-30 15:07:17 +09:00
if (!ValidateIP(Ip))
2021-03-22 18:19:39 +04:30
{
ShowMessages("incorrect ip address\n");
return;
}
2024-07-30 15:07:17 +09:00
if (CommandTokens.size() == 3)
2021-03-22 18:19:39 +04:30
{
2024-07-30 15:07:17 +09:00
if (!IsNumber(Port) || stoi(Port) > 65535 || stoi(Port) < 0)
2021-03-22 18:19:39 +04:30
{
ShowMessages("incorrect port\n");
return;
}
//
// connect to remote debugger
//
2024-07-30 15:07:17 +09:00
ConnectRemoteDebugger(Ip.c_str(), Port.c_str());
2021-03-22 18:19:39 +04:30
}
else
{
//
// connect to remote debugger (default port)
//
2024-07-30 15:07:17 +09:00
ConnectRemoteDebugger(Ip.c_str(), NULL);
2021-03-22 18:19:39 +04:30
}
}
else
{
ShowMessages("incorrect use of the '%s'\n\n",
GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
2021-03-22 18:19:39 +04:30
CommandConnectHelp();
2020-08-28 04:03:12 -07:00
return;
}
}