Created
December 20, 2020 22:25
-
-
Save JJTech0130/df452c5cd025155dce034800c8011f20 to your computer and use it in GitHub Desktop.
Python script to beat the PicoCTF "based" challenge
This file contains hidden or 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
#!/usr/bin/python3 | |
import socket | |
class bcolors: | |
HEADER = '\033[95m' | |
OKBLUE = '\033[94m' | |
OKCYAN = '\033[96m' | |
OKGREEN = '\033[92m' | |
WARNING = '\033[93m' | |
FAIL = '\033[91m' | |
ENDC = '\033[0m' | |
BOLD = '\033[1m' | |
UNDERLINE = '\033[4m' | |
string = input(bcolors.OKBLUE + "Paste the netcat string here: ") | |
nc = string.split(" ") | |
# create an INET, STREAMing socket | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
# now connect to the port | |
s.connect((nc[1], int(nc[2]))) | |
def asciiDecode(string, base): | |
str_converted = "" | |
for char in string.split(): | |
if char.isdigit(): | |
str_converted += chr(int(char, base)) | |
return str_converted | |
# Special case for hex because it can't seem to handle it the normal way ("OverflowError: Python int too large to convert to C int"?) | |
def asciiDecodeHex(string): | |
string = string.replace("Please give me the ", '').replace(" as a word.", '') | |
bytes_object = bytes.fromhex(string) | |
ascii_string = bytes_object.decode("ASCII") | |
return ascii_string | |
# Question 1 | |
data = s.recv(1024).decode("utf-8") | |
data = data.splitlines()[2] # Throw out the garbage | |
data = asciiDecode(data,2) | |
s.send(data.encode("utf-8") + b"\n") | |
# Question 2 | |
data = s.recv(1024).decode("utf-8") | |
data = data.splitlines()[0] # Throw out the garbage | |
data = asciiDecode(data,8) | |
s.send(data.encode("utf-8") + b"\n") | |
# Question 3 | |
data = s.recv(1024).decode("utf-8") | |
data = data.splitlines()[0] # Throw out the garbage | |
data = asciiDecodeHex(data) | |
s.send(data.encode("utf-8") + b"\n\n") | |
data = s.recv(1024).decode("utf-8") | |
print(bcolors.OKGREEN + data + bcolors.ENDC) | |
s.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment