下面是我翻译的c++不过运转不正常,请高手指点!不够可以不断加分!!
unit Port2Proc;
interface
uses
Windows,WinSock,AclAPI,AccCtrl,ComCtrls,PsAPI,SysUtils,QDialogs;
const
NT_HANDLE_LIST = 16;
MAX_HANDLE_LIST_BUF = $20000;
type
PHandleInfo=^_HANDLEINFO;
_HANDLEINFO=packed record
dwPid : WORD;
GreatorBackTraceIndex : WORD;
ObjType : Byte;
HandleAttributes : Byte;
HndlOffset : SHORT;
dwKeObject : DWORD;
GrantedAccess : ULONG
end;
function RaisePrivileges(hToken:THandle;pPriv: Pchar):Boolean;
procedure AdjustDACL(ProHandle:THandle);
procedure NTXPPort2Proc(ListView1:TListView);
implementation
//提升进程hToken特权
function RaisePrivileges(hToken:THandle;pPriv: Pchar):Boolean;
var
tkp : TOKEN_PRIVILEGES;
rl : Cardinal;
begin
LookupPrivilegeValue(nil,pPriv,tkp.Privileges[0].Luid);
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
tkp.PrivilegeCount := 1;
Result:=AdjustTokenPrivileges(hToken, False, tkp, 0, nil,rl);
end;
//调整目标进程DACL
procedure AdjustDACL(ProHandle:THandle);
var
pSD : PPSECURITY_DESCRIPTOR;
pNewDacl,pDacl : PACL;
ea : EXPLICIT_ACCESS;
begin
pSD:=nil;
pDacl:=nil;
pNewDacl:=nil;
GetSecurityInfo(ProHandle,SE_KERNEL_OBJECT,DACL_SECURITY_INFORMATION,
nil,nil,pDacl,nil,pSD);
ZeroMemory(@ea, sizeof(EXPLICIT_ACCESS));
ea.grfAccessPermissions:=STANDARD_RIGHTS_ALL or SPECIFIC_RIGHTS_ALL;
ea.grfAccessMode:=SET_ACCESS;
ea.grfInheritance:=NO_INHERITANCE;
ea.Trustee.pMultipleTrustee:=nil;
ea.Trustee.MultipleTrusteeOperation:=NO_MULTIPLE_TRUSTEE;
ea.Trustee.TrusteeForm:=TRUSTEE_IS_SID;
ea.Trustee.TrusteeType:=TRUSTEE_IS_USER;
ea.Trustee.ptstrName:='CURRENT_USER';
SetEntriesInAcl(1,@ea,pDacl,pNewDacl);
SetSecurityInfo(ProHandle,SE_KERNEL_OBJECT,DACL_SECURITY_INFORMATION,nil,nil,pNewDacl,nil);
end;
procedure NTXPPort2Proc(ListView1:TListView);
type
TNTQuerySystemInformation= function( pdwRecordType
WORD; pdwHandleList
DWORD;
pdwNumBytes
WORD;pdwNumBytesRet
WORD)
WORD;stdcall;
var
NTQuerySystemInfomation : TNTQuerySystemInformation;
DllHandle,hToken,CurrProcHandle,ProcHandle,MyHandle :THandle;
dwNumBytes,dwNumBytesRet,dwNumEntries,i : DWORD;
namelen,optlen,sockType: integer;
dwHandleList: PDWORD;
s : TSocket;
GInitData : TWSADATA;
PhInfo : PHandleInfo;
name : TSockAddrIn;
PortPath : array [0..MAX_PATH] of Char;
begin
ListView1.Clear;
MyHandle:=0;
WSAStartup($101, GInitData);
CurrProcHandle:=GetCurrentProcess;
OpenProcessToken(CurrProcHandle, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken);
RaisePrivileges(hToken,'SeSecurityPrivilege');
if hToken>0 then CloseHandle(HToken);
DllHandle := LoadLibrary('ntdll.dll');
if(DllHandle<>0) then
begin
NTQuerySystemInfomation:=GetProcAddress(DllHandle,'NtQuerySystemInformation');
dwNumBytes:=MAX_HANDLE_LIST_BUF;
dwNumbytesRet:=0;
//分配内存
GetMem(dwHandleList,dwNumBytes);
//成功调用将返回0值
NTQuerySystemInfomation(NT_HANDLE_LIST,dwHandleList,dwNumBytes,dwNumbytesRet);
end;
dwNumEntries:=dwHandleList^;
inc(dwHandleList^);
Phinfo:=@dwHandleList;
for i:=0 to dwNumEntries-1 do
begin
//★★需要判断操作系统类型 XP 和 NT或WIN2000
if (PhInf
bjType=$1A) and (PhInfo.dwPid>0) then
begin
ProcHandle:=OpenProcess(WRITE_DAC,FALSE,PhInfo.dwPid);
showmessage('rr:'+IntToStr(ProcHandle));
if ProcHandle>0 then
begin
AdjustDACL(ProcHandle);
CloseHandle(ProcHandle);
end;
ProcHandle:=OpenProcess(PROCESS_DUP_HANDLE,TRUE,PhInfo.dwPid);
if ProcHandle>0 then
begin
DuplicateHandle(ProcHandle,Cardinal(PhInfo.HndlOffset),CurrProcHandle,
@MYHandle,STANDARD_RIGHTS_REQUIRED,FALSE,0);
CloseHandle(ProcHandle);
end;
if MyHandle>0 then
begin
name.sin_family:=AF_INET;
namelen:=sizeof(TSockAddrIn);
s:=TSocket(MyHandle);
if GetSockName(s,name,namelen)<>SOCKET_ERROR then
begin
sockType:=0;
optlen:=4;
getsockopt(s,SOL_SOCKET,SO_TYPE,PChar(sockType),optlen);
ProcHandle:=OpenProcess(PROCESS_ALL_ACCESS,FALSE,PhInfo.dwPid);
GetModuleFileNameEx(ProcHandle,0,PortPath,sizeof(PortPath));
CloseHandle(ProcHandle);
//输出信息
with ListView1.Items.Add do
begin
//协议类型
Caption:='TCP';
//本机端口
SubItems.Add(Format('%6d',[ntohs(name.sin_port)]));
//远程主机IP
SubItems.Add('');
//远程主机端口
SubItems.Add('');
//进程路径
SubItems.Add(PortPath);
end;
end;
end;
end;
//★★
inc(PhInfo);
end;
//释放内存
FreeMem(dwHandleList);
showmessage('kkk');
FreeAndNil(PhInfo);
FreeAndNil(dwHandleList);
if CurrProcHandle>0 then CloseHandle(CurrProcHandle);
FreeLibrary(DllHandle);
WSACleanup;
end;
end.