Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using EasyHook;
- namespace RecvMetin
- {
- public partial class Form1 : Form
- {
- public interface ICipher
- {
- bool CanProcess { get; }
- void DecryptIngoing(byte[] to_decrypt);
- void EncryptOutgoing(byte[] to_encrypt);
- }
- public ICipher Cipher;
- public bool KeyAgreement;
- public int twice = 0;
- [UnmanagedFunctionPointer(CallingConvention.StdCall,
- CharSet = CharSet.Unicode,
- SetLastError = true)]
- delegate int Drecv(
- IntPtr socketHandle,
- IntPtr buf,
- int count,
- int socketFlags
- );
- [UnmanagedFunctionPointer(CallingConvention.StdCall,
- CharSet = CharSet.Unicode,
- SetLastError = true)]
- delegate int Dsend(
- IntPtr socketHandle,
- IntPtr buf,
- int count,
- int socketFlags
- );
- public string ByteArrayToStringHex(byte[] array)
- {
- var hex = BitConverter.ToString(array);
- return hex.Replace("-", " ");
- }
- public static byte[] StringToByteArray(string hex)
- {
- return Enumerable.Range(0, hex.Length)
- .Where(x => x % 2 == 0)
- .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
- .ToArray();
- }
- int recv_Hooked(IntPtr socketHandle,IntPtr buf,int count,int socketFlags)
- {
- int bytesCount = recv(socketHandle, buf, count, socketFlags);
- if (bytesCount > 0)
- {
- byte[] newBuffer = new byte[bytesCount];
- Marshal.Copy(buf, newBuffer, 0, bytesCount);
- string s;
- byte key = newBuffer.Take(1).ToArray()[0];
- var cip = Cipher as Metin2_Cipher;
- if (key == 251 && twice <= 1)
- {
- var PublicKey = newBuffer.Skip(5).ToArray();
- richTextBox2.AppendText("Cipher ->" + ByteArrayToStringHex(PublicKey) + "\n");
- richTextBox2.AppendText("PublicKey ->" + PublicKey.Length + "\n");
- cip.Activate(true, 256, PublicKey);
- richTextBox2.AppendText("~~~~Cipher~~~~ " + cip.CanProcess + "\n");
- } else if (key == 250 && twice <= 1)
- {
- richTextBox2.AppendText("~~~~Cipher~~~~ " + cip.CanProcess + "\n");
- KeyAgreement = true;
- richTextBox2.AppendText("Cipher working! Connection encrypted" + "\n");
- twice++;
- }
- if (KeyAgreement)
- {
- cip.DecryptIngoing(newBuffer);
- }
- s = ByteArrayToStringHex(newBuffer);
- richTextBox2.AppendText("RECV ->" + s + "\n");
- }
- return bytesCount;
- }
- int send_Hooked(IntPtr socketHandle, IntPtr buf, int count, int socketFlags)
- {
- int bytesCount = send(socketHandle, buf, count, socketFlags);
- if (bytesCount > 0)
- {
- byte[] newBuffer = new byte[bytesCount];
- Marshal.Copy(buf, newBuffer, 0, bytesCount);
- string s;
- if (KeyAgreement)
- {
- Cipher.DecryptIngoing(newBuffer);
- }
- s = ByteArrayToStringHex(newBuffer);
- richTextBox1.AppendText("CRYPTED SEND ->" + s + "\n");
- }
- return bytesCount;
- }
- [DllImport("Ws2_32.dll")]
- static extern int recv(
- IntPtr socketHandle,
- IntPtr buf,
- int count,
- int socketFlags
- );
- [DllImport("Ws2_32.dll")]
- static extern int send(
- IntPtr socketHandle,
- IntPtr buf,
- int count,
- int socketFlags
- );
- public Form1()
- {
- InitializeComponent();
- KeyAgreement = false;
- Cipher = new Metin2_Cipher();
- try
- {
- var CreateRecvHook = LocalHook.Create(
- LocalHook.GetProcAddress("ws2_32.dll", "recv"),
- new Drecv(recv_Hooked),
- this);
- CreateRecvHook.ThreadACL.SetExclusiveACL(new Int32[] { 0 });
- var CreateSendHook = LocalHook.Create(
- LocalHook.GetProcAddress("ws2_32.dll", "send"),
- new Dsend(send_Hooked),
- this);
- CreateSendHook.ThreadACL.SetExclusiveACL(new Int32[] { 0 });
- }
- catch (Exception ExtInfo)
- {
- Console.WriteLine("Error creating the Hook:" + ExtInfo);
- return;
- }
- }
- private void FlatButton2_Click(object sender, EventArgs e)
- {
- this.Hide();
- }
- private void FormSkin1_Click(object sender, EventArgs e)
- {
- }
- }
- public class Metin2_Cipher : Form1.ICipher
- {
- public Cipher cip;
- public bool Completed;
- public Metin2_Cipher()
- {
- cip = new Cipher();
- }
- public int AgreedLength => cip.agreed_length;
- public byte[] Key => cip.GetKey();
- public bool CanProcess => cip.Activated && Completed;
- public void DecryptIngoing(byte[] to_decrypt)
- {
- var decrypted = cip.Decrypt(to_decrypt);
- Console.WriteLine("Decrypted! " + decrypted[0]);
- Array.Copy(decrypted, to_decrypt, decrypted.Length);
- }
- public void EncryptOutgoing(byte[] to_encrypt)
- {
- if (!CanProcess)
- return;
- var encrypted = cip.Encrypt(to_encrypt);
- Array.Copy(encrypted, to_encrypt, encrypted.Length);
- }
- public void Stop() { Completed = false; }
- public bool Activate(bool Polarity, int AgreedLength, byte[] PublicKey)
- {
- if (cip.Prepare() == 0)
- {
- Console.WriteLine("Cipher.Prepare(): failure, result was 0");
- throw new FormatException("Cipher.Prepare: failure, result was 0");
- }
- Console.WriteLine("Cipher.Prepare()");
- return cip.Activate(Polarity, (uint)AgreedLength, PublicKey);
- }
- public void Start()
- {
- Completed = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement