protected override void OnReceiveRequest(HttpRequestLine e) { #if DEBUG Console.ForegroundColor = ConsoleColor.DarkBlue; Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId + " -> " + RequestLine + (RequestHeaders.Referer != null ? ", Referer: " + RequestHeaders.Referer : "") ); Console.ForegroundColor = ConsoleColor.Gray; #endif requestUri = RequestLine.Uri; // NOTE: used by ReceiveResponse if (blacklistHosts.Contains(requestUri?.Host)) { SocketBP.CloseSocket(); } var method = e.Method.ToUpper(); if (method != "CONNECT") { timer = new System.Threading.Timer(new System.Threading.TimerCallback(OnExpire), null, 300 * 1000, System.Threading.Timeout.Infinite); } if ((method == "POST" || method == "PUT" || method == "PATCH")) { if (skipHosts.Contains(e.Uri.Host)) { return; } // Typical requests endpoint: //http://summonerswar-gb.qpyou.cn/api/gateway_c2.php if (e.Uri.AbsoluteUri.Contains("summonerswar") && e.Uri.AbsoluteUri.Contains("/api/gateway")) { string bodyString = Encoding.ASCII.GetString(SocketBP.Buffer, 0, Array.IndexOf(SocketBP.Buffer, (byte)0)); bodyString = bodyString.Substring(bodyString.IndexOf("\r\n\r\n")); // TODO: FIXME: this needs to match first \r?\n\r?\n decRequest = decryptRequest(bodyString, e.Uri.AbsolutePath.Contains("_c2.php") ? 2 : 1); try { req = JsonConvert.DeserializeObject <JObject>(decRequest); if (!Directory.Exists("Json")) { Directory.CreateDirectory("Json"); } File.WriteAllText($"Json\\{req["command"]}" + #if DEBUG $"_{DateTime.Now.ToString("yyyyMMddHHmmssfff")}" + #endif ".req.json", JsonConvert.SerializeObject(req, Formatting.Indented)); Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine($">{req["command"]}"); Console.ForegroundColor = ConsoleColor.Gray; } catch { }; } } }
protected override void OnReceiveResponse() { if (ResponseStatusLine.StatusCode != HttpStatus.OK || !ResponseHeaders.Headers.ContainsKey("content-type")) { return; } if (RequestLine.Method != "POST" || !_requestUri.AbsoluteUri.Contains("api/gateway_c2")) { return; } var response = GetContent(); State.NextStep = null; string content; using (var sr = new StreamReader(GetResponseMessageStream(response))) { content = sr.ReadToEnd(); } SendResponseStatusAndHeaders(); SocketBP.TunnelDataTo(TunnelBP, response); if (SocketBP != null) { SocketBP.CloseSocket(); SocketBP = null; } if (SocketPS != null) { SocketPS.CloseSocket(); SocketPS = null; } State.bPersistConnectionBP = false; State.bPersistConnectionPS = false; var stringResponse = Decrypt.DecryptResponse(content); var json = JObject.Parse(stringResponse); MainWindow.Instance.HandleNewPacket(json); // Temp. saving all commands content to file using (var file = new StreamWriter($@"D:/SW-Commands/{json["command"].ToString()}.txt")) { file.WriteLine(json); file.Close(); } Debug.WriteLine($"Proxy Command: {json["command"].ToString()}"); Debug.WriteLine($"ts: {json["ts_val"].ToString()} / {Ut3()}"); }
private void OnExpire(object state) { #if DEBUG Console.ForegroundColor = ConsoleColor.DarkBlue; Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId + " -- " + this.requestUri.Host + " Expired"); Console.ForegroundColor = ConsoleColor.Gray; #endif if (SocketBP != null) { SocketBP.CloseSocket(); SocketBP = null; } if (SocketPS != null) { SocketPS.CloseSocket(); SocketPS = null; } State.bPersistConnectionBP = false; State.bPersistConnectionPS = false; State.NextStep = null; }