This is my solution to the NACTF2020 Challenge “Dr. J’s Vegetable Factory #3”.
Challenge #1
After years of collecting plush vegetable toys, Dr. J decided to take on his true passion: starting a vegetable factory. Dr. J is incredibly organized, so he likes all of his vegetables to be in the proper order. In fact, he built a robot “Turnipinator-1000” to alphabetize his vegetables for him! Unfortunately, Dr. J doesn’t know what instructions to give Turnipinator-1000. Can you help him out? 🥬🥕🌽🍆🥦🥒🥑🍄
nc challenges.ctfd.io 30267
Give instructions in the form of numbers separated by spaces. Entering the number x will swap the vegetable in position x with the vegetable in position x+1. Positions start at zero, not one. (Dr. J is a programmer after all.) For example, given the following vegetables: Avocado, Brocolli, Eggplant, Daikon Radish, Carrot, one possible solution is “3 2 3”
Avocado, Brocolli, Eggplant, Daikon Radish, Carrot
- (swap 3 and 4)
Avocado, Brocolli, Eggplant, Carrot, Daikon Radish
- (swap 2 and 3)
Avocado, Brocolli, Carrot, Eggplant, Daikon Radish
- (swap 3 and 4)
Avocado, Brocolli, Carrot, Daikon Radish, Eggplant
Challenge #2 adds the following:
Dr. J expanded his vegetable factory and he’s got hundreds of vegetables. Can you give Turnipinator-1000 the right instructions to sort Dr. J’s vegetables? 🥬🥕🌽🍆🥦🥒🥑🍄
Challenge #3 adds the following:
Rahul hates vegetables. Rahul hates vegetables so much that he snuck into Dr. J’s factory at night to sabotage Dr. J’s vegetable production! He brought a sledgehammer and broke the wheels of Dr. J’s robot! 😓 Now the robot is stuck in place, and instead of being able to swap any adjacent elements, it can only swap the elements in positions 0 and 1!
But Dr. J won’t let this incident stop him from giving the people the vegetables they deserve! Dr. J is a problem solver 🧠. He organized his vegetables in a circle, and added a conveyor-belt that allows him shift the positions of the vegetables. He thinks that the conveyor belt should make it possible to sort his vegetables, but he’s not 100% sure. Can you help him out?
nc challenges.ctfd.io 30267
Enter letters separated by spaces to sort Dr. J’s vegetables. Entering “c” will activate the conveyor belt and shift all vegetables left one position. Entering “s” will swap the vegetable in position 0 with the vegetable in position 1.
The challenge is solved with the following Python code:
import socket
s = socket.socket()
s.connect(("challenges.ctfd.io",30267))
info = s.recv(4096).decode("utf-8")
print(info)
s.send(b'3\n')
response = s.recv(8192).decode("utf-8")
response = response + s.recv(8192).decode("utf-8")
print(response)
stripList = response.split("\n\n")[1].split(', ')
print(stripList)
sortedList = sorted(stripList)
trackSort = []
while stripList != sortedList:
testList = [stripList[0], stripList[1]]
if (stripList[0] == sortedList[-1]) and (stripList[1] == sortedList[0]>
pass
elif [stripList[0], stripList[1]] != sorted(testList):
stripList[0], stripList[1] = stripList[1], stripList[0]
trackSort.append("s")
trackSort.append("c")
stripList.append(stripList.pop(0))
trackString = " ".join(trackSort) + "\n"
s.send(trackString.encode('utf-8'))
response = s.recv(8192).decode("utf-8")
print(response)
This spits out the flag:
nactf{1t_t4k35_tw0_t0_m4n90_8a51c7b47fbe227}