]>
jfr.im git - irc/quakenet/newserv.git/blob - proxyscan/proxyscan.c
6 #include <sys/socket.h>
8 #include "../core/error.h"
9 #include "../core/events.h"
13 #include "../nick/nick.h"
14 #include "../core/hooks.h"
15 #include "../lib/sstring.h"
16 #include "../irc/irc_config.h"
17 #include "../localuser/localuser.h"
18 #include "../core/config.h"
20 #include "../core/schedule.h"
22 #include "../irc/irc.h"
23 #include "../lib/irc_string.h"
24 #include "../lib/version.h"
25 #include "../channel/channel.h"
26 #include "../localuser/localuserchannel.h"
27 #include "../core/nsmalloc.h"
28 #include "../lib/irc_ipv6.h"
32 #define SCANTIMEOUT 60
34 #define SCANHOSTHASHSIZE 1000
35 #define SCANHASHSIZE 400
37 /* It's unlikely you'll get 100k of preamble before a connect... */
38 #define READ_SANITY_LIMIT 102400
40 scan
*scantable
[SCANHASHSIZE
];
42 CommandTree
*ps_commands
;
57 int numscans
; /* number of scan types currently valid */
58 scantype thescans
[PSCAN_MAXSCANS
];
60 unsigned int hitsbyclass
[10];
61 unsigned int scansbyclass
[10];
65 unsigned short listenport
;
68 unsigned int ps_mailip
;
69 unsigned int ps_mailport
;
72 unsigned long scanspermin
;
73 unsigned long tempscanspermin
=0;
74 unsigned long lastscants
=0;
76 unsigned int ps_start_ts
=0;
83 void handlescansock(int fd
, short events
);
84 void timeoutscansock(void *arg
);
85 void proxyscan_newnick(int hooknum
, void *arg
);
86 void proxyscan_lostnick(int hooknum
, void *arg
);
87 void proxyscan_onconnect(int hooknum
, void *arg
);
88 void proxyscanuserhandler(nick
*target
, int message
, void **params
);
89 void registerproxyscannick();
90 void killsock(scan
*sp
, int outcome
);
92 void proxyscanstats(int hooknum
, void *arg
);
93 void sendlagwarning();
94 void proxyscan_newip(nick
*np
, unsigned long ip
);
95 int proxyscan_addscantype(int type
, int port
);
96 int proxyscan_delscantype(int type
, int port
);
98 int proxyscandostatus(void *sender
, int cargc
, char **cargv
);
99 int proxyscandebug(void *sender
, int cargc
, char **cargv
);
100 int proxyscandosave(void *sender
, int cargc
, char **cargv
);
101 int proxyscandospew(void *sender
, int cargc
, char **cargv
);
102 int proxyscandoshowkill(void *sender
, int cargc
, char **cargv
);
103 int proxyscandoscan(void *sender
, int cargc
, char **cargv
);
104 int proxyscandoaddscan(void *sender
, int cargc
, char **cargv
);
105 int proxyscandodelscan(void *sender
, int cargc
, char **cargv
);
106 int proxyscandoshowcommands(void *sender
, int cargc
, char **cargv
);
108 int proxyscan_addscantype(int type
, int port
) {
109 /* Check we have a spare scan slot */
111 if (numscans
>=PSCAN_MAXSCANS
)
114 thescans
[numscans
].type
=type
;
115 thescans
[numscans
].port
=port
;
116 thescans
[numscans
].hits
=0;
123 int proxyscan_delscantype(int type
, int port
) {
126 for (i
=0;i
<numscans
;i
++)
127 if (thescans
[i
].type
==type
&& thescans
[i
].port
==port
)
133 memmove(thescans
+i
, thescans
+(i
+1), (PSCAN_MAXSCANS
-(i
+1)) * sizeof(scantype
));
143 ps_start_ts
= time(NULL
);
147 ps_cache_ext
= registernodeext("proxyscancache");
148 if( ps_cache_ext
== -1 ) {
149 Error("proxyscan",ERR_INFO
,"failed to reg node ext");
152 ps_extscan_ext
= registernodeext("proxyscanextscan");
153 if ( ps_extscan_ext
== -1) {
154 Error("proxyscan",ERR_INFO
,"failed to reg node ext");
158 memset(scantable
,0,sizeof(scantable
));
164 ps_starttime
=time(NULL
);
168 lastscants
=time(NULL
);
171 cfgstr
=getcopyconfigitem("proxyscan","port","9999",6);
172 listenport
=strtol(cfgstr
->content
,NULL
,10);
175 /* Max concurrent scans */
176 cfgstr
=getcopyconfigitem("proxyscan","maxscans","200",5);
177 maxscans
=strtol(cfgstr
->content
,NULL
,10);
180 /* Clean host timeout */
181 cfgstr
=getcopyconfigitem("proxyscan","rescaninterval","3600",7);
182 rescaninterval
=strtol(cfgstr
->content
,NULL
,10);
183 cachehostinit(rescaninterval
);
186 /* this default will NOT work well */
187 myipstr
=getcopyconfigitem("proxyscan","ip","127.0.0.1",16);
189 sscanf(myipstr
->content
,"%d.%d.%d.%d",&ipbits
[0],&ipbits
[1],&ipbits
[2],&ipbits
[3]);
191 myip
=((ipbits
[0]&0xFF)<<24)+((ipbits
[1]&0xFF)<<16)+
192 ((ipbits
[2]&0xFF)<<8)+(ipbits
[3]&0xFF);
194 #if defined(PROXYSCAN_MAIL)
196 cfgstr
=getcopyconfigitem("proxyscan","mailerip","",16);
200 sscanf(cfgstr
->content
,"%d.%d.%d.%d",&ipbits
[0],&ipbits
[1],&ipbits
[2],&ipbits
[3]);
201 ps_mailip
= ((ipbits
[0]&0xFF)<<24)+((ipbits
[1]&0xFF)<<16)+
202 ((ipbits
[2]&0xFF)<<8)+(ipbits
[3]&0xFF);
206 ps_mailname
=getcopyconfigitem("proxyscan","mailname","some.mail.server",HOSTLEN
);
207 Error("proxyscan",ERR_INFO
,"Proxyscan mailer enabled; mailing to %s as %s.",IPlongtostr(ps_mailip
),ps_mailname
->content
);
215 /* Set up our nick on the network */
216 scheduleoneshot(time(NULL
),®isterproxyscannick
,NULL
);
218 registerhook(HOOK_SERVER_END_OF_BURST
, &proxyscan_onconnect
);
220 registerhook(HOOK_NICK_NEWNICK
,&proxyscan_newnick
);
222 registerhook(HOOK_CORE_STATSREQUEST
,&proxyscanstats
);
224 /* Read in the clean hosts */
227 /* Read in any custom ports to scan */
230 /* Set up the database */
231 if ((proxyscandbinit())!=0) {
237 ps_commands
= newcommandtree();
238 addcommandtotree(ps_commands
, "showcommands", 0, 0, &proxyscandoshowcommands
);
239 addcommandtotree(ps_commands
, "status", 0, 0, &proxyscandostatus
);
240 addcommandtotree(ps_commands
, "listopen", 0, 0, &proxyscandolistopen
);
241 addcommandtotree(ps_commands
, "save", 0, 0, &proxyscandosave
);
242 addcommandtotree(ps_commands
, "spew", 0, 0, &proxyscandospew
);
243 addcommandtotree(ps_commands
, "showkill", 0, 0, &proxyscandoshowkill
);
244 addcommandtotree(ps_commands
, "scan", 0, 0, &proxyscandoscan
);
245 addcommandtotree(ps_commands
, "addscan", 0, 0, &proxyscandoaddscan
);
246 addcommandtotree(ps_commands
, "delscan", 0, 0, &proxyscandodelscan
);
248 /* Default scan types */
249 proxyscan_addscantype(STYPE_HTTP
, 8080);
250 proxyscan_addscantype(STYPE_HTTP
, 80);
251 proxyscan_addscantype(STYPE_HTTP
, 6588);
252 proxyscan_addscantype(STYPE_HTTP
, 8000);
253 proxyscan_addscantype(STYPE_HTTP
, 3128);
254 proxyscan_addscantype(STYPE_HTTP
, 3802);
255 proxyscan_addscantype(STYPE_HTTP
, 5490);
256 proxyscan_addscantype(STYPE_HTTP
, 7441);
257 proxyscan_addscantype(STYPE_HTTP
, 808);
258 proxyscan_addscantype(STYPE_HTTP
, 3332);
259 proxyscan_addscantype(STYPE_HTTP
, 2282);
260 proxyscan_addscantype(STYPE_SOCKS4
, 559);
261 proxyscan_addscantype(STYPE_SOCKS4
, 1080);
262 proxyscan_addscantype(STYPE_SOCKS5
, 1080);
263 proxyscan_addscantype(STYPE_SOCKS4
, 1075);
264 proxyscan_addscantype(STYPE_SOCKS5
, 1075);
265 proxyscan_addscantype(STYPE_SOCKS4
, 2280);
266 proxyscan_addscantype(STYPE_SOCKS5
, 2280);
267 proxyscan_addscantype(STYPE_SOCKS4
, 1180);
268 proxyscan_addscantype(STYPE_SOCKS5
, 1180);
269 proxyscan_addscantype(STYPE_SOCKS4
, 9999);
270 proxyscan_addscantype(STYPE_SOCKS5
, 9999);
271 proxyscan_addscantype(STYPE_WINGATE
, 23);
272 proxyscan_addscantype(STYPE_CISCO
, 23);
273 proxyscan_addscantype(STYPE_WINGATE
, 1181);
274 proxyscan_addscantype(STYPE_SOCKS5
, 1978);
275 proxyscan_addscantype(STYPE_SOCKS5
, 1029);
276 proxyscan_addscantype(STYPE_SOCKS5
, 3801);
277 proxyscan_addscantype(STYPE_SOCKS5
, 3331);
278 proxyscan_addscantype(STYPE_HTTP
, 65506);
279 proxyscan_addscantype(STYPE_HTTP
, 63809);
280 proxyscan_addscantype(STYPE_HTTP
, 63000);
281 proxyscan_addscantype(STYPE_SOCKS4
, 29992);
284 schedulerecurring(time(NULL
)+3600,0,3600,&dumpcachehosts
,NULL
);
286 ps_logfile
=fopen("logs/proxyscan.log","a");
289 /* if we're already connected, assume we're just reloading module (i.e. have a completed burst) */
295 void registerproxyscannick(void *arg
) {
296 sstring
*psnick
,*psuser
,*pshost
,*psrealname
;
297 /* Set up our nick on the network */
300 psnick
=getcopyconfigitem("proxyscan","nick","P",NICKLEN
);
301 psuser
=getcopyconfigitem("proxyscan","user","proxyscan",USERLEN
);
302 pshost
=getcopyconfigitem("proxyscan","host","some.host",HOSTLEN
);
303 psrealname
=getcopyconfigitem("proxyscan","realname","Proxyscan",REALLEN
);
305 proxyscannick
=registerlocaluser(psnick
->content
,psuser
->content
,pshost
->content
,
307 NULL
,UMODE_OPER
|UMODE_SERVICE
|UMODE_DEAF
,
308 &proxyscanuserhandler
);
313 freesstring(psrealname
);
315 cp
=findchannel("#twilightzone");
317 localcreatechannel(proxyscannick
,"#twilightzone");
319 localjoinchannel(proxyscannick
,cp
);
320 localgetops(proxyscannick
,cp
);
326 deregisterlocaluser(proxyscannick
,NULL
);
328 deregisterhook(HOOK_SERVER_END_OF_BURST
, &proxyscan_onconnect
);
330 deregisterhook(HOOK_NICK_NEWNICK
,&proxyscan_newnick
);
332 deregisterhook(HOOK_CORE_STATSREQUEST
,&proxyscanstats
);
334 deleteschedule(NULL
,&dumpcachehosts
,NULL
);
336 destroycommandtree(ps_commands
);
338 /* Kill any scans in progress */
341 /* Dump the database - AFTER killallscans() which prunes it */
342 dumpcachehosts(NULL
);
344 /* dump any cached hosts before deleting the extensions */
345 releasenodeext(ps_cache_ext
);
346 releasenodeext(ps_extscan_ext
);
348 /* free() all our structures */
349 nsfreeall(POOL_PROXYSCAN
);
351 freesstring(myipstr
);
352 freesstring(ps_mailname
);
353 #if defined(PROXYSCAN_MAIL)
354 if (psm_mailerfd
!=-1)
355 deregisterhandler(psm_mailerfd
,1);
362 void proxyscanuserhandler(nick
*target
, int message
, void **params
) {
370 scheduleoneshot(time(NULL
)+1,®isterproxyscannick
,NULL
);
376 sender
=(nick
*)params
[0];
378 if (IsOper(sender
)) {
379 cargc
= splitline((char *)params
[1], cargv
, 20, 0);
384 ps_command
= findcommandintree(ps_commands
, cargv
[0], 1);
387 sendnoticetouser(proxyscannick
,sender
, "Unknown command.");
391 if ( ps_command
->maxparams
< (cargc
-1) ) {
392 rejoinline(cargv
[ps_command
->maxparams
], cargc
- (ps_command
->maxparams
));
393 cargc
= (ps_command
->maxparams
) + 1;
396 (ps_command
->handler
)((void *)sender
, cargc
- 1, &(cargv
[1]));
405 void addscantohash(scan
*sp
) {
407 hash
=(sp
->fd
)%SCANHASHSIZE
;
409 sp
->next
=scantable
[hash
];
415 void delscanfromhash(scan
*sp
) {
419 hash
=(sp
->fd
)%SCANHASHSIZE
;
421 for (sh
=&(scantable
[hash
]);*sh
;sh
=&((*sh
)->next
)) {
431 scan
*findscan(int fd
) {
435 hash
=fd%SCANHASHSIZE
;
437 for (sp
=scantable
[hash
];sp
;sp
=sp
->next
)
444 void startscan(patricia_node_t
*node
, int type
, int port
, int class) {
448 if (scansdone
>maxscans
)
450 /* ignore the first maxscans as this will skew our scans per second! */
452 if ((lastscants
+60) <= time(NULL
))
454 /* ok, at least 60 seconds has passed, calculate the scans per minute figure */
455 scantmp
= time(NULL
) - lastscants
;
456 scantmp
= tempscanspermin
/ scantmp
;
457 scantmp
= (scantmp
* 60);
458 scanspermin
= scantmp
;
459 lastscants
= time(NULL
);
466 sp
->outcome
=SOUTCOME_INPROGRESS
;
472 sp
->totalbytesread
=0;
473 memset(sp
->readbuf
, '\0', PSCAN_READBUFSIZE
);
475 sp
->fd
=createconnectsocket(irc_in_addr_v4_to_int(&((patricia_node_t
*)sp
->node
)->prefix
->sin
),sp
->port
);
476 sp
->state
=SSTATE_CONNECTING
;
478 /* Couldn't set up the socket? */
479 derefnode(iptree
,sp
->node
);
483 /* Wait until it is writeable */
484 registerhandler(sp
->fd
,POLLERR
|POLLHUP
|POLLOUT
,&handlescansock
);
485 /* And set a timeout */
486 sp
->sch
=scheduleoneshot(time(NULL
)+SCANTIMEOUT
,&timeoutscansock
,(void *)sp
);
490 void timeoutscansock(void *arg
) {
491 scan
*sp
=(scan
*)arg
;
493 killsock(sp
, SOUTCOME_CLOSED
);
496 void killsock(scan
*sp
, int outcome
) {
502 scansbyclass
[sp
->class]++;
504 /* Remove the socket from the schedule/event lists */
505 deregisterhandler(sp
->fd
,1); /* this will close the fd for us */
506 deleteschedule(sp
->sch
,&timeoutscansock
,(void *)sp
);
511 /* See if we need to queue another scan.. */
512 if (sp
->outcome
==SOUTCOME_CLOSED
&&
513 ((sp
->class==SCLASS_CHECK
) ||
514 (sp
->class==SCLASS_NORMAL
&& (sp
->state
==SSTATE_SENTREQUEST
|| sp
->state
==SSTATE_GOTRESPONSE
))))
515 queuescan(sp
->node
, sp
->type
, sp
->port
, SCLASS_PASS2
, time(NULL
)+300);
517 if (sp
->outcome
==SOUTCOME_CLOSED
&& sp
->class==SCLASS_PASS2
)
518 queuescan(sp
->node
, sp
->type
, sp
->port
, SCLASS_PASS3
, time(NULL
)+300);
520 if (sp
->outcome
==SOUTCOME_CLOSED
&& sp
->class==SCLASS_PASS3
)
521 queuescan(sp
->node
, sp
->type
, sp
->port
, SCLASS_PASS4
, time(NULL
)+300);
523 if (sp
->outcome
==SOUTCOME_OPEN
) {
524 hitsbyclass
[sp
->class]++;
526 /* Lets try and get the cache record. If there isn't one, make a new one. */
527 if (!(chp
=findcachehost(sp
->node
))) {
528 chp
=addcleanhost(time(NULL
));
529 patricia_ref_prefix(sp
->node
->prefix
);
530 sp
->node
->exts
[ps_cache_ext
] = chp
;
532 /* Stick it on the cache's list of proxies, if necessary */
533 for (fpp
=chp
->proxies
;fpp
;fpp
=fpp
->next
)
534 if (fpp
->type
==sp
->type
&& fpp
->port
==sp
->port
)
541 fpp
->next
=chp
->proxies
;
547 loggline(chp
, sp
->node
);
548 irc_send("%s GL * +*@%s 1800 %jd :Open Proxy, see http://www.quakenet.org/openproxies.html - ID: %d",
549 mynumeric
->content
,IPtostr(((patricia_node_t
*)sp
->node
)->prefix
->sin
),(intmax_t)getnettime(), chp
->glineid
);
550 Error("proxyscan",ERR_DEBUG
,"Found open proxy on host %s",IPtostr(((patricia_node_t
*)sp
->node
)->prefix
->sin
));
552 loggline(chp
, sp
->node
); /* Update log only */
556 for(i
=0;i
<numscans
;i
++) {
557 if (thescans
[i
].type
==sp
->type
&& thescans
[i
].port
==sp
->port
) {
564 /* deref prefix (referenced in queuescan) */
565 derefnode(iptree
,sp
->node
);
568 /* kick the queue.. */
572 void handlescansock(int fd
, short events
) {
578 unsigned short netport
;
580 if ((sp
=findscan(fd
))==NULL
) {
581 /* Not found; return and hope it goes away */
582 Error("proxyscan",ERR_ERROR
,"Unexpected message from fd %d",fd
);
586 /* It woke up, delete the alarm call.. */
587 deleteschedule(sp
->sch
,&timeoutscansock
,(void *)sp
);
589 if (events
& (POLLERR
|POLLHUP
)) {
590 /* Some kind of error; give up on this socket */
591 if (sp
->state
==SSTATE_GOTRESPONSE
) {
592 /* If the error occured while we were waiting for a response, we might have
593 * received the "OPEN PROXY!" message and the EOF at the same time, so continue
595 /* Error("proxyscan",ERR_DEBUG,"Got error in GOTRESPONSE state for %s, continuing.",IPtostr(sp->host->IP)); */
597 killsock(sp
, SOUTCOME_CLOSED
);
602 /* Otherwise, we got what we wanted.. */
605 case SSTATE_CONNECTING
:
606 /* OK, we got activity while connecting, so we're going to send some
607 * request depending on scan type. However, we can reregister everything
608 * here to save duplicate code: This code is common for all handlers */
610 /* Delete the old handler */
611 deregisterhandler(fd
,0);
612 /* Set the new one */
613 registerhandler(fd
,POLLERR
|POLLHUP
|POLLIN
,&handlescansock
);
614 sp
->sch
=scheduleoneshot(time(NULL
)+SCANTIMEOUT
,&timeoutscansock
,(void *)sp
);
616 sp
->state
=SSTATE_SENTREQUEST
;
620 sprintf(buf
,"CONNECT %s:%d HTTP/1.0\r\n\r\n",myipstr
->content
,listenport
);
621 if ((write(fd
,buf
,strlen(buf
)))<strlen(buf
)) {
622 /* We didn't write the full amount, DIE */
623 killsock(sp
,SOUTCOME_CLOSED
);
629 /* set up the buffer */
631 netport
=htons(listenport
);
632 memcpy(&buf
[4],&netip
,4);
633 memcpy(&buf
[2],&netport
,2);
637 if ((write(fd
,buf
,9))<9) {
638 /* Didn't write enough, give up */
639 killsock(sp
,SOUTCOME_CLOSED
);
645 /* Set up initial request buffer */
649 if ((write(fd
,buf
,3))>3) {
650 /* Didn't write enough, give up */
651 killsock(sp
,SOUTCOME_CLOSED
);
655 /* Now the actual connect request */
661 netport
=htons(listenport
);
662 memcpy(&buf
[4],&netip
,4);
663 memcpy(&buf
[8],&netport
,2);
664 res
=write(fd
,buf
,10);
666 killsock(sp
,SOUTCOME_CLOSED
);
672 /* Send wingate request */
673 sprintf(buf
,"%s:%d\r\n",myipstr
->content
,listenport
);
674 if((write(fd
,buf
,strlen(buf
)))<strlen(buf
)) {
675 killsock(sp
,SOUTCOME_CLOSED
);
681 /* Send cisco request */
682 sprintf(buf
,"cisco\r\n");
683 if ((write(fd
,buf
,strlen(buf
)))<strlen(buf
)) {
684 killsock(sp
, SOUTCOME_CLOSED
);
688 sprintf(buf
,"telnet %s %d\r\n",myipstr
->content
,listenport
);
689 if ((write(fd
,buf
,strlen(buf
)))<strlen(buf
)) {
690 killsock(sp
, SOUTCOME_CLOSED
);
702 case SSTATE_SENTREQUEST
:
703 res
=read(fd
, sp
->readbuf
+sp
->bytesread
, PSCAN_READBUFSIZE
-sp
->bytesread
);
706 if ((errno
!=EINTR
&& errno
!=EWOULDBLOCK
) || res
==0) {
708 killsock(sp
, SOUTCOME_CLOSED
);
714 sp
->totalbytesread
+=res
;
715 for (i
=0;i
<sp
->bytesread
- MAGICSTRINGLENGTH
;i
++) {
716 if (!strncmp(sp
->readbuf
+i
, MAGICSTRING
, MAGICSTRINGLENGTH
)) {
717 /* Found the magic string */
718 /* If the offset is 0, this means it was the first thing we got from the socket,
719 * so it's an actual IRCD (sheesh). Note that when the buffer is full and moved,
720 * the thing moved to offset 0 would previously have been tested as offset
721 * PSCAN_READBUFSIZE/2.
723 * Skip this checking for STYPE_DIRECT scans, which are used to detect trojans setting
724 * up portforwards (which will therefore show up as ircds, we rely on the port being
725 * strange enough to avoid false positives */
726 if (i
==0 && (sp
->type
!= STYPE_DIRECT
)) {
727 killsock(sp
, SOUTCOME_CLOSED
);
731 killsock(sp
, SOUTCOME_OPEN
);
736 /* If the buffer is full, move half of it along to make room */
737 if (sp
->bytesread
== PSCAN_READBUFSIZE
) {
738 memcpy(sp
->readbuf
, sp
->readbuf
+ (PSCAN_READBUFSIZE
)/2, PSCAN_READBUFSIZE
/2);
739 sp
->bytesread
= PSCAN_READBUFSIZE
/2;
742 /* Don't read data forever.. */
743 if (sp
->totalbytesread
> READ_SANITY_LIMIT
) {
744 killsock(sp
, SOUTCOME_CLOSED
);
748 /* No magic string yet, we schedule another timeout in case it comes later. */
749 sp
->sch
=scheduleoneshot(time(NULL
)+SCANTIMEOUT
,&timeoutscansock
,(void *)sp
);
754 void killallscans() {
759 for(i
=0;i
<SCANHASHSIZE
;i
++) {
760 for(sp
=scantable
[i
];sp
;sp
=sp
->next
) {
761 /* If there is a pending scan, delete it's clean host record.. */
762 if ((chp
=findcachehost(sp
->node
)) && !chp
->proxies
) {
763 sp
->node
->exts
[ps_cache_ext
] = NULL
;
764 derefnode(iptree
,sp
->node
);
769 deregisterhandler(sp
->fd
,1);
770 deleteschedule(sp
->sch
,&timeoutscansock
,(void *)(sp
));
776 void proxyscanstats(int hooknum
, void *arg
) {
779 sprintf(buf
, "Proxyscn: %6d/%4d scans complete/in progress. %d hosts queued.",
780 scansdone
,activescans
,queuedhosts
);
781 triggerhook(HOOK_CORE_STATSREPLY
,buf
);
782 sprintf(buf
, "Proxyscn: %6u known clean hosts",cleancount());
783 triggerhook(HOOK_CORE_STATSREPLY
,buf
);
786 void sendlagwarning() {
790 for (i
=0;i
<MAXSERVERS
;i
++) {
791 if (serverlist
[i
].maxusernum
>0) {
792 for(j
=0;j
<serverlist
[i
].maxusernum
;j
++) {
793 np
=servernicks
[i
][j
];
794 if (np
!=NULL
&& IsOper(np
)) {
795 sendnoticetouser(proxyscannick
,np
,"Warning: More than 20,000 hosts to scan - I'm lagging behind badly!");
802 int pscansort(const void *a
, const void *b
) {
803 int ra
= *((const int *)a
);
804 int rb
= *((const int *)b
);
806 return thescans
[ra
].hits
- thescans
[rb
].hits
;
809 int proxyscandostatus(void *sender
, int cargc
, char **cargv
) {
810 nick
*np
= (nick
*) sender
;
813 int ord
[PSCAN_MAXSCANS
];
815 sendnoticetouser(proxyscannick
,np
,"Service uptime: %s",longtoduration(time(NULL
)-ps_starttime
, 1));
816 sendnoticetouser(proxyscannick
,np
,"Total scans completed: %d",scansdone
);
817 sendnoticetouser(proxyscannick
,np
,"Total hosts glined: %d",glinedhosts
);
819 sendnoticetouser(proxyscannick
,np
,"pendingscan structures: %lu x %lu bytes = %lu bytes total",countpendingscan
,
820 sizeof(pendingscan
), (countpendingscan
* sizeof(pendingscan
)));
822 sendnoticetouser(proxyscannick
,np
,"Currently active scans: %d/%d",activescans
,maxscans
);
823 sendnoticetouser(proxyscannick
,np
,"Processing speed: %lu scans per minute",scanspermin
);
824 sendnoticetouser(proxyscannick
,np
,"Normal queued scans: %d",normalqueuedscans
);
825 sendnoticetouser(proxyscannick
,np
,"Timed queued scans: %d",prioqueuedscans
);
826 sendnoticetouser(proxyscannick
,np
,"'Clean' cached hosts: %d",cleancount());
827 sendnoticetouser(proxyscannick
,np
,"'Dirty' cached hosts: %d",dirtycount());
829 sendnoticetouser(proxyscannick
,np
,"Extra scans: %d", extrascancount());
831 sendnoticetouser(proxyscannick
,np
,"Open proxies, class %1d: %d/%d (%.2f%%)",i
,hitsbyclass
[i
],scansbyclass
[i
],((float)hitsbyclass
[i
]*100)/scansbyclass
[i
]);
833 for (i
=0;i
<numscans
;i
++)
834 totaldetects
+=thescans
[i
].hits
;
836 for (i
=0;i
<numscans
;i
++)
839 qsort(ord
,numscans
,sizeof(int),pscansort
);
841 sendnoticetouser(proxyscannick
,np
,"Scan type Port Detections");
842 for (i
=0;i
<numscans
;i
++)
843 sendnoticetouser(proxyscannick
,np
,"%-9s %-5d %d (%.2f%%)",
844 scantostr(thescans
[ord
[i
]].type
), thescans
[ord
[i
]].port
, thescans
[ord
[i
]].hits
, ((float)thescans
[ord
[i
]].hits
*100)/totaldetects
);
846 sendnoticetouser(proxyscannick
,np
,"End of list.");
850 int proxyscandebug(void *sender
, int cargc
, char **cargv
) {
851 /* Dump all scans.. */
853 int activescansfound
=0;
854 int totalscansfound
=0;
856 nick
*np
= (nick
*)sender
;
858 sendnoticetouser(proxyscannick
,np
,"Active scans : %d",activescans
);
860 for (i
=0;i
<SCANHASHSIZE
;i
++) {
861 for (sp
=scantable
[i
];sp
;sp
=sp
->next
) {
862 if (sp
->outcome
==SOUTCOME_INPROGRESS
) {
866 sendnoticetouser(proxyscannick
,np
,"fd: %d type: %d port: %d state: %d outcome: %d IP: %s",
867 sp
->fd
,sp
->type
,sp
->port
,sp
->state
,sp
->outcome
,IPtostr(((patricia_node_t
*)sp
->node
)->prefix
->sin
));
871 sendnoticetouser(proxyscannick
,np
,"Total %d scans actually found (%d active)",totalscansfound
,activescansfound
);
875 void proxyscan_onconnect(int hooknum
, void *arg
) {
878 /* kick the queue.. */
882 int proxyscandosave(void *sender
, int cargc
, char **cargv
) {
883 nick
*np
= (nick
*)sender
;
885 sendnoticetouser(proxyscannick
,np
,"Saving cached hosts...");
886 dumpcachehosts(NULL
);
887 sendnoticetouser(proxyscannick
,np
,"Done.");
891 int proxyscandospew(void *sender
, int cargc
, char **cargv
) {
892 nick
*np
= (nick
*)sender
;
894 /* check our database for the ip supplied */
895 unsigned long a
,b
,c
,d
;
896 if (4 != sscanf(cargv
[0],"%lu.%lu.%lu.%lu",&a
,&b
,&c
,&d
)) {
897 sendnoticetouser(proxyscannick
,np
,"Usage: spew x.x.x.x");
900 proxyscanspewip(proxyscannick
,np
,a
,b
,c
,d
);
905 int proxyscandoshowkill(void *sender
, int cargc
, char **cargv
) {
906 nick
*np
= (nick
*)sender
;
908 /* check our database for the id supplied */
910 if (1 != sscanf(cargv
[0],"%lu",&a
)) {
911 sendnoticetouser(proxyscannick
,np
,"Usage: showkill <id>");
914 proxyscanshowkill(proxyscannick
,np
,a
);
919 int proxyscandoscan(void *sender
, int cargc
, char **cargv
) {
920 nick
*np
= (nick
*)sender
;
921 patricia_node_t
*node
;
922 struct irc_in_addr sin
;
926 if (0 == ipmask_parse(cargv
[0],&sin
, &bits
)) {
927 sendnoticetouser(proxyscannick
,np
,"Usage: scan <ip>");
929 if (bits
!= 128 || !irc_in_addr_is_ipv4(&sin
) || irc_in_addr_is_loopback(&sin
)) {
930 sendnoticetouser(proxyscannick
,np
,"You may only scan single IPv4 IP's");
933 sendnoticetouser(proxyscannick
,np
,"Forcing scan of %s",IPtostr(sin
));
934 // * Just queue the scans directly here.. plonk them on the priority queue * /
935 node
= refnode(iptree
, &sin
, bits
); /* node leaks node here - should only allow to scan a nick? */
936 for(i
=0;i
<numscans
;i
++) {
937 /* @@@TODO: we allow a forced scan to scan the same IP multiple times atm */
938 queuescan(node
,thescans
[i
].type
,thescans
[i
].port
,SCLASS_NORMAL
,time(NULL
));
944 int proxyscandoaddscan(void *sender
, int cargc
, char **cargv
) {
945 nick
*np
= (nick
*)sender
;
948 if (sscanf(cargv
[0],"%u %u",&a
,&b
) != 2) {
949 sendnoticetouser(proxyscannick
,np
,"Usage: addscan <type> <port>");
951 sendnoticetouser(proxyscannick
,np
,"Added scan type %u port %u",a
,b
);
952 proxyscan_addscantype(a
,b
);
958 int proxyscandodelscan(void *sender
, int cargc
, char **cargv
) {
959 nick
*np
= (nick
*)sender
;
962 if (sscanf(cargv
[0],"%u %u",&a
,&b
) != 2) {
963 sendnoticetouser(proxyscannick
,np
,"Usage: delscan <type> <port>");
965 sendnoticetouser(proxyscannick
,np
,"Delete scan type %u port %u",a
,b
);
966 proxyscan_delscantype(a
,b
);
971 int proxyscandoshowcommands(void *sender
, int cargc
, char **cargv
) {
972 nick
*np
= (nick
*)sender
;
973 Command
*cmdlist
[100];
976 n
=getcommandlist(ps_commands
,cmdlist
,100);
978 sendnoticetouser(proxyscannick
,np
,"The following commands are registered at present:");
980 sendnoticetouser(proxyscannick
,np
,"%s",cmdlist
[i
]->command
->content
);
982 sendnoticetouser(proxyscannick
,np
,"End of list.");