> For the complete documentation index, see [llms.txt](https://cajac.gitbook.io/ctf-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cajac.gitbook.io/ctf-notes/web/miscellaneous/url-encoding.md).

# URL Encoding

URL encoding, officially known as percent-encoding, is a method to [encode](https://en.wikipedia.org/wiki/Binary-to-text_encoding) arbitrary data in a [uniform resource identifier](https://en.wikipedia.org/wiki/Uniform_resource_identifier) (URI) using only the [US-ASCII](https://en.wikipedia.org/wiki/ASCII) characters legal within a URI. Although it is known as *URL encoding*, it is also used more generally within the main [Uniform Resource Identifier](https://en.wikipedia.org/wiki/Uniform_Resource_Identifier) (URI) set, which includes both [Uniform Resource Locator](https://en.wikipedia.org/wiki/Uniform_Resource_Locator) (URL) and [Uniform Resource Name](https://en.wikipedia.org/wiki/Uniform_Resource_Name) (URN). Consequently, it is also used in the preparation of data of the `application/x-www-form-urlencoded` [media type](https://en.wikipedia.org/wiki/Media_type), as is often used in the submission of [HTML](https://en.wikipedia.org/wiki/HTML) [form](https://en.wikipedia.org/wiki/Form_\(web\)) data in [HTTP](https://en.wikipedia.org/wiki/HTTP) requests.

## Reserved characters

<table data-header-hidden><thead><tr><th width="85"></th><th width="71"></th><th width="70"></th><th width="72"></th><th width="70"></th><th width="72"></th><th width="71"></th><th width="70"></th><th width="72"></th><th width="71"></th><th width="71"></th><th width="70"></th><th width="71"></th><th width="72"></th><th width="70"></th><th width="72"></th><th width="71"></th><th></th></tr></thead><tbody><tr><td><a href="https://en.wikipedia.org/wiki/Exclamation_mark"><code>!</code></a></td><td><a href="https://en.wikipedia.org/wiki/Number_sign"><code>#</code></a></td><td><a href="https://en.wikipedia.org/wiki/Dollar_sign"><code>$</code></a></td><td><a href="https://en.wikipedia.org/wiki/Ampersand"><code>&#x26;</code></a></td><td><a href="https://en.wikipedia.org/wiki/Apostrophe_(mark)"><code>'</code></a></td><td><a href="https://en.wikipedia.org/wiki/Parenthesis"><code>(</code></a></td><td><a href="https://en.wikipedia.org/wiki/Parenthesis"><code>)</code></a></td><td><a href="https://en.wikipedia.org/wiki/Asterisk"><code>*</code></a></td><td><a href="https://en.wikipedia.org/wiki/Plus_sign"><code>+</code></a></td><td><a href="https://en.wikipedia.org/wiki/Comma"><code>,</code></a></td><td><a href="https://en.wikipedia.org/wiki/Slash_(punctuation)"><code>/</code></a></td><td><a href="https://en.wikipedia.org/wiki/Colon_(punctuation)"><code>:</code></a></td><td><a href="https://en.wikipedia.org/wiki/Semicolon"><code>;</code></a></td><td><a href="https://en.wikipedia.org/wiki/Equal_sign"><code>=</code></a></td><td><a href="https://en.wikipedia.org/wiki/Question_mark"><code>?</code></a></td><td><a href="https://en.wikipedia.org/wiki/At_sign"><code>@</code></a></td><td><a href="https://en.wikipedia.org/wiki/Square_bracket"><code>[</code></a></td><td><a href="https://en.wikipedia.org/wiki/Square_bracket"><code>]</code></a></td></tr><tr><td><code>%21</code></td><td><code>%23</code></td><td><code>%24</code></td><td><code>%26</code></td><td><code>%27</code></td><td><code>%28</code></td><td><code>%29</code></td><td><code>%2A</code></td><td><code>%2B</code></td><td><code>%2C</code></td><td><code>%2F</code></td><td><code>%3A</code></td><td><code>%3B</code></td><td><code>%3D</code></td><td><code>%3F</code></td><td><code>%40</code></td><td><code>%5B</code></td><td><code>%5D</code></td></tr></tbody></table>

## Common characters

<table><thead><tr><th width="144" align="center">Character</th><th width="137" align="center">Encoding</th><th>Description</th></tr></thead><tbody><tr><td align="center"> <a href="https://en.wikipedia.org/wiki/Space_(punctuation)"><code>␣</code></a></td><td align="center"><code>%20</code></td><td>Space</td></tr><tr><td align="center"><a href="https://en.wikipedia.org/wiki/Double_quote"><code>"</code></a></td><td align="center"><code>%22</code></td><td>Quotation mark</td></tr><tr><td align="center"><a href="https://en.wikipedia.org/wiki/Percent_sign"><code>%</code></a></td><td align="center"><code>%25</code></td><td>Percent sign</td></tr><tr><td align="center"><a href="https://en.wikipedia.org/wiki/Hyphen"><code>-</code></a></td><td align="center"><code>%2D</code></td><td>Hyphen-minus</td></tr><tr><td align="center"><a href="https://en.wikipedia.org/wiki/Full_stop"><code>.</code></a></td><td align="center"><code>%2E</code></td><td>Full stop <em>or</em> period</td></tr><tr><td align="center"><a href="https://en.wikipedia.org/wiki/Angle_bracket"><code>&#x3C;</code></a></td><td align="center"><code>%3C</code></td><td>Less-than sign</td></tr><tr><td align="center"><a href="https://en.wikipedia.org/wiki/Angle_bracket"><code>></code></a></td><td align="center"><code>%3E</code></td><td>Greater-than sign</td></tr><tr><td align="center"><a href="https://en.wikipedia.org/wiki/Back_slash"><code>\</code></a></td><td align="center"><code>%5C</code></td><td>Backslash</td></tr><tr><td align="center"><a href="https://en.wikipedia.org/wiki/Caret"><code>^</code></a></td><td align="center"><code>%5E</code></td><td>Circumflex accent</td></tr><tr><td align="center"><a href="https://en.wikipedia.org/wiki/Underscore"><code>_</code></a></td><td align="center"><code>%5F</code></td><td>Underscore</td></tr><tr><td align="center"><a href="https://en.wikipedia.org/wiki/Grave_accent"><code>`</code></a></td><td align="center"><code>%60</code></td><td>Grave accent</td></tr><tr><td align="center"><a href="https://en.wikipedia.org/wiki/Curly_bracket"><code>{</code></a></td><td align="center"><code>%7B</code></td><td>Left Curly Bracket</td></tr><tr><td align="center"><a href="https://en.wikipedia.org/wiki/Vertical_bar"><code>|</code></a></td><td align="center"><code>%7C</code></td><td>Vertical bar</td></tr><tr><td align="center"><a href="https://en.wikipedia.org/wiki/Curly_bracket"><code>}</code></a></td><td align="center"><code>%7D</code></td><td>Right Curly Bracket</td></tr><tr><td align="center"><a href="https://en.wikipedia.org/wiki/Tilde"><code>~</code></a></td><td align="center"><code>%7E</code></td><td>Tilde</td></tr><tr><td align="center"><a href="https://en.wikipedia.org/wiki/%C2%A3"><code>£</code></a></td><td align="center"><code>%C2%A3</code></td><td>Pound sign</td></tr><tr><td align="center"><a href="https://en.wikipedia.org/wiki/%E2%82%AC"><code>€</code></a></td><td align="center"><code>%E2%82%AC</code></td><td>Euro sign</td></tr></tbody></table>

## URL-decoding

### URL-decode in Bash

```bash
#!/bin/bash

URL="%59%69%70%70%65%68%21%20%59%6F%75%72%20%55%52%4C%20%69%73%20%63%68%61%6C%6C%65%6E%67%65%2F%74%72%61%69%6E%69%6E%67%2F%65%6E%63%6F%64%69%6E%67%73%2F%75%72%6C%2F%73%61%77%5F%6C%6F%74%69%6F%6E%2E%70%68%70%3F%70%3D%61%62%67%6D%61%66%65%62%68%6D%62%64%26%63%69%64%3D%35%32%23%70%61%73%73%77%6F%72%64%3D%66%69%62%72%65%5F%6F%70%74%69%63%73%20%56%65%72%79%20%77%65%6C%6C%20%64%6F%6E%65%21"
echo $URL | tr -d '%' | xxd -r -p
```

### URL-decode in Python

You can URL-decode in Python with `urllib` and the [unquote function](https://docs.python.org/3/library/urllib.parse.html#urllib.parse.unquote):

```python
#!/usr/bin/python

from urllib.parse import unquote

enc = '%59%69%70%70%65%68%21%20%59%6F%75%72%20%55%52%4C%20%69%73%20%63%68%61%6C%6C%65%6E%67%65%2F%74%72%61%69%6E%69%6E%67%2F%65%6E%63%6F%64%69%6E%67%73%2F%75%72%6C%2F%73%61%77%5F%6C%6F%74%69%6F%6E%2E%70%68%70%3F%70%3D%61%62%67%6D%61%66%65%62%68%6D%62%64%26%63%69%64%3D%35%32%23%70%61%73%73%77%6F%72%64%3D%66%69%62%72%65%5F%6F%70%74%69%63%73%20%56%65%72%79%20%77%65%6C%6C%20%64%6F%6E%65%21'
print(unquote(enc))
```

## URL-encoding

You can URL-encode in Python with `urllib` and the [quote function](https://docs.python.org/3/library/urllib.parse.html#urllib.parse.quote):

```python
#!/usr/bin/python

from urllib.parse import quote

quoted_path = quote('/12d11acb 168887e1/2215f03a b5521a02')
print(quoted_path)
```

## Resorces

Percent-encoding - Wikipedia: <https://en.wikipedia.org/wiki/Percent-encoding>
