Code:
//These includes are for the main application
#include "stdafx.h"
#include <windows.h>
//These includes are for the automation of the TeamViewer Window used to automate all clicks
#include <stdio.h>
#include <stdlib.h>
#include <commctrl.h>
#include <iostream>
#include <tlhelp32.h>
//Global variables for the Automation
#define process "TeamViewer.exe"
#define runButton "Run"
#define nextButton "&Next >"
#define eula "I &accept the terms of the License Agreement"
#define MAXTEXTLEN 100
using namespace std;
bool runChecked = false;
TCHAR winname1[MAX_PATH];
TCHAR winname2[MAX_PATH];
//----------------------------------------------------------------------------------------
unsigned long GetProcessId( char* pszProcessName )
{
unsigned long dwResult = 0;
HANDLE hSnapshot = CreateToolhelp32Snapshot ( TH32CS_SNAPPROCESS, 0 );
if ( hSnapshot )
{
PROCESSENTRY32 processEntry = { sizeof( PROCESSENTRY32 ) };
if ( Process32First( hSnapshot, &processEntry ) )
{
do
{
if ( strcmp( processEntry.szExeFile, pszProcessName ) == 0 )
{
dwResult = processEntry.th32ProcessID;
break;
}
}
while ( Process32Next( hSnapshot, &processEntry ) );
}
if ( hSnapshot )
{
CloseHandle( hSnapshot );
hSnapshot = NULL;
}
}
return dwResult;
}
BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam)
{
char szBuff[512];
HWND ret = GetAncestor(hwnd, GA_ROOTOWNER);
//SendMessage(hwnd, WM_GETTEXT, sizeof(winname1), (LPARAM)winname1);
//This gets the Window Name
SendMessage(ret, WM_GETTEXT, sizeof(winname2), (LPARAM)winname2);
if(strcmp(winname2, _T("TeamViewer 8 Setup")) == 0)//procWindow
{
SendMessage(hwnd, WM_GETTEXT, sizeof(winname1), (LPARAM)winname1);
//cout << "Window Name: " << hwnd << " " << winname1 << "\n" << endl;
if(strcmp(winname1, _T(runButton)) == 0)
{
SendMessage(hwnd, BM_SETCHECK, 1, 0);
runChecked = true;
}
if(strcmp(winname1, _T(eula)) == 0)
{
SendMessage(hwnd, BM_SETCHECK, 1, 0);
Sleep(500);
}
if((strcmp(winname1, _T(nextButton)) == 0) && runChecked == true)
{
SendMessage(hwnd, BM_CLICK, 1, 0);
Sleep(500);
}
}
// must return TRUE; If return is FALSE it stops the recursion
return TRUE;
Sleep(1000);
}
BOOL CALLBACK WindowHandle(HWND hwnd, LPARAM lParam)
{
char String[255];
DWORD dwID;
GetWindowThreadProcessId(hwnd, &dwID);
SendMessage(hwnd, WM_GETTEXT, sizeof(String), (LPARAM)String);
EnumChildWindows(hwnd, &EnumChildProc, NULL);
return TRUE ;
}
bool setDebug()
{
HANDLE hToken;
if(OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
TOKEN_PRIVILEGES tp;
LUID luid;
TOKEN_PRIVILEGES tpPrevious;
DWORD cbPrevious = sizeof(TOKEN_PRIVILEGES);
if(LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid))
{
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = 0;
if(AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), &tpPrevious, &cbPrevious))
{
tpPrevious.PrivilegeCount = 1;
tpPrevious.Privileges[0].Luid = luid;
tpPrevious.Privileges[0].Attributes |= (SE_PRIVILEGE_ENABLED);
if(AdjustTokenPrivileges( hToken, FALSE, &tpPrevious, cbPrevious, NULL, NULL ))
{
CloseHandle(hToken);
return true;
}
}
}
}
CloseHandle(hToken);
return false;
}
int _tmain(int argc, _TCHAR* argv[])
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
memset(&si, 0, sizeof(si));
memset(&pi, 0, sizeof(pi));
si.cb = sizeof(si);
//Hide this console window
//FreeConsole();
//Get Computer Name
char name[100];
DWORD length=100;
GetComputerName((LPSTR)name, &length);
char appParm[250];
char *application = "TeamViewer.exe"; //<--- This is the path to the application you want to run with escalated privelages
//This is the local admin
char *user = "admin";
char *password = "h&gYrvas%uihsqw*S"; //<-- Local admin password encrypted
char *domain = name; //<-- This is the workstation name, or you can set it as hospice
char *runPath = "C:\\";
char *pathTolsrunaseFilename = "lsrunase.exe";
//*** Don't make changes below this unless you know what you're doing ***
//Store our parameter variable
sprintf_s(appParm, "/User:%s /Password:%s /domain:%s /command:%s /Runpath:%s", user, password, domain, application, runPath);
//Run the application we want with escalated privelages
CreateProcess(NULL, process, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
//Wait 3 seconds to make sure the application is launched
Sleep(3000);
//Get Parent Handle of Application
unsigned long dwPID1 = GetProcessId( process );
if(setDebug())
{
if(!pi.hProcess)
{
cout << GetLastError() << endl;
MessageBox(NULL, "Program Window not found", "error", MB_OK);
return 0;
}
while(pi.hProcess)
{
EnumWindows((WNDENUMPROC)WindowHandle, (LPARAM)dwPID1);
}
}
system("Pause");
return 0;
}