0

I am trying to write a python script that opens a telnet session and then answers addition problems presented in the session.

from telnetlib import Telnet
from pathlib import Path

host = 'fcd070be5ee67a1a.247ctf.com'
port = 50028
tn = Telnet(host, port)     #Sets the telnet connection
path = Path('log.txt')


def get_line():
    #Grabs and returns everything from the initial print of the telnet terminal and returns it.
    raw_line = tn.read_until(b'?', timeout = 5)
    path.write_text(str(raw_line))
    line = str(raw_line.split()).replace('b', '').replace('?', '') 
    print(line)
    return line

def get_nums():
    #Cleans up, strips, and sums the numbers pulled from the line in get_line()
    line = get_line()
    ans = int(line.split(' ')[-1].replace('\'', '').replace(']', '')) + int(line.split(' ')[-3].replace('\'', '').replace(',', ''))
    print(ans)
    return ans

def answer():
    #prints the sum from get_nums() into the telnet terminal and enters
    tn.write(bytes(get_nums()) + b'\n')

answer()
get_line()

#Test to see if calling the instance outside of get_lines() would work
raw_line = tn.read_until(b'?', timeout = 5) 
print(raw_line)

When get_lines() is called for the first time when answer() is ran, the code reads from the telnet session and answers a question just fine, but the second time that get_lines() is called, it only returns the characters: b'\r\n' in the raw_line variable that prints to the test log file, and prints an empty bracket on my terminal.

Example output when script is ran:

>PS C:\Users\jeffy\Documents\Python\247CTF> py .\telscript.py
>['Welcome', 'to', 'the', '247CTF', 'addition', 'verifier!', 'If', 'you', 'can', 'solve', '500', 'addition', 'prolems,', 'we', 'will', 'give', 'you', 'a', 'flag!', 'What', 'is', 'the', 'answer', 'to', '146', '+', '9']
>155
>[]
>b''
>PS C:\Users\jeffy\Documents\Python\247CTF>

Example of output when answered by hand via telnet terminal:

>Welcome to the 247CTF addition verifier!
>If you can solve 500 addition problems, we will give you a flag!
>What is the answer to 426 + 141?
>567
>Yes, Correct!
>What is the answer to 357 + 263?

I have tried calling the instance both inside, and outside of my function. This had no effect.

I have also tried stripping every line outside of their functions and it has only modified its output a bit.

from telnetlib import Telnet
from pathlib import Path

#Sets the telnet connection:
host = 'fcd070be5ee67a1a.247ctf.com'
port = 50028
tn = Telnet(host, port)
path = Path('log.txt')

#Gets and cleans the initial line
raw_line = tn.read_until(b'?', timeout = 1)
path.write_text(str(raw_line))
line = str(raw_line.split()).replace('b', '').replace('?', '') 
ans = int(line.split(' ')[-1].replace('\'', '').replace(']', '')) + int(line.split(' ')[-3].replace('\'', '').replace(',', ''))
print(line)
print(ans)

#Plugs the answer in to the telnet session
str_ans = str(ans)
tn.write(bytes(str_ans + '\n', 'utf-8'))

#Attempts to get next line
new_line = tn.read_until(b'?', timeout = 5)

print(new_line)`

Outputs:

>PS C:\Users\jeffy\Documents\Python\247CTF> py .\test.py
>['Welcome', 'to', 'the', '247CTF', 'addition', 'verifier!', 'If', 'you', 'can', 'solve', '500', 'addition', 'prolems,', 'we', 'will', 'give', 'you', 'a', 'flag!', 'What', 'is', 'the', 'answer', 'to', '170', '+', '486']
>656
>b'\r\n'
>PS C:\Users\jeffy\Documents\Python\247CTF>

Thank you for your help!

Cibo
  • 1
  • 2
  • please ammend the code and the errors in the code so that it works and that other users can reproduce the problem quickly: https://stackoverflow.com/help/minimal-reproducible-example – D.L Feb 02 '23 at 00:24
  • I amended the code with the telnet host and port so it could be ran properly – Cibo Feb 02 '23 at 12:46
  • I figured this out using [this](https://stackoverflow.com/questions/14606799/what-does-r-do-in-the-following-script) Needed a carriage return – Cibo Feb 02 '23 at 13:22

0 Answers0