Exploiting __VIEWSTATE without knowing the secrets
Last updated
Last updated
ViewState is the method that the ASP.NET framework uses by default to preserve page and control values between web pages. When the HTML for the page is rendered, the current state of the page and values that need to be retained during postback are serialized into base64-encoded strings and output in the ViewState hidden field or fields. The following properties or combination of properties apply to ViewState information:
Base64
Can be defined using EnableViewStateMac and ViewStateEncryptionMode attribute set to false
Base64 + MAC (Message Authentication Code) Enabled
Can be defined using EnableViewStateMac attribute set to true
Base64 + Encrypted
Can be defined using viewStateEncryptionMode attribute set to true
It is also possible to disable the ViewStateMAC completely by setting the AspNetEnforceViewStateMac
registry key to zero in:
Identifying ViewState Attributes
You can try to identify if ViewState is MAC protected by capturing a request containing this parameter with BrupSuite:
If Mac is not used to protect the parameter you can exploit it using YSoSerial.Net.
Developers can remove ViewState from becoming part of an HTTP Request (the user won't receive this cookie). One may assume that if ViewState is not present, their implementation is secure from any potential vulnerabilities arising with ViewState deserialization. However, that is not the case. If we add ViewState parameter to the request body and send our serialized payload created using ysoserial, we will still be able to achieve code execution as shown in Case 1.
In order to enable ViewState MAC for a specific page we need to make following changes on a specific aspx file:
We can also do it for overall application by setting it on the web.config file as shown below:
As the parameter is MAC protected this time to successfully execute the attack we first need the key used. In this case, BurpSuite will let us know that the parameter is MAC protected:
You can try to use Blacklist3r(AspDotNetWrapper.exe) to find the key used.
If you are lucky and the key is found,you can proceed with the attack using YSoSerial.Net:
In cases where _VIEWSTATEGENERATOR
parameter isn't sent by the server you don't need to provide the --generator
parameter but these ones:
In this case Burp doesn't find if the parameter is protected with MAC because it doesn't recognise the values. Then, the value is probably encrypted and you will need the Machine Key to encrypt your payload to exploit the vulnerability.
In this case the Blacklist3r module is under development...
Prior to .NET 4.5, ASP.NET can accept an unencrypted __VIEWSTATE
_parameter from the users even if ViewStateEncryptionMode
has been set to Always_. ASP.NET only checks the presence of the __VIEWSTATEENCRYPTED
parameter in the request. If one removes this parameter, and sends the unencrypted payload, it will still be processed.
Threfore, if the Machinekey is known (e.g. via a directory traversal issue), YSoSerial.Net command used in the Case 2, can be used to perform RCE using ViewState deserialization vulnerability.
Remove __VIEWSTATEENCRYPTED
parameter from the request in order to exploit the ViewState deserialization vulnerability, else it will return a Viewstate MAC validation error and exploit will fail as shown in Figure:
We can force the usage of ASP.NET framework by specifying the below parameter inside the web.config file as shown below.
Alternatively, this can be done by specifying the below option inside the machineKey
paramter of web.config file.
As in the previous case Burp doesn't identify if the request is MAC protected because the value is encrypted. Then, to send a valid payload the attacker need the key.
You can try to use Blacklist3r(AspDotNetWrapper.exe) to find the key being used:
For a more detailed description for IISDirPath and TargetPagePath refer here
Once a valid Machine key is identified, the next step is to generate a serialized payload using YSoSerial.Net
If you have the value of __VIEWSTATEGENERATOR
you can try to use the --generator
parameter with that value and omit the parameters --path
and --apppath
If the ViewState deserialization vulnerability is successfully exploited, an attacker-controlled server will receive an out of band request containing the username. PoC of Successful Exploitation
The ViewStateUserKey property can be used to defend against a CSRF attack. If such a key has been defined in the application and we try to generate the ViewState payload with the methods discussed till now, the payload won’t be processed by the application. You need to use one more parameter in order to create correctly the payload:
For all the test cases, if the ViewState YSoSerial.Net payload works successfully then the server responds with “500 Internal server error” having response content “The state information is invalid for this page and might be corrupted” and we get the OOB request as shown in Figures below:
out of band request with the current username