12 def runSocks4A(nonce
, targetHost
, targetPort
, targetURL
):
13 s
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
14 s
.connect(("127.0.0.1", SOCKS_PORT
))
15 socksheader
= struct
.pack("!BBHL", 4, 0x01, targetPort
, 1)
17 socksheader
= "%s%s\x00%s\x00" %(socksheader
, username
, nonce
)
20 version
,status
,port
=struct
.unpack("!BBH",response
[:4])
22 print "Error: non-successful SOCKS status"
26 s
.send("GET %s HTTP/1.0\r\nHost: %s\r\n\r\n"%(targetURL
,targetHost
))
30 print "WOOT! Got a web page."
34 HOSTS_TO_TEST
= [ "serifos", "chaoscomputerclub", "NetWorkXXIII", "caethaver2",
35 "theoryorg", "samaire", "alrua", "ihopethisisunique",
36 "xolotl", "cacophony", "ghettocluster", "torserverzillion",
37 "ned", "richhomednsorg", "subzeronet"]
38 EXITS_TO_TEST
= [ "pvt", ]
40 HOSTS_THAT_WORK
= [ "serifos", "rodos", "moria2", "chaoscomputerclub"]
41 EXITS_THAT_WORK
= [ "serifos", "rodos"]
43 TARGETS
= [ ("belegost.mit.edu", "/"),
46 N_CIRCS_TO_TRY
= 5*len(HOSTS_TO_TEST
)
53 htw
= HOSTS_THAT_WORK
[:]
55 path
= htw
[:CIRC_LEN
-2] + \
56 [random
.choice(HOSTS_TO_TEST
)] + \
57 [random
.choice(EXITS_THAT_WORK
)]
58 circid
= TorControl
.extend_circuit(s
, 0, path
)
61 lst
= HOST_STATUS
.setdefault(name
,[0,0])
68 _h
= lambda body
,circs
=circs
,s1
=s1
,s2
=s2
,s
=s
:handleEvent(s
,body
,
70 TorControl
._event
_handler
= _h
71 TorControl
.set_events(s
,
72 [TorControl
.EVENT_TYPE
.CIRCSTATUS
,
73 TorControl
.EVENT_TYPE
.STREAMSTATUS
])
74 TorControl
.set_option(s
,"__LeaveStreamsUnattached 1")
76 while N_CIRCS_DONE
< N_CIRCS_TO_TRY
:
77 while len(circs
) < CIRCS_AT_A_TIME
:
79 print "launching circuit %s to %s"%(c
,p
)
81 _
, tp
, body
= TorControl
.receive_message(s
)
82 if tp
== TorControl
.MSG_TYPE
.EVENT
:
83 handleEvent(s
, body
, circs
, s1
,s2
)
84 i
= HOST_STATUS
.items()
86 for n
,(all
,good
) in i
:
87 print "%s in %s circuits; %s/%s ok"%(n
,all
,good
,all
)
89 def handleEvent(s
, body
, circs
, streamsByNonce
, streamsByIdent
):
91 event
, args
= TorControl
.unpack_event(body
)
92 if event
== TorControl
.EVENT_TYPE
.STREAMSTATUS
:
93 status
, ident
, target
= args
94 print "Got stream event:",TorControl
.STREAM_STATUS
.nameOf
[status
],\
96 if status
in (TorControl
.STREAM_STATUS
.NEW_CONNECT
,
97 TorControl
.STREAM_STATUS
.NEW_RESOLVE
,
98 TorControl
.STREAM_STATUS
.DETACHED
):
99 target
,port
=target
.split(":")
100 if not target
.endswith(".exnonce"):
101 TorControl
.attach_stream(s
, ident
, 0)
103 circid
, (host
,url
) = streamsByNonce
[target
]
104 streamsByIdent
[ident
] = circid
,(host
,url
)
105 print "Redirecting circuit",circid
,"to",host
106 TorControl
.redirect_stream(s
, ident
, host
)
107 TorControl
.attach_stream(s
, ident
, circid
)
108 elif status
in (TorControl
.STREAM_STATUS
.CLOSED
,
109 TorControl
.STREAM_STATUS
.FAILED
):
110 circid
, (host
,url
) = streamsByIdent
[ident
]
111 if circs
.has_key(circid
):
112 for name
in circs
[circid
]:
113 HOST_STATUS
[name
][1] += 1
116 print N_CIRCS_DONE
, "circuit attempts done"
117 del streamsByIdent
[ident
]
118 elif event
== TorControl
.EVENT_TYPE
.CIRCSTATUS
:
119 status
, ident
, path
= args
120 print "Got circuit event",TorControl
.CIRC_STATUS
.nameOf
[status
],\
122 if status
in (TorControl
.CIRC_STATUS
.CLOSED
,
123 TorControl
.CIRC_STATUS
.FAILED
):
124 if circs
.has_key(ident
):
125 print "Circuit failed."
128 print N_CIRCS_DONE
, "circuit attempts done"
129 elif status
== TorControl
.CIRC_STATUS
.BUILT
:
130 nonce
= random
.randint(1,100000000)
131 nonce
= "%s.exnonce" % nonce
132 host
,url
= random
.choice(TARGETS
)
133 streamsByNonce
[nonce
] = ident
, (host
,url
)
134 print "Launching socks4a connection"
135 t
= threading
.Thread(target
=runSocks4A
, args
=(nonce
, host
, 80, url
))
141 s
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
142 s
.connect(("127.0.0.1", CONTROL_PORT
))
143 TorControl
.authenticate(s
)
146 if __name__
== '__main__':