-
-
Save marceloalmeida/ca5c0aefd6f741413139161ba24a9717 to your computer and use it in GitHub Desktop.
Python Netcat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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:') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment