c++ - 使用 WINRM 在远程主机上推送二进制文件

标签 c++ c windows winapi

我可以使用 WINRM 将二进制文件推送到远程主机 Windows 上吗? 如果没有,是否有任何其他机制允许我在远程主机上推送二进制文件。

最佳答案

我想我找到了解决方案。

#include <windows.h>
#include <tchar.h>    

#define           SIZEOF_BUFFER      0x100

// Remote Parameters
LPCTSTR           lpszMachine    = NULL;
LPCTSTR           lpszPassword   = NULL;
LPCTSTR           lpszUser       = NULL;
LPCTSTR           lpszDomain     = NULL;
LPCTSTR           lpszCommandExe = NULL;
LPCTSTR           lpszLocalIP    = _T("\\\\127.0.0.1");

char        szThisMachine[SIZEOF_BUFFER] = "";
char        szPassword[SIZEOF_BUFFER]    = "";

LPCTSTR GetParamValue( LPCTSTR lpszParam )
{
    DWORD dwParamLength = _tcslen( lpszParam );

    for ( int i = 1; i < __argc; i++ )
        if ( __targv[i][0] == _T('\\') || __targv[i][0] == _T('.'))
            continue;
        else
            if ( __targv[i][0] == _T('/') || __targv[i][0] == _T('-') )
            {
                if ( _tcsnicmp( __targv[i] + 1, lpszParam, dwParamLength ) == 0 )
                      return __targv[i] + dwParamLength + 1;
          }
          else
                return NULL;    
      return NULL;
}

LPCTSTR GetNthParameter( DWORD n, DWORD& argvIndex )
{
      DWORD index = 0;    
      for( int i = 1; i < __argc; i++ )
      {
            if ( __targv[i][0] != _T('/') && __targv[i][0] != _T('-') )
                  index++;

            if ( index == n )
            {
                  argvIndex = i;
                  return __targv[i];
            }
      }    
      return NULL;
}

BOOL SetConnectionCredentials()
{
      lpszPassword = GetParamValue( _T("pwd:") );
      lpszUser     = GetParamValue( _T("user:") );
      return TRUE;
}

LPCTSTR GetRemoteMachineName()
{
      DWORD dwIndex = 0;
      LPCTSTR lpszMachine = GetNthParameter( 1, dwIndex );    
      if ( lpszMachine == NULL )
            // return NULL;
            return lpszLocalIP;    
      if ( _tcsnicmp( lpszMachine, _T(" "), 2 ) == 0 )
            return lpszLocalIP;    
      if ( _tcsnicmp( lpszMachine, _T("\\\\"), 2 ) == 0 )
            return lpszMachine;
      // If a dot is entered we take it as localhost
      if ( _tcsnicmp( lpszMachine, _T("."), 2 ) == 0 )
            return lpszLocalIP;    
      return NULL;
}    

// Establish Connection to Remote Machine
BOOL EstablishConnection( LPCTSTR lpszRemote, LPCTSTR lpszResource, BOOL bEstablish )
{
      TCHAR szRemoteResource[_MAX_PATH];
      DWORD rc;
      _stprintf( szRemoteResource, _T("%s\\%s"), lpszRemote, lpszResource );
      NETRESOURCE nr;
      nr.dwType = RESOURCETYPE_ANY;
      nr.lpLocalName = NULL;
      nr.lpRemoteName = (LPTSTR)&szRemoteResource;
      nr.lpProvider = NULL;

      //Establish connection (using username/pwd)
      rc = WNetAddConnection2( &nr, lpszPassword, lpszUser, FALSE );
      if ( rc == NO_ERROR ) 
            return TRUE; // indicate success
      return FALSE;
}
BOOL CopyBinaryToRemoteSystem()
{
      TCHAR drive[_MAX_DRIVE];
      TCHAR dir[_MAX_DIR];
      TCHAR fname[_MAX_FNAME];
      TCHAR ext[_MAX_EXT];
      TCHAR szRemoteResource[_MAX_PATH];

      // Gets the file name and extension
      _tsplitpath( lpszCommandExe, drive, dir, fname, ext );    
      _stprintf( szRemoteResource, _T("%s\\ADMIN$\\System32\\%s%s"), lpszMachine, fname, ext );    
      // Copy the Command's exe file to \\remote\ADMIN$\System32
      return CopyFile( lpszCommandExe, szRemoteResource, FALSE );
}

int _tmain( DWORD, TCHAR**, TCHAR** )
{
      int   rc = 0;
      DWORD dwIndex = 0;

      lpszMachine    = GetRemoteMachineName();
      lpszCommandExe = GetNthParameter( 2, dwIndex );
      SetConnectionCredentials();

      if ( !EstablishConnection( lpszMachine, _T("ADMIN$"), TRUE ) )
      {
            rc = -2;
      }
      if ( !CopyBinaryToRemoteSystem())
      {
      }
      return 0;
}

关于c++ - 使用 WINRM 在远程主机上推送二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4274478/

相关文章:

c++ - Bison:$$ 和 $1 指向相同的内存位置(两种指针类型的 union YYSTYPE)

Javascript 对象,在 C 中制作类似

c - 程序异常终止 Turbo C

windows - 从 MAC 地址获取 IP。 arp -a 不显示设备

c++ - 10038 套接字错误

c++ - 简单的客户端/服务器 TCP c++

c++ - 如果 RTC 失败,则在计时器上运行 Arduino 开发板

c++ - wglGetProcAddress 返回 NULL

linux - 从 Windows 和 Eclipse 连接到 gitolite 服务器

linux - 使用 GUI 开发程序