我最近买了Justin Seitz编写的Black Hat Python书,因为我一直有兴趣进入Python并且玩networking安全。
我在Kali Linux工作,本书中的一个例子是一个简单的TCP代理。 我已经写了几个通过端口连接的脚本,但是现在我在尝试创build到远程服务器(如Google)的连接时遇到了问题。
现在,我承认,我只是一个Python的完全新手。 我主要用C ++编程。 作为一个披露,我确实把main()函数移到了代码的顶部,以便按照执行的顺序进行组织。 在实际来源中,它位于最底层。
import sys import socket import threading import time import os def main(): if len(sys.argv[1:]) != 5: print "Usage: nproxy.py [localhost] [localport] [remotehost] [remoteport] [receive_first]" print "Example: nproxy.py 127.0.0.1 5555 ftp.example.com 5555 True" sys.exit(0) #setup listening parameters local_host = sys.argv[1] local_port = sys.argv[2] #setup remote target remote_host = sys.argv[3] remote_port = sys.argv[4] #this tells our proxy to connect and receive data before sending to the remote host receive_first = sys.argv[5] if "True" in receive_first: receive_first = True else: receive_first = False
从这里开始的一切只是main()函数获取所有参数和variables的parsing和设置。
Nginx作为反向代理
github oauth和Nginx代理
parsing代理url时出错。 坏的端口号
如何在Heroku上以REST API后端优化服务静态文件
任何HTTP代理具有显式的,可configuration的支持请求/响应缓冲和延迟连接?
#spin listening socket server_loop(local_host,local_port,remote_host,remote_port,receive_first) #status class for my timeout function to stop the socket.accept() class status: connected = False
server_loop()函数是我的问题的源函数。 它包含连接客户端到服务器的所有代码。
第一部分是设置我的服务器套接字以及为我自己创build一些执行快捷方式。
def server_loop(local_host,receive_first): #setup server socket server = socket.socket(socket.AF_INET,socket.soCK_STREAM) if "@" in local_host: local_host = "127.0.0.1" if "@" in local_port: local_port = remote_port local_addr = (local_host,int(local_port)) remote_addr = (remote_host,int(remote_port))
绑定服务器到我的本地地址工作得很好。 这是在while循环期间,问题开始,我想。
try: server.bind(local_addr) except: print "[!!] Failed to listen on %s:%d" % local_addr print "[!!] Check for other listening sockets or correct permissions." sys.exit(0) print "[*] Listening on %s:%d" % local_addr server.listen(5)
这里启动while循环来保持程序打开来处理传入的连接。 tout线程包含超时和closures服务器套接字的代码。
while True: tout = threading.Thread(target=timeout,args=(local_host,local_port)) tout.start() print "[*] Connecting..."
这似乎是我的程序挂起的地方。 你看,我不是100%确定这是应该如何处理。 在本书中,代码对他来说工作得很好,并将成功从server.accept()返回并将client_socket连接到正确的地址。
然而,在我的执行过程中,程序在accept()函数处停止,并且永远不会返回任何套接字或地址数据。 除非我对accept()函数的理解是错误的,代码并不是考虑到远程服务器的devise。
我不完全确定accept()函数是如何工作的。 它是向主机发送一个SYN数据包来发起一个连接请求,还是只是坐在那里等待的function,当主机要发送一个SYN-ACK的时候,当没有SYN被发送的时候呢?
client_socket,addr = server.accept() #The timeout function is designed to close the server by Feeding #it the localhost address. If this happens,the statement below catches the #program and calls that the server request has timed out. if "127.0.0.1" in addr[0]: print "[!!] Server connection has timed out." sys.exit(0) #print out the local connection @R_787_4045@ion status.connected = True print "[==>] Received incoming connection from %s:%d" % (addr[0],addr[1]) #start a thread to talk to the remote host
如果server.accept()正常返回,则client_socket将连接到主机,并在其正常发送和接收数据时初始化代理的其余部分。 其余的代码是完成的情况下,我错过了一些关键的事实上是失败的原因。
proxy_thread = threading.Thread(target=proxy_handler,args=(client_socket,remote_addr,receive_first)) proxy_thread.start() def timeout(local_host,local_port): t = 5 while True: if status.connected is True: break if t <= 0: wake = socket.socket(socket.AF_INET,socket.soCK_STREAM) wake.connect((local_host,int(local_port))) break time.sleep(0.1) t-=0.1 def proxy_handler(client_socket,receive_first): #connect to the remote host remote_socket = socket.socket(socket.AF_INET,socket.soCK_STREAM) remote_socket.connect(remote_addr) if receive_first: remote_buffer = receive_from(remote_socket) hexdump(remote_buffer) #send it to our response handler remote_buffer = response_handler(remote_buffer) #if we have data to send to our local client,send it if len(remote_buffer): print "[<==] Sending %d bytes to localhost." % len(remote_buffer) client_socket.send(remote_buffer) #Now let's loop and read from local #send to remote,send to local #rinse and repeat while True: #read from localhost local_buffer = receive_from(client_socket) if len(local_buffer): print "[==>] Received %d bytes from localhost." % len(local_buffer) hexdump(local_buffer) #send it to our request handler local_buffer = request_handler(local_buffer) #send off the data to the remote host remote_socket.send(local_buffer) print "[==>] Sent to remote." #receive back a response remote_buffer = receive_from(remote_socket) if len(remote_buffer): print "[<==] Received %d bytes from remote" % len(remote_buffer) hexdump(remote_buffer) #send to our response handler remote_buffer = response_handler(remote_buffer) #send the response to the local socket client_socket.send(remote_buffer) print "[<==] Sent to localhost." #if no more data on either side,close the connection if not len(local_buffer) or not len(remote_buffer): client_socket.close() remote_socket.close() print "[*] No more data. Closing connections..." break def hexdump(src,length=16): result = [] digits = 4 if isinstance(src,unicode) else 2 for i in xrange(0,len(src),length): s = src[i:i+length] hexa = b' '.join(["%0*X" % (digits,ord(x)) for x in s]) text = b''.join([x if 0x20 <= ord(x) < 0x7F else b'.' for x in s]) result.append(b"%04X %-*s %s" % (i,length*digits + 1),hexa,text) print b'n'.join(result) def receive_from(connection): buffer = "" #we set a 2 second timeout; depending on your target,this may need to be adjusted connection.settimeout(2) try: #keep reading into the buffer until there's no more data or we time out while True: data = connection.recv(4096) if not data: break buffer += data except: pass return buffer #modify any requests destined for remote host def request_handler(buffer): #perform packet modifications return buffer #modify any responses destined for the localhost def response_handler(buffer): #perform packet modification return buffer main()
当我运行该程序时,我使用: sudo python ./nproxy.py @ @ www.google.com 21 True
为了调用我的程序,前两个参数是自动连接到本地主机地址以及我们尝试连接的主机端口的快捷方式。 在这种情况下,它是21,但我已经尝试了端口80以及相同的结果。 我也尝试了十几个不同的网站以及没有似乎返回我的连接请求。
提前感谢您提供任何build议或帮助。
Nginx:将移动请求redirect到/ mobile / $ uri
linux ubuntu服务器,不能完成“sudo apt-get install ubuntu-desktop”
在代理之后使用git
Java HttpURLConnection使用SOCKS代理而不是HTTP
在Cent OS 7上,forticlientsslvpn不能与代理一起使用
尽管hexdump的格式化字符串无效,并且proxy_handler应该是proxy_handler(client_socket,receive_first) ,但程序proxy_handler预期工作。 我也禁用超时,因为我在本地使用代理,我不'想要关闭它的请求。 以下面的方式使用它为我工作:
# Start proxy on local port 12345 and route to google $ python @ 12345 www.google.com 80 True # In another terminal,request from the server # This should print the same as `curl www.google.com` $ curl 127.0.0.1:1235
我认为你误解了这个应该做的事情。 这个引用是主要原因。
然而,在我的执行过程中,程序在accept()函数处停止,并且永远不会返回任何套接字或地址数据。 除非我对accept()函数的理解是错误的,代码并不是考虑到远程服务器的设计。
我将首先说一个套接字上的方法基本上是相应的C函数周围的薄包装,使用man 2 socket或man 2 accept更多细节比python文档可能提供。
为了回答你的问题, accept()被阻塞,因为没有客户端。 它正在等待另一个程序发送一个SYN数据包到它的开放套接字,它将用SYN | ACK进行响应。 这部分都与连接到代理的客户端有关,你似乎认为它涉及远程主机莫名其妙。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。