1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
#define OS_ERROR -1
#define OS_WIN32_WINDOWS_XP 6
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <userenv.h>
using namespace std;
static LPWSTR msg_error = L"";
LRESULT CALLBACK WndProcErreur(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HINSTANCE hInstanceErr;
static HWND hrlblErr;
static HBRUSH hbrBackgroundErr;
switch (message)
{
case WM_CREATE:
hInstanceErr = ((LPCREATESTRUCT)lParam)->hInstance;
hrlblErr = CreateWindow(L"STATIC", msg_error, WS_CHILD | WS_VISIBLE, 10, 10, 280, 80,
hwnd, (HMENU)1, hInstanceErr, NULL);
CreateWindow(L"BUTTON", L"OK", WS_CHILD | WS_VISIBLE | WS_TABSTOP, 100, 90, 100, 24,
hwnd, (HMENU)2, hInstanceErr, NULL);
hbrBackgroundErr = GetSysColorBrush(COLOR_3DFACE);
// chfont(50, hrlblErr);
break;
case WM_CTLCOLORDLG:
return (LONG)hbrBackgroundErr;
case WM_CTLCOLORSTATIC:
{
HDC hdcStatic = (HDC)wParam;
SetBkMode(hdcStatic, TRANSPARENT);
return (LONG)hbrBackgroundErr;
}
case WM_COMMAND:
/***************************************************\
* LOWORD(wParam) = ID du contrôle ou du menu *
* HIWORD(wParam) = Raison du message (notification) *
\***************************************************/
switch(LOWORD(wParam))
{
case 2:
switch(HIWORD(wParam))
{
case BN_CLICKED:
PostQuitMessage(0);
break;
default:
break;
}
break;
default:
break;
}
break; /* case WM_COMMAND */
case WM_DESTROY:
// PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
void fenetreErreur()
{
WNDCLASS wc;
HWND hWnd;
MSG msg;
//----- Réccupération du 'hInstance' de la fenetre -----
char title[500]; // to hold title
// get title of console window
GetConsoleTitleA( title, 500 );
// get HWND of console, based on its title
HWND hwndConsole = FindWindowA( NULL, title );
// get HINSTANCE of console, based on HWND
HINSTANCE hInstanceErr = (HINSTANCE)GetWindowLong(hwndConsole, GWL_HINSTANCE);
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hInstance = hInstanceErr;
wc.lpfnWndProc = WndProcErreur;
wc.lpszClassName = L"Classe 2";
wc.lpszMenuName = NULL;
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wc);
hWnd = CreateWindow(L"Classe 2", /* Classe de la fenêtre */
L"Erreur", /* Titre de la fenêtre */
WS_OVERLAPPEDWINDOW | WS_VISIBLE, /* Style de la fenêtre */
100, /* Abscisse du coin supérieur gauche */
100, /* Ordonnée du coin supérieur gauche */
300, /* Largeur de la fenêtre */
160, /* Hauteur de la fenêtre */
NULL, /* Fenêtre parent */
NULL, /* Menu */
hInstanceErr, // <Instance de notre application>
NULL /* Paramètres additionnels */
);
ShowWindow(hWnd, SW_SHOW);
while (GetMessage(&msg, NULL, 0, 0))
{
if (!IsDialogMessage(hWnd, &msg))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&msg);
/* Send message to WindowProcedure */
DispatchMessage(&msg);
}
}
DestroyWindow(hWnd);
}
void DisplayError(LPWSTR pszAPI)
{
LPVOID lpvMessageBuffer;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&lpvMessageBuffer, 0, NULL);
//
//... now display this string
//
wprintf(L"ERROR: API = %s.\n", pszAPI);
wprintf(L" error code = %d.\n", GetLastError());
wprintf(L" message = %s.\n", (LPWSTR)lpvMessageBuffer);
msg_error = (LPWSTR)lpvMessageBuffer;
fenetreErreur();
//
// Free the buffer allocated by the system
//
LocalFree(lpvMessageBuffer);
ExitProcess(GetLastError());
}
int runAsUser(LPCWSTR user, LPCWSTR domain, LPCWSTR password)
{
DWORD dwSize;
HANDLE hToken;
LPVOID lpvEnv;
TCHAR buffer[256] = TEXT("");
PROCESS_INFORMATION pi = {0};
STARTUPINFO si = {0};
WCHAR szUserProfile[256] = L"";
TCHAR commande[200] = TEXT("");
TCHAR part[55] = TEXT("\\Desktop\\Domainjoin\\netdom7.exe C:\\Windows\\netdom.exe\"");
TCHAR part1[56] = TEXT("\\Desktop\\Domainjoin\\netdomXP.exe C:\\Windows\\netdom.exe\"");
if (!GetUserName(buffer, &dwSize))
{
_tprintf(TEXT("GetUserName failed (%d)\n"), GetLastError());
return 1;
}
OSVERSIONINFOEX osvi;
BOOL bOsVersionInfoEx;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
if(!(bOsVersionInfoEx=GetVersionEx((OSVERSIONINFO *)&osvi)))
{
osvi.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
if (!GetVersionEx((OSVERSIONINFO *)&osvi) )
return OS_ERROR;
}
else if(osvi.dwMajorVersion==5 && osvi.dwMinorVersion==1 && osvi.dwPlatformId==VER_PLATFORM_WIN32_NT)
{
wcscat_s(commande, TEXT("cmd.exe /C \"move /-y C:\\Documents and Settings\\"));
wcscat_s(commande, buffer);
wcscat_s(commande, part1);
}
else
{
wcscat_s(commande, TEXT("cmd.exe /C \"move /-y C:\\Users\\"));
wcscat_s(commande, buffer);
wcscat_s(commande, part);
}
_tprintf(TEXT("%s"), commande);
// Se connecter en tant qu'admin
if (!LogonUser(user, domain, password, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, &hToken))
{
if(GetLastError() == 1326)
{
return 1326;
}
else
{
DisplayError(L"LogonUser");
}
}
// On crée son environnement
if (!CreateEnvironmentBlock(&lpvEnv, hToken, TRUE))
DisplayError(L"CreateEnvironmentBlock");
dwSize = sizeof(szUserProfile)/sizeof(WCHAR);
if (!GetUserProfileDirectory(hToken, szUserProfile, &dwSize))
DisplayError(L"GetUserProfileDirectory");
// On crée le processus qui va effectuer la commande
if (!CreateProcessWithLogonW(user, domain, password, LOGON_WITH_PROFILE, NULL, commande, CREATE_UNICODE_ENVIRONMENT, lpvEnv, szUserProfile, &si, &pi))
{
if(GetLastError() == 1326)
return 1326;
else
DisplayError(L"CreateProcessWithLogonW");
}
if (!DestroyEnvironmentBlock(lpvEnv))
DisplayError(L"DestroyEnvironmentBlock");
CloseHandle(hToken);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}
void _tmain(void)
{
runAsUser(TEXT("blabla#"), TEXT("."), TEXT("bloblo"));
} |
Partager