pythondynamically-generatedformatted-text

an idea or module te generate structured text in python?


Well first am sorry because i didn't know how to ask my question, last time in a security challenge i was trying to send some request with curl, after a few moment where they have a lot test to find out how the challenge is really working , i tried to write some python code for generating my request automatically and win some time

here are some of the request that i used to try : The basic one

curl http://10.20.0.50:80/

then i have to specify the path example :

curl http://10.20.0.50:80/transfert/solde
curl http://10.20.0.50:80/account/creat
...

some time add authorization or cookie ...

curl http://10.20.0.50:80/transfert/solde -H "Authorization:Basic bXlhcGk6U3VwZXJTZWNyZXRQYXMkdzByZA=="  -H "cookie: PHPSESSID=23c3jc3spuh27cru38kf9l2au5;" 

or add some parameters :

curl http://10.20.0.50:80/transfert/solde -H "Authorization:Basic bXlhcGk6U3VwZXJTZWNyZXRQYXMkdzByZA=="  -H "cookie: PHPSESSID=23c3jc3spuh27cru38kf9l2au5;" --data-raw '{"id":"521776"}' -v

So the thing is i have to test a lot of thing with and without authorization with and without cookie and changing cookie some times and add --data-raw ... i tried to write a script to do this for me but it's ugly :

url = "http://10.20.0.50:80/"
auth = ' -H "Authorization:Basic bXlhcGk6U3VwZXJTZWNyZXRQYXMkdzByZA=="'
def generate(path,c=None,h=True,plus = None):
    #c cookie , h if we put authentification
    #plus add more code at the end of the request
    global auth  # authentification
    global url
    if c:
    cook = ' -H "cookie: PHPSESSID={};"'.format(c)
    req = "curl "+url+path 
    if h:#h bool
        req += auth
    if c :
        req += cook
    if plus :
        req += plus
    req+=" -v "
    return req

I removed one parameter the --data-row for readability, the idea is that i want to know if there is a better way for doing that ! and not just with this example but in general, if i want to create python code that generate a code source of class where i have to specify the name of class the attributes and type and the code generate a template ...

I hope that you can help me :D PS : Sorry for my English if i mad some mistakes


Solution

  • Maybe, a one way to "improve" your code is doing something like this:

    def generate(command = "", headers = [], raws = [], other = [], v = True):
        if headers:
            command += "".join(" -H " + k for k in h)
        if raws:
            command += "".join(" --data-raw " + k for k in raw)
        if v:
            command += " -v"
        if other:
            command += "".join(" " + k for k in other)
        return command
    
    h = ['"Authorization:Basic bXlhcGk6U3VwZXJTZWNyZXRQYXMkdzByZA=="', '"cookie: PHPSESSID=23c3jc3spuh27cru38kf9l2au5;"']
    raw = ["'{\"id\":\"521776\"}'"]
    cmd = "curl http://10.20.0.50:80/transfert/solde"
    
    command1 = generate(command=cmd,headers=h,raws= raw)
    command2 = generate(command=cmd,headers=h,raws=raw, v=False)
    command3 = generate(command=cmd,v = False)
    
    print("command1:",command1)
    print("command2:", command2)
    print("command3:", command3)
    

    Output:

    command1: curl http://10.20.0.50:80/transfert/solde -H "Authorization:Basic bXlhcGk6U3VwZXJTZWNyZXRQYXMkdzByZA==" -H "cookie: PHPSESSID=23c3jc3spuh27cru38kf9l2au5;" --data-raw '{"id":"521776"}' -v
    command2: curl http://10.20.0.50:80/transfert/solde -H "Authorization:Basic bXlhcGk6U3VwZXJTZWNyZXRQYXMkdzByZA==" -H "cookie: PHPSESSID=23c3jc3spuh27cru38kf9l2au5;" --data-raw '{"id":"521776"}'
    command3: curl http://10.20.0.50:80/transfert/solde