This question cho tôi biết cách phát hiện phiên máy tính từ xa.Phát hiện nguồn của kết nối máy tính từ xa
Có ai biết nếu có thể tìm ra nơi kết nối từ xa được khởi tạo không?
-Vegar
This question cho tôi biết cách phát hiện phiên máy tính từ xa.Phát hiện nguồn của kết nối máy tính từ xa
Có ai biết nếu có thể tìm ra nơi kết nối từ xa được khởi tạo không?
-Vegar
Vì nó trong cửa sổ sử dụng netstat để kiểm tra mà máy bạn đang kết nối đến và đi trên đó cổng và chỉ phân tích ra địa chỉ cho một trong đó sử dụng cổng mà máy tính từ xa sử dụng.
@Vegar, bạn có thể sử dụng các chức năng WTSEnumerateSessions và WTSQuerySessionInformation để truy xuất thông tin này.
kiểm tra điều này link để biết ví dụ bằng cách sử dụng Jedi Api Headers.
kiểm tra mã này.
program ProjectTsInfo;
{$APPTYPE CONSOLE}
Uses
Windows,
JwaWinType,
JwaWtsApi32,
JwaWinsock2,
SysUtils,
TypInfo;
type
PWtsSessionInfoAArray = ^TWtsSessionInfoAArray;
TWtsSessionInfoAArray = array[0..ANYSIZE_ARRAY-1] of WTS_SESSION_INFOA;
//Get the info for all clients connected
procedure GetAll_TSClientsInfo;
var
SessionInfoAArray: PWtsSessionInfoAArray;
ClientAddr : PWtsClientAddress;
ClientName : PAnsiChar;
//ClientInfo : PWTSCLIENT;
RetBytes : Cardinal;
IPAddr : String;
i : integer;
pCount : Cardinal;
SessionId : Cardinal;
begin
if WtsEnumerateSessions(WTS_CURRENT_SERVER, 0, 1, PWTS_SESSION_INFO(SessionInfoAArray), pCount) then
begin
for i := 0 to pCount - 1 do
begin
SessionId:=SessionInfoAArray^[i].SessionId;
WTSQuerySessionInformation(WTS_CURRENT_SERVER, SessionId, WTSClientAddress, Pointer(ClientAddr), RetBytes);
WTSQuerySessionInformation(WTS_CURRENT_SERVER, SessionId, WTSClientName, Pointer(ClientName), RetBytes);
//WTSQuerySessionInformation(WTS_CURRENT_SERVER, SessionId, WTSClientInfo, Pointer(ClientInfo), RetBytes); //This value is supported for Windows Server 2008 and Windows Vista with SP1.
try
case ClientAddr^.AddressFamily of
AF_INET:
IPAddr:= Format('%d.%d.%d.%d', [
ClientAddr^.Address[2],
ClientAddr^.Address[3],
ClientAddr^.Address[4],
ClientAddr^.Address[5]
]);
else
IPAddr:= '<unknow>';
end;
WriteLn(Format('Session Id : %d ', [SessionId]));
WriteLn(Format('Client Name : %s ', [ClientName]));
WriteLn(Format('Station Name: %s ', [SessionInfoAArray^[i].pWinStationName]));
WriteLn(Format('State : %s ', [GetEnumName(TypeInfo(WTS_CONNECTSTATE_CLASS),integer(SessionInfoAArray^[i].State))]));
WriteLn(Format('IP : %s ', [IPAddr]));
//supported for Windows Server 2008 and Windows Vista with SP1.
{
WriteLn(Format('ClientName : %s ', [ClientInfo^.ClientName]));
WriteLn(Format('Domain : %s ', [ClientInfo^.Domain]));
WriteLn(Format('UserName : %s ', [ClientInfo^.UserName]));
WriteLn(Format('WorkDirectory : %s ', [ClientInfo^.WorkDirectory]));
WriteLn(Format('InitialProgram : %s ', [ClientInfo^.InitialProgram]));
WriteLn(Format('EncryptionLevel : %d ', [ClientInfo^.EncryptionLevel]));
WriteLn(Format('HRes : %d ', [ClientInfo^.HRes]));
WriteLn(Format('VRes : %d ', [ClientInfo^.VRes]));
WriteLn(Format('ColorDepth : %d ', [ClientInfo^.ColorDepth]));
WriteLn(Format('ClientDirectory : %s ', [ClientInfo^.ClientDirectory]));
}
Writeln('');
finally
WTSFreeMemory(ClientAddr);
WTSFreeMemory(ClientName);
end;
end;
end;
WtsFreeMemory(SessionInfoAArray);
end;
//Get the ip address of the actual connected client
function GetIpActualClient : string;
var
ClientAddr : PWtsClientAddress;
RetBytes : Cardinal;
IPAddr : String;
SessionId : Cardinal;
begin
SessionId:=WTS_CURRENT_SESSION;
WTSQuerySessionInformation(WTS_CURRENT_SERVER, SessionId, WTSClientAddress, Pointer(ClientAddr), RetBytes);
try
case ClientAddr^.AddressFamily of
AF_INET:
IPAddr:= Format('%d.%d.%d.%d', [
ClientAddr^.Address[2],
ClientAddr^.Address[3],
ClientAddr^.Address[4],
ClientAddr^.Address[5]
]);
else
IPAddr:= '<unknow>';
end;
Result:=IPAddr;
finally
WTSFreeMemory(ClientAddr);
end;
end;
begin
Writeln('IP Actual client '+GetIpActualClient);
Writeln('-----------------------------------');
GetAll_TSClientsInfo;
Readln;
end.
CẬP NHẬT
Như @Remko nói, WTSQuerySessionInformation chức năng với WTSClientAddress loại, có thể trả lại IP cục bộ của khách hàng. nếu bạn muốn có được ip thực, bạn có thể sử dụng chức năng trợ giúp WinStationGetRemoteIPAddress nằm trong đơn vị JwaWinSta.
Var
Port : Word;
IpAddr : WideString;
Begin
WinStationGetRemoteIPAddress(WTS_CURRENT_SERVER,WTS_CURRENT_SESSION,IpAddr,Port);
End;
Có cần thiết phải sử dụng 'WTSEnumerateSessions' không? Tôi nghĩ rằng việc sử dụng 'wts_Current_Session' cho ID phiên sẽ là đủ. –
Để giúp bạn có phiên làm việc riêng, hãy sử dụng ProcessIdToSessionId –
@Rob bạn đúng là hàm WTSEnumerateSessions là lấy thông tin cho tất cả các phiên, tôi đã đăng một ví dụ bằng cách sử dụng wts_Current_Session và WTSEnumerateSessions. ;) – RRUZ
thử chạy qwinsta
WTSQuerySessionInformation trả về client IP như khách hàng báo cáo nó, điều này có lẽ sẽ được (một) của nó là địa phương Địa chỉ IP. Nếu bạn muốn biết địa chỉ IP thực và cổng được kết nối, bạn có thể sử dụng WinStationQueryInformationW với lớp thông tin WinStationRemoteAddress. Bạn sẽ cần đơn vị JwaWinsta của tôi từ Jedi Apilib.
tôi đã cung cấp một wrapper đơn giản trong cùng đơn vị cũng như:
function WinStationGetRemoteIPAddress(hServer: HANDLE; SessionId: DWORD;
var RemoteIPAddress: WideString; var Port: WORD): Boolean;
Đối với tôi, điều này làm việc, nó được tên của máy được kết nối.
Environment.GetEnvironmentVariable("CLIENTNAME")
Nếu bạn muốn nhận ID phiên từ xa và nhận địa chỉ IP được kết nối qua Citrix, bạn có thể sử dụng bên dưới. Điều này được thiết kế để chạy khi người dùng kết nối với một máy chủ thông qua một phiên Citrix và hiển thị/tạo một chuỗi cho địa chỉ IP mà nó đang kết nối từ đó.
// Prints out ICA or RDP session ID of current user & gets ICA session clientAddress variable
using System;
using Microsoft.Win32;
namespace ViaRegedit
{
class Program03
{
static void Main(string[] args)
{
// Obtain an instance of RegistryKey for the CurrentUser registry
RegistryKey rkCurrentUser = Registry.CurrentUser;
// Obtain the test key (read-only) and display it.
RegistryKey rkTest = rkCurrentUser.OpenSubKey("Remote");
foreach (string valueName in rkTest.GetSubKeyNames())
{
//Getting path to RDP/Citrix session ID
string RDPICApath = "";
if (rkTest.OpenSubKey(valueName) != null && rkTest.OpenSubKey(valueName) != null) { RDPICApath = rkTest.OpenSubKey(valueName).ToString(); }
Console.WriteLine("Getting CurrentUser ICA-RDP path from string = " + RDPICApath);
//Split RDPICApath to get session number
string RDPICAnumber = RDPICApath.Substring(RDPICApath.LastIndexOf('\\') + 1);
Console.WriteLine("Current User RDPICAnumber = " + RDPICAnumber);
//Getting reg local machine info for Citrix based on RDP/Citrix session ID "RDPICAnumber"
string regLocal = @"SOFTWARE\Citrix\Ica\Session\" + RDPICAnumber + @"\Connection";
RegistryKey localKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey citrixKey = localKey.OpenSubKey(regLocal);
Console.WriteLine("Registry " + citrixKey + " Does Exist - going to get ClientAddress");
//getting clietAddress var from citrixKey
string clientAddress = "";
if (citrixKey != null && citrixKey.GetValue("clientAddress") != null)
{clientAddress = citrixKey.GetValue("clientAddress").ToString();}
Console.WriteLine("Getting current user clientAddress from string = " + clientAddress);
}
rkTest.Close();
rkCurrentUser.Close();
Console.ReadLine();
}
}
}
Và vì máy tính từ xa luôn sử dụng cổng 3389, điều này sẽ hoạt động. Cảm ơn! – Vegar
Điều này sẽ chỉ hoạt động nếu bạn có một kết nối từ xa khác, bạn không thể xem phiên nào kết nối với máy từ xa nào. –
Sản lượng 'netstat' có bị ảnh hưởng bởi ngôn ngữ hiện tại không? Điều đó có thể làm cho việc phân tích cú pháp chuỗi * nhiều * phức tạp hơn. Ưu tiên giải pháp API dựa trên giải pháp dựa trên các chương trình bên ngoài và định dạng đầu ra biến đổi. –