|
- #coding=utf-8
-
- '''
- python netcat client
- code by Xc0d3r
-
- In [3]: import nc
- In [4]: nc.
- nc.NC nc.NC_DEFAULT_TIMEOUT nc.telnetlib
- nc.NC_DEFAULT_PORT nc.main nc.thread
- In [4]: nc.NC_DEFAULT_PORT
- Out[4]: 23
- In [5]: nc.NC_DEFAULT_TIMEOUT
- Out[5]: 30
- In [6]: nc = nc.NC()
- In [7]: nc.open('localhost', 8888)
- In [8]: nc.
- nc.client nc.debuglevel nc.host nc.port nc.timeout
- nc.close nc.get_socket nc.open nc.set_debuglevel nc.tn
- In [8]: nc.set_debuglevel(1)
- In [9]: nc.cl
- nc.client nc.close
- In [9]: nc.client()
- '''
-
- import telnetlib
- import thread
-
- NC_DEFAULT_PORT = 23
- NC_DEFAULT_TIMEOUT = 30
-
- class NC:
- '''
- python netcat client
- '''
- def __init__(self, host=None, port=NC_DEFAULT_PORT, timeout=NC_DEFAULT_TIMEOUT):
- '''
- init netcat client
- can just open client on class start
- use:
- nc.NC('your_host', [your_port], [your_timeout])
- '''
- self.host = host
- self.port = port
- self.timeout = timeout
- self.tn = None
- self.debuglevel = 0
- if host is not None:
- self.open(host, port, timeout)
-
- def open(self, host=None, port=NC_DEFAULT_PORT, timeout=NC_DEFAULT_TIMEOUT):
- '''
- open netcat client
- must after class init
- use:
- nc = nc.NC()
- nc.open('your_host', [your_port], [your_timeout])
- '''
- self.host = host
- self.port = port
- self.timeout = timeout
- if self.tn is None:
- self.tn = telnetlib.Telnet(host, port, timeout)
- self.tn.set_debuglevel(self.debuglevel)
- else:
- print '*** Connection already open ***'
-
- def close(self):
- '''
- close netcat client
- '''
- if self.tn:
- self.tn.close()
-
- def set_debuglevel(self, debuglevel):
- '''
- set netcat debuglevel
- aim to set the telnetlib's debuglevel
- '''
- self.debuglevel = debuglevel
-
- def _start_listener(self):
- '''
- start netcat client listener
- this way can work well in cmd
- '''
- self.tn.listener()
-
- def _send_input(self):
- '''
- loop send user's input data to remote server
- '''
- while True:
- self.tn.write(raw_input() + '\r\n')
-
- def client(self):
- '''
- give a work well netcat client
- must after self.open() function
- '''
- thread.start_new_thread(self._send_input, ())
- self._start_listener()
-
- def get_socket(self):
- '''
- return the socket object
- '''
- return self.tn.get_socket()
-
- def main():
- import os, sys
- if len(sys.argv) == 2:
- nc = NC()
- nc.open(argv[1])
- elif len(sys.argv) == 3:
- nc = NC()
- nc.open(sys.argv[1], int(sys.argv[2]))
- elif len(sys.argv) == 4:
- nc = NC()
- nc.open(sys.argv[1], int(sys.argv[2]), int(sys.argv[3]))
- else:
- print '[+] usage: %s <host> [port] [timeout]'%os.path.basename(__file__)
- return
- nc.set_debuglevel(1)
- nc.client()
- nc.close()
-
- if __name__ == '__main__':
- main()
复制代码 |
|