fix dh (by plagiarism), refactor pcapparser

This commit is contained in:
raphaeIl
2025-01-16 14:13:15 -05:00
parent fe508d14d1
commit f269800704
26 changed files with 92455 additions and 42630 deletions

View File

@@ -1,6 +1,8 @@
using System;
using System.Security.Cryptography;
using System.Text;
using Mono.Math;
using Mono.Security.Cryptography;
using NSec.Cryptography;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Modes;
@@ -24,7 +26,7 @@ namespace Novaria.Common.Crypto
public static readonly int IVSize = 16;
public static byte[] PS_REQUEST_NONCE = new byte[12]; // hardcoded nonce, probably makes it easier for server idk? could also just randomly generate but me lazy
public static byte[] PS_PUBLIC_KEY { get => DiffieHellman.Instance.ServerPublicKey.ToByteArray(); }
//public static byte[] PS_PUBLIC_KEY { get => DiffieHellman.Instance.ServerPublicKey.GetBytes(); }
public static string TOKEN = "seggs"; // hardcoded for now
public static readonly byte[] DEFAULT_SERVERLIST_KEY = new byte[] { 74, 72, 42, 67, 80, 51, 50, 57, 89, 120, 111, 103, 81, 74, 69, 120 };
@@ -34,8 +36,9 @@ namespace Novaria.Common.Crypto
public static readonly byte[] IKE_KEY = Encoding.ASCII.GetBytes("3LS9&oYdsp^5wi8&ZxC#c7MZg73hbEDw");
private static DiffieHellmanManaged SendKey;
private static BigInteger p = BigInteger.Parse("1552518092300708935130918131258481755631334049434514313202351194902966239949102107258669453876591642442910007680288864229150803718918046342632727613031282983744380820890196288509170691316593175367469551763119843371637221007210577919");
private static GcmBlockCipher cipher;
private static AesEngine engine;
public delegate void AeadEncryptHandler(Span<byte> result, ReadOnlySpan<byte> key, ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> data, bool needAssociatedData);
@@ -57,6 +60,25 @@ namespace Novaria.Common.Crypto
PS_REQUEST_NONCE[11] = 42;
InitAeadTool();
InitDH();
}
private static int InitDH()
{
int nRandom = 1;
byte[] bytes = BitConverter.GetBytes(2);
if (BitConverter.IsLittleEndian)
{
Array.Reverse<byte>(bytes);
}
byte[] bytes2 = BitConverter.GetBytes(nRandom);
if (BitConverter.IsLittleEndian)
{
Array.Reverse<byte>(bytes2);
}
SendKey = new DiffieHellmanManaged(p.GetBytes(), bytes, bytes2);
return nRandom;
}
public static void InitAeadTool()
@@ -84,6 +106,24 @@ namespace Novaria.Common.Crypto
AeadTool.cipher = new GcmBlockCipher(AeadTool.engine);
}
public static void SetAeadKey(byte[] pubKey)
{
byte[] array = SendKey.DecryptKeyExchange(pubKey);
int num = array.Length;
if (num > 32)
{
num = 32;
}
key3 = new byte[32];
Buffer.BlockCopy(array, 0, key3, 0, num);
}
public static byte[] GetPubKey()
{
return new BigInteger(SendKey.CreateKeyExchange()).GetBytes();
}
private static void Encrypt_AESGCM_BouncyCastle(Span<byte> result, ReadOnlySpan<byte> key, ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> data, bool needAssociatedData)
{
if (!needAssociatedData)

View File

@@ -1,11 +1,11 @@
using Novaria.Common.Util;
using System.Numerics;
using Mono.Math;
using Novaria.Common.Util;
namespace Novaria.Common.Crypto
{
public class DiffieHellman : Singleton<DiffieHellman>
{
private BigInteger p = BigInteger.Parse("1552518092300708935130918131258481755631334049434514313202351194902966239949102107258669453876591642442910007680288864229150803718918046342632727613031282983744380820890196288509170691316593175367469551763119843371637221007210577919");
private System.Numerics.BigInteger old_p = System.Numerics.BigInteger.Parse("1552518092300708935130918131258481755631334049434514313202351194902966239949102107258669453876591642442910007680288864229150803718918046342632727613031282983744380820890196288509170691316593175367469551763119843371637221007210577919");
private BigInteger g = 2;
@@ -15,37 +15,49 @@ namespace Novaria.Common.Crypto
public DiffieHellman()
{
Console.WriteLine(spriv);
//Console.WriteLine(spriv);
//g** Spriv mod p
ServerPublicKey = BigInteger.ModPow(g, spriv, p);
//ServerPublicKey = this.g.ModPow(spriv, p);
}
public byte[] CalculateKey(byte[] clientPubKey) // server calculates key like this
{
BigInteger clientPubKeyInt = new BigInteger(clientPubKey.Reverse().ToArray());
//Cpub**Spriv mod p
Console.WriteLine(clientPubKeyInt.ToString());
var result = BigInteger.ModPow(clientPubKeyInt, spriv, p);
// old stuff
//System.Numerics.BigInteger clientPubKeyInt = new System.Numerics.BigInteger(clientPubKey.Reverse().ToArray());
//var result = System.Numerics.BigInteger.ModPow(clientPubKeyInt, new System.Numerics.BigInteger(new byte[] { 1, 2, 3, 4 }), old_p);
//Console.WriteLine(result);
//if (result < 0)
//{
// return result.ToByteArray(false, true)[..32];
// Console.WriteLine("THIS SHOULD CRASH");
//}
return result.ToByteArray(true, true)[..32];
}
// this is for manual pcap parsing use only, officalServerPubKey is in the first IKE response, client priv will be in frida
public byte[] CalculateKey(byte[] officalServerPubKey, byte[] officialClientPriv)
{
BigInteger officalServerPubKeyInt = new BigInteger(officalServerPubKey.Reverse().ToArray());
BigInteger officialClientPrivInt = new BigInteger(officialClientPriv.Reverse().ToArray());
//BigInteger bigInteger = new BigInteger(clientPubKey.Reverse().ToArray()).ModPow(this.spriv, this.p);
BigInteger result = BigInteger.ModPow(officalServerPubKeyInt, officialClientPrivInt, p);
//return bigInteger.GetBytes()[..32];
return null;
//BigInteger clientPubKeyInt = new BigInteger(clientPubKey.Reverse().ToArray());
return result.ToByteArray(true, true)[..32];
////Cpub**Spriv mod p
//Console.WriteLine(clientPubKeyInt.ToString());
//var result = BigInteger.ModPow(clientPubKeyInt, spriv, p);
//Console.WriteLine("------------test-------------------");
////Utils.PrintByteArray(BigInteger.Abs());
////if (result < 0)
////{
//// return result.ToByteArray(false, true)[..32];
////}
//Utils.PrintByteArray(result.ToByteArray(true, true));
//Console.WriteLine("----------------------------------");
//return result.GetBytes()[..32];
//return result.ToByteArray(true, true)[..32];
}
}
}