Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using EasyHook;
  13.  
  14. namespace RecvMetin
  15. {
  16. public partial class Form1 : Form
  17. {
  18. public interface ICipher
  19. {
  20. bool CanProcess { get; }
  21. void DecryptIngoing(byte[] to_decrypt);
  22. void EncryptOutgoing(byte[] to_encrypt);
  23. }
  24.  
  25. public ICipher Cipher;
  26. public bool KeyAgreement;
  27. public int twice = 0;
  28. [UnmanagedFunctionPointer(CallingConvention.StdCall,
  29. CharSet = CharSet.Unicode,
  30. SetLastError = true)]
  31. delegate int Drecv(
  32. IntPtr socketHandle,
  33. IntPtr buf,
  34. int count,
  35. int socketFlags
  36. );
  37.  
  38. [UnmanagedFunctionPointer(CallingConvention.StdCall,
  39. CharSet = CharSet.Unicode,
  40. SetLastError = true)]
  41. delegate int Dsend(
  42. IntPtr socketHandle,
  43. IntPtr buf,
  44. int count,
  45. int socketFlags
  46. );
  47.  
  48. public string ByteArrayToStringHex(byte[] array)
  49. {
  50. var hex = BitConverter.ToString(array);
  51. return hex.Replace("-", " ");
  52. }
  53.  
  54. public static byte[] StringToByteArray(string hex)
  55. {
  56. return Enumerable.Range(0, hex.Length)
  57. .Where(x => x % 2 == 0)
  58. .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
  59. .ToArray();
  60. }
  61.  
  62. int recv_Hooked(IntPtr socketHandle,IntPtr buf,int count,int socketFlags)
  63. {
  64. int bytesCount = recv(socketHandle, buf, count, socketFlags);
  65. if (bytesCount > 0)
  66. {
  67. byte[] newBuffer = new byte[bytesCount];
  68. Marshal.Copy(buf, newBuffer, 0, bytesCount);
  69. string s;
  70. byte key = newBuffer.Take(1).ToArray()[0];
  71. var cip = Cipher as Metin2_Cipher;
  72. if (key == 251 && twice <= 1)
  73. {
  74. var PublicKey = newBuffer.Skip(5).ToArray();
  75. richTextBox2.AppendText("Cipher ->" + ByteArrayToStringHex(PublicKey) + "\n");
  76. richTextBox2.AppendText("PublicKey ->" + PublicKey.Length + "\n");
  77. cip.Activate(true, 256, PublicKey);
  78. richTextBox2.AppendText("~~~~Cipher~~~~ " + cip.CanProcess + "\n");
  79. } else if (key == 250 && twice <= 1)
  80. {
  81. richTextBox2.AppendText("~~~~Cipher~~~~ " + cip.CanProcess + "\n");
  82. KeyAgreement = true;
  83. richTextBox2.AppendText("Cipher working! Connection encrypted" + "\n");
  84. twice++;
  85. }
  86. if (KeyAgreement)
  87. {
  88. cip.DecryptIngoing(newBuffer);
  89. }
  90. s = ByteArrayToStringHex(newBuffer);
  91. richTextBox2.AppendText("RECV ->" + s + "\n");
  92.  
  93. }
  94. return bytesCount;
  95. }
  96.  
  97. int send_Hooked(IntPtr socketHandle, IntPtr buf, int count, int socketFlags)
  98. {
  99. int bytesCount = send(socketHandle, buf, count, socketFlags);
  100. if (bytesCount > 0)
  101. {
  102. byte[] newBuffer = new byte[bytesCount];
  103. Marshal.Copy(buf, newBuffer, 0, bytesCount);
  104. string s;
  105. if (KeyAgreement)
  106. {
  107. Cipher.DecryptIngoing(newBuffer);
  108. }
  109. s = ByteArrayToStringHex(newBuffer);
  110. richTextBox1.AppendText("CRYPTED SEND ->" + s + "\n");
  111. }
  112. return bytesCount;
  113. }
  114.  
  115. [DllImport("Ws2_32.dll")]
  116. static extern int recv(
  117. IntPtr socketHandle,
  118. IntPtr buf,
  119. int count,
  120. int socketFlags
  121. );
  122.  
  123. [DllImport("Ws2_32.dll")]
  124. static extern int send(
  125. IntPtr socketHandle,
  126. IntPtr buf,
  127. int count,
  128. int socketFlags
  129. );
  130.  
  131. public Form1()
  132. {
  133. InitializeComponent();
  134. KeyAgreement = false;
  135. Cipher = new Metin2_Cipher();
  136. try
  137. {
  138. var CreateRecvHook = LocalHook.Create(
  139. LocalHook.GetProcAddress("ws2_32.dll", "recv"),
  140. new Drecv(recv_Hooked),
  141. this);
  142.  
  143. CreateRecvHook.ThreadACL.SetExclusiveACL(new Int32[] { 0 });
  144.  
  145. var CreateSendHook = LocalHook.Create(
  146. LocalHook.GetProcAddress("ws2_32.dll", "send"),
  147. new Dsend(send_Hooked),
  148. this);
  149.  
  150. CreateSendHook.ThreadACL.SetExclusiveACL(new Int32[] { 0 });
  151. }
  152. catch (Exception ExtInfo)
  153. {
  154. Console.WriteLine("Error creating the Hook:" + ExtInfo);
  155. return;
  156. }
  157. }
  158.  
  159. private void FlatButton2_Click(object sender, EventArgs e)
  160. {
  161. this.Hide();
  162. }
  163.  
  164. private void FormSkin1_Click(object sender, EventArgs e)
  165. {
  166.  
  167. }
  168. }
  169.  
  170. public class Metin2_Cipher : Form1.ICipher
  171. {
  172. public Cipher cip;
  173. public bool Completed;
  174.  
  175. public Metin2_Cipher()
  176. {
  177. cip = new Cipher();
  178. }
  179.  
  180. public int AgreedLength => cip.agreed_length;
  181.  
  182. public byte[] Key => cip.GetKey();
  183. public bool CanProcess => cip.Activated && Completed;
  184.  
  185. public void DecryptIngoing(byte[] to_decrypt)
  186. {
  187. var decrypted = cip.Decrypt(to_decrypt);
  188. Console.WriteLine("Decrypted! " + decrypted[0]);
  189. Array.Copy(decrypted, to_decrypt, decrypted.Length);
  190. }
  191.  
  192. public void EncryptOutgoing(byte[] to_encrypt)
  193. {
  194. if (!CanProcess)
  195. return;
  196. var encrypted = cip.Encrypt(to_encrypt);
  197. Array.Copy(encrypted, to_encrypt, encrypted.Length);
  198. }
  199.  
  200. public void Stop() { Completed = false; }
  201. public bool Activate(bool Polarity, int AgreedLength, byte[] PublicKey)
  202. {
  203. if (cip.Prepare() == 0)
  204. {
  205. Console.WriteLine("Cipher.Prepare(): failure, result was 0");
  206. throw new FormatException("Cipher.Prepare: failure, result was 0");
  207. }
  208. Console.WriteLine("Cipher.Prepare()");
  209. return cip.Activate(Polarity, (uint)AgreedLength, PublicKey);
  210. }
  211. public void Start()
  212. {
  213. Completed = true;
  214. }
  215. }
  216.  
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement