#include <stdio.h>
#include <stdlib.h>
#include <ws2tcpip.h>
#include <winsock.h>
#include <signal.h>
#include <errno.h>
#include <io.h>
#include <string.h>
#include <errno.h>
#define MAKEWORD(a, b) ((WORD)(((BYTE)(((DWORD_PTR)(a)) & 0xff)) | ((WORD)((BYTE)(((DWORD_PTR)(b)) & 0xff))) << 8))
#define MAKELONG(a, b) ((LONG)(((WORD)(((DWORD_PTR)(a)) & 0xffff)) | ((DWORD)((WORD)(((DWORD_PTR)(b)) & 0xffff))) << 16))
#pragma warning(disable: 4996)
#pragma comment(lib, "ws2_32.lib")
#define VER 0x00
#define CMD 0x01
#define RSV 0x02
#define DST_ADDR 0x04
#define MAXSIZE 0x5000
#define IPSIZE 20
#define CONNECTNUM 5
typedef struct _SOCKS5_REQUEST
{
char version; // 客户端支持的Socks版本,0x04或者0x05
char cmd; // 客户端命令,CONNECT为0x01,BIND为0x02,UDP为0x03,一般为0x01
char reserved; // 保留位,恒定为0x00
char address_type; // 客户端请求的真实主机的地址类型,IPv4为0x00,IPv6为0x04,DOMAIN为0x03
char address_port[1]; // 如果address_type为域名,此处第一字节为域名长度,其后为域名本身,无0字符结尾,域名后为真实主机绑定端口
} SOCKS5_REQUEST;
typedef struct _SOCKS5_RESPONSE
{
char version; // 服务器支持的Socks版本,0x04或者0x05
char reply; // 代理服务器连接真实主机的结果,0x00成功
char reserved; // 保留位,恒定为0x00
char address_type; // Socks代理服务器绑定的地址类型,IPv4为0x01,IPv6为0x04,域名为0x03
char address_port[1]; // 如果address_type为域名,此处第一字节为域名长度,其后为域名本身,无0字符结尾,域名后为Socks代理服务器绑定端口
} SOCKS5_RESPONSE;
enum CMDTYPE
{
SOCKS_CONNECT = 0x01,
SOCKS_BIND,
SOCKS_UDP
};
char UserName[]="admin";
char Password[]="admin";
int NeedAuth=0;
// define 2 socket struct
struct transocket
{
SOCKET fd1;
SOCKET fd2;
};
// define function
void usage(char *prog);
void transmitdata(LPVOID data);
void getctrlc(int j);
void closeallfd();
void makelog(char *buffer, int length);
void proxy(int port);
void bind2bind(int port1, int port2);
int checkSocks5(SOCKET sockfd);
void socks52conn(char *host, int port2);
void conn2conn(char *host1, int port1, char *host2, int port2);
int testifisvalue(char *str);
int create_socket();
int create_server(int sockfd, int port);
int client_connect(int sockfd, char* server, int port);
BOOL IsFirstSocketConnect(int sockfdfirst,int sockfdsecond);
extern int errno;
FILE *fp;
int method=0;
//int connectnum=0;
//************************************************************************************
//
// function main
//
//************************************************************************************
VOID main(int argc, char* argv[])
{
char **p;
char sConnectHost[IPSIZE], sTransmitHost[IPSIZE];
int iConnectPort=0, iTransmitPort=0;
char *logfile=NULL;
memset(sConnectHost, 0, IPSIZE);
memset(sTransmitHost, 0, IPSIZE);
p=argv;
while(*p)
{
if(stricmp(*p, "-log") == 0)
{
if(testifisvalue(*(p+1)))
{
logfile = *(++p);
}
else
{
printf("[-] ERROR: Must supply logfile name.\n");
return;
}
p++;
continue;
}
p++;
}
if(logfile !=NULL)
{
fp=fopen(logfile,"a");
if(fp == NULL )
{
printf("[-] ERROR: open logfile");
return;
}
makelog("====== Start ======\n", 22);
}
// Win Start Winsock.
WSADATA wsadata;
WSAStartup(MAKEWORD(1, 1), &wsadata);
signal(SIGINT, &getctrlc);
if(argc > 2)
{
if(stricmp(argv[1], "-listen") == 0 && argc >= 4)
{
iConnectPort = atoi(argv[2]);
iTransmitPort = atoi(argv[3]);
method = 1;
}
else if(stricmp(argv[1], "-socks5") == 0 && argc >= 4)
{
iConnectPort = atoi(argv[3]);
strncpy(sTransmitHost, argv[2], strlen(argv[2]));
method = 2;
}
else if(stricmp(argv[1], "-slave") == 0 && argc >= 6)
{
strncpy(sConnectHost, argv[2], strlen(argv[2]));
iConnectPort = atoi(argv[3]);
strncpy(sTransmitHost, argv[4], strlen(argv[4]));
iTransmitPort = atoi(argv[5]);
method = 3;
}
}
switch(method)
{
case 1:
bind2bind(iConnectPort, iTransmitPort);
break;
case 2:
socks52conn(sTransmitHost,iConnectPort);
break;
case 3:
conn2conn(sConnectHost, iConnectPort, sTransmitHost, iTransmitPort);
break;
default:
usage(argv[0]);
break;
}
if(method)
{
closeallfd();
}
WSACleanup();
return;
}
VOID usage(char* prog)
{
printf("[Usage of Packet Transmit:]\n");
printf(" %s - [-log logfile]\n", prog);
printf("[option:]\n");
printf(" -listen \n");
printf(" -socks5 \n");
printf(" -slave \n\n");
return;
}
//************************************************************************************
//
// test if is value
//
//************************************************************************************
int testifisvalue(char *str)
{
if(str == NULL ) return(0);
if(str[0]=='-') return(0);
return(1);
}
//************************************************************************************
//
// LocalHost:ConnectPort transmit to LocalHost:TransmitPort
//
//************************************************************************************
void bind2bind(int port1, int port2)
{
SOCKET fd1,fd2, sockfd1=0, sockfd2;
struct sockaddr_in client1;
int size1,size2,ifConn=0,ret=0,flag=0;
HANDLE hThread=NULL;
DWORD dwThreadID;
transocket* sock;
char conn[10]={0};
if((fd1=create_socket())==0) return;
if((fd2=create_socket())==0) return;
printf("[+] Listening port %d ......\n",port1);
fflush(stdout);
if(create_server(fd1, port1)==0)
{
closesocket(fd1);
return;
}
printf("[+] Listen OK!\n");
printf("[+] Listening port %d ......\n",port2);
fflush(stdout);
if(create_server(fd2, port2)==0)
{
closesocket(fd2);
return;
}
printf("[+] Listen OK!\n");
size1=size2=sizeof(struct sockaddr);
while(1)
{
memset(conn,10,0);
flag=0;
printf("[+] Waiting Client on port left\n");
if((sockfd1 = accept(fd1,(struct sockaddr *)&client1,&size1))<0)
{
printf("[-] Accept1 error.\n");
continue;
}
printf("[+] Accept a Client on port left\n");
if((sockfd2 = accept(fd2,(struct sockaddr *)&client1,&size1))<0)
{
printf("[-] Accept1 error.\n");
continue;
}
printf("[+] Accept a Client on port right.\n");
while(1)
{
ret=send(sockfd1,"conn",strlen("conn"),0);
if(ret==4)
{
ret=recv(sockfd1,conn,10,0);
if(ret>0 && !strcmp(conn,"ok"))
{
flag=1;
break;
}
}
printf("[+] send online error.\n");
flag=0;
break;
}
if(flag==0)
{
closesocket(sockfd2);
closesocket(sockfd1);
continue;
}
printf("[+] Accept Connect OK!\n");
sock=(transocket*)VirtualAlloc(NULL,8,MEM_COMMIT,PAGE_READWRITE);
sock->fd1 = sockfd1;
sock->fd2 = sockfd2;
hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)transmitdata, (LPVOID)sock, 0, &dwThreadID);
if(hThread == NULL)
{
printf("[-] CreateThread Failed!\n");
TerminateThread(hThread, 0);
closesocket(sockfd1);
closesocket(sockfd2);
return;
}
Sleep(1000);
printf("[+] CreateThread OK!\n");
}
}
//************************************************************************************
//
// LocalHost:ConnectPort transmit to TransmitHost:TransmitPort
//
//************************************************************************************
bool Auth(int s,char* username,char* password,bool NeedAuth)
{
/*
+----+----------+----------+
|VER | NMETHODS | METHODS |
+----+----------+----------+
| 1 | 1 | 1 to 255 |
+----+----------+----------+
*/
int i = 0,ret;
char buffer[1024];
memset(buffer,0,1024);
ret=recv(s,buffer,2,0);
int type = buffer[1];
ret=recv(s,buffer,type,0);
for (i = 0;i < type;++i)
if (buffer[i] == 0x02)
break;
/*
+----+-----------------+
|VER | METHOD CHOSSED |
+----+------
评论0