Skip to content

Instantly share code, notes, and snippets.

@gbroiles
Last active June 16, 2020 21:24
Show Gist options
  • Save gbroiles/6398a0736db01ba4d3c22fcda32dc24b to your computer and use it in GitHub Desktop.
Save gbroiles/6398a0736db01ba4d3c22fcda32dc24b to your computer and use it in GitHub Desktop.
CTF_NAHAMCON_Merriam-Webster

This was a quick hack written under serious time pressure to solve a CTF puzzle. I'd refactor it except there's no point, and the puzzle server likely doesn't even exist any more. I don't like the way I ended up using global variables, I don't like the lack of clarity about the relationship between the two significant functions and the main() event loop, and in retrospect it does unnecessary things (e.g., maintaining a count of the number of items held in a list, when I could have just used len(list) instead) because of the way the program evolved as my understand of the puzzle evolved.

Here it is, if anyone else worked on this puzzle and wants to see how I ended up solving it. I get the impression most people used the enchant library, which I'd never heard of, to be honest, so I went old school with /usr/share/dict/words.

import socket
import sys
import time
BUFFER = 4096
SLEEP = 0.5
question = ""
goodwords = []
notwords = []
count = 0
wordcount = 0
combined = ""
w = ""
iteration = 0
def wordsort():
global combined, goodwords, notwords, count, wordcount, w
goodwords = []
notwords = []
count = 0
wordcount = 0
if "." in combined:
sep = "."
else:
sep = "?"
qpos = combined.find(sep)
print("Word start position: {}".format(qpos))
w = combined[qpos + 1 :]
for i in w.split():
word = i.strip()
# print(word)
if word in dictionary:
goodwords.append(i)
count += 1
else:
notwords.append(i)
wordcount += 1
notwords_sort = sorted(notwords)
goodwords_sort = sorted(goodwords)
print("Words found:{}".format(goodwords))
print("Not-words found:{}".format(notwords))
return ()
def getnext():
global combined, question, goodwords, notwords, count, wordcount, w, iteration
iteration += 1
print("<<<<<<<<<< {} >>>>>>>>>>".format(iteration))
sock.settimeout(2)
combined = ""
while True:
try:
line = sock.recv(BUFFER)
except:
break
line = line.decode("utf-8")
line = line.strip()
line = line.strip(":")
line = line.strip(">")
if line == "":
break
print("Received: >{}<".format(line))
with open("received_lines", "a") as f:
f.write("\n{}".format(line))
combined = combined + line
combined = combined.strip()
print("Combined: [{}]".format(combined))
if "{" in combined:
sleep(600)
wordsort()
question = combined
if "FIRED" in combined:
print("Question: {}<".format(question))
print("Wordlist: {}<".format(w))
print("Good word count: {}".format(count))
print("Not word count: {}".format(wordcount - count))
print("Good words: {}<".format(" ".join(goodwords)))
print("Not words: {}<".format(" ".join(notwords)))
sys.exit(0)
if "how many" in combined:
if "NOT" in combined:
print("Number of not words")
out = str(wordcount - count)
else:
print("Number of words")
out = str(count)
elif "ALPHABETICAL" in question:
if "NOT" in question:
print("Sorted list not words")
out = sorted(notwords)
out = " ".join(out)
else:
print("Sorted list of words")
out = sorted(goodwords)
out = " ".join(out)
elif "CHRONOLOGICAL" in question:
if "NOT" in question:
print("Natural list not words")
out = notwords
out = " ".join(out)
else:
print("Natural list of words")
out = goodwords
out = " ".join(out)
else:
out = ""
return out
with open("/usr/share/dict/words", "r") as f:
dirty = f.readlines()
dictionary = []
for i in dirty:
dictionary.append(i.strip())
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ("161.35.252.71", 50012)
print("connecting to {}".format(server_address))
sock.connect(server_address)
sock.settimeout(60)
out = getnext()
while True:
print("Starting loop")
time.sleep(SLEEP)
if out != "":
print("Sending: {},{}".format(out, type(out)))
sent = sock.send("{}\n".format(out).encode("utf-8"))
print("Sent {} bytes".format(sent))
out = getnext()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment