using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Text.RegularExpressions; using System.Collections.Specialized; using System.Net; using System.IO; namespace CustomRequest { public partial class frmMain : Form { public frmMain() { InitializeComponent(); } private DataGridViewRow rowHost; private DataGridViewRow rowPath; private DataGridViewRow rowHTTP; private Regex regUrl = new Regex("^((?https?)\\:\\/\\/)?(?[a-zA-Z0-9\\.\\-_]+)(?\\/.*?)?(?\\?.*?)?$", RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.ExplicitCapture); private void frmMain_Load(object sender, EventArgs e) { this.New(); string[] arguments = Environment.GetCommandLineArgs(); if(arguments.Length>1) { if (this.LoadFile(arguments[1])) this.Run(); } } public void New() { initRequestheaderView(); this.txtResponse.Text = ""; this.txtResponseCode.Text = ""; this.txtUrl.Text = ""; this.dgvCookies.Rows.Clear(); this.dgvGET.Rows.Clear(); this.dgvPOST.Rows.Clear(); } protected void initRequestheaderView() { this.rowHost = new DataGridViewRow(); this.rowPath = new DataGridViewRow(); this.rowHTTP = new DataGridViewRow(); DataGridViewRow rowMethod = new DataGridViewRow(); rowMethod.Cells.AddRange(new DataGridViewCell[] { new DataGridViewTextBoxCell() { Value = "Method" }, new DataGridViewCheckBoxCell() { Value = true }, //new DataGridViewTextBoxCell() { Value = "GET" } new DataGridViewComboBoxCell() { Value = "GET" } }); ((DataGridViewComboBoxCell)rowMethod.Cells[2]).Items.AddRange("GET", "POST", "PATCH", "PUT", "CONNECT", "HEAD", "DELETE", "TRACE"); rowPath.Cells.AddRange(new DataGridViewCell[] { new DataGridViewTextBoxCell() { Value = "Path" }, new DataGridViewCheckBoxCell() { Value = true }, new DataGridViewTextBoxCell() { Value = "" } }); rowHTTP.Cells.AddRange(new DataGridViewCell[] { new DataGridViewTextBoxCell() { Value = "HTTP Version" }, new DataGridViewCheckBoxCell() { Value = true }, new DataGridViewComboBoxCell() { Value = "HTTP/1.1" } }); ((DataGridViewComboBoxCell)rowHTTP.Cells[2]).Items.AddRange("HTTP/1.0", "HTTP/1.1"); rowHost.Cells.AddRange(new DataGridViewCell[] { new DataGridViewTextBoxCell() { Value = "host" }, new DataGridViewCheckBoxCell() { Value = true }, new DataGridViewTextBoxCell() { Value = "" } }); DataGridViewRow rowConnection = new DataGridViewRow(); rowConnection.Cells.AddRange(new DataGridViewCell[] { new DataGridViewTextBoxCell() { Value = "Connection" }, new DataGridViewCheckBoxCell() { Value = true }, new DataGridViewComboBoxCell() { Value = "Close" } }); ((DataGridViewComboBoxCell)rowConnection.Cells[2]).Items.AddRange("Close", "keep-alive"); DataGridViewRow rowContentType = new DataGridViewRow(); rowContentType.Cells.AddRange(new DataGridViewCell[] { new DataGridViewTextBoxCell() { Value = "Content-Type" }, new DataGridViewCheckBoxCell() { Value = false }, new DataGridViewTextBoxCell() { Value = "application/x-www-form-urlencoded" } }); DataGridViewRow rowContentLength = new DataGridViewRow(); rowContentLength.Cells.AddRange(new DataGridViewCell[] { new DataGridViewTextBoxCell() { Value = "Content-Length" }, new DataGridViewCheckBoxCell() { Value = false }, new DataGridViewTextBoxCell() { Value = "auto" } }); DataGridViewRow rowPragma = new DataGridViewRow(); rowPragma.Cells.AddRange(new DataGridViewCell[] { new DataGridViewTextBoxCell() { Value = "pragma" }, new DataGridViewCheckBoxCell() { Value = true }, new DataGridViewTextBoxCell() { Value = "no-cache" } }); DataGridViewRow rowCacheControl = new DataGridViewRow(); rowCacheControl.Cells.AddRange(new DataGridViewCell[] { new DataGridViewTextBoxCell() { Value = "cache-control" }, new DataGridViewCheckBoxCell() { Value = true }, new DataGridViewTextBoxCell() { Value = "no-cache" } }); DataGridViewRow rowUpgradeInsecureRequests = new DataGridViewRow(); rowUpgradeInsecureRequests.Cells.AddRange(new DataGridViewCell[] { new DataGridViewTextBoxCell() { Value = "Upgrade-Insecure-Requests" }, new DataGridViewCheckBoxCell() { Value = true }, new DataGridViewTextBoxCell() { Value = "1" } }); DataGridViewRow rowUserAgent = new DataGridViewRow(); rowUserAgent.Cells.AddRange(new DataGridViewCell[] { new DataGridViewTextBoxCell() { Value = "User-Agent" }, new DataGridViewCheckBoxCell() { Value = true }, new DataGridViewTextBoxCell() { Value = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.158 Safari/537.36 Vivaldi/2.5.1525.43" } }); DataGridViewRow rowDNT = new DataGridViewRow(); rowDNT.Cells.AddRange(new DataGridViewCell[] { new DataGridViewTextBoxCell() { Value = "DNT" }, new DataGridViewCheckBoxCell() { Value = true }, new DataGridViewTextBoxCell() { Value = "1" } }); rowDNT.Cells[0].ToolTipText = "Do not Track"; DataGridViewRow rowAccept = new DataGridViewRow(); rowAccept.Cells.AddRange(new DataGridViewCell[] { new DataGridViewTextBoxCell() { Value = "Accept" }, new DataGridViewCheckBoxCell() { Value = true }, new DataGridViewTextBoxCell() { Value = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3" } }); DataGridViewRow rowReferer = new DataGridViewRow(); rowReferer.Cells.AddRange(new DataGridViewCell[] { new DataGridViewTextBoxCell() { Value = "referer" }, new DataGridViewCheckBoxCell() { Value = false }, new DataGridViewTextBoxCell() { Value = "https://www.google.com/" } }); DataGridViewRow rowAcceptEncoding = new DataGridViewRow(); rowAcceptEncoding.Cells.AddRange(new DataGridViewCell[] { new DataGridViewTextBoxCell() { Value = "Accept-Encoding" }, new DataGridViewCheckBoxCell() { Value = true }, new DataGridViewTextBoxCell() { Value = "gzip, deflate" } }); DataGridViewRow rowAcceptLanguage = new DataGridViewRow(); rowAcceptLanguage.Cells.AddRange(new DataGridViewCell[] { new DataGridViewTextBoxCell() { Value = "Accept-Language" }, new DataGridViewCheckBoxCell() { Value = true }, new DataGridViewTextBoxCell() { Value = "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7" } }); dgvRequest.Rows.Clear(); dgvRequest.Rows.AddRange(new DataGridViewRow[] { rowMethod, rowPath, rowHTTP, rowHost, rowConnection, rowPragma, rowUpgradeInsecureRequests, rowUserAgent, rowDNT, rowAccept, rowReferer, rowAcceptEncoding, rowAcceptLanguage }); } private void txtUrl_TextChanged(object sender, EventArgs e) { if(string.IsNullOrEmpty(txtUrl.Text)) { rowHost.Cells[2].Value = ""; rowPath.Cells[2].Value = ""; } else { Match m = regUrl.Match(this.txtUrl.Text); if (m.Groups["host"].Success) rowHost.Cells[2].Value = m.Groups["host"].Value; if (m.Groups["path"].Success) rowPath.Cells[2].Value = m.Groups["path"].Value; } } private void cmdSend_Click(object sender, EventArgs e) { this.Run(); } protected void Run() { string dt = DateTime.Now.Year.ToString() + "_" + (DateTime.Now.Month < 10 ? "0" : "") + DateTime.Now.Month + "_" + (DateTime.Now.Day < 10 ? "0" : "") + DateTime.Now.Day + "_" + (DateTime.Now.Hour < 10 ? "0" : "") + DateTime.Now.Hour + "_" + (DateTime.Now.Minute < 10 ? "0" : "") + DateTime.Now.Minute; string index = ""; if (!System.IO.Directory.Exists("log/")) System.IO.Directory.CreateDirectory("log/"); for(int i=1;i<10;i++) { if (!System.IO.File.Exists("log / request_" + dt + index + ".log")) break; else index = "_" + i; } System.IO.StreamWriter sw = new StreamWriter("log/request_"+dt+index+".log"); tslState.Text = "Check Header"; tslProgress.Maximum = 100; tslProgress.Value = 0; tslState.Visible = true; tslError.Visible = false; tslProgress.Visible = true; bool bValidHeader = CheckHeader(); tslProgress.Value = 5; tslState.Text = "Generate Header"; NameValueCollection header = GenerateHeader(); tslProgress.Value = 10; tslProgress.Visible = false; string cookieStr = ""; sw.WriteLine("Basis Info\r\n----------------------------------------------------------"); string[] urlParts = txtUrl.Text.Split('?'); string url = urlParts[0]; string query = ""; foreach(DataGridViewRow row in this.dgvGET.Rows) { if (row.Cells[1].Value != null && (bool)row.Cells[1].Value == true) { if (!string.IsNullOrEmpty((string)row.Cells[2].Value)) query += (query.Length == 0 ? "?" : "&") + WebUtility.UrlEncode(row.Cells[0].Value.ToString()) + "=" + WebUtility.UrlEncode(row.Cells[2].Value.ToString()); } } sw.WriteLine("Send query: " + query); System.Diagnostics.Debug.WriteLine("Send Query: " + query); foreach (DataGridViewRow row in this.dgvCookies.Rows) { if (row.Cells[1].Value != null && (bool)row.Cells[1].Value == true) { if (!string.IsNullOrEmpty((string)row.Cells[2].Value)) cookieStr += (cookieStr.Length == 0 ? "" : "; ") + row.Cells[0].Value.ToString() + "=" + row.Cells[2].Value.ToString(); } } sw.WriteLine("Send cookie: " + cookieStr); System.Diagnostics.Debug.WriteLine("Send Cookie: " + cookieStr); tslProgress.Value = 15; tslState.Text = "Generate Content"; string post = ""; foreach (DataGridViewRow row in this.dgvPOST.Rows) { if (row.Cells[1].Value != null && (bool)row.Cells[1].Value == true) { if (!string.IsNullOrEmpty((string)row.Cells[2].Value)) post += (post.Length == 0 ? "" : "&") + WebUtility.UrlEncode(row.Cells[0].Value.ToString()) + "=" + WebUtility.UrlEncode(row.Cells[2].Value.ToString()); } } sw.WriteLine("Send post: " + post); System.Diagnostics.Debug.WriteLine("Send Post: " + post); string callUrl = (url.Contains("://")?"":"http://") + url + (url.IndexOf("/",9)==-1?"/":"") + query; sw.WriteLine("URL: " + callUrl); sw.WriteLine(""); System.Diagnostics.Debug.WriteLine(callUrl); HttpWebRequest request = WebRequest.CreateHttp(callUrl); request.Method = this.GetHeaderField("Method").ToUpper(); request.AllowAutoRedirect = false; System.Diagnostics.Debug.WriteLine(request.Method + " " + callUrl.Substring(callUrl.IndexOf("/", 9)) + " HTTP/" + request.ProtocolVersion.Major.ToString() + "." + request.ProtocolVersion.Minor.ToString()); sw.WriteLine(""); sw.WriteLine(""); sw.WriteLine("Request\r\n----------------------------------------------------------"); sw.WriteLine(request.Method + " " + callUrl.Substring(callUrl.IndexOf("/", 9)) + " HTTP/" + request.ProtocolVersion.Major.ToString() + "." + request.ProtocolVersion.Minor.ToString()); if (HasHeaderField("Host")) { request.Host = GetHeaderField("Host"); } if (HasHeaderField("User-Agent")) { request.UserAgent = GetHeaderField("User-Agent"); } if (HasHeaderField("Accept")) { request.Accept = GetHeaderField("Accept"); } if (HasHeaderField("Content-Type")) { request.ContentType = GetHeaderField("Content-Type"); } request.CookieContainer = new CookieContainer(); foreach (DataGridViewRow row in this.dgvCookies.Rows) { if (row.Cells[1].Value != null && (bool)row.Cells[1].Value == true) { if (!string.IsNullOrEmpty((string)row.Cells[2].Value) && !string.IsNullOrEmpty((string)row.Cells[0].Value)) request.CookieContainer.Add(new Uri(txtUrl.Text), new Cookie((string)row.Cells[0].Value, (string)row.Cells[2].Value)); } } //request.Headers.Add(header); foreach (string k in header.AllKeys) { try { request.Headers[k] = header[k]; } catch(Exception ex) { System.Diagnostics.Debug.WriteLine("Failed to add Header: '" + k + "' = '" + header[k] + "'"); } } if (request.Method=="POST") { string xlen = GetHeaderField("Content-Length").Trim().ToLower(); if (!HasHeaderField("Content-Type") && !HasHeaderField("Content-Length")) { if(MessageBox.Show("Es wurde kein Content-Type und keine Content-Length gesetzt\r\nSollen die Felder automatisch befüllt werden?","POST Request", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { request.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); xlen = "auto"; } } else if(!HasHeaderField("Content-Type")) { if (MessageBox.Show("Es wurde kein Content-Type gesetzt\r\nSoll das Feld automatisch befüllt werden?", "POST Request", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { request.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); } } else if (!HasHeaderField("Content-Length")) { if (MessageBox.Show("Es wurde keine Content-Length gesetzt\r\nSoll das Feld automatisch befüllt werden?", "POST Request", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { xlen = "auto"; } } string ct = header["Content-Type"]; Encoding enc = System.Text.Encoding.ASCII; /*if (!String.IsNullOrEmpty(ct)) { ct = ct.ToLower(); if (ct.Contains("utf8") || ct.Contains("utf-8")) { enc = Encoding.UTF8; } else if (ct.Contains("unicode") || ct.Contains("utf16") || ct.Contains("utf-8")) { enc = Encoding.Unicode; } else if (ct.Contains("utf32") || ct.Contains("utf-32")) { enc = Encoding.UTF32; } }*/ byte[] content = enc.GetBytes(post); if (xlen == "auto") request.ContentLength = content.Length; else { long val = 0; if(long.TryParse(xlen, out val)) { request.ContentLength = val; } else { request.ContentLength = content.Length; } } Stream dataStream = request.GetRequestStream(); dataStream.Write(content, 0, content.Length); dataStream.Close(); sw.WriteLine(""); sw.WriteLine(post); } foreach (string hKey in request.Headers.AllKeys) { sw.WriteLine(hKey + ": " + request.Headers[hKey]); System.Diagnostics.Debug.WriteLine(hKey + ": " + request.Headers[hKey]); } tslProgress.Value = 20; tslState.Text = "Sending..."; sw.WriteLine(""); sw.WriteLine(""); sw.WriteLine("Response\r\n----------------------------------------------------------"); WebResponse response = request.GetResponse(); tslState.Text = "Get response"; string resHeader = ""; tslProgress.Value = 70; foreach (string k in response.Headers.AllKeys) { resHeader += k + ": " + response.Headers[k] + "\r\n"; } sw.WriteLine(resHeader); this.txtResponse.Text = resHeader; tslProgress.Value = 75; Stream data = response.GetResponseStream(); StreamReader tr = new StreamReader(data); string raw = tr.ReadToEnd(); this.txtResponseCode.Text = raw; if(raw.Trim().Contains("<") && raw.Contains("]*>[\s\S]*?", "", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture); browserContent = Regex.Replace(browserContent, "(\\]\\>)", "", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture); this.webBrowser1.DocumentText = browserContent; this.txtResponseCode.Text = browserContent; sw.WriteLine(""); sw.WriteLine(""); sw.WriteLine("Content\r\n----------------------------------------------------------"); sw.WriteLine(raw); } sw.Close(); } private bool CheckHeader() { bool res = true; string[] required = new string[] { "Path", "Method", "Host", "HTTP Version", "Connection" }; foreach (string h in required) { if(!this.HasHeaderField(h)) { tslError.Text = "Header '" + h + "' required"; return false; } } return false; } private bool HasHeaderField(string fieldName) { foreach (DataGridViewRow row in this.dgvRequest.Rows) { if (row.Cells[1].Value != null && (bool)row.Cells[1].Value == true) { if (((string)row.Cells[0].Value).ToLower().Trim() == fieldName.ToLower().Trim()) if (!string.IsNullOrEmpty((string)row.Cells[2].Value)) return true; } } return false; } private string GetHeaderField(string fieldName) { foreach (DataGridViewRow row in this.dgvRequest.Rows) { if (row.Cells[1].Value != null && (bool)row.Cells[1].Value == true) { if (((string)row.Cells[0].Value).ToLower().Trim() == fieldName.ToLower().Trim()) if (!string.IsNullOrEmpty((string)row.Cells[2].Value)) return (string)row.Cells[2].Value; } } return ""; } private NameValueCollection GenerateHeader() { string query_string = ""; NameValueCollection res = new NameValueCollection(); foreach (DataGridViewRow row in this.dgvRequest.Rows) { if (row.Cells[1].Value != null && (bool)row.Cells[1].Value == true) { if (row.Cells[0].Value != null && !String.IsNullOrEmpty(row.Cells[2].Value.ToString()) && row.Cells[2].Value != null) { string var = row.Cells[0].Value.ToString().Trim(); if(var!="Method" && var!="Path" && var!= "HTTP Version") { res[var] = row.Cells[2].Value.ToString(); } } } } return res; } private void öffnenToolStripMenuItem_Click(object sender, EventArgs e) { openFileDialog1.Filter = "Request Dateien (*.req)|*.req|Alle Dateien|*.*"; openFileDialog1.Title = "Öffnen"; if(openFileDialog1.ShowDialog() == DialogResult.OK) { LoadFile(openFileDialog1.FileName); } } private string loadedFile = ""; protected bool LoadFile(string file) { if (!System.IO.File.Exists(file)) return false; this.loadedFile = file; SaveData s = System.Fire.Serialize.Serializer.DeserializeObject(file, System.Fire.Serialize.Property.ArraySize.bits_16, System.Fire.Serialize.Property.StringEncoding.UTF8); if(s==null) { return false; } this.New(); this.txtUrl.Text = s.url; foreach (SaveData_Row r in s.headers) { DataGridViewRow row = new DataGridViewRow(); row.Cells.AddRange(new DataGridViewCell[] { new DataGridViewTextBoxCell() { Value = r.variable}, new DataGridViewCheckBoxCell() {Value = r.active}, new DataGridViewTextBoxCell() { Value = r.value} }); dgvRequest.Rows.Add(row); } foreach (SaveData_Row r in s.get) { DataGridViewRow row = new DataGridViewRow(); row.Cells.AddRange(new DataGridViewCell[] { new DataGridViewTextBoxCell() { Value = r.variable}, new DataGridViewCheckBoxCell() {Value = r.active}, new DataGridViewTextBoxCell() { Value = r.value} }); dgvGET.Rows.Add(row); } foreach (SaveData_Row r in s.post) { DataGridViewRow row = new DataGridViewRow(); row.Cells.AddRange(new DataGridViewCell[] { new DataGridViewTextBoxCell() { Value = r.variable}, new DataGridViewCheckBoxCell() {Value = r.active}, new DataGridViewTextBoxCell() { Value = r.value} }); dgvPOST.Rows.Add(row); } foreach (SaveData_Row r in s.cookies) { DataGridViewRow row = new DataGridViewRow(); row.Cells.AddRange(new DataGridViewCell[] { new DataGridViewTextBoxCell() { Value = r.variable}, new DataGridViewCheckBoxCell() {Value = r.active}, new DataGridViewTextBoxCell() { Value = r.value} }); dgvCookies.Rows.Add(row); } return true; } protected bool Save() { if(string.IsNullOrEmpty(this.loadedFile)) { return SaveAs(); } return Save(this.loadedFile); } protected bool Save(string file) { this.loadedFile = file; SaveData s = new SaveData(); s.url = txtUrl.Text; s.headers = new List(); foreach (DataGridViewRow row in this.dgvRequest.Rows) { if (row.Cells[0].Value != null) { s.headers.Add(new SaveData_Row() { variable = (string)row.Cells[0].Value, active = (row.Cells[1].Value == null ? false : (bool)row.Cells[1].Value == true), value = (row.Cells[0].Value==null?"":(string)row.Cells[2].Value) }); } } s.get = new List(); foreach(DataGridViewRow row in this.dgvGET.Rows) { if (row.Cells[0].Value != null) { s.headers.Add(new SaveData_Row() { variable = (string)row.Cells[0].Value, active = (row.Cells[1].Value==null?false:(bool)row.Cells[1].Value == true), value = (row.Cells[0].Value == null ? "" : (string)row.Cells[2].Value) }); } } s.post = new List(); foreach (DataGridViewRow row in this.dgvPOST.Rows) { if (row.Cells[0].Value != null) { s.headers.Add(new SaveData_Row() { variable = (string)row.Cells[0].Value, active = (row.Cells[1].Value == null ? false : (bool)row.Cells[1].Value == true), value = (row.Cells[0].Value == null ? "" : (string)row.Cells[2].Value) }); } } s.cookies = new List(); foreach (DataGridViewRow row in this.dgvCookies.Rows) { if (row.Cells[0].Value != null) { s.headers.Add(new SaveData_Row() { variable = (string)row.Cells[0].Value, active = (row.Cells[1].Value == null ? false : (bool)row.Cells[1].Value == true), value = (row.Cells[0].Value == null ? "" : (string)row.Cells[2].Value) }); } } System.Fire.Serialize.Serializer.SerializeObject(file, s, System.Fire.Serialize.Property.ArraySize.bits_16, System.Fire.Serialize.Property.StringEncoding.UTF8); return true; } protected bool SaveAs() { saveFileDialog1.Filter = "Request Dateien (*.req)|*.req|Alle Dateien|*.*"; ; saveFileDialog1.Title = "Speichern unter"; if(saveFileDialog1.ShowDialog()== DialogResult.OK) { return this.Save(saveFileDialog1.FileName); } return false; } private void speichernToolStripMenuItem_Click(object sender, EventArgs e) { this.Save(); } private void speichernUnterToolStripMenuItem_Click(object sender, EventArgs e) { this.SaveAs(); } } }