Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python Netcat
import socket
class Netcat:
""" Python 'netcat like' module """
def __init__(self, ip, port):
self.buff = ""
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((ip, port))
def read(self, length = 1024):
""" Read 1024 bytes off the socket """
return self.socket.recv(length)
def read_until(self, data):
""" Read data into the buffer until we have data """
while not data in self.buff:
self.buff += self.socket.recv(1024)
pos = self.buff.find(data)
rval = self.buff[:pos + len(data)]
self.buff = self.buff[pos + len(data):]
return rval
def write(self, data):
self.socket.send(data)
def close(self):
self.socket.close()
# below is a extract from a sample exploit that
# interfaces with a tcp socket
from netcat import Netcat
# start a new Netcat() instance
nc = Netcat('127.0.0.1', 53121)
# get to the prompt
nc.read_until('>')
# start a new note
nc.write('new' + '\n')
nc.read_until('>')
# set note 0 with the payload
nc.write('set' + '\n')
nc.read_until('id:')
@Dangle76

This comment has been minimized.

Dangle76 commented Jan 6, 2017

This may be a dumb question, however I'm a bit of a novice with python in general. If I connect to say, port 21 on a windows machine with this and it spits out the "Microsoft FTP" banner, how would I capture that response since it doesn't seem to output on stdout?

@ewan115

This comment has been minimized.

ewan115 commented Mar 7, 2017

Does it work with Netcat windows version?

@thobiasn

This comment has been minimized.

thobiasn commented Sep 10, 2017

@Dangle76 I am looking for this answer as well. Will respond if I get it working.

Easier than I thought
Turns out you can simply call:

output = nc.read()
print output

Which will save the current output to a variable and print it.

@yuvalyo

This comment has been minimized.

yuvalyo commented May 24, 2018

hi
im getting this error
Traceback (most recent call last):
File "C:\Users\magshimim\Desktop\pingPong.py", line 62, in
main()
File "C:\Users\magshimim\Desktop\pingPong.py", line 46, in main
output = nc.read_until(":")
File "C:\Users\magshimim\Desktop\pingPong.py", line 24, in read_until
self.buff += self.socket.recv(1024)
TypeError: must be str, not bytes

can you tell what it means?

@kolayne

This comment has been minimized.

kolayne commented Sep 13, 2018

hi
im getting this error
Traceback (most recent call last):
File "C:\Users\magshimim\Desktop\pingPong.py", line 62, in
main()
File "C:\Users\magshimim\Desktop\pingPong.py", line 46, in main
output = nc.read_until(":")
File "C:\Users\magshimim\Desktop\pingPong.py", line 24, in read_until
self.buff += self.socket.recv(1024)
TypeError: must be str, not bytes

can you tell what it means?

Replace line 9 with self.buff = b''
And use output = nc.read_until(b":") except of output = nc.read_until(":") in your code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment