
This is very much like Calculator.
Solve Code
from pwn import remote
import re
def count_letters(s):
return sum(1 for char in s if char.isalpha())
def main():
# Connect to the server on localhost at port 12222
conn = remote("ctf.local", 12222)
try:
for _ in range(100):
# Receive the challenge from the server
challenge = conn.recvline(timeout=2).decode().strip()
print(f"Received challenge: {challenge}")
# Extract the random string after "Count the letters:"
match = re.search(r"Count the letters: (.+)", challenge)
if not match:
print("Failed to parse the challenge.")
break
random_string = match.group(1)
letter_count = count_letters(random_string)
# Send the correct answer back to the server
print(f"Sending answer: {letter_count}")
conn.sendline(str(letter_count))
# Receive the result
result = conn.recvline(timeout=2).decode().strip()
print(f"Received result: {result}")
# If the response is "No Good!", break out of the loop
if result != "Correct!":
print("Incorrect answer or timeout.")
break
# Receive the flag if all answers were correct
final_message = conn.recvline(timeout=2).decode().strip()
print(f"Received final message: {final_message}")
except Exception as e:
print(f"Error: {e}")
finally:
conn.close()
if __name__ == "__main__":
main()