PageRenderTime 100ms CodeModel.GetById 71ms RepoModel.GetById 1ms app.codeStats 0ms

/md5server/md5server/Program.cs

https://github.com/s3krit/md5crack
C# | 211 lines | 187 code | 15 blank | 9 comment | 27 complexity | 447d6da7785d1d6ecd2a774f9eeb79d1 MD5 | raw file
  1. // Licensed under Stallman's beard
  2. using System;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using System.Security.Cryptography;
  7. using System.Text.RegularExpressions;
  8. using System.IO;
  9. namespace server
  10. {
  11. class MainClass
  12. {
  13. static bool isMD5(string s)
  14. {
  15. if (!String.IsNullOrEmpty(s))
  16. {
  17. if (Regex.IsMatch(s, @"[a-fA-F0-9]{32}"))
  18. return true;
  19. }
  20. return false;
  21. }
  22. static bool logSanity(string logFile){
  23. StreamReader SR = new StreamReader(logFile);
  24. StringBuilder stringBuffer = new StringBuilder("");
  25. string logRegex = @"^[\d/: ]+[AP]M.*Hash: [a-fA-F0-9]{32}.*Last Cracked: \d+$";
  26. // only needs to check the latest log; that is, the first 3 lines
  27. for (int i = 0; i < 3; i++){
  28. stringBuffer.Append(SR.ReadLine());
  29. }
  30. Match match = Regex.Match(stringBuffer.ToString(),logRegex,RegexOptions.Singleline);
  31. if (match.Success){
  32. return true;
  33. }
  34. return false;
  35. }
  36. static void writeLog(string logFile, string hash, int last){
  37. if (!File.Exists(logFile)){
  38. File.Create(logFile);
  39. }
  40. StreamReader SR = new StreamReader(logFile);
  41. StringBuilder textBuffer = new StringBuilder("");
  42. textBuffer.AppendLine(DateTime.Now.ToString());
  43. textBuffer.AppendLine("Hash: " + hash);
  44. textBuffer.AppendLine("Last Cracked: " + last.ToString());
  45. for (int i = 0; i < 20; i++)
  46. textBuffer.Append('-');
  47. textBuffer.AppendLine();
  48. textBuffer.Append(SR.ReadToEnd());
  49. SR.Close();
  50. StreamWriter SW = new StreamWriter(logFile);
  51. SW.Write(textBuffer.ToString());
  52. SW.Close();
  53. }
  54. static void Main(string[] args)
  55. {
  56. // TODO: debug that weird 'client started before server' thing (maybe on client)
  57. // TODO: find minimum byte size
  58. int start = 0;
  59. int listeningPort = 8008;
  60. int remotePort = 8009;
  61. string returnData = "";
  62. string plaintext = "";
  63. string hash = null;
  64. string logFile = @"./md5log.txt";
  65. if (File.Exists(logFile))
  66. {
  67. if (logSanity(logFile)){
  68. StreamReader SR = File.OpenText(logFile);
  69. SR.ReadLine();
  70. string tempmd5 = SR.ReadLine().Split()[1];
  71. if (isMD5(tempmd5))
  72. {
  73. hash = tempmd5;
  74. Int32.TryParse(SR.ReadLine().Split()[2], out start);
  75. }
  76. SR.Close();
  77. } else {
  78. Console.WriteLine("Corrupt log file. Proceeding without.");
  79. }
  80. } else {
  81. Console.WriteLine("No log found. Proceeding without");
  82. }
  83. // check that hash contains a valid MD5 sum
  84. setHash(ref hash);
  85. if (hash == null)
  86. {
  87. if (args.Length > 0 && isMD5(args[0]))
  88. hash = args[0];
  89. else
  90. {
  91. while (!isMD5(hash))
  92. {
  93. Console.WriteLine("MD5 hash not present or not valid. Please enter MD5 hash");
  94. hash = Console.ReadLine();
  95. }
  96. }
  97. File.Create(logFile);
  98. start = 0;
  99. }
  100. if(!File.Exists(logFile)){
  101. writeLog(logFile,hash,start);
  102. }
  103. bool complete = false;
  104. UdpClient udpClient = new UdpClient(listeningPort);
  105. Byte[] recieveBytes = new Byte[1024]; // buffer to read the data into 1 kilobyte at a time
  106. //the IP Address.any allows any valid matching address for this machine to be used
  107. //i.e. loopback, broadcast, IPv4, IPv6
  108. IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, listeningPort); //open port 8008 on this machine
  109. Console.WriteLine("Server is Started");
  110. //recieve the data from the UDP packet
  111. //loop until q is sent
  112. while (true)
  113. {
  114. recieveBytes = udpClient.Receive(ref remoteIPEndPoint);
  115. returnData = Encoding.ASCII.GetString(recieveBytes);
  116. returnData.TrimEnd();
  117. if (!complete && returnData.Length > 2)
  118. { // a bit shitty, checks the packet has something
  119. if (returnData.Substring(0, 3) == "NEW")
  120. {
  121. sendBlock(start, remoteIPEndPoint.Address, remotePort,hash);
  122. writeLog(logFile,hash,start);
  123. start += Int32.Parse(returnData.Substring(3));
  124. }
  125. if (returnData.Substring(0, 3) == "FIN")
  126. {
  127. plaintext = returnData.Substring(3);
  128. complete = true;
  129. Console.WriteLine(plaintext);
  130. }
  131. }
  132. else
  133. {
  134. sendFin(remoteIPEndPoint.Address, remotePort);
  135. }
  136. }
  137. }
  138. static void setHash(ref string hash)
  139. {
  140. if (!String.IsNullOrEmpty(hash))
  141. {
  142. Console.WriteLine("Found hash {0} in file, use this? [Y/n]", hash);
  143. string response = Console.ReadLine();
  144. while (true)
  145. {
  146. switch (response)
  147. {
  148. case "y":
  149. return;
  150. case "":
  151. return;
  152. case "n":
  153. hash = null;
  154. return;
  155. default:
  156. Console.WriteLine("Please enter y or n");
  157. response = Console.ReadLine();
  158. break;
  159. }
  160. }
  161. }
  162. }
  163. static void sendBlock(int start, IPAddress dest, int port, string hash)
  164. {
  165. UdpClient client = new UdpClient();
  166. Byte[] sendBytes = new Byte[1024];
  167. try
  168. {
  169. client.Connect(dest, port);
  170. sendBytes = Encoding.ASCII.GetBytes("NEW" + hash + start);
  171. client.Send(sendBytes, sendBytes.GetLength(0));
  172. Console.WriteLine("Sent block {0} to {1}", start, dest.ToString());
  173. }
  174. catch (Exception e)
  175. {
  176. Console.WriteLine("Error with the Server Name: {0}", e.ToString());
  177. }
  178. }
  179. static void sendFin(IPAddress dest, int port)
  180. {
  181. UdpClient client = new UdpClient();
  182. Byte[] sendBytes = new Byte[1024];
  183. try
  184. {
  185. client.Connect(dest, port);
  186. sendBytes = Encoding.ASCII.GetBytes("FIN");
  187. client.Send(sendBytes, sendBytes.GetLength(0));
  188. Console.WriteLine("Sent FIN to {0}", dest.ToString());
  189. }
  190. catch (Exception e)
  191. {
  192. Console.WriteLine("Error with the Server Name: {0}", e.ToString());
  193. }
  194. }
  195. }
  196. }