11-19-2017, 06:05 +0100
AuthorPost
thommey  09-01-2010, 16:18   | [TCL] Coroutine enabled portscanner Jabber
(Administrator)
Member since 03/2004
62 Posts
Self-documenting
                                                                                                                              
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env tclsh8.6
# portscan.tcl

package require Tcl 8.6

# Helper function 1 (uplevel executes in callers stack - just code grouping)
proc portscan_getfeedback {} {
    uplevel 1 {
        lassign [yield] s port state
        incr myconns -1
        while {$state eq "timeout" && $port in $notimeoutports} {
            lassign [yield] s port state
        }
    }
}
# Helper function 2 (uplevel executes in callers stack - just code grouping)
proc portscan_assignstate {} {
    uplevel 1 {
        if {$state eq "open"} {
            lappend notimeoutports $port
            if {[fconfigure $s -error] eq ""} {
                lappend openports $port
            }
        }
        catch {close $s}
    }
}
# Here's the real thing.
# Syntax: portscan IP [list PORT1 PORT2 ..] MAXCONNECTIONS TIMEOUT_IN_MS
# Ex: portscan 127.0.0.1 {80 8080 3128 22 21 23 119} 3 5000
# This MUST be called *[b]in/from[/b]* a coroutine
proc portscan {ip ports conns timeout} {
    set myconns 0
    set openports [list]
    set notimeoutports [list]
    foreach port $ports {
        set s [socket -async $ip $port]
        fileevent $s writable [list [info coroutine] [list $s $port open]]
        after $timeout catch [list [list [info coroutine] [list $s $port timeout]]]
        incr myconns
        if {$myconns < $conns} {
            continue
        } else {
            portscan_getfeedback
            portscan_assignstate
        }
    }
    while {$myconns} {
        portscan_getfeedback
        portscan_assignstate
    }
    return $openports
}
proc testscan {ip ports conns timeout} {
    set openports [portscan $ip $ports $conns $timeout]
    puts "Open ports: $openports"
    exit 0
}
if {[llength $argv] != 4} {
    puts "Syntax: $argv0 <ip> <port1,port2,..> <connections> <timeout in ms>"
    puts "Ex: $argv0 127.0.0.1 80,3128,8080,22,23 2 2500"
    exit 1
}
lassign $argv ip ports conns timeout
coroutine coro_789572 testscan $ip [split $ports ,] $conns $timeout
vwait forever

This post was edited 2 times, last on 01-10-2011, 17:56 by thommey
Advanced options for this topic:

Ignore this topic (Do not list this topic in the "unread topics" search. You are currently not ignoring this topic.)
Hide this topic (Hidden topics are not displayed in the topics list. This topic is currently not hidden.)
Go to forum

Unclassified NewsBoard 1.5.3-d | © 2003-4 by Yves Goergen