Skip to content

Instantly share code, notes, and snippets.

@NaokiStark
Created March 2, 2016 18:36
Show Gist options
  • Save NaokiStark/42cd7f6f4b382cc7f0b8 to your computer and use it in GitHub Desktop.
Save NaokiStark/42cd7f6f4b382cc7f0b8 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace VoxSpam
{
public class HTTPSocksRequest
{
public string SocksAddr{get;set;}
public int SocksPrt{get;set;}
string actualHost = "";
Socket sck;
public List<KeyValuePair<string, List<KeyValuePair<string,string>>>> Cookies { get; set; }
public List<KeyValuePair<string, string>> Headers { get; set; }
public string UserAgent { get; set; }
public HTTPSocksRequest(string SocksAddress, int SocksPort) {
SocksAddr = SocksAddress;
SocksPrt = SocksPort;
Cookies = new List<KeyValuePair<string, List<KeyValuePair<string, string>>>>();
loadDefaults();
}
public string Get(string url,string ip = null) {
byte[] bytes = new byte[256];
Uri urlS = new Uri(url);
actualHost = urlS.Host;
string requestF = "GET " + urlS.AbsolutePath +urlS.Query+ " HTTP/1.1\r\n"
+"Host: "+urlS.Host+"\r\n"
+ "Connection: close\r\n"
+ getHeaders()+"\r\n"
;
string toGo = (ip == null)?urlS.Host:ip;
sck = LMKR.SocksProxy.ConnectToSocks5Proxy(SocksAddr, (ushort)SocksPrt, toGo, (ushort)urlS.Port, "", "");
sck.Send(System.Text.Encoding.ASCII.GetBytes(requestF));
int c=sck.Receive(bytes,bytes.Length,0);
string response = "";
while (c > 0)
{
response += Encoding.UTF8.GetString(bytes, 0, c);
c = sck.Receive(bytes, bytes.Length, 0);
}
return response;
}
public string Post(string url, List<KeyValuePair<string, string>> Body, FileStream file = null, string ip = null, string fileField=null)
{
byte[] bytes = new byte[256];
Uri urlS = new Uri(url);
actualHost = urlS.Host;
string boundary = "---------------------------" + new Random().Next(10, 99) + "6149129" + new Random().Next(10, 99);
string requestF = "POST " + urlS.AbsolutePath + urlS.Query + " HTTP/1.1\r\n"
+ "Host: " + urlS.Host + "\r\n"
+ "Connection: keep-alive\r\n"
+ ((true) ? "Content-Type: multipart/form-data; boundary="+boundary: "Content-Type: application/x-www-form-urlencoded") + "\r\n"
+ getHeaders();
int cr = 0;
string paramss = "";
foreach (KeyValuePair<string, string> dat in Body)
{
if (cr != 0) {
paramss += "&";
}
paramss += string.Format("{0}={1}", dat.Key, dat.Value);
cr++;
}
byte[] byBuffer;
if (true)
{
byBuffer = processFile(requestF, Body, file, boundary, fileField);
}
else {
string rr = requestF + "\r\n" + paramss + "\r\n\r\n";
byBuffer = System.Text.Encoding.UTF8.GetBytes(rr);
}
string toGo = (ip == null) ? urlS.Host : ip;
sck = LMKR.SocksProxy.ConnectToSocks5Proxy(SocksAddr, (ushort)SocksPrt, toGo, (ushort)urlS.Port, "", "");
sck.Send(byBuffer);
Debug.WriteLine(Encoding.UTF8.GetString(byBuffer));
int c = sck.Receive(bytes, bytes.Length, 0);
string response = "";
while (c > 0)
{
response += Encoding.UTF8.GetString(bytes, 0, c);
c = sck.Receive(bytes, bytes.Length, 0);
}
Console.WriteLine("Recieved: {0}",response);
return response;
}
private byte[] processFile(string requestF, List<KeyValuePair<string, string>> Body, FileStream file, string boundary,string fileField)
{
Stream memStream = new MemoryStream();
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
byte[] boundarybytesend = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
string formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";
string headerTemplateField = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
string header = string.Format(headerTemplate, fileField, "happyFile.jpg");
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
memStream.Write(boundarybytes, 0, boundarybytes.Length);
if (file != null)
{
memStream.Write(headerbytes, 0, headerbytes.Length);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = file.Read(buffer, 0, buffer.Length)) != 0)
{
memStream.Write(buffer, 0, bytesRead);
}
file.Close();
memStream.Write(boundarybytes, 0, boundarybytes.Length);
}
else
{
header = string.Format(headerTemplateField, fileField, "null");
headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
memStream.Write(headerbytes, 0, headerbytes.Length);
if(Body.Count>0)
memStream.Write(boundarybytes, 0, boundarybytes.Length);
}
int index = Body.Count;
foreach (KeyValuePair<string, string> dat in Body)
{
index--;
header = string.Format(headerTemplateField, dat.Key, dat.Value);
headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
memStream.Write(headerbytes, 0, headerbytes.Length);
if(index>0)
memStream.Write(boundarybytes, 0, boundarybytes.Length);
}
memStream.Position = memStream.Position - 1;
memStream.Write(boundarybytesend, 0, boundarybytesend.Length);
MemoryStream hStream = new MemoryStream();
requestF += "Content-Length: " + memStream.Length + "\r\n\r\n";
hStream.Write(Encoding.UTF8.GetBytes(requestF), 0, Encoding.UTF8.GetBytes(requestF).Length);
memStream.Position = 0;
hStream.Position = 0;
byte[] tempBuffer = new byte[hStream.Length];
byte[] tbuff = new byte[memStream.Length];
hStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Read(tbuff, 0, tbuff.Length);
hStream.Close();
memStream.Close();
byte[] rv = new byte[tempBuffer.Length + tbuff.Length];
System.Buffer.BlockCopy(tempBuffer, 0, rv, 0, tempBuffer.Length);
System.Buffer.BlockCopy(tbuff, 0, rv, tempBuffer.Length, tbuff.Length);
return rv;
}
public string SendRequestWithType(string url, string Type, string RawBody)
{
return "";
}
private string buildCookies(string host)
{
string ggg = "";
foreach (KeyValuePair<string,List<KeyValuePair<string,string>>> kk in Cookies)
{
if(kk.Key.ToLower().Contains(host.ToLower())){
foreach (KeyValuePair<string, string> kkk in kk.Value)
{
ggg += string.Format("{0}={1}; ",kkk.Key,kkk.Value);
}
}
}
return ggg + "\r\n";
}
private string getHeaders() {
string hdrs="";
foreach (KeyValuePair<string, string> hdr in Headers)
{
hdrs += string.Format("{0}: {1}\r\n",hdr.Key,hdr.Value);
}
hdrs += "Cookie: " + buildCookies(actualHost);
return hdrs;
}
//Defaults
private void loadDefaults() {
UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36";
Headers = new List<KeyValuePair<string, string>>();
Headers.Add(new KeyValuePair<string,string>("User-Agent",UserAgent));
Headers.Add(new KeyValuePair<string,string>("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment