另一套备用的代码,通过service调用和登陆用户交互
结合这两个东东,应该差不多可以搞定我需要的事情啦。
因为更改不了service代码,所以希望从service调用的代码里,争脱出来,和本地登陆用户进行交互启动。
#include <windows.h>
#include <stdio.h>
#include <process.h>
#include <Tlhelp32.h>
#include <tchar.h>
#include <psapi.h>
#include <stdio.h>
#include <STDLIB.H>
#include <tlhelp32.h>
#include <WtsApi32.h>
#include <windows.h>
#include <stdio.h>
#include <Userenv.h>
#include <Wtsapi32.h>
#pragma comment(lib, "WtsApi32.lib")
#pragma comment (lib,"psapi")
#pragma comment (lib,"user32.lib")
#pragma comment (lib,"advapi32.lib")
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"ws2_32.lib")
//Function to run a process as active user from windows service
void ImpersonateActiveUserAndRun()
{
DWORD session_id = -1;
DWORD session_count = 0;
WTS_SESSION_INFOA *pSession = NULL;
if (WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pSession, &session_count))
{
printf("pSession=====%d\n", pSession);
printf("session_count=====%d\n", session_count);
}
else
{
printf("WTSEnumerateSessions ===============failed \n");
return;
}
for (DWORD i = 0; i < session_count; i++)
{
session_id = pSession[i].SessionId;
printf("session_id=====%d\n", session_id);
WTS_CONNECTSTATE_CLASS wts_connect_state = WTSDisconnected;
WTS_CONNECTSTATE_CLASS* ptr_wts_connect_state = NULL;
DWORD bytes_returned = 0;
if (::WTSQuerySessionInformation(
WTS_CURRENT_SERVER_HANDLE,
session_id,
WTSConnectState,
reinterpret_cast<LPTSTR*>(&ptr_wts_connect_state),
&bytes_returned))
{
wts_connect_state = *ptr_wts_connect_state;
::WTSFreeMemory(ptr_wts_connect_state);
printf("wts_connect_state=====%d\n", wts_connect_state);
// if (wts_connect_state != WTSActive) continue;
}
else
{
printf("WTSQuerySessionInformation ===============failed \n");
continue;
}
HANDLE hImpersonationToken = 0;
BOOL bRet = WTSQueryUserToken(session_id, &hImpersonationToken);
if (bRet == false)
{
printf(" WTSQueryUserToken ERROR: %d\n", GetLastError());
}
printf("hImpersonationToken=====%d\n", hImpersonationToken);
//Get real token from impersonation token
DWORD neededSize1 = 0;
HANDLE *realToken = new HANDLE;
if (GetTokenInformation(hImpersonationToken, (::TOKEN_INFORMATION_CLASS) TokenLinkedToken, realToken, sizeof(HANDLE), &neededSize1))
{
CloseHandle(hImpersonationToken);
hImpersonationToken = *realToken;
}
else
{
//log error
continue;
}
}
}
bool GetSessionToken(DWORD dwSessionId, char token[256])
{
LPTSTR pBuffer = NULL;
DWORD dwBufferLen;
BOOL bRes = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, dwSessionId, WTSOEMId, &pBuffer, &dwBufferLen);
if (bRes == FALSE)
{
return false;
}
lstrcpy(token ,pBuffer);
WTSFreeMemory(pBuffer);
return true;
}
bool GetSessionUserName(DWORD dwSessionId, char username[256])
{
LPTSTR pBuffer = NULL;
DWORD dwBufferLen;
BOOL bRes = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, dwSessionId, WTSUserName, &pBuffer, &dwBufferLen);
if (bRes == FALSE)
{
return false;
}
lstrcpy(username ,pBuffer);
WTSFreeMemory(pBuffer);
return true;
}
bool GetSessionDomain(DWORD dwSessionId, char domain[256])
{
LPTSTR pBuffer = NULL;
DWORD dwBufferLen;
BOOL bRes = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, dwSessionId, WTSDomainName, &pBuffer, &dwBufferLen);
if (bRes == FALSE)
{
printf("WTSQuerySessionInformation Fail!\n");
return false;
}
lstrcpy(domain,pBuffer);
WTSFreeMemory(pBuffer);
return true;
}
HANDLE GetProcessHandle(LPSTR szExeName) //遍历进程PID
{
PROCESSENTRY32 Pc = { sizeof(PROCESSENTRY32) };
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
if(Process32First(hSnapshot, &Pc))
{
do
{
if(!_stricmp(Pc.szExeFile, szExeName))
{ //返回explorer.exe进程的PID
printf("explorer's PID=%d\n",Pc.th32ProcessID);
return OpenProcess(PROCESS_ALL_ACCESS, TRUE, Pc.th32ProcessID);
}
}while(Process32Next(hSnapshot, &Pc));
}
return NULL;
}
void Usage(void)
{
fprintf(stderr,"==============================\n"
"\tname:run programe at any session ,need system permission\n"
"\tsession 1 D:\\callsession\\bin\\callsession.exe \n"
"==================================\n");
}
int main(int argc, char **argv)
{
ImpersonateActiveUserAndRun();
//遍历所有的session
// 函数的句柄
HMODULE hInstKernel32 = NULL;
HMODULE hInstWtsapi32 = NULL;
typedef DWORD (WINAPI *WTSGetActiveConsoleSessionIdPROC)();
WTSGetActiveConsoleSessionIdPROC WTSGetActiveConsoleSessionId = NULL;
hInstKernel32 = LoadLibrary("Kernel32.dll");
if (!hInstKernel32)
{
return FALSE;
}
WTSGetActiveConsoleSessionId = (WTSGetActiveConsoleSessionIdPROC)GetProcAddress(hInstKernel32,"WTSGetActiveConsoleSessionId");
printf("WTSGetActiveConsoleSessionId=====%d\n", WTSGetActiveConsoleSessionId);
if (!WTSGetActiveConsoleSessionId)
{
return FALSE;
}
// WTSQueryUserToken 函数,通过会话ID得到令牌
typedef BOOL (WINAPI *WTSQueryUserTokenPROC)(ULONG SessionId, PHANDLE phToken );
WTSQueryUserTokenPROC WTSQueryUserToken = NULL;
hInstWtsapi32 = LoadLibrary("Wtsapi32.dll");
printf("WTSQueryUserToken=====%d\n", WTSQueryUserToken);
printf("hInstWtsapi32=====%d\n", hInstWtsapi32);
if (!hInstWtsapi32)
{
return FALSE;
}
WTSQueryUserToken = (WTSQueryUserTokenPROC)GetProcAddress(hInstWtsapi32,"WTSQueryUserToken");
printf("WTSQueryUserToken=====%d\n", WTSQueryUserToken);
if (!WTSQueryUserToken)
{
return FALSE;
}
//遍历3389登录的session:
/*
typedef struct _WTS_SESSION_INFO {
DWORD SessionId;
LPTSTR pWinStationName;
WTS_CONNECTSTATE_CLASS State;
}WTS_SESSION_INFO, *PWTS_SESSION_INFO;
*/
WTS_SESSION_INFO *sessionInfo = NULL;
DWORD sessionInfoCount;
char domain1[256];
char username1[256];
char token1[256];
BOOL result = WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &sessionInfo, &sessionInfoCount);
unsigned int userCount(0);
int num=0;
for(unsigned int i = 0; i < sessionInfoCount; ++i)
{
if( (sessionInfo[i].State == WTSActive) && (sessionInfo[i].State != WTSDisconnected) )
{
printf("session %d information:\n",num++);
printf("\tsessionInfo.SessionId=%d\n",sessionInfo[i].SessionId);
GetSessionDomain(sessionInfo[i].SessionId, domain1);
printf("\tSession Domain = %s\n",domain1);
GetSessionUserName(sessionInfo[i].SessionId,username1);
printf("\tSession user's name = %s\n",username1);
GetSessionToken(sessionInfo[i].SessionId,token1);
printf("\tSession user's token = %s\n",token1);
HANDLE hImpersonationToken;
if (!WTSQueryUserToken(sessionInfo[i].SessionId, &hImpersonationToken))
{
printf("\thImpersonationToken failed\n");
continue;
}
DWORD neededSize1 = 0;
HANDLE *realToken = new HANDLE;
if (GetTokenInformation(hImpersonationToken, (::TOKEN_INFORMATION_CLASS) TokenLinkedToken, realToken, sizeof(HANDLE), &neededSize1))
{
CloseHandle(hImpersonationToken);
hImpersonationToken = *realToken;
printf("\thImpersonationToken token = %s\n",hImpersonationToken);
}
else
{
printf("\tGetTokenInformation failed\n");
continue;
}
userCount++;
}
}
printf("session's number:%d\n\n",userCount);
WTSFreeMemory(sessionInfo); //释放
if(argc==1)
{
Usage();
}
else if(argc==3) //session 1 c:\win2003\temp\klog.exe
{
// 得到当前登录用户的令
HANDLE hTokenThis = NULL;
HANDLE hTokenDup = NULL;
HMODULE hInstKernel32 = NULL;
HMODULE hInstWtsapi32 = NULL;
BOOL bRes;
DWORD dwSessionId = NULL;
/*
bRes = WTSQueryUserToken(dwSessionId, &hTokenDup);
printf("WTSQueryUserToken %d\n",bRes);
if (!bRes)
{
printf("WTSQueryUserToken Failed!%d\n",GetLastError());
return FALSE;
}
bRes = ImpersonateLoggedOnUser(hTokenDup);
if (!bRes)
{
printf("ImpersonateLoggedOnUser!%d\n",GetLastError());
return FALSE;
}
MessageBox(NULL,"test2","test1",MB_OK);
system("winver.exe");
*/
// HANDLE hThisProcess = GetCurrentProcess(); // 获取当前进程句柄
HANDLE hThisProcess = GetProcessHandle("explorer.exe");
if(hThisProcess == NULL)
return 0;
printf("hThisProcess %d\n",hThisProcess);
// 打开当前进程令牌
OpenProcessToken(hThisProcess, TOKEN_ALL_ACCESS, &hTokenThis);
// 复制一个进程令牌,目的是为了修改session id属性,以便在其它session中创建进程
DuplicateTokenEx(hTokenThis, TOKEN_ALL_ACCESS,NULL, SecurityIdentification, TokenPrimary, &hTokenDup);
//获取活动session id,这里要注意,如果服务器还没有被登录而使用了远程桌面,这样用是可以的,如果有多个session存在,
//不能简单使用此函数,需要枚举所有session并确定你需要的一个,或者干脆使用循环,针对每个session都执行后面的代码
dwSessionId=atoi(argv[1]); //与会话进行连接
bRes = SetTokenInformation(hTokenDup, TokenSessionId, &dwSessionId, sizeof(DWORD)); //把session id设置到备份的令牌中
printf("hTokenThis: %d\n",hTokenThis);
printf("hTokenDup: %d\n",hTokenDup);
printf("dwSessionId: %d\n",dwSessionId);
printf("TokenSessionId==%d\n",TokenSessionId);
if (!bRes)
{
printf("SetTokenInformation!%d\n",GetLastError());
return FALSE;
}
// 好了,现在要用新的令牌来创建一个服务进程。注意:是“服务”进程!如果需要以用户身份运行,必须在前面执行LogonUser来获取用户令牌
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(STARTUPINFO));
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
si.cb = sizeof(STARTUPINFO);
si.lpDesktop = "WinSta0\\Default";
LPVOID pEnv = NULL;
DWORD dwCreationFlag = NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE; // 注意标志
//CreateEnvironmentBlock(&pEnv, hTokenDup, FALSE); // 创建环境块
// 创建新的进程,这个进程就是你要弹出窗口的进程,它将工作在新的session中
char path[MAX_PATH];
lstrcpy(path,argv[2]);
CreateProcessAsUser(hTokenDup, NULL, (char *)path, NULL, NULL, FALSE, dwCreationFlag, pEnv, NULL, &si, &pi);
}
else
{
printf("argc value is : %d", argc);
Usage();
}
return 0;
}
另一套备用的代码,通过service调用和登陆用户交互的更多相关文章
- 遇到很多次,要注意区分service调用,本地用户调用这些区别
WTSQueryUserToken返回1314 The WTSQueryUserToken function obtains the primary access token of the log ...
- 项目笔记---Windows Service调用Windows API问题
概要 此文来自于最近一个“诡异”的Windows API调用发现Windows Service在调用某些Windows API的过程中失效,在经过漫长的Baidu,之后终于在StackOverFlow ...
- Android 使用代码主动去调用控件的点击事件(模拟人手去触摸控件)
使用代码主动去调用控件的点击事件(模拟人手去触摸控件) //View 可以是LinearLayout,Button,TextView View.performClick();
- 基于CkEditor实现.net在线开发之路(2)编写C#代码,怎么调用它。
上一章简约的介绍了CkEditor编辑器,可以编辑js逻辑代码,css,html,C#代码,这章我根据实际例子,讲解怎么编写C#代码和怎么调用它. 大家都还记得刚刚接触程序编时的hello Word吧 ...
- Reporting Service 没有权限登陆
在配置好Reporting Service之后,登陆Report Mananger( http://localhost/Reports/Pages/Folder.aspx)出现一个异常,本地用户没有权 ...
- MSIL 教程(二):数组、分支、循环、使用不安全代码和如何调用Win32 API(转)
转自:http://www.cnblogs.com/Yahong111/archive/2007/08/16/857574.html 续上文[翻译]MSIL 教程(一) ,本文继续讲解数组.分支.循环 ...
- 用户交互与while循环<代码>
#用户交互1 age_oldboy = 56 guess_age = int(input(">>:")) if guess_age == age_oldboy: pri ...
- C# 脚本代码自动登录淘宝获取用户信息
C# 脚本代码自动登录淘宝获取用户信息 最近遇到的一个需求是如何让程序自动登录淘宝, 获取用户名称等信息. 其实这个利用SS (SpiderStudio的简称) 实现起来非常简单. 十数行代码就可 ...
- java 为什么wait(),notify(),notifyAll()必须在同步(Synchronized)方法/代码块中调用?
wait()作用:该方法用来将当前线程置入休眠状态,直到接到通知或被中断为止.条件:在调用wait()之前,线程必须要获得该对象的对象级别锁,即只能在同步方法或同步块中调用wait()方法.进入wai ...
随机推荐
- 递推DP Codeforces Round #260 (Div. 1) A. Boredom
题目传送门 /* DP:从1到最大值,dp[i][1/0] 选或不选,递推更新最大值 */ #include <cstdio> #include <algorithm> #in ...
- RHEL5.6更新yum源
RHEL5.6更新yum源记录,2017年2月20日 root用户切换目录至:/etc/yum.repos.d/ [root@localhost yum.repos.d]# pwd /etc/yum. ...
- VS2013使用单元测试
一.开发环境 开发工具:VS2013 二.开发流程 1.添加一个控制台项目UnitDemo namespace UnitDemo { public class Program { static voi ...
- linux小白成长之路5————安装Docker
1.安装docker 命令: yum -y install docker   2.启动docker 命令: systemctl start docker.service 3.查看docker版本 ...
- 微信小程序一些常见的坑
1.小程序都报wxss编译错误 解决方法: 在控制台输入openVendor() ,清除里面的wcsc wcsc.exe 然后重启工具 2.微信小程序wx:for警告 Now you can prov ...
- Jmeter接口测试---JDBC简单实践
我的环境:MySQL:mysql-5.6.24-win32 jdbc驱动:mysql-connector-java-5.1.22-bin.jar JMeter:apache-jmeter-2.13 1 ...
- poi导出word时设置兼容性
接上一篇poi导出word http://www.cnblogs.com/xiufengd/p/4708680.html. public static void setAuto(XWPFDocumen ...
- 【笔记JS/HTML/CSS】用div实现个性化button,背景半透明
html中的button默认样式..不太能看,如果调一调背景色和字体的话也挺适合简洁的页面设计 于是决定配合JS,用html中的div完成button 最终结果图: html代码:(first_pas ...
- Javascript 原型链与constructor
Javascript中的constructor与prototype 在学习javascript面向对象编程的过程中, constructor和prototype一直让我觉得理解不透,慢慢的学习过程中记 ...
- 洛谷——P3389 【模板】高斯消元法
P3389 [模板]高斯消元法 以下内容都可省略,直接转大佬博客%%% 高斯消元总结 只会背板子的蒟蒻,高斯消元是什么,不知道诶,看到大佬们都会了这个水题,蒟蒻只好也来切一切 高斯消元最大用途就是解多 ...