Seite 1 von 1

Remote Desktop Client IP / Adresse

Verfasst: 05.08.2010 13:02
von X360 Andy
Hallo,

kennt einer von euch eine Möglichkeit die Client IP bei einem Remote Zugriff über die Windows Remotedesktopverbindung auf einen Windows Server 2008 herauszufinden ?

Super wäre auch der Benutzername woher der Client kommt.

Gruß Andreas

Re: Remote Desktop Client IP / Adresse

Verfasst: 06.08.2010 14:42
von jpd
Hallo X360 Andy,

leider habe ich heute nicht zu viel zeit... dadurch nur ein prototype der einigermaßen funzioniert.

Code: Alles auswählen


; coded by jpd, july 2010

#WTS_CURRENT_SERVER_HANDLE = 0
#WTS_CURRENT_SESSION = -1

Enumeration ;WTS_INFO_CLASS
  #WTSInitialProgram=0
  #WTSApplicationName
  #WTSWorkingDirectory
  #WTSOEMId
  #WTSSessionId
  #WTSUserName
  #WTSWinStationName
  #WTSDomainName
  #WTSConnectState
  #WTSClientBuildNumber
  #wtsclientname
  #WTSClientDirectory
  #WTSClientProductId
  #WTSClientHardwareId
  #WTSClientAddress
  #WTSClientDisplay
  #WTSClientProtocolType
EndEnumeration

#AF_UNSPEC = 0;0 The address family is unspecified.
#AF_INET = 2  ;2 The Internet Protocol version 4 (IPv4) address family.
#AF_IPX = 6   ;6 The IPX/SPX address family. This address family is only supported If the NWLink IPX/SPX NetBIOS Compatible Transport protocol is installed. 
#AF_NETBIOS = 17;17 The NetBIOS address family. This address family is only supported If the Windows Sockets provider For NetBIOS is installed. 
#AF_INET6 = 23 ;23 The Internet Protocol version 6 (IPv6) address family.

Structure WTS_CLIENT_ADDRESS
  AddressFamily.l
  byte.b[20]; As byte
EndStructure

Prototype WTSQuerySessionInformation(hServer.l,SessionId.l,WtsInfoClass.l,ppBuffer.l,pBytesReturned.l)
Global  WTSQuerySessionInformation.WTSQuerySessionInformation
Prototype WTSFreeMemory(lpBuffer.l)
Global  WTSFreeMemory.WTSFreeMemory

Prototype ConvertSidToStringSid(SidS.l,pSid.l) 
Global ConvertSidToStringSid.ConvertSidToStringSid


Procedure LoadWTSAPI() 
  Protected Result 

  Result = OpenLibrary(#PB_Any, "wtsapi32.dll") 
  If Result 
    
    WTSFreeMemory=GetFunction(Result, "WTSFreeMemory")
    CompilerIf #PB_Compiler_Unicode
      WTSQuerySessionInformation = GetFunction(Result, "WTSQuerySessionInformationW") 
    CompilerElse
      WTSQuerySessionInformation = GetFunction(Result, "WTSQuerySessionInformationA") 
    CompilerEndIf
    
    If WTSQuerySessionInformation = 0 
      CloseLibrary(Result) 
      Result = 0 
    EndIf      
  EndIf 
  
  ProcedureReturn Result 
  
EndProcedure 


Procedure.s CurrentSessionUserName()
  
  Protected pBytesReturnedaddress.l
  Protected UnameLength
  Protected Username.s
  Protected DomainName.s
  *username=AllocateMemory(256)
  *domainname=AllocateMemory(256)
  WTSQuery= WTSQuerySessionInformation(#WTS_CURRENT_SERVER_HANDLE,#WTS_CURRENT_SESSION , #WTSUserName ,@*Username, @pBytesReturnedaddress)
  UnameLength=MemoryStringLength(*Username)
  Username=PeekS(*Username,UnameLength)
  WTSFreeMemory(*Username)
  WTSQuery= WTSQuerySessionInformation(#WTS_CURRENT_SERVER_HANDLE,#WTS_CURRENT_SESSION , #WTSDomainName,@*domainname, @pBytesReturnedaddress)
  UnameLength=MemoryStringLength(*domainname)
  DomainName=PeekS(*domainname,UnameLength)
  WTSFreeMemory(*domainname)
  
  If DomainName <> ""
    ProcedureReturn DomainName+"\"+Username 
  Else
    ProcedureReturn Username
  EndIf
  
EndProcedure




Procedure.s WTSQuery(ToQuery.l)
  Select ToQuery
    Case #WTSClientAddress
      Protected pBytesReturnedaddress.l
      actualaddress.WTS_CLIENT_ADDRESS
      Protected *addressbuffer=AllocateMemory(20)
      Protected getip.s
      WTSQuery= WTSQuerySessionInformation(#WTS_CURRENT_SERVER_HANDLE,#WTS_CURRENT_SESSION , ToQuery ,@*addressbuffer, @pBytesReturnedaddress)
      CopyMemory( *addressbuffer, actualaddress, pBytesReturnedaddress)
      
      ;Debug actualaddress\AddressFamily
    
      getip= Str(actualaddress\byte[2] & $FF)+"."+Str(actualaddress\byte[3] & $FF)+"."+Str(actualaddress\byte[4] & $FF)+"."+Str(actualaddress\byte[5] & $FF)
      WTSFreeMemory(*addressbuffer)
      ProcedureReturn getIP
    Case #WTSClientProtocolType
      Protected *protType.l=AllocateMemory(4)
      Protected pBytesprottype.l
      WTSQuery= WTSQuerySessionInformation(#WTS_CURRENT_SERVER_HANDLE,#WTS_CURRENT_SESSION , ToQuery ,@*protype, @pBytesprottype)
      ;Debug *protype
    Case #WTSWinStationName  
      *WinStationName=AllocateMemory(256)
      WTSQuery= WTSQuerySessionInformation(#WTS_CURRENT_SERVER_HANDLE,#WTS_CURRENT_SESSION , ToQuery ,@*WinStationName, @pBytesReturnedaddress)
      UnameLength=MemoryStringLength(*WinStationName)
      WinStationName.s=PeekS(*WinStationName,UnameLength)
      ;Debug WinStationName
      WTSFreeMemory(*WinStationName)
      ProcedureReturn UCase(WinStationName)
      
    Case  #WTSClientName
      *WTSClientName=AllocateMemory(256)
      WTSQuery= WTSQuerySessionInformation(#WTS_CURRENT_SERVER_HANDLE,#WTS_CURRENT_SESSION , ToQuery ,@*WTSClientName, @pBytesReturnedaddress)
      UnameLength=MemoryStringLength(*WTSClientName)
      WTSClientName.s=PeekS(*WTSClientName,UnameLength)
      ;Debug WTSClientName
      WTSFreeMemory(*WTSClientName)
      ProcedureReturn WTSClientName
  EndSelect    
      
EndProcedure



libWTS=LoadWTSAPI()
If libWTS
  Debug "loaded"
  If FindString(WTSQuery(#WTSWinStationName),"RDP-TCP",1)
    Debug "RDP tcp sitzung"
    Debug "Connected from Workstation: "+WTSQuery(#WTSClientName)
    Debug "Workstation IP: "+ WTSQuery(#WTSClientAddress)
    Debug "Session Username: "+CurrentSessionUserName()
  EndIf  
    
 
  CloseLibrary(libWTS)
  
Else  
  Debug "libWTS not loaded"
EndIf
für den benutzername woher der client kommt musst du möglicherweiße
WTSClientInfo = 23
bei WTSQuerySessionInformation verwenden damit wird eine structure namens WTSCLIENT
sieht so aus:

Code: Alles auswählen

typedef struct _WTSCLIENT {
  WCHAR   ClientName[CLIENTNAME_LENGTH + 1];
  WCHAR   Domain[DOMAIN_LENGTH + 1 ];
  WCHAR   UserName[USERNAME_LENGTH + 1];
  WCHAR   WorkDirectory[MAX_PATH + 1];
  WCHAR   InitialProgram[MAX_PATH + 1];
  BYTE    EncryptionLevel;
  ULONG   ClientAddressFamily;
  USHORT  ClientAddress[CLIENTADDRESS_LENGTH + 1];
  USHORT  HRes;
  USHORT  VRes;
  USHORT  ColorDepth;
  WCHAR   ClientDirectory[MAX_PATH + 1];
  ULONG   ClientBuildNumber;
  ULONG   ClientHardwareId;
  USHORT  ClientProductId;
  USHORT  OutBufCountHost;
  USHORT  OutBufCountClient;
  USHORT  OutBufLength;
  WCHAR     DeviceId[MAX_PATH + 1];
  } WTSCLIENT, *PWTSCLIENT;
http://msdn.microsoft.com/en-us/library ... S.85).aspx
Ciao
jpd

Re: Remote Desktop Client IP / Adresse

Verfasst: 07.08.2010 01:19
von X360 Andy
WOW richtig dickes Danke an dich, funktioniert (fast) tadellos!
Bekomme zwar nen IMA wenn ich den Code in mein Programm einsetzt aber das liegt nicht ( direkt ) an dem Code.

Super 1000 Dank !

Den Code solltest du finde ich in die Code/Tricks Section posten, damit man ihn besser findet.

Gruß Andreas

Re: Remote Desktop Client IP / Adresse

Verfasst: 07.08.2010 20:20
von jpd
Freut mich das es dir gefällt.

hoffe das es bei dir irgendwann fehlerfrei funzioniert! :)

bei tips und tricks sind schon teile dieses beispiel von mir gepostet wurden.

Ciao
jpd