News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Run As Admin -- Must it be 64-bit?

Started by Dan-TheStarman, January 29, 2021, 06:47:24 AM

Previous topic - Next topic

Dan-TheStarman

#30
Quote from: HSE on February 05, 2021, 07:35:36 AM
Quote from: Dan-TheStarman on February 05, 2021, 06:37:25 AM
What about the frustrating issue of: Why does program display Info window when answering "Yes" to the UAC runas Admin?
It's not the program.

As you said, error was... well maybe technically... "not in the code"; but my in what I expected it to do.

jj2007

What about reading the FM? Sometimes it helps...

Dan-TheStarman

OK,

   After going back to the "ShellExecuteA function" documentation at Microsoft, it appears this function when using "runas" is only for executing a program OUTSIDE of whatever program you use it in!

   I got it all wrong apparently by assuming the handful of programs I have downloaded from reputable sources that upon execution used the Microsoft Windows UAC display to ask me if they could execute with Admin privileges was a single executable!

I might just as well tell my users to right-click and choose to run my program "as Admin"! 


Dan.

Dan-TheStarman

I wanted everyone who spent some time on replying to me to know I appreciate it AND that I definitely learned things from all of you!
THANK YOU.

THIS IS NOT a completed CopyMBR program, but after spending all the time here, it does at least incorporate code that checks to see if it is already running with Admin privileges!  This is 32-bit assembly code that can be assembled and linked using masm32 and runs just fine... displaying different messages depending upon whether it does or does not have Admin privileges:


.386
.model flat, stdcall
option casemap: none

include \masm32\include\windows.inc
include \masm32\macros\macros.asm
uselib kernel32, user32, shell32, advapi32

; See: https://docs.microsoft.com/en-us/windows/win32/secauthz/access-mask-format
GENERIC_READ EQU 80000000h
GENERIC_WRITE EQU 40000000h
GENERIC_EXECUTE EQU 20000000h

INFOI EQU 40h

.data

winCapt db "CopyMBR - Copies MBR Sector of Disk 0 to a File", 0

asAdminMsg db "CopyMBR is running with Administrator privileges.", 0

notAdmin db "CopyMBR requires Administrator privileges in order "
db " to copy the Master Boot Record (MBR) sector of this"
db " PC's first disk!", 0dh, 0ah, 0dh, 0ah
db 'Right-click on "CopyMBR.exe" and choose: ', 0dh, 0ah
db '"Run as administrator" to perform this copy operation.', 0

winOpTxt db "If CopyMBR were complete, it would have made a copy of your "
db " first disk's Master Boot Record (MBR) Sector to a file.", 0

.code

main proc

; Note: Only processes with Administrator privileges are able to open a database handle.
; So, first check for Generic Access Rights from the Service Control Manager (SCM) (see both:
; https://docs.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-openscmanagera  and:
; https://docs.microsoft.com/en-us/windows/win32/services/service-security-and-access-rights )
; setting "GENERIC ACCESS" rights according to the 'Access Mask Format' found here:
; https://docs.microsoft.com/en-us/windows/win32/secauthz/access-mask-format

  push GENERIC_READ OR GENERIC_WRITE OR GENERIC_EXECUTE ; 8h OR 4h OR 2h = Eh, so
; we end up with E0000000 if you check registers.
  push 0
  push 0
  call OpenSCManagerA ; No computer name provided, so connects to the SCM on the local
; computer. No database name provided, so SERVICES_ACTIVE_DATABASE
; is opened by default. If successful, return value is a 'handle'
; to the SCM Database.  If not, it is NULL.
  cmp eax, 0
  jnz @F ; If we are already running as Admin (have Generic Read, Write
; and Execute), then jump forward to the nearest "@@:" label.

; Set up the parameters for calling the "MessageBoxA" Function (see:
; https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-messageboxa
; for more information on all the available parameters you could use):

  invoke MessageBoxA, 0, addr notAdmin, addr winCapt, INFOI ; Not running with Admin privileges!

  push eax ; EAX contains the Exit Code from MessageBox Function which
; can be passed to the ExitProcess (from Kernel32.lib).
; (Exit Codes are 32-bit; even for 64-bit programs.)
  jmp @exit ; Jump and use Exit Code from above.

@@:

; Program is already running with Administrator privileges!

invoke MessageBoxA, 0, addr asAdminMsg, addr winCapt, INFOI

; ============================
; Main Body of the code would be here!
; ============================

invoke MessageBoxA, 0, addr winOpTxt, addr winCapt, INFOI
push 0 ; Set exit code to zero.

@exit:
call ExitProcess ; Call Windows API Function "ExitProcess"; no values returned.

main ENDP

END main ; This "END Directive" is required at end of file when
;  assembling & linking at a Command Prompt.
; (Note: "end <proc name>" is actually required here by masm32.)


   I already made a 64-bit version similar to this under VS2019 Community, and attempted to do so for 32-bit as well, and kept getting so many errors; even after reading a lot about how to write the code, etc. that I soon realized WHY hutch had created his masm32 blog/website and went ahead with what we now have here today!  (Writing what VS2019 requires for 64-bit Assembly code is a dream by comparison to 32-bit over there!  So all my 32-bit coding will always be done here.)

Dan, TheStarman.

jj2007

Try naming any 32-bit exe "Setup123.exe" :cool:

TimoVJL

May the source be with you

Dan-TheStarman

#36
Quote from: jj2007 on February 06, 2021, 07:57:51 PM
Try naming any 32-bit exe "Setup123.exe" :cool:

Thanks jj2007!  Works great... All you need to do is put "Setup" in front of your own filename to save users the hassle of clicking and choosing "runas"!

Example:  SetupCopyMBR_32.exe

Only primary mouse clicks required.