WinAsm Studio, The Assembly IDE - Free Downloads, Source Code
Articles
Programming Quick Start
32-bit Assembler is Easy
Porting Iczelion tutorials
What is FASM
Hard Drive Recovery
Wiring your own LAN
 
Forum
 

Standalone version of the "Run as" function

Slider
Quote Post


Extremely Active Member
******

Group: Moderators
Posts: 313
Member No.: 5004
Joined: 11-May 07


Japheth wrote this.

I thought someone might be interested in the code.

Best regards,
Andy

CODE


; RunAs.asm By Japheth Standalone version of the "Run as" function
;                      Thursday, January 24, 2013
;
.386
.model flat, stdcall
option casemap:none

.nolist
.nocref
_WIN32_WINNT equ 501h
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\shell32.inc
.list
.cref

; includelib <kernel32.lib>
;  includelib <advapi32.lib>
;  includelib <user32.lib>
;  includelib <shell32.lib>

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\advapi32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\shell32.lib

CreateProcessWithLogonW proto :ptr, :ptr, :ptr, :dword, :ptr, :ptr, :dword, :ptr, :ptr, :ptr, :ptr

STARTUPINFOW struct
cb DWORD ?
lpReserved LPWSTR ?
lpDesktop LPWSTR ?
lpTitle LPWSTR ?
dwX DWORD ?
dwY DWORD ?
dwXSize DWORD ?
dwYSize DWORD ?
dwXCountChars DWORD ?
dwYCountChars DWORD ?
dwFillAttribute DWORD ?
dwFlags DWORD ?
wShowWindow WORD ?
cbReserved2 WORD ?
lpReserved2 LPBYTE ?
hStdInput HANDLE ?
hStdOutput HANDLE ?
hStdError HANDLE ?
STARTUPINFOW ends

L macro parms:VARARG
local wstr
   wstr textequ <>
   for parm,<parms>
     ifidn <">,@SubStr(parm,1,1)
%       forc chr$, <@SubStr(parm,2,@SizeStr(parm)-2)>
         ifnb wstr
            wstr CatStr wstr,<,>
         endif
         wstr CatStr wstr,<'&chr$'>
       endm
     else
       ifnb wstr
          wstr CatStr wstr,<,>
       endif
       wstr CatStr wstr,<parm>
     endif
   endm
   exitm <wstr>
endm

ID_PASSWORD equ 100

CStr macro text:vararg
local xxx
.const
xxx db text,0
.code
exitm <offset xxx>
endm

_T macro text:vararg
local xxx
.const
xxx dw L(text),0
.code
exitm <offset xxx>
endm

.data

szPW dw 20 dup (0)

.const

;--- template for password dialog
pwdlg DLGTEMPLATE <DS_MODALFRAME or DS_CENTER or WS_POPUP or WS_CAPTION, 0, 2, 0, 0, 186, 28>
dw 0; no menu
dw 0; dialog class
dw L("Account "),0;caption
align 4
DLGITEMTEMPLATE <WS_CHILD or WS_VISIBLE, 0, 8, 9, 40, 10, -1>
dw -1, 0082h;0082h is "static" window class (see MSDN)
dw L("Password"),0;initial text
dw 0;creation data
align 4
DLGITEMTEMPLATE <WS_CHILD or WS_VISIBLE or WS_TABSTOP or ES_PASSWORD, WS_EX_CLIENTEDGE, 52, 8, 100, 10, ID_PASSWORD>
dw -1, 0081h;0081h is "Edit" window class (see MSDN)
dw 0;initial text
dw 0;creation data
align 4

.code

;--- dlgproc for password dialog
pwproc PROC hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

local szCaption[80]:WORD

mov eax, uMsg
.if ( eax == WM_INITDIALOG )
 invoke GetWindowTextW, hWnd, addr szCaption, lengthof szCaption
 invoke lstrcatW, addr szCaption, lParam
 invoke SetWindowTextW, hWnd, addr szCaption
 invoke SendDlgItemMessageW, hWnd, ID_PASSWORD, EM_LIMITTEXT, lengthof szPW, 0
 mov eax, 1
.elseif ( eax == WM_COMMAND )
 .if ( wParam == IDOK )
  invoke GetDlgItemTextW, hWnd, ID_PASSWORD, addr szPW, lengthof szPW
  invoke EndDialog, hWnd, addr szPW
 .endif
.else
 xor eax, eax
.endif
ret
pwproc endp

StartInteractiveClientProcess PROC lpszUsername:LPWSTR, lpszDomain:LPWSTR, lpszPassword:LPWSTR, lpCommandLine:LPWSTR

local bResult:BOOL
local pi:PROCESS_INFORMATION
local _si:STARTUPINFOW
local buffer[512]:byte

invoke RtlZeroMemory, addr _si, sizeof _si
mov _si.cb, sizeof _si
invoke CreateProcessWithLogonW, lpszUsername, lpszDomain, lpszPassword, LOGON_WITH_PROFILE,
 NULL, lpCommandLine, NORMAL_PRIORITY_CLASS or CREATE_NEW_CONSOLE, NULL, NULL, addr _si, addr pi
mov bResult, eax
.if eax
 invoke CloseHandle, pi.hProcess
 invoke CloseHandle, pi.hThread
.else
 invoke GetLastError
 invoke wsprintf, addr buffer, CStr('CreateProcessWithLogonW("%S") failed [%u]'), lpCommandLine, eax
 invoke MessageBox, NULL, addr buffer, NULL, MB_OK
.endif
mov eax, bResult
ret

StartInteractiveClientProcess ENDP

main PROC uses ebx argc:dword, argv:ptr LPWSTR

mov ecx, argc
mov ebx, argv
.if ( ecx < 4 )
 invoke MessageBox, NULL, CStr("Usage: RunAs <account> <password> <executable>",10,"Use ? for <password> if a prompt is wanted."), CStr("RunAs"), MB_OK
 ret
.endif
mov ecx, dword ptr [ebx+2*4]
.if ( dword ptr [ecx] == "?" )
 invoke DialogBoxIndirectParamW, NULL, addr pwdlg, NULL, addr pwproc, dword ptr [ebx+1*4]
 .if ( eax == 0 || eax == -1 )
  ret
 .endif
 mov ecx, eax
.endif
invoke StartInteractiveClientProcess, dword ptr [ebx+1*4], _T("."), ecx, dword ptr [ebx+3*4]
ret
main ENDP

start proc

local argc:dword

invoke GetCommandLineW
mov ecx, eax
invoke CommandLineToArgvW, ecx, addr argc
invoke main, argc, eax
invoke ExitProcess, eax
start endp

END start

Sponsored Links
PMEmail Poster
Top
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

Topic Options Reply to this topicStart new topicStart Poll

 

Sponsors
Computer Science

Internet
C/C++
Hardware & PC maintenance

HiEditor

General Discussions
Suggestions/Bug Reports
WinAsm Studio

General Discussions
Suggestions/Bug Reports
WinAsm Studio FAQ
Multilingual User Interface
Add-Ins
Assembly Programming

Main
Newbies
Projects
Custom Controls
Snippets
Announcements & Rules

Announcements

General

Online Degrees - Distance Learning
The Heap
Russian