2014-04-04 130 views
0

每当我的客户端通过telnet蟒蛇telnet服务器崩溃

telnet myip 43 

连接到我的服务器当客户打在自己的机器上的Ctrl + C继续它会导致telnet服务器崩溃......这可怎么停?还有什么机会可以通过缓冲区溢出攻击我的服务器?

这是我的脚本(非常基本的脚本是)

#!/usr/bin/env python 
import sys 
import socket 
import urllib2 
def main(): 
    s = socket.socket()   # Create a socket object 
    host = '' 
    BUFFER_SIZE = 2048 
    port = 43     # Reserve a port for your service. 
    s.bind((host, port))  # Bind to the port 
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
    s.listen(120)    # Now wait for client connection. 
    while True: 
     data = '' 
     print 'waiting for connection...' 
     c, addr = s.accept()  # Establish connection with client. 
     print 'Got connection from', addr 
     data = c.recv(BUFFER_SIZE) 
     print 'requested website: '+data 
     print x 
     c.send(x) 
     c.close()    # Close the connection 
if __name__ == '__main__': 
    main() 
+0

当您的服务器崩溃时,您是否收到错误消息和回溯? – geoffspear

+0

我认为你删掉了有趣的部分。我敢打赌,你试图打开文件,而不检查它是否存在或是否是相同的文件名。这就是为什么Python崩溃。 – Mathias

+0

打印之前x有 x = urllib2.urlopen('http:// localhost:2020 /?id ='+ data).read() 但有什么方法可以从字符串中去除坏字符? 像在PHP中有htmlspecialchar()或东西 @Mathias –

回答

1

尝试this

import string 
valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits) 

[...] 

     data = c.recv(BUFFER_SIZE) 
     data=''.join(c for c in data if c in valid_chars) 
     print 'requested website: '+data 
     if len(data)>0: 
      try: 
       urllib2.urlopen('localhost:2020/?id='+data).read() 
       print x 
       c.send(x) 
      except: 
       pass 
     c.close()    # Close the connection 

编辑valid_chars只允许允许您的ID参数中的字符。

+0

工作,谢谢:D –