Last active
November 30, 2019 10:24
-
-
Save tbhaxor/cb28eef08456e36a828e642855a5322e to your computer and use it in GitHub Desktop.
Simple NCAT Implementation
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
from argparse import ArgumentParser, RawDescriptionHelpFormatter | |
from socket import socket, AF_INET, SOCK_STREAM | |
# configuring arguments | |
parser = ArgumentParser(description="Portable NCAT", | |
formatter_class=RawDescriptionHelpFormatter) | |
parser.add_argument("-l", | |
"--listen", | |
action="store_true", | |
default=False, | |
dest="listen", | |
help="sets the program on listening mode") | |
parser.add_argument("-c", | |
"--connect", | |
action="store_true", | |
default=False, | |
dest="connect", | |
help="sets the program on connecting mode") | |
parser.add_argument("-P", | |
help="sets the port", | |
metavar="PORT_NUMBER", | |
type=int, | |
dest="PORT", | |
required=True) | |
parser.add_argument("-H", | |
help="sets the host", | |
metavar="HOSTNAME", | |
dest="HOST", | |
required=True) | |
# parsing the args | |
args = parser.parse_args() | |
# safeguarding | |
if args.connect and args.listen: | |
raise ValueError("Method not allowed") | |
# if selected connect mode | |
if args.connect: | |
# instantiate the socket | |
client = socket(AF_INET, SOCK_STREAM) | |
# performing safe connection | |
try: | |
client.connect((args.HOST, args.PORT)) | |
print("[#] Connected to the Server") | |
except ConnectionRefusedError: | |
print("[X] Unable to Connect To: tcp://%s:%d" % (args.HOST, args.PORT)) | |
exit(1) | |
# iterate on connction | |
while True: | |
# recieve message from server | |
data = client.recv(4096).decode() | |
# exit if no data | |
if not data: | |
client.close() | |
break | |
# printining the response on client | |
print("SERVER>", data) | |
# getting the client info | |
data = input("CLIENT> ") | |
# sending on the server | |
client.sendall(data.encode()) | |
pass | |
elif args.listen: | |
# instantiate server | |
server = socket(AF_INET, SOCK_STREAM) | |
# safe binding host and port | |
try: | |
server.bind((args.HOST, args.PORT)) | |
print("[#] Server will listen on: tcp://%s:%d" % | |
(args.HOST, args.PORT)) | |
except OSError: | |
print("[#] Port already in use") | |
exit(1) | |
# listening to the server | |
server.listen() | |
print("[#] Listening") | |
# iterating | |
while True: | |
# accepting the connection | |
client, address = server.accept() | |
print("[!] Client Connected: tcp://%s:%d" % (address[0], address[1])) | |
# iterating while client is available | |
while True: | |
# trying to send data | |
try: | |
# getting input | |
data = input("SERVER> ") | |
# sending it to the client | |
client.sendall(data.encode()) | |
# receving from the client | |
data = client.recv(4096).decode() | |
# disconnect if no data recieved | |
if not data: | |
print("[!] Client Disconnected: tcp://%s:%d" % | |
(address[0], address[1])) | |
client.close() | |
break | |
# print the client data | |
print("CLIENT>", data) | |
except Exception: | |
print("[!] Client Disconnected: tcp://%s:%d" % | |
(address[0], address[1])) | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment