搜索
查看: 228|回复: 0

netcat client by python

[复制链接]

1839

主题

2255

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
11913
发表于 2014-12-19 00:18:27 | 显示全部楼层 |阅读模式
  1. #coding=utf-8

  2. '''
  3. python netcat client
  4. code by Xc0d3r

  5. In [3]: import nc
  6. In [4]: nc.
  7. nc.NC                 nc.NC_DEFAULT_TIMEOUT nc.telnetlib
  8. nc.NC_DEFAULT_PORT    nc.main               nc.thread
  9. In [4]: nc.NC_DEFAULT_PORT
  10. Out[4]: 23
  11. In [5]: nc.NC_DEFAULT_TIMEOUT
  12. Out[5]: 30
  13. In [6]: nc = nc.NC()
  14. In [7]: nc.open('localhost', 8888)
  15. In [8]: nc.
  16. nc.client         nc.debuglevel     nc.host           nc.port           nc.timeout
  17. nc.close          nc.get_socket     nc.open           nc.set_debuglevel nc.tn
  18. In [8]: nc.set_debuglevel(1)
  19. In [9]: nc.cl
  20. nc.client nc.close
  21. In [9]: nc.client()
  22. '''

  23. import telnetlib
  24. import thread

  25. NC_DEFAULT_PORT = 23
  26. NC_DEFAULT_TIMEOUT = 30

  27. class NC:
  28.     '''
  29.     python netcat client
  30.     '''
  31.     def __init__(self, host=None, port=NC_DEFAULT_PORT, timeout=NC_DEFAULT_TIMEOUT):
  32.         '''
  33.         init netcat client
  34.         can just open client on class start
  35.         use:
  36.             nc.NC('your_host', [your_port], [your_timeout])
  37.         '''
  38.         self.host = host
  39.         self.port = port
  40.         self.timeout = timeout
  41.         self.tn = None
  42.         self.debuglevel = 0
  43.         if host is not None:
  44.             self.open(host, port, timeout)
  45.             
  46.     def open(self, host=None, port=NC_DEFAULT_PORT, timeout=NC_DEFAULT_TIMEOUT):
  47.         '''
  48.         open netcat client
  49.         must after class init
  50.         use:
  51.             nc = nc.NC()
  52.             nc.open('your_host', [your_port], [your_timeout])
  53.         '''
  54.         self.host = host
  55.         self.port = port
  56.         self.timeout = timeout
  57.         if self.tn is None:
  58.             self.tn = telnetlib.Telnet(host, port, timeout)
  59.             self.tn.set_debuglevel(self.debuglevel)
  60.         else:
  61.             print '*** Connection already open ***'
  62.          
  63.     def close(self):
  64.         '''
  65.         close netcat client
  66.         '''
  67.         if self.tn:
  68.             self.tn.close()
  69.          
  70.     def set_debuglevel(self, debuglevel):
  71.         '''
  72.         set netcat debuglevel
  73.         aim to set the telnetlib's debuglevel
  74.         '''
  75.         self.debuglevel = debuglevel
  76.          
  77.     def _start_listener(self):
  78.         '''
  79.         start netcat client listener
  80.         this way can work well in cmd
  81.         '''
  82.         self.tn.listener()
  83.          
  84.     def _send_input(self):
  85.         '''
  86.         loop send user's input data to remote server
  87.         '''
  88.         while True:
  89.             self.tn.write(raw_input() + '\r\n')
  90.          
  91.     def client(self):
  92.         '''
  93.         give a work well netcat client
  94.         must after self.open() function
  95.         '''
  96.         thread.start_new_thread(self._send_input, ())
  97.         self._start_listener()
  98.          
  99.     def get_socket(self):
  100.         '''
  101.         return the socket object
  102.         '''
  103.         return self.tn.get_socket()
  104.          
  105. def main():
  106.     import os, sys
  107.     if len(sys.argv) == 2:
  108.         nc = NC()
  109.         nc.open(argv[1])
  110.     elif len(sys.argv) == 3:
  111.         nc = NC()
  112.         nc.open(sys.argv[1], int(sys.argv[2]))
  113.     elif len(sys.argv) == 4:
  114.         nc = NC()
  115.         nc.open(sys.argv[1], int(sys.argv[2]), int(sys.argv[3]))
  116.     else:
  117.         print '[+] usage: %s <host> [port] [timeout]'%os.path.basename(__file__)
  118.         return
  119.     nc.set_debuglevel(1)
  120.     nc.client()
  121.     nc.close()
  122.      
  123. if __name__ == '__main__':
  124.     main()
复制代码
过段时间可能会取消签到功能了
您需要登录后才可以回帖 登录 | Join BUC

本版积分规则

Powered by Discuz!

© 2012-2015 Baiker Union of China.

快速回复 返回顶部 返回列表