Skip to content

Instantly share code, notes, and snippets.

@YaseenTwati
Last active December 21, 2017 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YaseenTwati/d11318747a6f3e4bb53e05dc77e9113c to your computer and use it in GitHub Desktop.
Save YaseenTwati/d11318747a6f3e4bb53e05dc77e9113c to your computer and use it in GitHub Desktop.
Socks5 Connecting Demonstration
// Yaseen M. Twati
#ifdef _WIN32 // Windows
#include <winsock2.h>
#include <ws2tcpip.h>
#define MSG_NOSIGNAL 0
#else // Linuc + Max
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <errno.h>
#include <err.h>
#define SOCKET_ERROR -1
#define INVALID_SOCKET -1
typedef int SOCKET;
typedef sockaddr SOCKADDR;
typedef sockaddr_in SOCKADDR_IN;
#define closesocket close
#ifdef __APPLE__
#define MSG_NOSIGNAL 0
#endif
#endif
#include <iostream>
//#define IPV4_TEST
int main()
{
SOCKET Socket;
SOCKADDR_IN SocketAddr;
Socket = socket(AF_INET, SOCK_STREAM, 0);
SocketAddr.sin_family = AF_INET;
SocketAddr.sin_port = htons(9050);
SocketAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
std::cout << "[*] Connecting" <<std::endl;
if( connect(Socket, (SOCKADDR*)&SocketAddr, sizeof(SOCKADDR_IN)) < 0)
{
std::cout << "[-] Connection Error ( is Server Running? )" << std::endl;
return(-1);
}
char Req1[3] =
{
0x05, // SOCKS 5
0x01, // One Authentication Method
0x00 // No AUthentication
};
send(Socket, Req1, 3, MSG_NOSIGNAL);
char Resp1[2];
recv(Socket, Resp1, 2, 0);
if(Resp1[1] != 0x00)
{
std::cout << "[*] Error Authenticating " << std::endl;
return(-1); // Error
}
#ifdef IPV4_TEST
char* IpAddr = "172.217.21.78"; // google.com
int IpAddrInt = inet_addr(IpAddr);
short Port = htons(80);
char Req2[10] = {
0x05, // SOCKS5
0x01, // CONNECT
0x00, // RESERVED
0x01, // IPV4
};
// Copy the IP
memcpy(Req2+4, &IpAddrInt, 4);
// Copy The Port
memcpy(Req2+4+4, &Port, 2);
// Send
send(Socket, Req2, 10, 0);
#else
char* Domain = "facebookcorewwwi.onion";
char DomainLen = (char)strlen(Domain);
short Port = htons(80);
char TmpReq[4] = {
0x05, // SOCKS5
0x01, // CONNECT
0x00, // RESERVED
0x03, // DOMAIN
};
char* Req2 = new char[4 + 1 + DomainLen + 2];
memcpy(Req2, TmpReq, 4); // 5, 1, 0, 3
memcpy(Req2 + 4, &DomainLen, 1); // Domain Length
memcpy(Req2 + 5, Domain, DomainLen); // Domain
memcpy(Req2 + 5 + DomainLen, &Port, 2); // Port
send(Socket, (char*)Req2, 4 + 1 + DomainLen + 2, MSG_NOSIGNAL);
delete[] Req2;
#endif
char Resp2[10];
recv(Socket, Resp2, 10, 0);
if(Resp2[1] != 0x00)
{
std::cout << "[*] Error : " << std::hex << Resp2[1] << std::endl;
return(-1); // ERROR
}
std::cout << "[*] Connected " << std::endl << std::endl;
// Here you can normally use send and recv
// Testing With a HTTP GET Request
std::cout << "[*] Testing with GET Request \n" << std::endl;
send(Socket, "GET / \n\r\n\r", strlen("GET / \n\r\n\r"), MSG_NOSIGNAL);
char RecvBuffer[2048];
size_t Rcved = recv(Socket, RecvBuffer, 2048, 0);
std::cout.write(RecvBuffer, Rcved);
std::cout << std::endl;
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment