]>
jfr.im git - irc/quakenet/newserv.git/blob - proxyscan/proxyscan.c
6 #include <sys/socket.h>
9 #include "../core/error.h"
10 #include "../core/events.h"
14 #include "../nick/nick.h"
15 #include "../core/hooks.h"
16 #include "../lib/sstring.h"
17 #include "../irc/irc_config.h"
18 #include "../localuser/localuser.h"
19 #include "../core/config.h"
21 #include "../core/schedule.h"
23 #include "../irc/irc.h"
24 #include "../lib/irc_string.h"
25 #include "../lib/version.h"
26 #include "../channel/channel.h"
27 #include "../localuser/localuserchannel.h"
28 #include "../core/nsmalloc.h"
29 #include "../lib/irc_ipv6.h"
33 #define SCANTIMEOUT 60
35 #define SCANHOSTHASHSIZE 1000
36 #define SCANHASHSIZE 400
38 /* It's unlikely you'll get 100k of preamble before a connect... */
39 #define READ_SANITY_LIMIT 102400
41 scan
*scantable
[SCANHASHSIZE
];
43 CommandTree
*ps_commands
;
58 int numscans
; /* number of scan types currently valid */
59 scantype thescans
[PSCAN_MAXSCANS
];
61 unsigned int hitsbyclass
[10];
62 unsigned int scansbyclass
[10];
66 unsigned short listenport
;
69 unsigned int ps_mailip
;
70 unsigned int ps_mailport
;
73 unsigned long scanspermin
;
74 unsigned long tempscanspermin
=0;
75 unsigned long lastscants
=0;
77 unsigned int ps_start_ts
=0;
84 void handlescansock(int fd
, short events
);
85 void timeoutscansock(void *arg
);
86 void proxyscan_newnick(int hooknum
, void *arg
);
87 void proxyscan_lostnick(int hooknum
, void *arg
);
88 void proxyscan_onconnect(int hooknum
, void *arg
);
89 void proxyscanuserhandler(nick
*target
, int message
, void **params
);
90 void registerproxyscannick();
91 void killsock(scan
*sp
, int outcome
);
93 void proxyscanstats(int hooknum
, void *arg
);
94 void sendlagwarning();
95 void proxyscan_newip(nick
*np
, unsigned long ip
);
96 int proxyscan_addscantype(int type
, int port
);
97 int proxyscan_delscantype(int type
, int port
);
99 int proxyscandostatus(void *sender
, int cargc
, char **cargv
);
100 int proxyscandebug(void *sender
, int cargc
, char **cargv
);
101 int proxyscandosave(void *sender
, int cargc
, char **cargv
);
102 int proxyscandospew(void *sender
, int cargc
, char **cargv
);
103 int proxyscandoshowkill(void *sender
, int cargc
, char **cargv
);
104 int proxyscandoscan(void *sender
, int cargc
, char **cargv
);
105 int proxyscandoscanfile(void *sender
, int cargc
, char **cargv
);
106 int proxyscandoaddscan(void *sender
, int cargc
, char **cargv
);
107 int proxyscandodelscan(void *sender
, int cargc
, char **cargv
);
108 int proxyscandoshowcommands(void *sender
, int cargc
, char **cargv
);
110 int proxyscan_addscantype(int type
, int port
) {
111 /* Check we have a spare scan slot */
113 if (numscans
>=PSCAN_MAXSCANS
)
116 thescans
[numscans
].type
=type
;
117 thescans
[numscans
].port
=port
;
118 thescans
[numscans
].hits
=0;
125 int proxyscan_delscantype(int type
, int port
) {
128 for (i
=0;i
<numscans
;i
++)
129 if (thescans
[i
].type
==type
&& thescans
[i
].port
==port
)
135 memmove(thescans
+i
, thescans
+(i
+1), (PSCAN_MAXSCANS
-(i
+1)) * sizeof(scantype
));
141 void ignorepipe(int signal_
) {
142 signal(SIGPIPE
, ignorepipe
); /* HACK */
149 signal(SIGPIPE
, ignorepipe
); /* HACK */
151 ps_start_ts
= time(NULL
);
155 ps_cache_ext
= registernodeext("proxyscancache");
156 if( ps_cache_ext
== -1 ) {
157 Error("proxyscan",ERR_INFO
,"failed to reg node ext");
160 ps_extscan_ext
= registernodeext("proxyscanextscan");
161 if ( ps_extscan_ext
== -1) {
162 Error("proxyscan",ERR_INFO
,"failed to reg node ext");
166 memset(scantable
,0,sizeof(scantable
));
172 ps_starttime
=time(NULL
);
176 lastscants
=time(NULL
);
179 cfgstr
=getcopyconfigitem("proxyscan","port","9999",6);
180 listenport
=strtol(cfgstr
->content
,NULL
,10);
183 /* Max concurrent scans */
184 cfgstr
=getcopyconfigitem("proxyscan","maxscans","200",10);
185 maxscans
=strtol(cfgstr
->content
,NULL
,10);
188 /* Clean host timeout */
189 cfgstr
=getcopyconfigitem("proxyscan","rescaninterval","3600",7);
190 rescaninterval
=strtol(cfgstr
->content
,NULL
,10);
191 cachehostinit(rescaninterval
);
194 /* this default will NOT work well */
195 myipstr
=getcopyconfigitem("proxyscan","ip","127.0.0.1",16);
197 sscanf(myipstr
->content
,"%d.%d.%d.%d",&ipbits
[0],&ipbits
[1],&ipbits
[2],&ipbits
[3]);
199 myip
=((ipbits
[0]&0xFF)<<24)+((ipbits
[1]&0xFF)<<16)+
200 ((ipbits
[2]&0xFF)<<8)+(ipbits
[3]&0xFF);
202 #if defined(PROXYSCAN_MAIL)
204 cfgstr
=getcopyconfigitem("proxyscan","mailerip","",16);
208 sscanf(cfgstr
->content
,"%d.%d.%d.%d",&ipbits
[0],&ipbits
[1],&ipbits
[2],&ipbits
[3]);
209 ps_mailip
= ((ipbits
[0]&0xFF)<<24)+((ipbits
[1]&0xFF)<<16)+
210 ((ipbits
[2]&0xFF)<<8)+(ipbits
[3]&0xFF);
214 ps_mailname
=getcopyconfigitem("proxyscan","mailname","some.mail.server",HOSTLEN
);
215 Error("proxyscan",ERR_INFO
,"Proxyscan mailer enabled; mailing to %s as %s.",IPlongtostr(ps_mailip
),ps_mailname
->content
);
223 /* Set up our nick on the network */
224 scheduleoneshot(time(NULL
),®isterproxyscannick
,NULL
);
226 registerhook(HOOK_SERVER_END_OF_BURST
, &proxyscan_onconnect
);
228 registerhook(HOOK_NICK_NEWNICK
,&proxyscan_newnick
);
230 registerhook(HOOK_CORE_STATSREQUEST
,&proxyscanstats
);
232 /* Read in the clean hosts */
235 /* Read in any custom ports to scan */
238 /* Set up the database */
239 if ((proxyscandbinit())!=0) {
245 ps_commands
= newcommandtree();
246 addcommandtotree(ps_commands
, "showcommands", 0, 0, &proxyscandoshowcommands
);
247 addcommandtotree(ps_commands
, "status", 0, 0, &proxyscandostatus
);
248 addcommandtotree(ps_commands
, "listopen", 0, 0, &proxyscandolistopen
);
249 addcommandtotree(ps_commands
, "save", 0, 0, &proxyscandosave
);
250 addcommandtotree(ps_commands
, "spew", 0, 1, &proxyscandospew
);
251 addcommandtotree(ps_commands
, "showkill", 0, 1, &proxyscandoshowkill
);
252 addcommandtotree(ps_commands
, "scan", 0, 1, &proxyscandoscan
);
253 addcommandtotree(ps_commands
, "scanfile", 0, 1, &proxyscandoscanfile
);
254 addcommandtotree(ps_commands
, "addscan", 0, 1, &proxyscandoaddscan
);
255 addcommandtotree(ps_commands
, "delscan", 0, 1, &proxyscandodelscan
);
257 /* Default scan types */
258 proxyscan_addscantype(STYPE_HTTP
, 8080);
259 proxyscan_addscantype(STYPE_HTTP
, 8118);
260 proxyscan_addscantype(STYPE_HTTP
, 80);
261 proxyscan_addscantype(STYPE_HTTP
, 6588);
262 proxyscan_addscantype(STYPE_HTTP
, 8000);
263 proxyscan_addscantype(STYPE_HTTP
, 3128);
264 proxyscan_addscantype(STYPE_HTTP
, 3802);
265 proxyscan_addscantype(STYPE_HTTP
, 5490);
266 proxyscan_addscantype(STYPE_HTTP
, 7441);
267 proxyscan_addscantype(STYPE_HTTP
, 808);
268 proxyscan_addscantype(STYPE_HTTP
, 3332);
269 proxyscan_addscantype(STYPE_HTTP
, 2282);
271 proxyscan_addscantype(STYPE_HTTP
, 1644);
272 proxyscan_addscantype(STYPE_HTTP
, 8081);
273 proxyscan_addscantype(STYPE_HTTP
, 443);
274 proxyscan_addscantype(STYPE_HTTP
, 1337);
275 proxyscan_addscantype(STYPE_HTTP
, 8888);
276 proxyscan_addscantype(STYPE_HTTP
, 8008);
277 proxyscan_addscantype(STYPE_HTTP
, 6515);
278 proxyscan_addscantype(STYPE_HTTP
, 27977);
280 proxyscan_addscantype(STYPE_SOCKS4
, 559);
281 proxyscan_addscantype(STYPE_SOCKS4
, 1080);
282 proxyscan_addscantype(STYPE_SOCKS5
, 1080);
283 proxyscan_addscantype(STYPE_SOCKS4
, 1075);
284 proxyscan_addscantype(STYPE_SOCKS5
, 1075);
285 proxyscan_addscantype(STYPE_SOCKS4
, 2280);
286 proxyscan_addscantype(STYPE_SOCKS5
, 2280);
287 proxyscan_addscantype(STYPE_SOCKS4
, 1180);
288 proxyscan_addscantype(STYPE_SOCKS5
, 1180);
289 proxyscan_addscantype(STYPE_SOCKS4
, 9999);
290 proxyscan_addscantype(STYPE_SOCKS5
, 9999);
291 proxyscan_addscantype(STYPE_WINGATE
, 23);
292 proxyscan_addscantype(STYPE_CISCO
, 23);
293 proxyscan_addscantype(STYPE_WINGATE
, 1181);
294 proxyscan_addscantype(STYPE_SOCKS5
, 1978);
295 proxyscan_addscantype(STYPE_SOCKS5
, 1029);
296 proxyscan_addscantype(STYPE_SOCKS5
, 3801);
297 proxyscan_addscantype(STYPE_SOCKS5
, 3331);
298 proxyscan_addscantype(STYPE_HTTP
, 65506);
299 proxyscan_addscantype(STYPE_HTTP
, 63809);
300 proxyscan_addscantype(STYPE_HTTP
, 63000);
301 proxyscan_addscantype(STYPE_SOCKS4
, 29992);
302 proxyscan_addscantype(STYPE_DIRECT_IRC
, 6666);
303 proxyscan_addscantype(STYPE_DIRECT_IRC
, 6667);
304 proxyscan_addscantype(STYPE_DIRECT_IRC
, 6668);
305 proxyscan_addscantype(STYPE_DIRECT_IRC
, 6669);
306 proxyscan_addscantype(STYPE_DIRECT_IRC
, 6670);
307 proxyscan_addscantype(STYPE_ROUTER
, 3128);
308 proxyscan_addscantype(STYPE_SOCKS5
, 27977);
311 schedulerecurring(time(NULL
)+3600,0,3600,&dumpcachehosts
,NULL
);
313 ps_logfile
=fopen("logs/proxyscan.log","a");
316 /* if we're already connected, assume we're just reloading module (i.e. have a completed burst) */
322 void registerproxyscannick(void *arg
) {
323 sstring
*psnick
,*psuser
,*pshost
,*psrealname
;
324 /* Set up our nick on the network */
327 psnick
=getcopyconfigitem("proxyscan","nick","P",NICKLEN
);
328 psuser
=getcopyconfigitem("proxyscan","user","proxyscan",USERLEN
);
329 pshost
=getcopyconfigitem("proxyscan","host","some.host",HOSTLEN
);
330 psrealname
=getcopyconfigitem("proxyscan","realname","Proxyscan",REALLEN
);
332 proxyscannick
=registerlocaluser(psnick
->content
,psuser
->content
,pshost
->content
,
334 NULL
,UMODE_OPER
|UMODE_SERVICE
|UMODE_DEAF
,
335 &proxyscanuserhandler
);
340 freesstring(psrealname
);
342 cp
=findchannel("#twilightzone");
344 localcreatechannel(proxyscannick
,"#twilightzone");
346 localjoinchannel(proxyscannick
,cp
);
347 localgetops(proxyscannick
,cp
);
353 deregisterlocaluser(proxyscannick
,NULL
);
355 deregisterhook(HOOK_SERVER_END_OF_BURST
, &proxyscan_onconnect
);
357 deregisterhook(HOOK_NICK_NEWNICK
,&proxyscan_newnick
);
359 deregisterhook(HOOK_CORE_STATSREQUEST
,&proxyscanstats
);
361 deleteschedule(NULL
,&dumpcachehosts
,NULL
);
363 destroycommandtree(ps_commands
);
365 /* Kill any scans in progress */
368 /* Dump the database - AFTER killallscans() which prunes it */
369 dumpcachehosts(NULL
);
371 /* dump any cached hosts before deleting the extensions */
372 releasenodeext(ps_cache_ext
);
373 releasenodeext(ps_extscan_ext
);
375 /* free() all our structures */
376 nsfreeall(POOL_PROXYSCAN
);
378 freesstring(myipstr
);
379 freesstring(ps_mailname
);
380 #if defined(PROXYSCAN_MAIL)
381 if (psm_mailerfd
!=-1)
382 deregisterhandler(psm_mailerfd
,1);
389 void proxyscanuserhandler(nick
*target
, int message
, void **params
) {
397 scheduleoneshot(time(NULL
)+1,®isterproxyscannick
,NULL
);
403 sender
=(nick
*)params
[0];
405 if (IsOper(sender
)) {
406 cargc
= splitline((char *)params
[1], cargv
, 20, 0);
411 ps_command
= findcommandintree(ps_commands
, cargv
[0], 1);
414 sendnoticetouser(proxyscannick
,sender
, "Unknown command.");
418 if ( ps_command
->maxparams
< (cargc
-1) ) {
419 rejoinline(cargv
[ps_command
->maxparams
], cargc
- (ps_command
->maxparams
));
420 cargc
= (ps_command
->maxparams
) + 1;
423 (ps_command
->handler
)((void *)sender
, cargc
- 1, &(cargv
[1]));
432 void addscantohash(scan
*sp
) {
434 hash
=(sp
->fd
)%SCANHASHSIZE
;
436 sp
->next
=scantable
[hash
];
442 void delscanfromhash(scan
*sp
) {
446 hash
=(sp
->fd
)%SCANHASHSIZE
;
448 for (sh
=&(scantable
[hash
]);*sh
;sh
=&((*sh
)->next
)) {
458 scan
*findscan(int fd
) {
462 hash
=fd%SCANHASHSIZE
;
464 for (sp
=scantable
[hash
];sp
;sp
=sp
->next
)
471 void startscan(patricia_node_t
*node
, int type
, int port
, int class) {
475 if (scansdone
>maxscans
)
477 /* ignore the first maxscans as this will skew our scans per second! */
479 if ((lastscants
+60) <= time(NULL
))
481 /* ok, at least 60 seconds has passed, calculate the scans per minute figure */
482 scantmp
= time(NULL
) - lastscants
;
483 scantmp
= tempscanspermin
/ scantmp
;
484 scantmp
= (scantmp
* 60);
485 scanspermin
= scantmp
;
486 lastscants
= time(NULL
);
493 sp
->outcome
=SOUTCOME_INPROGRESS
;
499 sp
->totalbytesread
=0;
500 memset(sp
->readbuf
, '\0', PSCAN_READBUFSIZE
);
502 sp
->fd
=createconnectsocket(&((patricia_node_t
*)sp
->node
)->prefix
->sin
,sp
->port
);
503 sp
->state
=SSTATE_CONNECTING
;
505 /* Couldn't set up the socket? */
506 derefnode(iptree
,sp
->node
);
510 /* Wait until it is writeable */
511 registerhandler(sp
->fd
,POLLERR
|POLLHUP
|POLLOUT
,&handlescansock
);
512 /* And set a timeout */
513 sp
->sch
=scheduleoneshot(time(NULL
)+SCANTIMEOUT
,&timeoutscansock
,(void *)sp
);
517 void timeoutscansock(void *arg
) {
518 scan
*sp
=(scan
*)arg
;
520 killsock(sp
, SOUTCOME_CLOSED
);
523 void killsock(scan
*sp
, int outcome
) {
530 scansbyclass
[sp
->class]++;
532 /* Remove the socket from the schedule/event lists */
533 deregisterhandler(sp
->fd
,1); /* this will close the fd for us */
534 deleteschedule(sp
->sch
,&timeoutscansock
,(void *)sp
);
539 /* See if we need to queue another scan.. */
540 if (sp
->outcome
==SOUTCOME_CLOSED
&&
541 ((sp
->class==SCLASS_CHECK
) ||
542 (sp
->class==SCLASS_NORMAL
&& (sp
->state
==SSTATE_SENTREQUEST
|| sp
->state
==SSTATE_GOTRESPONSE
))))
543 queuescan(sp
->node
, sp
->type
, sp
->port
, SCLASS_PASS2
, time(NULL
)+300);
545 if (sp
->outcome
==SOUTCOME_CLOSED
&& sp
->class==SCLASS_PASS2
)
546 queuescan(sp
->node
, sp
->type
, sp
->port
, SCLASS_PASS3
, time(NULL
)+300);
548 if (sp
->outcome
==SOUTCOME_CLOSED
&& sp
->class==SCLASS_PASS3
)
549 queuescan(sp
->node
, sp
->type
, sp
->port
, SCLASS_PASS4
, time(NULL
)+300);
551 if (sp
->outcome
==SOUTCOME_OPEN
) {
552 hitsbyclass
[sp
->class]++;
554 /* Lets try and get the cache record. If there isn't one, make a new one. */
555 if (!(chp
=findcachehost(sp
->node
))) {
556 chp
=addcleanhost(time(NULL
));
557 patricia_ref_prefix(sp
->node
->prefix
);
558 sp
->node
->exts
[ps_cache_ext
] = chp
;
560 /* Stick it on the cache's list of proxies, if necessary */
561 for (fpp
=chp
->proxies
;fpp
;fpp
=fpp
->next
)
562 if (fpp
->type
==sp
->type
&& fpp
->port
==sp
->port
)
569 fpp
->next
=chp
->proxies
;
574 /* the purpose of this lastgline stuff is to stop gline spam from one scan */
575 if (!chp
->glineid
|| (now
>=chp
->lastgline
+SCANTIMEOUT
)) {
582 loggline(chp
, sp
->node
);
583 ip
= IPtostr(((patricia_node_t
*)sp
->node
)->prefix
->sin
);
584 irc_send("%s GL * +*@%s 1800 %jd :Open Proxy, see http://www.quakenet.org/openproxies.html - ID: %d",
585 mynumeric
->content
,ip
,(intmax_t)getnettime(), chp
->glineid
);
586 Error("proxyscan",ERR_DEBUG
,"Found open proxy on host %s",ip
);
588 snprintf(buf
, sizeof(buf
), "proxy-gline %lu %s %s %hu %s", time(NULL
), ip
, scantostr(sp
->type
), sp
->port
, "irc.quakenet.org");
589 triggerhook(HOOK_SHADOW_SERVER
, (void *)buf
);
591 loggline(chp
, sp
->node
); /* Update log only */
595 for(i
=0;i
<numscans
;i
++) {
596 if (thescans
[i
].type
==sp
->type
&& thescans
[i
].port
==sp
->port
) {
603 /* deref prefix (referenced in queuescan) */
604 derefnode(iptree
,sp
->node
);
607 /* kick the queue.. */
611 void handlescansock(int fd
, short events
) {
617 unsigned short netport
;
619 if ((sp
=findscan(fd
))==NULL
) {
620 /* Not found; return and hope it goes away */
621 Error("proxyscan",ERR_ERROR
,"Unexpected message from fd %d",fd
);
625 /* It woke up, delete the alarm call.. */
626 deleteschedule(sp
->sch
,&timeoutscansock
,(void *)sp
);
628 if (events
& (POLLERR
|POLLHUP
)) {
629 /* Some kind of error; give up on this socket */
630 if (sp
->state
==SSTATE_GOTRESPONSE
) {
631 /* If the error occured while we were waiting for a response, we might have
632 * received the "OPEN PROXY!" message and the EOF at the same time, so continue
634 /* Error("proxyscan",ERR_DEBUG,"Got error in GOTRESPONSE state for %s, continuing.",IPtostr(sp->host->IP)); */
636 killsock(sp
, SOUTCOME_CLOSED
);
641 /* Otherwise, we got what we wanted.. */
644 case SSTATE_CONNECTING
:
645 /* OK, we got activity while connecting, so we're going to send some
646 * request depending on scan type. However, we can reregister everything
647 * here to save duplicate code: This code is common for all handlers */
649 /* Delete the old handler */
650 deregisterhandler(fd
,0);
651 /* Set the new one */
652 registerhandler(fd
,POLLERR
|POLLHUP
|POLLIN
,&handlescansock
);
653 sp
->sch
=scheduleoneshot(time(NULL
)+SCANTIMEOUT
,&timeoutscansock
,(void *)sp
);
655 sp
->state
=SSTATE_SENTREQUEST
;
659 sprintf(buf
,"CONNECT %s:%d HTTP/1.0\r\n\r\n\r\n",myipstr
->content
,listenport
);
660 if ((write(fd
,buf
,strlen(buf
)))<strlen(buf
)) {
661 /* We didn't write the full amount, DIE */
662 killsock(sp
,SOUTCOME_CLOSED
);
668 /* set up the buffer */
670 netport
=htons(listenport
);
671 memcpy(&buf
[4],&netip
,4);
672 memcpy(&buf
[2],&netport
,2);
676 if ((write(fd
,buf
,9))<9) {
677 /* Didn't write enough, give up */
678 killsock(sp
,SOUTCOME_CLOSED
);
684 /* Set up initial request buffer */
688 if ((write(fd
,buf
,3))>3) {
689 /* Didn't write enough, give up */
690 killsock(sp
,SOUTCOME_CLOSED
);
694 /* Now the actual connect request */
700 netport
=htons(listenport
);
701 memcpy(&buf
[4],&netip
,4);
702 memcpy(&buf
[8],&netport
,2);
703 res
=write(fd
,buf
,10);
705 killsock(sp
,SOUTCOME_CLOSED
);
711 /* Send wingate request */
712 sprintf(buf
,"%s:%d\r\n",myipstr
->content
,listenport
);
713 if((write(fd
,buf
,strlen(buf
)))<strlen(buf
)) {
714 killsock(sp
,SOUTCOME_CLOSED
);
720 /* Send cisco request */
721 sprintf(buf
,"cisco\r\n");
722 if ((write(fd
,buf
,strlen(buf
)))<strlen(buf
)) {
723 killsock(sp
, SOUTCOME_CLOSED
);
727 sprintf(buf
,"telnet %s %d\r\n",myipstr
->content
,listenport
);
728 if ((write(fd
,buf
,strlen(buf
)))<strlen(buf
)) {
729 killsock(sp
, SOUTCOME_CLOSED
);
739 case STYPE_DIRECT_IRC
:
740 sprintf(buf
,"PRIVMSG\r\n");
741 if ((write(fd
,buf
,strlen(buf
)))<strlen(buf
)) {
742 killsock(sp
, SOUTCOME_CLOSED
);
750 sprintf(buf
,"GET /nonexistent HTTP/1.0\r\n\r\n");
751 if ((write(fd
,buf
,strlen(buf
)))<strlen(buf
)) {
752 killsock(sp
, SOUTCOME_CLOSED
);
761 case SSTATE_SENTREQUEST
:
762 res
=read(fd
, sp
->readbuf
+sp
->bytesread
, PSCAN_READBUFSIZE
-sp
->bytesread
);
765 if ((errno
!=EINTR
&& errno
!=EWOULDBLOCK
) || res
==0) {
767 killsock(sp
, SOUTCOME_CLOSED
);
773 sp
->totalbytesread
+=res
;
777 int magicstringlength
;
779 if(sp
->type
== STYPE_DIRECT_IRC
) {
780 magicstring
= MAGICIRCSTRING
;
781 magicstringlength
= MAGICIRCSTRINGLENGTH
;
782 } else if(sp
->type
== STYPE_ROUTER
) {
783 magicstring
= MAGICROUTERSTRING
;
784 magicstringlength
= MAGICROUTERSTRINGLENGTH
;
786 magicstring
= MAGICSTRING
;
787 magicstringlength
= MAGICSTRINGLENGTH
;
788 if(sp
->totalbytesread
- res
== 0) {
794 for (i
=0;i
<sp
->bytesread
- magicstringlength
;i
++) {
795 if (!strncmp(sp
->readbuf
+i
, magicstring
, magicstringlength
)) {
796 /* Found the magic string */
797 /* If the offset is 0, this means it was the first thing we got from the socket,
798 * so it's an actual IRCD (sheesh). Note that when the buffer is full and moved,
799 * the thing moved to offset 0 would previously have been tested as offset
800 * PSCAN_READBUFSIZE/2.
802 * Skip this checking for STYPE_DIRECT scans, which are used to detect trojans setting
803 * up portforwards (which will therefore show up as ircds, we rely on the port being
804 * strange enough to avoid false positives */
805 if (i
==0 && (sp
->type
!= STYPE_DIRECT
)) {
806 killsock(sp
, SOUTCOME_CLOSED
);
810 killsock(sp
, SOUTCOME_OPEN
);
816 /* If the buffer is full, move half of it along to make room */
817 if (sp
->bytesread
== PSCAN_READBUFSIZE
) {
818 memcpy(sp
->readbuf
, sp
->readbuf
+ (PSCAN_READBUFSIZE
)/2, PSCAN_READBUFSIZE
/2);
819 sp
->bytesread
= PSCAN_READBUFSIZE
/2;
822 /* Don't read data forever.. */
823 if (sp
->totalbytesread
> READ_SANITY_LIMIT
) {
824 killsock(sp
, SOUTCOME_CLOSED
);
828 /* No magic string yet, we schedule another timeout in case it comes later. */
829 sp
->sch
=scheduleoneshot(time(NULL
)+SCANTIMEOUT
,&timeoutscansock
,(void *)sp
);
834 void killallscans() {
839 for(i
=0;i
<SCANHASHSIZE
;i
++) {
840 for(sp
=scantable
[i
];sp
;sp
=sp
->next
) {
841 /* If there is a pending scan, delete it's clean host record.. */
842 if ((chp
=findcachehost(sp
->node
)) && !chp
->proxies
) {
843 sp
->node
->exts
[ps_cache_ext
] = NULL
;
844 derefnode(iptree
,sp
->node
);
849 deregisterhandler(sp
->fd
,1);
850 deleteschedule(sp
->sch
,&timeoutscansock
,(void *)(sp
));
856 void proxyscanstats(int hooknum
, void *arg
) {
859 sprintf(buf
, "Proxyscn: %6d/%4d scans complete/in progress. %d hosts queued.",
860 scansdone
,activescans
,queuedhosts
);
861 triggerhook(HOOK_CORE_STATSREPLY
,buf
);
862 sprintf(buf
, "Proxyscn: %6u known clean hosts",cleancount());
863 triggerhook(HOOK_CORE_STATSREPLY
,buf
);
866 void sendlagwarning() {
870 for (i
=0;i
<MAXSERVERS
;i
++) {
871 if (serverlist
[i
].maxusernum
>0) {
872 for(j
=0;j
<serverlist
[i
].maxusernum
;j
++) {
873 np
=servernicks
[i
][j
];
874 if (np
!=NULL
&& IsOper(np
)) {
875 sendnoticetouser(proxyscannick
,np
,"Warning: More than 20,000 hosts to scan - I'm lagging behind badly!");
882 int pscansort(const void *a
, const void *b
) {
883 int ra
= *((const int *)a
);
884 int rb
= *((const int *)b
);
886 return thescans
[ra
].hits
- thescans
[rb
].hits
;
889 int proxyscandostatus(void *sender
, int cargc
, char **cargv
) {
890 nick
*np
= (nick
*) sender
;
893 int ord
[PSCAN_MAXSCANS
];
895 sendnoticetouser(proxyscannick
,np
,"Service uptime: %s",longtoduration(time(NULL
)-ps_starttime
, 1));
896 sendnoticetouser(proxyscannick
,np
,"Total scans completed: %d",scansdone
);
897 sendnoticetouser(proxyscannick
,np
,"Total hosts glined: %d",glinedhosts
);
899 sendnoticetouser(proxyscannick
,np
,"pendingscan structures: %lu x %lu bytes = %lu bytes total",countpendingscan
,
900 sizeof(pendingscan
), (countpendingscan
* sizeof(pendingscan
)));
902 sendnoticetouser(proxyscannick
,np
,"Currently active scans: %d/%d",activescans
,maxscans
);
903 sendnoticetouser(proxyscannick
,np
,"Processing speed: %lu scans per minute",scanspermin
);
904 sendnoticetouser(proxyscannick
,np
,"Normal queued scans: %d",normalqueuedscans
);
905 sendnoticetouser(proxyscannick
,np
,"Timed queued scans: %d",prioqueuedscans
);
906 sendnoticetouser(proxyscannick
,np
,"'Clean' cached hosts: %d",cleancount());
907 sendnoticetouser(proxyscannick
,np
,"'Dirty' cached hosts: %d",dirtycount());
909 sendnoticetouser(proxyscannick
,np
,"Extra scans: %d", extrascancount());
911 sendnoticetouser(proxyscannick
,np
,"Open proxies, class %1d: %d/%d (%.2f%%)",i
,hitsbyclass
[i
],scansbyclass
[i
],((float)hitsbyclass
[i
]*100)/scansbyclass
[i
]);
913 for (i
=0;i
<numscans
;i
++)
914 totaldetects
+=thescans
[i
].hits
;
916 for (i
=0;i
<numscans
;i
++)
919 qsort(ord
,numscans
,sizeof(int),pscansort
);
921 sendnoticetouser(proxyscannick
,np
,"Scan type Port Detections");
922 for (i
=0;i
<numscans
;i
++)
923 sendnoticetouser(proxyscannick
,np
,"%-9s %-5d %d (%.2f%%)",
924 scantostr(thescans
[ord
[i
]].type
), thescans
[ord
[i
]].port
, thescans
[ord
[i
]].hits
, ((float)thescans
[ord
[i
]].hits
*100)/totaldetects
);
926 sendnoticetouser(proxyscannick
,np
,"End of list.");
930 int proxyscandebug(void *sender
, int cargc
, char **cargv
) {
931 /* Dump all scans.. */
933 int activescansfound
=0;
934 int totalscansfound
=0;
936 nick
*np
= (nick
*)sender
;
938 sendnoticetouser(proxyscannick
,np
,"Active scans : %d",activescans
);
940 for (i
=0;i
<SCANHASHSIZE
;i
++) {
941 for (sp
=scantable
[i
];sp
;sp
=sp
->next
) {
942 if (sp
->outcome
==SOUTCOME_INPROGRESS
) {
946 sendnoticetouser(proxyscannick
,np
,"fd: %d type: %d port: %d state: %d outcome: %d IP: %s",
947 sp
->fd
,sp
->type
,sp
->port
,sp
->state
,sp
->outcome
,IPtostr(((patricia_node_t
*)sp
->node
)->prefix
->sin
));
951 sendnoticetouser(proxyscannick
,np
,"Total %d scans actually found (%d active)",totalscansfound
,activescansfound
);
955 void proxyscan_onconnect(int hooknum
, void *arg
) {
958 /* kick the queue.. */
962 int proxyscandosave(void *sender
, int cargc
, char **cargv
) {
963 nick
*np
= (nick
*)sender
;
965 sendnoticetouser(proxyscannick
,np
,"Saving cached hosts...");
966 dumpcachehosts(NULL
);
967 sendnoticetouser(proxyscannick
,np
,"Done.");
971 int proxyscandospew(void *sender
, int cargc
, char **cargv
) {
972 nick
*np
= (nick
*)sender
;
977 /* check our database for the ip supplied */
978 unsigned long a
,b
,c
,d
;
979 if (4 != sscanf(cargv
[0],"%lu.%lu.%lu.%lu",&a
,&b
,&c
,&d
)) {
980 sendnoticetouser(proxyscannick
,np
,"Usage: spew x.x.x.x");
983 proxyscanspewip(proxyscannick
,np
,a
,b
,c
,d
);
988 int proxyscandoshowkill(void *sender
, int cargc
, char **cargv
) {
989 nick
*np
= (nick
*)sender
;
994 /* check our database for the id supplied */
996 if (1 != sscanf(cargv
[0],"%lu",&a
)) {
997 sendnoticetouser(proxyscannick
,np
,"Usage: showkill <id>");
1000 proxyscanshowkill(proxyscannick
,np
,a
);
1005 void startnickscan(nick
*np
) {
1006 time_t t
= time(NULL
);
1008 for(i
=0;i
<numscans
;i
++) {
1009 /* @@@TODO: we allow a forced scan to scan the same IP multiple times atm */
1010 queuescan(np
->ipnode
,thescans
[i
].type
,thescans
[i
].port
,SCLASS_NORMAL
,t
);
1014 int proxyscandoscan(void *sender
, int cargc
, char **cargv
) {
1015 nick
*np
= (nick
*)sender
;
1016 patricia_node_t
*node
;
1017 struct irc_in_addr sin
;
1024 if (0 == ipmask_parse(cargv
[0],&sin
, &bits
)) {
1025 sendnoticetouser(proxyscannick
,np
,"Usage: scan <ip>");
1027 if (bits
!= 128 || !irc_in_addr_is_ipv4(&sin
) || irc_in_addr_is_loopback(&sin
)) {
1028 sendnoticetouser(proxyscannick
,np
,"You may only scan single IPv4 IP's");
1031 if (bits
!= 128 || irc_in_addr_is_loopback(&sin
)) {
1032 sendnoticetouser(proxyscannick
,np
,"You may only scan single IP's");
1037 sendnoticetouser(proxyscannick
,np
,"Forcing scan of %s",IPtostr(sin
));
1038 // * Just queue the scans directly here.. plonk them on the priority queue * /
1039 node
= refnode(iptree
, &sin
, bits
); /* node leaks node here - should only allow to scan a nick? */
1041 for(i
=0;i
<numscans
;i
++) {
1042 /* @@@TODO: we allow a forced scan to scan the same IP multiple times atm */
1043 queuescan(node
,thescans
[i
].type
,thescans
[i
].port
,SCLASS_NORMAL
,t
);
1049 int proxyscandoscanfile(void *sender
, int cargc
, char **cargv
) {
1050 nick
*np
= (nick
*)sender
;
1052 time_t t
= time(NULL
);
1053 int pscantypes
[PSCAN_MAXSCANS
];
1058 if ((fp
=fopen("data/doscan.txt","r"))==NULL
) {
1059 sendnoticetouser(proxyscannick
,np
,"Unable to open file for reading!");
1067 for(i
=0;i
<numscans
;i
++)
1068 if(thescans
[i
].type
> maxno
)
1069 maxno
= thescans
[i
].type
;
1071 tscantypes
= malloc(sizeof(int) * maxno
);
1072 for(i
=0;i
<maxno
;i
++)
1075 for(i
=0;i
<numscans
;i
++)
1076 tscantypes
[thescans
[i
].type
] = 1;
1078 for(i
=0,maxtypes
=0;i
<maxno
;i
++)
1080 pscantypes
[maxtypes
++] = i
;
1087 patricia_node_t
*node
;
1088 struct irc_in_addr sin
;
1090 unsigned short port
;
1091 char buf
[512], ip
[512];
1099 res
=sscanf(buf
,"%s %hu",ip
,&port
);
1104 if (0 == ipmask_parse(ip
,&sin
, &bits
)) {
1107 node
= refnode(iptree
, &sin
, bits
); /* LEAKS */
1109 for(i
=0;i
<maxtypes
;i
++) {
1110 queuescan(node
,pscantypes
[i
],port
,SCLASS_NORMAL
,t
);
1119 sendnoticetouser(proxyscannick
,np
,"Started %d scans...", count
);
1123 int proxyscandoaddscan(void *sender
, int cargc
, char **cargv
) {
1124 nick
*np
= (nick
*)sender
;
1130 if (sscanf(cargv
[0],"%u %u",&a
,&b
) != 2) {
1131 sendnoticetouser(proxyscannick
,np
,"Usage: addscan <type> <port>");
1133 sendnoticetouser(proxyscannick
,np
,"Added scan type %u port %u",a
,b
);
1134 proxyscan_addscantype(a
,b
);
1140 int proxyscandodelscan(void *sender
, int cargc
, char **cargv
) {
1141 nick
*np
= (nick
*)sender
;
1147 if (sscanf(cargv
[0],"%u %u",&a
,&b
) != 2) {
1148 sendnoticetouser(proxyscannick
,np
,"Usage: delscan <type> <port>");
1150 sendnoticetouser(proxyscannick
,np
,"Delete scan type %u port %u",a
,b
);
1151 proxyscan_delscantype(a
,b
);
1156 int proxyscandoshowcommands(void *sender
, int cargc
, char **cargv
) {
1157 nick
*np
= (nick
*)sender
;
1158 Command
*cmdlist
[100];
1161 n
=getcommandlist(ps_commands
,cmdlist
,100);
1163 sendnoticetouser(proxyscannick
,np
,"The following commands are registered at present:");
1165 sendnoticetouser(proxyscannick
,np
,"%s",cmdlist
[i
]->command
->content
);
1167 sendnoticetouser(proxyscannick
,np
,"End of list.");