如何从服务访问通过RDP映射的磁盘?

时间:2012-04-07 21:36:11

标签: windows rdp unc

RDP具有将来自远程用户工作站的磁盘导出到RDP服务器的出色能力。据我所知,它不仅仅是一个Windows资源管理器技巧,但各种程序都可以使用快捷方式“X:”,“Y:”,“Z:”等... RDP磁盘复制上的stack overflow entry提到指向RDP用户的计算机的'\ tsclient'别名。现在,很明显,如果有2个或更多用户,他们将拥有自己的,没有冲突的tsclient目的地。

所以,我的问题是:如何从服务获取所有远程用户共享资源(磁盘)的列表并访问它?理想情况下,如果说2个用户已经连接并共享了他们的C:驱动器,我会得到如下列表:

  • \ UserJohnDoe \ VolumeXyzC - John's C drive
  • \ UserJaneRoe \ VolumeXyzC - Jane的C盘

帮助表示感谢! UPDATE:

这是一段有效的代码(gist snippet

// rdpjoker by Konrads

#include "stdafx.h"
#define SERVER "XXX.compute-1.amazonaws.com"
#define CMD  "cmd.exe /C dir \\tsclient\\c >output.txt"

int main(int argc, char **argv){
    HANDLE server;
    PWTS_SESSION_INFOA ppSessionInfo=NULL;
    WTS_SESSION_INFOA pSessionInfo;
    DWORD pCount;
    DWORD pLevel=1;
    DWORD i=0;
    LPSTR ppBuffer;
    DWORD bytesReturned;    
    HANDLE userToken=NULL;
    HANDLE pUserToken=NULL;
    ULONG sessionid;
    DWORD dwCreationFlags=0;
    LPVOID environment=NULL;
    STARTUPINFOA si;
    PROCESS_INFORMATION pi;
    char *cmdline;
    char *username;
    char *homedir;//[MAX_PATH];
    char desktop[8192];

    server=WTSOpenServerA(WTS_CURRENT_SERVER_NAME);
    if(argc>2){
        sessionid=atol(argv[1]);
        printf("[*] Impersonating session: %i\n",sessionid);
        if(WTSQueryUserToken(sessionid,&userToken)){
            //if(DuplicateTokenEx(userToken,MAXIMUM_ALLOWED,NULL,SecurityIdentification,TokenPrimary,&pUserToken)){
                if(CreateEnvironmentBlock(&environment,pUserToken,FALSE)){
                    ZeroMemory( &si, sizeof( STARTUPINFO ) );
                    //WTSQuerySessionInformationA(server,sessionid,WTSWinStationName,&ppBuffer,&bytesReturned);
                    //sprintf_s(desktop,8192,"%s\\default",ppBuffer);
                    si.lpDesktop = "winsta0\\default";;
                    si.cb=sizeof(STARTUPINFO);
                    //WTSFreeMemory(ppBuffer);
                    ZeroMemory( &pi,sizeof(pi));
                    cmdline=(char *)malloc(MAX_PATH +1);
                    //GetUserProfileDirectoryA(userToken,homedir,&bytesReturned);
                    //WTSUserConfigTerminalServerProfilePath
                    //WTSQuerySessionInformationA(server,sessionid,WTSUserName,&ppBuffer,&bytesReturned);   
                    WTSQuerySessionInformationA(server,sessionid,WTSUserName,&ppBuffer,&bytesReturned);

                    username=_strdup(ppBuffer);
                    WTSFreeMemory(ppBuffer);
                    //WTSQueryUserConfigA(WTS_CURRENT_SERVER_NAME,username,WTSUserConfigTerminalServerProfilePath,&ppBuffer,&bytesReturned);
                    homedir=(char *)malloc(MAX_PATH);
                    sprintf_s(homedir,MAX_PATH,"C:\\Users\\%s\\",username);
                    //homedir=_strdup(ppBuffer);
                    //WTSFreeMemory(ppBuffer);
                    printf("[D] homedir: %s\n",homedir);
                    sprintf_s(cmdline,MAX_PATH,"cmd.exe /C dir %s >output.txt",argv[2]);
                    dwCreationFlags|= CREATE_UNICODE_ENVIRONMENT | NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE;

                    //WTSQuerySessionInformationA(server,sessionid,WTSWinStationName,&ppBuffer,&bytesReturned);
                    //printf("station: %s",ppBuffer);


                    if(CreateProcessAsUserA(userToken,
                        NULL,
                        cmdline,
                        NULL,
                        NULL,
                        FALSE,
                        dwCreationFlags,
                        environment,
                        homedir,
                        &si,
                        &pi)){
                            printf("[*]CreateProcessAsUserA succeeded! pid:%i, tid:%i\n",pi.dwProcessId,pi.dwProcessId);
                    }else{
                        printf("[E] CreateProcessAsUserA failed: %i\n", GetLastError());
                    }


                //}else{
                    //printf("[E] CreateEnvironmentBlock failed: %i\n", GetLastError());
        //      }
        }else{
                    printf("[E] DuplicateTokenEx failed: %i\n", GetLastError());
                }


        }
        else{
            printf("[E] WTSQueryUserToken failed: %i\n", GetLastError());
            exit(-1);
        }
    }

    else{ // no arguments specified
        if(WTSEnumerateSessionsA(server,0,1,&ppSessionInfo,&pCount)){

            //  printf("pCount: %i,",pCount);
            for (i=0;i<pCount;++i){
                //  printf("i = %i\n",i);
                pSessionInfo=ppSessionInfo[i];
                printf("Session ID: %i; name: %s, ",pSessionInfo.SessionId,pSessionInfo.pWinStationName);
                if(WTSQuerySessionInformationA(server,pSessionInfo.SessionId,WTSUserName,&ppBuffer,&bytesReturned)){
                    printf("user: %s, ",ppBuffer);
                    WTSFreeMemory(ppBuffer);
                }else{
                    printf("WTSQuerySessionInformation[WTSUserName] failed: %i\n", GetLastError());
                }
                if(WTSQuerySessionInformationA(server,pSessionInfo.SessionId,WTSWinStationName,&ppBuffer,&bytesReturned)){
                    printf("station: %s",ppBuffer);
                    WTSFreeMemory(ppBuffer);
                }else{
                    printf("WTSQuerySessionInformation[WTSWinStationName] failed: %i\n", GetLastError());
                }
                printf("\n");
            }
            WTSFreeMemory(ppSessionInfo);
        }else //0014fb3c
        {
            printf("EnumerateSessions failed: %i\n", GetLastError());
        }
    }
}

1 个答案:

答案 0 :(得分:2)

我认为您可以使用CreateProcessAsUser实现此目的,但您需要检索用户会话的令牌,您可以通过WTSQueryUserToken执行此操作。