tor-resolve.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/usr/bin/python
  2. #$Id$
  3. import socket
  4. import struct
  5. import sys
  6. def socks4AResolveRequest(hostname):
  7. version = 4
  8. command = 0xF0
  9. port = 0
  10. addr = 0x0000001
  11. username = ""
  12. reqheader = struct.pack("!BBHL", version, command, port, addr)
  13. return "%s%s\x00%s\x00"%(reqheader,username,hostname)
  14. def socks4AParseResponse(response):
  15. RESPONSE_LEN = 8
  16. if len(response) < RESPONSE_LEN:
  17. return None
  18. assert len(response) >= RESPONSE_LEN
  19. version,status,port = struct.unpack("!BBH",response[:4])
  20. assert version == 0
  21. assert port == 0
  22. if status == 90:
  23. return "%d.%d.%d.%d"%tuple(map(ord, response[4:]))
  24. else:
  25. return "ERROR (status %d)"%status
  26. def socks5Hello():
  27. return "\x05\x01\x00"
  28. def socks5ParseHello(response):
  29. if response != "\x05\x00":
  30. raise ValueError("Bizarre socks5 response")
  31. def socks5ResolveRequest(hostname, atype=0x03, command=0xF0):
  32. version = 5
  33. rsv = 0
  34. port = 0
  35. reqheader = struct.pack("!BBBB",version, command, rsv, atype)
  36. if atype == 0x03:
  37. reqheader += struct.pack("!B", len(hostname))
  38. portstr = struct.pack("!H",port)
  39. return "%s%s%s"%(reqheader,hostname,portstr)
  40. def socks5ParseResponse(r):
  41. if len(r)<8:
  42. return None
  43. version, reply, rsv, atype = struct.unpack("!BBBB",r[:4])
  44. assert version==5
  45. assert rsv==0
  46. if reply != 0x00:
  47. return "ERROR",reply
  48. assert atype in (0x01,0x03,0x04)
  49. if atype != 0x03:
  50. expected_len = 4 + ({1:4,4:16}[atype]) + 2
  51. if len(r) < expected_len:
  52. return None
  53. elif len(r) > expected_len:
  54. raise ValueError("Overlong socks5 reply!")
  55. addr = r[4:-2]
  56. if atype == 0x01:
  57. return "%d.%d.%d.%d"%tuple(map(ord,addr))
  58. else:
  59. # not really the right way to format IPv6
  60. return "IPv6: %s"%(":".join([hex(ord(c)) for c in addr]))
  61. else:
  62. hlen, = struct.unpack("!B", r[4])
  63. expected_len = 5 + hlen + 2
  64. if len(r) < expected_len:
  65. return None
  66. return r[5:-2]
  67. def socks5ResolvePTRRequest(hostname):
  68. return socks5ResolveRequest(socket.inet_aton(hostname),
  69. atype=1, command = 0xF1)
  70. def parseHostAndPort(h):
  71. host, port = "localhost", 9050
  72. if ":" in h:
  73. i = h.index(":")
  74. host = h[:i]
  75. try:
  76. port = int(h[i+1:])
  77. except ValueError:
  78. print "Bad hostname %r"%h
  79. sys.exit(1)
  80. elif h:
  81. try:
  82. port = int(h)
  83. except ValueError:
  84. host = h
  85. return host, port
  86. def resolve(hostname, sockshost, socksport, socksver=4, reverse=0):
  87. assert socksver in (4,5)
  88. if socksver == 4:
  89. fmt = socks4AResolveRequest
  90. parse = socks4AParseResponse
  91. elif not reverse:
  92. fmt = socks5ResolveRequest
  93. parse = socks5ParseResponse
  94. else:
  95. fmt = socks5ResolvePTRRequest
  96. parse = socks5ParseResponse
  97. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  98. s.connect((sockshost,socksport))
  99. if socksver == 5:
  100. s.send(socks5Hello())
  101. socks5ParseHello(s.recv(2))
  102. s.send(fmt(hostname))
  103. answer = s.recv(6)
  104. result = parse(answer)
  105. while result is None:
  106. more = s.recv(1)
  107. if not more:
  108. return None
  109. answer += more
  110. result = parse(answer)
  111. print "Got answer",result
  112. m = s.recv(1)
  113. if m:
  114. print "Got extra data too: %r"%m
  115. return result
  116. if __name__ == '__main__':
  117. if len(sys.argv) not in (2,3,4):
  118. print "Syntax: resolve.py [-4|-5] hostname [sockshost:socksport]"
  119. sys.exit(0)
  120. socksver = 4
  121. reverse = 0
  122. while sys.argv[1][0] == '-':
  123. if sys.argv[1] in ("-4", "-5"):
  124. socksver = int(sys.argv[1][1])
  125. del sys.argv[1]
  126. elif sys.argv[1] == '-x':
  127. reverse = 1
  128. del sys.argv[1]
  129. elif sys.argv[1] == '--':
  130. break
  131. if len(sys.argv) >= 4:
  132. print "Syntax: resolve.py [-x] [-4|-5] hostname [sockshost:socksport]"
  133. sys.exit(0)
  134. if len(sys.argv) == 3:
  135. sh,sp = parseHostAndPort(sys.argv[2])
  136. else:
  137. sh,sp = parseHostAndPort("")
  138. if reverse and socksver == 4:
  139. socksver = 5
  140. resolve(sys.argv[1], sh, sp, socksver, reverse)