· last year · Dec 18, 2023, 04:55 PM
1using System;
2using System.Net.Http;
3using System.Text;
4using System.Threading.Tasks;
5using OpenQA.Selenium;
6using OpenQA.Selenium.Chrome;
7using OpenQA.Selenium.Support.UI;
8
9namespace 2CaptchaTesting
10{
11 internal class Program
12 {
13 static string driverPath = @"chromedriver.exe";
14 static string promoURL = "https://rbxgold.com/promo/A38QYNR5XL";
15 static string apiKey = ""; //My api key
16 static string SID = ""; //Account cookie
17 static void Main()
18 {
19 IWebDriver driver = new ChromeDriver(driverPath);
20 WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
21
22 try
23 {
24 driver.Navigate().GoToUrl("https://rbxgold.com");
25
26 Cookie cookie = new Cookie("SID", SID, ".rbxgold.com", "/", DateTime.Now.AddDays(200));
27 driver.Manage().Cookies.AddCookie(cookie);
28
29 driver.Navigate().GoToUrl(promoURL);
30
31 By submitButtonXPath = By.XPath("//*[@id='modal-confirm-button']");
32 IWebElement submitButton = wait.Until(driver => driver.FindElement(submitButtonXPath));
33 submitButton.Click();
34
35 By iframeXPath = By.XPath("//*[@id='modal-content']/div/iframe");
36 IWebElement iframe = wait.Until(driver => driver.FindElement(iframeXPath));
37
38 string iframeSrc = iframe.GetAttribute("src");
39
40 Uri uri = new Uri(iframeSrc);
41 string sitekey = System.Web.HttpUtility.ParseQueryString(uri.Fragment).Get("sitekey");
42
43 Console.ForegroundColor = ConsoleColor.Red;
44 Console.WriteLine($"IFrame source URL: {iframeSrc}");
45 Console.WriteLine($"Extracted Sitekey: {sitekey}");
46 Console.ForegroundColor = ConsoleColor.White;
47
48 string captchaSolution = SolveCaptcha(sitekey).Result;
49
50 Console.WriteLine($"Captcha solved: {captchaSolution}");
51
52 UpdateTextarea(driver, captchaSolution);
53
54 Thread.Sleep(5000);
55
56 SendPostRequest(captchaSolution, sitekey).Wait();
57
58 Console.WriteLine("Press Enter to close the browser...");
59 Console.ReadLine();
60 }
61 finally
62 {
63 driver.Quit();
64 }
65 }
66
67 static async Task SendPostRequest(string captchaSolution, string sitekey)
68 {
69 try
70 {
71 string url = $"https://api.hcaptcha.com/checkcaptcha/{sitekey}/{captchaSolution}";
72
73 using (HttpClient client = new HttpClient())
74 {
75 client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36");
76 client.DefaultRequestHeaders.Add("Accept", "*/*");
77 client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br");
78 client.DefaultRequestHeaders.Add("Accept-Language", "en");
79 client.DefaultRequestHeaders.Add("Sec-Ch-Ua", "\"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"");
80 client.DefaultRequestHeaders.Add("Sec-Ch-Ua-Mobile", "?0");
81 client.DefaultRequestHeaders.Add("Sec-Ch-Ua-Platform", "\"Windows\"");
82 client.DefaultRequestHeaders.Add("Sec-Fetch-Dest", "empty");
83 client.DefaultRequestHeaders.Add("Sec-Fetch-Mode", "cors");
84 client.DefaultRequestHeaders.Add("Sec-Fetch-Site", "same-site");
85
86 var payload = new
87 {
88 answer = captchaSolution,
89 siteKey = sitekey
90 };
91
92 var jsonPayload = Newtonsoft.Json.JsonConvert.SerializeObject(payload);
93
94 var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
95
96 HttpResponseMessage response = await client.PostAsync(url, content);
97
98 if (response.IsSuccessStatusCode)
99 {
100 string responseBody = await response.Content.ReadAsStringAsync();
101 Console.WriteLine($"POST request successful. Response: {responseBody}");
102 }
103 else
104 {
105 Console.WriteLine($"POST request failed. Status Code: {response.StatusCode}");
106 }
107 }
108 }
109 catch (Exception ex)
110 {
111 Console.WriteLine($"Error sending POST request: {ex.Message}");
112 }
113 }
114
115 static void UpdateTextarea(IWebDriver driver, string captchaSolution)
116 {
117 By divXPath = By.XPath("//*[@id='modal-content']/div");
118 By textareaXPath = By.XPath(".//textarea");
119
120 IWebElement div = null;
121 IList<IWebElement> textareas = null;
122
123 try
124 {
125 div = driver.FindElement(divXPath);
126 textareas = div.FindElements(textareaXPath);
127 }
128 catch (NoSuchElementException)
129 {
130 Console.WriteLine("Div or textarea not found.");
131 }
132
133 if (textareas != null && textareas.Count > 0)
134 {
135 for (int i = 0; i < textareas.Count; i++)
136 {
137 if (i == 2)
138 {
139 ((IJavaScriptExecutor)driver).ExecuteScript($"arguments[0].innerHTML = '{captchaSolution}';", textareas[i]);
140 Console.WriteLine($"Textarea {i + 1} inner HTML updated with captcha solution.");
141 }
142 }
143 }
144 else
145 {
146 Console.WriteLine("No textareas found.");
147 }
148 }
149
150 static async Task<string> SolveCaptcha(string sitekey)
151 {
152 try
153 {
154 string apiUrl = $"http://2captcha.com/in.php?key={apiKey}&method=hcaptcha&sitekey={sitekey}&pageurl={promoURL}";
155
156 using (HttpClient client = new HttpClient())
157 {
158 string response = await client.GetStringAsync(apiUrl);
159
160 if (response.StartsWith("OK|"))
161 {
162 string captchaId = response.Split('|')[1];
163 Console.WriteLine($"Captcha ID: {captchaId}");
164
165 await Task.Delay(20000);
166
167 string solutionUrl = $"http://2captcha.com/res.php?key={apiKey}&action=get&id={captchaId}";
168 response = await client.GetStringAsync(solutionUrl);
169 await Console.Out.WriteLineAsync(response);
170 if (response.StartsWith("OK|"))
171 return response.Split('|')[1];
172 else
173 Console.WriteLine($"Error solving captcha: {response}");
174 }
175 else
176 Console.WriteLine($"Error requesting captcha solve: {response}");
177 }
178 }
179 catch (Exception ex)
180 {
181 Console.WriteLine($"Error solving captcha: {ex.Message}");
182 }
183
184 return null;
185 }
186 }
187}