Advertisement
Guest User

SolveCaptcha.cs

a guest
Mar 23rd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. //Solves Captcha on the NZ Government's "Hand in firearms" form https://forms.police.govt.nz/forms/firearms-hand-in
  2. //Probably can be adapted to break captchas for other shitty Math Based captchas.
  3.  
  4. using NCalc;
  5. using System;
  6. using System.Collections.Generic;
  7. using Flurl.Http;
  8. using HtmlAgilityPack;
  9.  
  10. namespace CaptchaBreaker
  11. {
  12. class Program
  13. {
  14.  
  15. static Dictionary<String, String> NumberStrings = new Dictionary<String, String>
  16. {
  17. { "zero", "0" },
  18. { "one", "1" },
  19. { "two", "2" },
  20. { "three", "3" },
  21. { "four", "4" },
  22. { "five", "5" },
  23. { "six", "6" },
  24. { "seven", "7" },
  25. { "eight", "8" },
  26. { "nine", "9" },
  27. { "ten", "10" },
  28. { "eleven", "11" },
  29. { "twelve", "12" },
  30. { "thirteen", "13" },
  31. { "fourteen", "14" },
  32. { "fifteen", "15" },
  33. { "plus", "+" },
  34. { "minus", "-" },
  35. { "times", "*" },
  36. { "equals", "=" },
  37. { "devide", "/" }
  38. };
  39.  
  40.  
  41. static string FindNeededValue(String Equation)
  42. {
  43. string[] Components = Equation.Split(' ');
  44.  
  45. int i = 0;
  46. foreach (String Component in Components)
  47. {
  48. if (Component == "=")
  49. {
  50. return Components[i + 1];
  51. }
  52. i++;
  53. }
  54. return "0";
  55. }
  56.  
  57. static int FindX(String Equation)
  58. {
  59. int i = 0;
  60.  
  61. int NeededValue = int.Parse(FindNeededValue(Equation));
  62. String FinalEquation = Equation.Substring(0, Equation.IndexOf('='));
  63.  
  64. while (true)
  65. {
  66. string Try = FinalEquation.Replace("X", i.ToString());
  67. Expression e = new Expression(Try);
  68. if ((int)e.Evaluate() == NeededValue)
  69. {
  70. return i;
  71. }
  72.  
  73. i++;
  74. }
  75. }
  76.  
  77. static int SolveEquation(string Equation)
  78. {
  79. String[] Components = Equation.Split(' ');
  80.  
  81. List<String> Reconstructed = new List<String>();
  82.  
  83. foreach(String Component in Components)
  84. {
  85. try
  86. {
  87. int.Parse(Component);
  88. Reconstructed.Add(Component);
  89. }
  90. catch(Exception)
  91. {
  92. if (Component == "X")
  93. {
  94. Reconstructed.Add("X");
  95. }
  96. if (NumberStrings.ContainsKey(Component))
  97. {
  98. Reconstructed.Add(NumberStrings[Component]);
  99. }
  100.  
  101. }
  102. }
  103.  
  104.  
  105. String FinalEquation = String.Join(" ", Reconstructed.ToArray());
  106.  
  107. if(FinalEquation.Contains("X"))
  108. {
  109. return FindX(FinalEquation);
  110. }
  111.  
  112. try
  113. {
  114. FinalEquation = FinalEquation.Substring(0, FinalEquation.IndexOf('='));
  115. }
  116. catch (Exception) { };
  117.  
  118.  
  119. Expression e = new Expression(FinalEquation);
  120. return (int)e.Evaluate();
  121. }
  122.  
  123. static void Main(string[] args)
  124. {
  125. while(true)
  126. {
  127. var Request = "https://forms.police.govt.nz/forms/firearms-hand-in".GetStringAsync();
  128. Request.Wait();
  129. String PageHtml = Request.Result;
  130.  
  131. var doc = new HtmlDocument();
  132. doc.LoadHtml(PageHtml);
  133.  
  134. var Node = doc.DocumentNode.SelectSingleNode("//*[@id=\"webform-client-form-155\"]/div/fieldset/div/div[2]/span[1]");
  135. var Node2 = doc.DocumentNode.SelectSingleNode("//*[@id=\"webform-client-form-155\"]/div/fieldset/div/div[2]/span[2]");
  136.  
  137. String FullEquation;
  138.  
  139. String Eq1 = Node.InnerText;
  140. String Eq2 = "";
  141.  
  142. if (Node2 != null)
  143. {
  144. Eq2 = Node2.InnerText;
  145. FullEquation = Eq1 + "X" + Eq2;
  146. }
  147. else if (Eq1.StartsWith(" "))
  148. {
  149. FullEquation = "X" + Eq1;
  150. }
  151. else
  152. {
  153. FullEquation = Eq1;
  154. }
  155.  
  156.  
  157. Console.WriteLine(FullEquation);
  158. Console.WriteLine(SolveEquation(FullEquation));
  159. }
  160. }
  161. }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement