Skip to content

Instantly share code, notes, and snippets.

@AndrewSav
Last active January 3, 2024 03:15
Show Gist options
  • Save AndrewSav/2dd6a55c310cbb6749cfa67f4585aca8 to your computer and use it in GitHub Desktop.
Save AndrewSav/2dd6a55c310cbb6749cfa67f4585aca8 to your computer and use it in GitHub Desktop.
<Query Kind="Program">
<NuGetReference>YamlDotNet</NuGetReference>
<Namespace>YamlDotNet.Serialization</Namespace>
<Namespace>YamlDotNet.Core</Namespace>
<Namespace>System.Security.Cryptography</Namespace>
<Namespace>System.Runtime.InteropServices</Namespace>
</Query>
void Main()
{
var key = "Proxy Config Sec";
var path = @"C:\Users\user\AppData\Roaming\JetBrains\DataGrip2023.3\c.pwd";
var deserializer = new DeserializerBuilder()
.WithTypeConverter(new ByteArrayConverter())
.WithTagMapping("tag:yaml.org,2002:binary", typeof(byte[]))
.Build();
Dictionary<object, object> yamlObject = (Dictionary<object, object>)deserializer.Deserialize(File.ReadAllText(path))!;
var data = (byte[])yamlObject["value"];
data = ProtectedData.Unprotect(data, null, DataProtectionScope.CurrentUser);
var span = new Span<byte>(data);
MemoryExtensions.Reverse(span[..4]);
var ivlen = MemoryMarshal.Read<int>(span[..4]);
var iv = span[4..(4 + ivlen)];
data = span[(4 + ivlen)..].ToArray();
using var aesAlg = Aes.Create();
aesAlg.Key = Encoding.ASCII.GetBytes(key);
aesAlg.IV = iv.ToArray();
var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using var msDecrypt = new MemoryStream(data);
using var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
using var msPlain = new MemoryStream();
csDecrypt.CopyTo(msPlain);
var decryptedBytes = msPlain.ToArray();
Console.WriteLine(Encoding.UTF8.GetString(decryptedBytes));
}
public class ByteArrayConverter : IYamlTypeConverter
{
public bool Accepts(Type type)
{
return type == typeof(byte[]);
}
public object ReadYaml(YamlDotNet.Core.IParser parser, Type type)
{
var scalar = (YamlDotNet.Core.Events.Scalar)parser.Current;
var bytes = Convert.FromBase64String(scalar.Value);
parser.MoveNext();
return bytes;
}
public void WriteYaml(YamlDotNet.Core.IEmitter emitter, object value, Type type)
{
var bytes = (byte[])value;
emitter.Emit(new YamlDotNet.Core.Events.Scalar(
null,
"tag:yaml.org,2002:binary",
Convert.ToBase64String(bytes),
ScalarStyle.Plain,
false,
false
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment