通常服务器是放在内网中,内部局域网中,并且分配了私有IP地址,而如果想要在外部直接访问内部服务器则需要端口映射,端口映射也叫做端口转发,或者叫做虚拟服务器,在渗透时当我们拿到目标机器权限以后,目标机器实在内网环境中,如果想要外部直接访问内部的特定机器的特定端口,则需要使用映射来实现,常用的映射工具是LCX,或者自己实现。
LCX实现的映射方法Windows 版LCX源代码(魔改版)
代码语言:javascript复制#include <iostream>
#include <stdlib.h>
#include <errno.h>
#include <winsock2.h>
#include <io.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
typedef struct{
SOCKET s1;
SOCKET s2;
}Stu_sock;
//函数声明
void version();
int param(int argc, char **argv); //对命令行参数
void Funlisten(int port1, int port2); //监听功能函数
bool bindAndFunlisten(SOCKET s, int port);//绑定socket的地址结构
void datatrans(LPVOID data); //数据转发函数
void slave(char* hostIp, char * slaveIp, int destionPort, int slavePort);//slave函数
bool checkIP(char * str);
int client_connect(int sockfd, char* server, int port);//连接服务端
void main(int argc, char **argv)
{
version();
//参数判断
WSADATA wsadata;
WSAStartup(MAKEWORD(1, 1), &wsadata);
char hostIp[20] = { 0 };
char slaveIp[20] = { 0 };
int ret = 1;
ret = param(argc, argv);
if (ret == 1)//Funlisten
{
Funlisten(atoi(argv[2]), atoi(argv[3]));
}
else if (ret == 2)
{
strcpy(hostIp, argv[2]);
strcpy(slaveIp, argv[4]);
slave(argv[2], argv[4], atoi(argv[3]), atoi(argv[5]));
}
}
int param(int argc, char **argv)
{
//stricmp==strcmp ignore case忽略大小写
if (argc == 4 && stricmp(argv[1], "-listen") == 0)
{
// cout<<"Funlisten"<<endl;
return 1;
}
if (argc == 6 && stricmp(argv[1], "-slave") == 0 && checkIP(argv[2]) && checkIP(argv[4]))
{
return 2;
}
else
{
version();
return -1;
}
}
/************************************************************************/
/* */
/* listen 功能模块 */
/************************************************************************/
void Funlisten(int port1, int port2)
{
Stu_sock stu_sock;
//创建套接字
SOCKET sock1 = socket(AF_INET, SOCK_STREAM, 0);
SOCKET sock2 = socket(AF_INET, SOCK_STREAM, 0);
if (sock1<0 || sock1<0)
{
cout << "[-] Create socket error" << endl;
return;
}
//绑定端口到socket并监听
if (!bindAndFunlisten(sock1, port1) || !bindAndFunlisten(sock2, port2))
{
return;
}
//都监听好了接下来……
int SizeOfAddr = sizeof(sockaddr);
while (true)
{
cout << "[ ] Waiting for Client ......" << endl;
sockaddr_in remoteSockAddr;
//sock1等待连接
SOCKET recvSock1 = accept(sock1, (sockaddr *)&remoteSockAddr, &SizeOfAddr);
cout << recvSock1 << endl;
if (recvSock1<0)
{
cout << "[-] Accept error." << endl;
continue;
}
cout << "[ ] Accept a Client on port " << port1 << " from " << inet_ntoa(remoteSockAddr.sin_addr) << endl;
cout << "[ ] Waiting another Client on port:" << port2 << "...." << endl;
SOCKET recvSock2 = accept(sock2, (sockaddr *)&remoteSockAddr, &SizeOfAddr);
cout << recvSock2 << endl;
if (recvSock2<0)
{
cout << "[-] Accept error." << endl;
continue;
}
cout << "[ ] Accept a Client on port" << port1 << " from " << inet_ntoa(remoteSockAddr.sin_addr) << endl;
//两个都连上来了
cout << "[ ] Accept Connect OK!" << endl;
stu_sock.s1 = recvSock1; stu_sock.s2 = recvSock2;
DWORD dwThreadID;
//创建一个转发数据的线程
HANDLE hThread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)datatrans, (LPVOID)&stu_sock, 0, &dwThreadID);
if (hThread == NULL)
{
TerminateThread(hThread, 0);
return;//线程错了直接退出
}
cout << "[ ] CreateThread OK!" << endl;
Sleep(800);//挂起当前线程
}
}
bool bindAndFunlisten(SOCKET s, int port)
{
//地址结构
sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_port = htons(port);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
char on = 1;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
//绑定地址结构
if (bind(s, (const sockaddr *)&addr, sizeof(sockaddr))<0)
{
cout << "[-] Socket bind error." << endl;
return false;
}
//监听端口
if (listen(s, 5)<0)
{
cout << "[-] Listen error." << endl;
return false;
}
return true;
}
void datatrans(LPVOID data)
{
char host_slave[20] = { 0 };
char host_hacker[20] = { 0 };
Stu_sock *stuSock = (Stu_sock *)data;
SOCKET s1 = stuSock->s1; //接受的是slave的数据
SOCKET s2 = stuSock->s2; //发送出去的socket
int sentacount1 = 0;
sockaddr_in addr = { 0 };
int sizeofaddr = sizeof(sockaddr);
if (getpeername(s1, (sockaddr *)&addr, &sizeofaddr))
{
strcpy(host_slave, inet_ntoa(addr.sin_addr));
int port_slave = ntohs(addr.sin_port);
}
if (getpeername(s2, (sockaddr *)&addr, &sizeofaddr))
{
strcpy(host_hacker, inet_ntoa(addr.sin_addr));
int port_hacker = ntohs(addr.sin_port);
}
cout << "[ ] Start Transport (" << host_slave << "<-> " << host_hacker << ") ......" << endl;
char RecvBuffer[20480];
char SendBuffer[20480];
fd_set readfd; fd_set writefd;
timeval timeset;
timeset.tv_sec = 300;
timeset.tv_usec = 0;
int maxfd = max(s1, s2) 1;
bool flag = false;
int readsize;
while (TRUE)
{
readsize = 0;
FD_ZERO(&readfd);
FD_ZERO(&writefd);
FD_SET((UINT)s1, &readfd);
FD_SET((UINT)s2, &readfd);
FD_SET((UINT)s1, &writefd);
FD_SET((UINT)s2, &writefd);
int result = select(maxfd, &readfd, &writefd, NULL, ×et);
if (result<0 && (errno != EINTR))
{
cout << "[-] Select error." << endl;
break;
}
else if (result == 0)
{
cout << "[-] Socket time out." << endl;
break;
}
//没出错,没超时
if (FD_ISSET(s1, &readfd) && flag)
{
// if (totalread1<20408)
{
readsize = recv(s1, RecvBuffer, 20480, 0);//接受host的请求。。
if (readsize == -1)
{
break;
}
if (readsize == SOCKET_ERROR || readsize == 0)
{
cout << "!!!" << endl;
}
memcpy(SendBuffer, RecvBuffer, readsize);
memset(RecvBuffer, 0, 20480);
cout << " [ ] Recv " << readsize << " bytes " << "from host." << endl;
}
}
if (FD_ISSET(s1, &writefd) && flag && (readsize>0))
{
int sendsize = send(s2, SendBuffer, readsize, 0);//发给slave
if (sendsize == 0)
{
break;
}
if (sendsize<0 && (errno != EINTR))
{
cout << "[-] Send to s2 unknow error." << endl;
break;
}
memset(SendBuffer, 0, 20480);
cout << " Send " << sendsize << " bytes " << endl;
}
if (FD_ISSET(s2, &readfd) && (!flag))
{
{
readsize = recv(s2, RecvBuffer, 20480, 0);//接受slave返回数据
memcpy(SendBuffer, RecvBuffer, readsize);
cout << " [ ] Recv " << readsize << " bytes " << "from host." << endl;
//totalread1 =readsize;
memset(RecvBuffer, 0, 20480);
}
}
if (FD_ISSET(s1, &writefd) && (!flag) && (readsize>0))
{
readsize = send(s1, SendBuffer, readsize, 0);//发给host
if (readsize == 0)
{
break;
}
if (readsize<0)
{
cout << "[-] Send to s2 unknow error." << endl;
break;
}
cout << " Send " << readsize << " bytes " << endl;
memset(SendBuffer, 0, 20480);
}
flag = !flag;
Sleep(5);
}
closesocket(s1);
closesocket(s2);
cout << "[ ] OK! I Closed The Two Socket." << endl;
}
/************************************************************************/
/* */
/* slave 功能模块 */
/************************************************************************/
void slave(char* hostIp, char * slaveIp, int destionPort, int slavePort)
{
//checkIP(hostIp);
Stu_sock stu_sock;
fd_set fd;
char buffer[20480];
int l;
while (TRUE)
{
//创建套接字
SOCKET sock1 = socket(AF_INET, SOCK_STREAM, 0);
SOCKET sock2 = socket(AF_INET, SOCK_STREAM, 0);
cout << "[ ] Make a Connection to " << hostIp << "on port:" << slaveIp << "...." << endl;
if (sock1<0 || sock1<0)
{
cout << "[-] Create socket error" << endl;
return;
}
fflush(stdout);
if (client_connect(sock1, hostIp, destionPort) == 0)
{
closesocket(sock1);
closesocket(sock2);
continue;/*跳过这次循环*/
}
memset(buffer, 0, 20480);
while (1)
{
//把sock清零,加入set集
FD_ZERO(&fd);
FD_SET(sock1, &fd);
//select事件 读 写 异常
if (select(sock1 1, &fd, NULL, NULL, NULL) == SOCKET_ERROR)
{
if (errno == WSAEINTR) continue;
break;
}
//FD_ISSET返回值>0 表示SET里的可读写
if (FD_ISSET(sock1, &fd))
{
l = recv(sock1, buffer, 20480, 0);
break;
}
Sleep(5);
}
if (l <= 0)
{
cout << "[-] There is a error...Create a new connection." << endl;
continue;
}
while (1)
{
cout << "[ ] Connect OK! n[ ] xlcTeam congratulations!" << endl;
cout << "[ ] Make a Connection to " << hostIp << "on port:" << slaveIp << "...." << endl;
fflush(stdout);
if (client_connect(sock2, slaveIp, slavePort) == 0)
{
closesocket(sock1);
closesocket(sock2);
continue;
}
if (send(sock2, buffer, l, 0) == SOCKET_ERROR)
{
cout << "[-] Send failed." << endl;
continue;
}
l = 0;
memset(buffer, 0, 20480);
break;
}
cout << "[ ] All Connect OK!" << endl;
stu_sock.s1 = sock1;
stu_sock.s2 = sock2;
HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)datatrans, (LPVOID)&stu_sock, 0, NULL);
if (hThread == NULL)
{
TerminateThread(hThread, 0);
return;
}
}
}
//检查IP地址格式是否正确
bool checkIP(char * str)
{
if (INADDR_NONE == inet_addr(str))
return FALSE;
return true;
}
int client_connect(int sockfd, char* server, int port)
{ /*sock*/ /*远程IP*/ /*远程端口*/
struct sockaddr_in cliaddr;
struct hostent *host;
if (!(host = gethostbyname(server))) //获得远程主机的IP
{
// printf("[-] Gethostbyname(%s) error:%sn",server,strerror(errno));
return(0);
}
//给地址结构赋值
memset(&cliaddr, 0, sizeof(struct sockaddr));
cliaddr.sin_family = AF_INET;
cliaddr.sin_port = htons(port);/*远程端口*/
cliaddr.sin_addr = *((struct in_addr *)host->h_addr);//host ip
//去连接远程正在listen的主机
if (connect(sockfd, (struct sockaddr *)&cliaddr, sizeof(struct sockaddr))<0)
{
// printf("[-] Connect error.rn");
return(0);
}
return(1);
}
void version()
{
cout << "options:" << endl;
cout << "-slave remoteIp remotePort1 localIp localPort" << endl;
cout << "-listen remotePort1 remotePort2" << endl;
cout << endl;
}
Linux 版LCX源代码
代码语言:javascript复制/*
Lcx: Port Data Transfer
Compile Environment: Windows / Linux / Mac OS / Android, Gcc
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <stdint.h>
#define BUF_LEN 8192
#ifdef WIN32 //WINDOWS COMPILE
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#define SOCKET_INIT {WSADATA wsa;WSAStartup(MAKEWORD(2,2),&wsa);}
#define ThreadReturn DWORD WINAPI
#define delay(x) Sleep(x)
#else //LINUX COMPILE
#define PTW32_STATIC_LIB
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/select.h>
#define SOCKET_INIT
typedef int SOCKET;
#define ThreadReturn void*
#define delay(x) usleep(x*1000)
#define closesocket(x) close(x)
#endif
typedef ThreadReturn (*Func)(void*);
FILE* lcx_log = NULL;
FILE* lcx_hex = NULL;
FILE* lcx_text = NULL;
int total_connect = 0;
int main_func(int argc,char**argv);
void ctrl_c(int32_t i)
{
fprintf(stdout,"n[-] Receive: Ctrl C..I'll quit..n");
fprintf(stdout,"n[ ] Let me exit....n");
fprintf(stdout,"[ ] All Right!nn");
exit(0);
}
int in_createthread(Func run,void* data)
{
#ifdef WIN32
HANDLE h = CreateThread(NULL,0,run,data,0,NULL);
CloseHandle(h);
#else
pthread_attr_t attr;
pthread_attr_init (&attr);
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
pthread_t tt;
pthread_create(&tt,&attr,run,data);
#endif
delay(5);
return 0;
}
ThreadReturn in_data_tran(void* p)
{
SOCKET t[2];
t[0]=((int*)p)[0];
t[1]=((int*)p)[1];
struct sockaddr_in sa[2];
const unsigned char* ip[2];
unsigned short port[2];
socklen_t len = sizeof(struct sockaddr_in);
if(getpeername(t[0],(struct sockaddr*)sa,&len)==-1 || getpeername(t[1],(struct sockaddr*)(sa 1),&len)==-1)
{
fprintf(stdout,"n[-] Get Remote Host Failedn");
if(lcx_log)fprintf(lcx_log,"n[-] Get Remote Host Failedn"),fflush(lcx_log);
closesocket(t[0]);
closesocket(t[1]);
return 0;
}
ip[0] = (unsigned char*)&sa[0].sin_addr.s_addr;
ip[1] = (unsigned char*)&sa[1].sin_addr.s_addr;
port[0] = htons(sa[0].sin_port);
port[1] = htons(sa[1].sin_port);
fd_set fd_list,check_list;
FD_ZERO(&fd_list);
FD_SET(t[0],&fd_list);
FD_SET(t[1],&fd_list);
unsigned char buf[BUF_LEN];
int OK = 1;
int total_byte = 0;
total_connect;
while( OK && ( (check_list = fd_list),(select(FD_SETSIZE,&check_list,NULL,NULL,NULL)>0)) )
{
int i;
for(i=0;i<2; i)
{
if(FD_ISSET(t[i],&check_list))
{
int len = recv(t[i],buf,BUF_LEN,0);
if(len>0 && send(t[i==0],buf,len,0)>0 )
{
total_byte = len;
char out[100];
sprintf(out,"n[ ] Send <Total %d>: %d.%d.%d.%d:%d -> %d.%d.%d.%d:%d, %d Bytesn",
total_connect,ip[i][0],ip[i][1],ip[i][2],ip[i][3],port[i],ip[i==0][0],ip[i==0][1],ip[i==0][2],ip[i==0][3],port[i==0],len);
fprintf(stdout,"%s",out);fflush(stdout);
if(lcx_log)fprintf(lcx_log,"%s",out),fflush(lcx_log);
if(lcx_text)
{
fprintf(lcx_text,"n%sn",out);
fwrite(buf,1,len,lcx_text);
fflush(lcx_text);
}
if(lcx_hex)
{
fprintf(lcx_hex,"n%s",out);
int i;
for(i=0;i<len; i)
{
if(i==0)fprintf(lcx_hex,"n");
fprintf(lcx_hex,"X ",buf[i]);
}
fflush(lcx_hex);
}
}
else
{
OK = 0;
fprintf(stdout,"n[ ] Connection <Total %d> Cutdown, Total : %d Bytesnn",total_connect,total_byte);fflush(stdout);
if(lcx_log)fprintf(lcx_log,"n[ ] Connection <Total %d> Cutdown, Total : %d Bytesnn",total_connect,total_byte),fflush(lcx_log);
break;
}
}
}
}
--total_connect;
closesocket(t[0]);
closesocket(t[1]);
#ifdef WIN32
return 0;
#else
return NULL;
#endif
}
long gethost(const char* name)
{
if(name)
{
struct hostent *host = gethostbyname(name);
long i;
if(host&&host->h_addr)
{
i = *(long *)(host->h_addr);
return i;
}
}
fprintf(stdout,"nERROR: %s: Wrong host addressnn",name);
return -1;
}
int lcx_slave(const char* ip1_str,unsigned short port1,const char* ip2_str,unsigned short port2)
{
char out1[100],out2[100];
while(1)
{
unsigned long ip1 = gethost(ip1_str);
if(-1 == ip1)
{
fprintf(stdout,"n[-] Reslove Host %s Failed...n",ip1_str),fflush(stdout);
break;
}
unsigned long ip2 = gethost(ip2_str);
if(-1 == ip2)
{
fprintf(stdout,"n[-] Reslove Host %s Failed...n",ip2_str),fflush(stdout);
break;
}
SOCKET s[2];
s[0] = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
s[1] = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
struct sockaddr_in sa[2];
sa[0].sin_family = AF_INET;
sa[0].sin_port = htons(port1);
sa[0].sin_addr.s_addr = (ip1);
sa[1].sin_family = AF_INET;
sa[1].sin_port = htons(port2);
sa[1].sin_addr.s_addr = (ip2);
unsigned char*ip[2];
ip[0] = (unsigned char*)&ip1;
ip[1] = (unsigned char*)&ip2;
sprintf(out1,"%d.%d.%d.%d:%d",ip[0][0],ip[0][1],ip[0][2],ip[0][3],port1);
sprintf(out2,"%d.%d.%d.%d:%d",ip[1][0],ip[1][1],ip[1][2],ip[1][3],port2);
if(s[0]!=-1 && s[1]!=-1)
{
fprintf(stdout,"n[ ] Connect %s, Please Waitn",out1);fflush(stdout);
if(lcx_log)fprintf(lcx_log,"n[ ] Connect %s, Please Waitn",out1),fflush(lcx_log);
if(connect(s[0],(struct sockaddr*)&sa[0],sizeof(struct sockaddr))!=0)
{
fprintf(stdout,"n[-] Connect %s Failed,CutDown...n",out2);
if(lcx_log)fprintf(lcx_log,"n[-] Connect %s Failed,CutDown...n",out2),fflush(lcx_log);
closesocket(s[0]);
closesocket(s[1]);
}
else
{
fprintf(stdout,"n[ ] Connect %s Successed,Now Connect %sn",out1,out2);fflush(stdout);
if(lcx_log)fprintf(lcx_log,"n[ ] Connect %s Successed,Now Connect %sn",out1,out2),fflush(lcx_log);
if(connect(s[1],(struct sockaddr*)&sa[1],sizeof(struct sockaddr))==0)
{
fprintf(stdout,"n[ ] Connect %s Successed,Transfering...n",out2);fflush(stdout);
if(lcx_log)fprintf(lcx_log,"n[ ] Connect %s Successed,Transfering...n",out2),fflush(lcx_log);
in_data_tran(s);
}
else
{
fprintf(stdout,"n[-] Connect %s Failed,CutDown...n",out2);
if(lcx_log)fprintf(lcx_log,"n[-] Connect %s Failed,CutDown...n",out2),fflush(lcx_log);
closesocket(s[0]);
closesocket(s[1]);
}
}
}
else
{
fprintf(stdout,"n[-] Create Socket Failedn");
if(lcx_log)fprintf(lcx_log,"n[-] Create Socket Failedn"),fflush(lcx_log);
return -1;
}
delay(1000);
}
return 0;
}
int lcx_listen(unsigned short port1,unsigned short port2)
{
SOCKET s[2]={-1,-1};
unsigned short p[2];
p[0]=port1;
p[1]=port2;
struct sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = INADDR_ANY;
int i;
int OK = 0;
for(i=0; i<2; i)
{
s[i] = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(s[i]!=-1)
{
fprintf(stdout,"n[ ] Create Socket %d Successedn",i 1);fflush(stdout);
if(lcx_log)fprintf(lcx_log,"n[ ] Create Socket %d Successedn",i 1),fflush(lcx_log);
sa.sin_port = htons(p[i]);
int flag = 1;
setsockopt(s[i], SOL_SOCKET, SO_REUSEADDR, (const void*)&flag, sizeof(flag));
if(bind(s[i],(struct sockaddr*)&sa,sizeof(sa))==0)
{
fprintf(stdout,"n[ ] Bind On Port %u Successn",p[i]);fflush(stdout);
if(lcx_log)fprintf(lcx_log,"n[ ] Bind On Port %u Successn",p[i]),fflush(lcx_log);
if(listen(s[i],SOMAXCONN)==0)
{
fprintf(stdout,"n[ ] Listen On Port %u Successedn",p[i]);fflush(stdout);
if(lcx_log)fprintf(lcx_log,"n[ ] Listen On Port %u Successedn",p[i]),fflush(lcx_log);
OK = 1;
}
else
{
fprintf(stdout,"n[-] Listen On Port %u Failedn",p[i]);
if(lcx_log)fprintf(lcx_log,"n[-] Listen On Port %u Failedn",p[i]),fflush(lcx_log);
break;
}
}
else
{
fprintf(stdout,"n[-] Bind On Port %u Failedn",p[i]);
if(lcx_log)fprintf(lcx_log,"n[-] Bind On Port %u Failedn",p[i]),fflush(lcx_log);
break;
}
}
else
{
fprintf(stdout,"n[-] Create Socket %d Failedn",i 1);
if(lcx_log)fprintf(lcx_log,"n[-] Create Socket %d Failedn",i 1),fflush(lcx_log);
break;
}
}
if(!OK)
{
closesocket(s[0]);
closesocket(s[1]);
return -1;
}
i = 0;
SOCKET t[2];
socklen_t sz = sizeof(sa);
while(1)
{
fprintf(stdout,"n[ ] Waiting Connect On Port %un",p[i]);fflush(stdout);
if(lcx_log)fprintf(lcx_log,"n[ ] Waiting Connect On Port %un",p[i]),fflush(lcx_log);
t[i] = accept(s[i],(struct sockaddr*)&sa,&sz);
const unsigned char *ip = (unsigned char*)&sa.sin_addr.s_addr;
if(t[i]!=-1)
{
fprintf(stdout,"n[ ] Connect From %d.%d.%d.%d:%d On Port %dn",ip[0],ip[1],ip[2],ip[3],htons(sa.sin_port),p[i]);fflush(stdout);
if(lcx_log)fprintf(lcx_log,"n[ ] Connect From %d.%d.%d.%d:%d On Port %dn",ip[0],ip[1],ip[2],ip[3],htons(sa.sin_port),p[i]),fflush(lcx_log);
if(i==1)
{
in_createthread(in_data_tran,t);
}
i = (i==0);
}
else
{
fprintf(stdout,"n[-] Accept Failed On Port %dn",p[i]);
if(lcx_log)fprintf(lcx_log,"n[-] Accept Failed On Port %dn",p[i]),fflush(lcx_log);
i=0;
}
}
return 0;
}
int lcx_tran(unsigned short port1,const char* ip2_str,unsigned short port2)
{
SOCKET s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
struct sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_port = htons(port1);
sa.sin_addr.s_addr = INADDR_ANY;
socklen_t ok =0;
if(s!=-1)
{
int flag = 1;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const void*)&flag, sizeof(flag));
if(bind(s,(struct sockaddr*)&sa,sizeof(sa))==0)
{
if(listen(s,SOMAXCONN)==0)
{
ok = 1;
fprintf(stdout,"n[ ] Listening On Port %d...n",port1);fflush(stdout);
if(lcx_log)fprintf(lcx_log,"n[ ] Listening On Port %d...n",port1),fflush(lcx_log);
}
else
{
fprintf(stdout,"n[-] Listen Failedn");
if(lcx_log)fprintf(lcx_log,"n[-] Listen Failedn"),fflush(lcx_log);
}
}
else
{
fprintf(stdout,"n[-] Bind On Port %d Failedn",port1);
if(lcx_log)fprintf(lcx_log,"n[-] Bind On Port %d Failedn",port1),fflush(lcx_log);
}
}
else
{
fprintf(stdout,"n[-] Create Socket Failedn");
if(lcx_log)fprintf(lcx_log,"n[-] Create Socket Failedn"),fflush(lcx_log);
}
if(!ok)
{
closesocket(s);
return -1;
}
SOCKET tt[2];
SOCKET ac=-1;
ok = sizeof(sa);
char out1[100],out2[100];
while(1)
{
unsigned long ip2 = gethost(ip2_str);
if(-1 == ip2)
{
fprintf(stdout,"n[-] Reslove Host %s Failed...n",ip2_str),fflush(stdout);
break;
}
fprintf(stdout,"n[ ] Waiting Connect On Port %d...n",port1);fflush(stdout);
if(lcx_log)fprintf(lcx_log,"n[ ] Waiting Connect On Port %d...n",port1),fflush(lcx_log);
if(ac=accept(s,(struct sockaddr*)&sa,&ok),ac==-1)
{
break;
}
unsigned char* ip =(unsigned char*)&sa.sin_addr.s_addr;
sprintf(out1,"%d.%d.%d.%d:%d",ip[0],ip[1],ip[2],ip[3],htons(sa.sin_port));
ip = (unsigned char*)&ip2;
sprintf(out2,"%d.%d.%d.%d:%d",ip[0],ip[1],ip[2],ip[3],(port2));
fprintf(stdout,"n[ ] Connect From %s, Now Connect to %sn",out1,out2);fflush(stdout);
if(lcx_log)fprintf(lcx_log,"n[ ] Connect From %s, Now Connect to %sn",out1,out2),fflush(lcx_log);
sa.sin_port = htons(port2);
sa.sin_addr.s_addr = ip2;
SOCKET s2 = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(connect(s2,(struct sockaddr*)&sa,sizeof(sa))==0)
{
tt[0]=ac;
tt[1]=s2;
fprintf(stdout,"n[ ] Connect %s Successed,Start Transfer...n",out2);fflush(stdout);
if(lcx_log)fprintf(lcx_log,"n[ ] Connect %s Successed,Start Transfer...n",out2),fflush(lcx_log);
in_createthread(in_data_tran,tt);
}
else
{
fprintf(stdout,"n[-] Connect %s Failed...n",out2),fflush(stdout);
if(lcx_log)fprintf(lcx_log,"n[-] Connect %s Failed...n",out2),fflush(lcx_log);
closesocket(s2);
closesocket(ac);
}
}
closesocket(s);
closesocket(ac);
return 0;
}
void help(const char* name)
{
fprintf(stdout,"nUsage of Packet Transmit:n");
fprintf(stdout," %s -<listen|tran|slave> <option> [<-log|-hex|-text> file] n",name);
fprintf(stdout," %s -aboutnn",name);
fprintf(stdout,"[options:]n");
fprintf(stdout," -listen <local port1> <local port2>n");
fprintf(stdout," -tran <local port> <remote host> <remote port>n");
fprintf(stdout," -slave <remote host1> <remote port1> <remote host2> <remote port2>nn");
fprintf(stdout," -hex : hex mode data dumpn");
fprintf(stdout," -text : text mode data dumpn");
fprintf(stdout," -log : save transfer lognn");
}
void about()
{
#ifdef ANONYMOUS
fprintf(stdout,"nBy: Anonymousn");
#else
fprintf(stdout,"nBy: XiaoBaWangn");
fprintf(stdout,"Email: windworst@gmail.comnn");
#endif
}
long getport(const char *str)
{
long port = EOF;
port = atoi(str);
if (port<=0||port>65535)
{
fprintf(stdout,"nERROR: %s: Wrong port numbernn",str);
}
return port;
}
void setfile(FILE** fp,const char*file)
{
*fp = fopen(file,"w");
if (*fp==NULL)
{
fprintf(stdout,"nERROR: Can not Write to File: %snn",file);
}
}
int main_func(int argc,char**argv)
{
if (argc<2)
{
help(argv[0]);
return 0;
}
const char* command[] = {"-about","-listen","-slave","-tran"};
int32_t i,s = sizeof(command)/sizeof(*command);
for (i=0; i<s; i )
{
if (strcmp(command[i],argv[1])==0)
break;
}
int32_t n = 2;
const char * addr1=NULL, *addr2=NULL;
int32_t port1=0,port2=0;
switch (i)
{
case 0:
about();
break;
case 1:
if (argc<4)
{
help(argv[0]);
return 0;
}
else
{
port1 = getport(argv[n]);
port2 = getport(argv[ n]);
}
break;
case 2:
if (argc<6)
{
help(argv[0]);
return 0;
}
else
{
addr1 = argv[n];
port1 = getport(argv[ n]);
addr2 = argv[ n];
port2 = getport(argv[ n]);
}
break;
case 3:
if (argc<5)
{
help(argv[0]);
return 0;
}
else
{
port1 = getport(argv[n]);
addr2 = argv[ n];
port2 = getport(argv[ n]);
}
break;
default:
{
help(argv[0]);
return 0;
}
break;
}
if(port1==-1 || port2==-1 )return 0;
const char* logpath=NULL,*hexpath=NULL,*textpath=NULL;
while ( n<argc)
{
if (strcmp(argv[n],"-hex")==0)
{
if (argc-1< n)
{
fprintf(stdout,"n[-] ERROR: -hex Must supply file name.nn");
return 0;
}
hexpath = argv[n];
}
else if (strcmp(argv[n],"-text")==0)
{
if (argc-1< n)
{
fprintf(stdout,"n[-] ERROR: -text Must supply file name.nn");
return 0;
}
textpath = argv[n];
}
else if (strcmp(argv[n],"-log")==0)
{
if (argc-1< n)
{
fprintf(stdout,"n[-] ERROR: -log Must supply file name.nn");
return 0;
}
logpath = argv[n];
}
else
{
fprintf(stdout,"n[-] ERROR: %s Undefined.nn",argv[n]);
return 0;
}
}
if (logpath)
{
setfile(&lcx_log,logpath);
if(lcx_log==NULL)return 0;
}
if (hexpath)
{
setfile(&lcx_hex,hexpath);
if(lcx_hex==NULL)return 0;
}
if (textpath)
{
setfile(&lcx_text,textpath);
if(lcx_text==NULL)return 0;
}
switch(i)
{
case 1:lcx_listen(port1,port2);break;
case 2:lcx_slave(addr1,port1,addr2,port2);break;
case 3:lcx_tran(port1,addr2,(uint16_t)port2);break;
default:break;
}
return 0;
}
#define ARGC_MAXCOUNT 10
int main(int argc,char** argv)
{
signal(SIGINT,ctrl_c);
SOCKET_INIT
int ret = main_func(argc,argv);
#ifdef COMMAND_MODE
while(1)
{
char input_buf[BUF_LEN]={0};
char *argv_list[ARGC_MAXCOUNT]={"lcx"};
printf(">");
int argc_count = 1;
int flag = 0;
int i;
for(i=0;i<BUF_LEN; i)
{
input_buf[i] = getchar();
if(input_buf[i] == 'n' || input_buf[i] == -1 )
{
input_buf[i] = '