Skip to content

Instantly share code, notes, and snippets.

@Paradoxis
Last active February 10, 2024 23:00
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save Paradoxis/6336c2eaea20a591dd36bb1f5e227da2 to your computer and use it in GitHub Desktop.
Save Paradoxis/6336c2eaea20a591dd36bb1f5e227da2 to your computer and use it in GitHub Desktop.
Agressive URL encode

Agressive URL encode

Python based CLI tool to agressively url-encode strings, rather than just encoding non-url characters this tool will encode every character in the URL.

Usage:

Firstly make a function in your .bash_profile to call the script

function url-encode()
{
    python ~/<path to script>/url_encode.py $@
}

Now simply call the script from your command line :)

$ url-encode "foo"
foo
$ url-encode --agressive "foo"
%66%6f%6f
$ echo "foo" | url-encode
foo
$ echo "foo" | url-encode --agressive
%66%6f%6f
#!/usr/bin/env python3
from argparse import ArgumentParser
from sys import stdin, stdout, argv
from urllib.parse import quote as url_encode
def aggressive_url_encode(string):
return "".join("%{0:0>2}".format(format(ord(char), "x")) for char in string)
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument("-a", "--agressive", help="Forcibly encode every character rather than non-url safe characters", action="store_true")
parser.add_argument("string", nargs="*")
args = parser.parse_args()
if args.agressive:
if args.string:
stdout.write(aggressive_url_encode(" ".join(args.string)))
else:
stdout.write(aggressive_url_encode(stdin.read()[:-1]))
else:
if args.string:
stdout.write(url_encode(" ".join(args.string)))
else:
stdout.write(url_encode(stdin.read()[:-1]))
@dusekdan
Copy link

dusekdan commented Sep 8, 2018

Thanks for this. I write this comment to explain the star that was there and which I removed - I usually use stars as bookmarks. Once I implemented functionality I needed I removed it, but my gratitude remains.

@BrooksCunningham
Copy link

Thank you for the gist. This is super helpful to encode SQL injections and other web attacks.

@bew
Copy link

bew commented Mar 15, 2022

NOTE, the aggressive encoding can be slightly simplified:

def aggressive_url_encode(string):
    return "".join("%{:02x}".format(ord(char)) for char in string)

we can embed the hex formatting in one format call

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment