📄 端口到进程实现的源代码.txt
字号:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <aclapi.h>
#include <comdef.h>
#include <winioctl.h>
#pragma comment ( lib, "ws2_32.lib" )
#define nt_handle_list 16
#define max_handle_list_buf 0x200000
typedef struct _handleinfo
{
ushort dwpid;
ushort creatorbacktraceindex;
byte objtype;
byte handleattributes;
ushort hndloffset;
dword dwkeobject;
ulong grantedaccess;
}handleinfo, *phandleinfo;
typedef struct _io_status_block {
dword status;
ulong information;
} io_status_block, *pio_status_block;
typedef struct _lsa_unicode_string {
ushort length;
ushort maximumlength;
pwstr buffer;
} lsa_unicode_string, *plsa_unicode_string;
typedef lsa_unicode_string unicode_string, *punicode_string;
typedef struct _object_attributes {
ulong length;
handle rootdirectory;
unicode_string *objectname;
ulong attributes;
psecurity_descriptor securitydescriptor;
psecurity_quality_of_service securityqualityofservice;
} object_attributes,*pobject_attributes;
// 申明ntquerysysteminformation()函数
typedef dword (callback* ntquerysysteminformation)( dword, pdword, dword, pvoid );
ntquerysysteminformation ntquerysysteminformation;
typedef void (callback* rtlinitunicodestring)(punicode_string,pcwstr);
rtlinitunicodestring rtlinitunicodestring;
typedef dword (callback* zwopensection)(pvoid, dword,pobject_attributes);
zwopensection zwopensection;
typedef void(callback* initializeobjectattributes)(pobject_attributes,punicode_string,ulong,handle,psecurity_descriptor);
initializeobjectattributes initializeobjectattributes;
typedef dword (callback* zwopenfile)(phandle,dword,pobject_attributes,pio_status_block,ulong,ulong);
zwopenfile zwopenfile;
dword getmap(phandleinfo get1,lpvoid addr,handle pm,char * buf)
{
dword readset;
lpvoid pmaddr1;
int i;
readset = (get1->dwkeobject>>0x16);
readset = *((lpdword)((dword)addr + 4*readset));
if((readset&0x000000ff)==1)
{
return 0;
}
else
{
if((readset&0x000000ff)==0x80)
{
pmaddr1 = mapviewoffile(pm,4,0,readset&0xfffff000,0x1000);
readset = (get1->dwkeobject>>0x0c) & 0x3ff;
readset = *((lpdword)((dword)pmaddr1 + 4*readset));
unmapviewoffile(pmaddr1);
readset = readset & 0x0fffff000;
}
else
{
readset=(readset&0xfffff000)+(get1->dwkeobject&0x003ff000);
}
pmaddr1 =mapviewoffile(pm,4,0,readset,0x1000);
if(pmaddr1!=null)
{
readset = get1->dwkeobject&0x00000fff;
readset = (dword)pmaddr1+readset;
for(i=0;i<0x70;i++)
buf[i] = *((char *)(readset + i));
unmapviewoffile(pmaddr1);
}
else
{
return 0;
}
}
return readset;
}
int main( )
{
dword readset1;
dword readset2;
dword readset3;
overlapped la;
hmodule hntdll = null;
dword dwnumentries;
phandleinfo phandleinfo;
handle htcp;
handle pmy;
handle hudp;
handle myhand;
handle h1=null;
hntdll = loadlibrary( "ntdll.dll" );
dword status;
lpvoid pmaddr;
token_privileges newstate;
dword dwnumbytes = max_handle_list_buf;
pdword pdwhandlelist;
pdword pdwhandinfo;
dword dwnumbytesret;
handle htoken;
bool isok;
unicode_string dn;
io_status_block ch3;
int port1;
int port2;
handle hproc;
wchar_t * ch1 = l"\\device\\tcp";
wchar_t * ch2 = l"\\device\\udp";
object_attributes ofs;
dword i;
dword p=0;
char buf1[0x70];
char buf2[0x70];
char buf3[0x70];
char in[0x18];
char in1[0x18];
char out[0x38];
char out1[0x30];
phandleinfo tcpdnum;
phandleinfo udpdnum;
if ( !hntdll )
{
printf( "loadlibrary( ntdll.dll ) error:%d\n", getlasterror() );
return false;
}
ntquerysysteminformation = (ntquerysysteminformation)
getprocaddress( hntdll, "ntquerysysteminformation");
rtlinitunicodestring = (rtlinitunicodestring)
getprocaddress( hntdll, "rtlinitunicodestring");
zwopensection = (zwopensection)
getprocaddress( hntdll, "zwopensection");;
initializeobjectattributes = (initializeobjectattributes)
getprocaddress( hntdll, "initializeobjectattributes");
zwopenfile = (zwopenfile)
getprocaddress( hntdll, "zwopenfile");;
rtlinitunicodestring(&dn,l"\\device\\physicalmemory");
object_attributes udm= {
sizeof(object_attributes), // length
null, // rootdirectory
&dn, // objectname
0, // attributes
null, // securitydescriptor
null, // securityqualityofservice
};
status = zwopensection(&h1,section_map_read,&udm);
if(status == 0)
{
pmy = getcurrentprocess();
pmaddr =mapviewoffile(h1,4,0,0x30000,0x1000);
newstate.privilegecount=1;
newstate.privileges[0].attributes=2;
newstate.privileges[0].luid.highpart=0;
newstate.privileges[0].luid.lowpart=0;
isok=lookupprivilegevalue(0,se_debug_name,&newstate.privileges[0].luid);
isok=openprocesstoken(pmy,0x20,&htoken);
isok=adjusttokenprivileges(htoken,0,&newstate,0x10,0,0);
closehandle(htoken);
rtlinitunicodestring(&dn,ch1);
ofs.securitydescriptor = 0;
ofs.objectname = &dn;
ofs.length =0x18;
ofs.rootdirectory = 0;
ofs.attributes =0x40;
ofs.securityqualityofservice =0;
status=zwopenfile(&htcp,0x100000,&ofs,&ch3,3,0);
rtlinitunicodestring(&dn,ch2);
ofs.objectname = &dn;
status=zwopenfile(&hudp,0x100000,&ofs,&ch3,3,0);
pdwhandlelist = (pdword)malloc(dwnumbytes);
pdwhandinfo = (pdword)malloc(2048);
dwnumbytesret = 0x10;
isok = (*ntquerysysteminformation)(0x10,pdwhandlelist,dwnumbytes,&dwnumbytesret);
if( !isok)
{
dwnumentries = pdwhandlelist[0];
phandleinfo = (phandleinfo)( pdwhandlelist + 1 );
for (i = 0; i < dwnumentries; i++ )
{
if(phandleinfo->dwpid == getcurrentprocessid() && phandleinfo->hndloffset ==(int)htcp)
{
tcpdnum = phandleinfo;
break;
}
phandleinfo++;
}
phandleinfo = (phandleinfo)( pdwhandlelist + 1 );
for (i = 0; i < dwnumentries; i++ )
{
if(phandleinfo->dwpid == getcurrentprocessid() && phandleinfo->hndloffset ==(int)hudp)
{
udpdnum = phandleinfo;
break;
}
phandleinfo++;
}
zeromemory(buf1,0x70);
zeromemory(buf2,0x70);
readset1 = getmap(tcpdnum,pmaddr,h1,buf1);
if(readset1==0)
{
printf("map tcp faile\n");
return 0;
}
readset2 = getmap(udpdnum,pmaddr,h1,buf2);
if(readset2==0)
{
printf("map udp faile\n");
return 0;
}
la.hevent = createevent(0,1,0,0);;
la.internal = 0;
la.internalhigh=0;
la.offset = 0;
la.offsethigh = 0;
phandleinfo = (phandleinfo)( pdwhandlelist + 1 );
for (i = 0; i < dwnumentries; i++ )
{
zeromemory(buf3,0x70);
if(phandleinfo->objtype == tcpdnum->objtype)
{
readset3 = getmap(phandleinfo,pmaddr,h1,buf3);
if(readset3==0)
{
phandleinfo++;
continue;
}
if(buf3[4]==buf1[4] && buf3[5]==buf1[5]&& buf3[6]==buf1[6]&& buf3[7]==buf1[7])
{
if((buf3[16]==1 || buf3[16]==2) && buf3[17]==0 && buf3[18]==0 && buf3[19]==0)
{
hproc = openprocess(0x40,0,phandleinfo->dwpid);
if(hproc==null)
{
phandleinfo++;
continue;
}
duplicatehandle(hproc,(handle)phandleinfo->hndloffset,pmy,&myhand,0,0,2);
closehandle(hproc);
if(myhand==null)
{
phandleinfo++;
continue;
}
zeromemory(out1,0x30);
zeromemory(out,0x38);
zeromemory(in,0x18);
zeromemory(in1,0x18);
in[0x10] = 4;
in1[0x10]=3;
if(buf3[16]==2)
{
p = 0;
isok = deviceiocontrol(myhand,0x210012,&in,0x18,&out,0x38,&p,&la);
if(isok==false)
{
phandleinfo++;
continue;
}
}
isok = deviceiocontrol(myhand,0x210012,&in1,0x18,&out1,0x30,&p,&la);
if(isok)
{
port1 = out1[12];
if(port1<0)
port1 = 256 + port1;
port1 = port1*256;
port2 = out1[13];
if(port2<0)
port1 = port1 + 256 + port2;
else
port1 = port1 + port2;
printf("tcp pid:%d; port:%d\n",phandleinfo->dwpid,port1);
}
closehandle(myhand);
}
}
}
phandleinfo++;
}
phandleinfo = (phandleinfo)( pdwhandlelist + 1 );
for (i = 0; i < dwnumentries; i++ )
{
zeromemory(buf3,0x70);
if(phandleinfo->objtype == udpdnum->objtype)
{
readset3 = getmap(phandleinfo,pmaddr,h1,buf3);
if(readset3==0)
{
phandleinfo++;
continue;
}
if(buf3[4]==buf2[4] && buf3[5]==buf2[5]&& buf3[6]==buf2[6]&& buf3[7]==buf2[7])
{
if((buf3[16]==1 || buf3[16]==2) && buf3[17]==0 && buf3[18]==0 && buf3[19]==0)
{
hproc = openprocess(0x40,0,phandleinfo->dwpid);
if(hproc==null)
{
phandleinfo++;
continue;
}
duplicatehandle(hproc,(handle)phandleinfo->hndloffset,pmy,&myhand,0,0,2);
closehandle(hproc);
if(myhand==null)
{
phandleinfo++;
continue;
}
zeromemory(out1,0x30);
zeromemory(out,0x38);
zeromemory(in,0x18);
zeromemory(in1,0x18);
in[0x10] = 4;
in1[0x10]=3;
if(buf3[16]==2)
{
p = 0;
isok = deviceiocontrol(myhand,0x210012,&in,0x18,&out,0x38,&p,&la);
if(isok==false)
{
phandleinfo++;
continue;
}
}
isok = deviceiocontrol(myhand,0x210012,&in1,0x18,&out1,0x30,&p,&la);
if(isok)
{
port1 = out1[12];
if(port1<0)
port1 = 256 + port1;
port1 = port1*256;
port2 = out1[13];
if(port2<0)
port1 = port1 + 256 + port2;
else
port1 = port1 + port2;
printf("udp pid:%d; port:%d\n",phandleinfo->dwpid,(unsigned short)port1);
}
closehandle(myhand);
}
}
}
phandleinfo++;
}
closehandle(la.hevent);
free(pdwhandlelist);
free(pdwhandinfo);
closehandle(h1);
closehandle(pmy);
closehandle(htcp);
closehandle(hudp);
unmapviewoffile(pmaddr);
}
else
printf("error open handlelist\n");
}
else
printf("error open physcalmemory\n");
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -