|
| 1 | +(******************************************************************************* |
| 2 | +
|
| 3 | + Jean-Pierre LESUEUR (@DarkCoderSc) |
| 4 | + https://www.phrozen.io/ |
| 5 | + |
| 6 | +
|
| 7 | + License : MIT |
| 8 | +
|
| 9 | +*******************************************************************************) |
| 10 | + |
| 11 | +program RunAs; |
| 12 | + |
| 13 | +{$APPTYPE CONSOLE} |
| 14 | + |
| 15 | +{$R *.res} |
| 16 | + |
| 17 | +uses |
| 18 | + Windows, |
| 19 | + System.SysUtils, |
| 20 | + UntFunctions in 'Units\UntFunctions.pas'; |
| 21 | + |
| 22 | +{------------------------------------------------------------------------------- |
| 23 | + Usage Banner |
| 24 | +-------------------------------------------------------------------------------} |
| 25 | +function DisplayHelpBanner() : String; |
| 26 | +begin |
| 27 | + result := ''; |
| 28 | + /// |
| 29 | + |
| 30 | + WriteLn; |
| 31 | + WriteColoredWord('RunAs'); |
| 32 | + Write('.'); |
| 33 | + WriteColoredWord('exe'); |
| 34 | + WriteLn(' -u <username> -p <password> -e <executable> [-d <domain>] [-a <arguments>] [-h]'); |
| 35 | + WriteLn; |
| 36 | + WriteLn('-h : Start process hidden.'); |
| 37 | + WriteLn; |
| 38 | +end; |
| 39 | + |
| 40 | + |
| 41 | +var SET_USERNAME : String = ''; |
| 42 | + SET_PASSWORD : String = ''; |
| 43 | + SET_DOMAINNAME : String = ''; |
| 44 | + SET_PROGRAM : String = ''; |
| 45 | + SET_ARGUMENTS : String = ''; |
| 46 | + SET_HIDDEN : Boolean = False; |
| 47 | + |
| 48 | + LRet : Integer; |
| 49 | + |
| 50 | +begin |
| 51 | + try |
| 52 | + { |
| 53 | + Parse Arguments |
| 54 | + } |
| 55 | + if NOT GetCommandLineOption('u', SET_USERNAME) then |
| 56 | + raise Exception.Create(''); |
| 57 | + |
| 58 | + if NOT GetCommandLineOption('p', SET_PASSWORD) then |
| 59 | + raise Exception.Create(''); |
| 60 | + |
| 61 | + if NOT GetCommandLineOption('e', SET_PROGRAM) then |
| 62 | + raise Exception.Create(''); |
| 63 | + |
| 64 | + GetCommandLineOption('d', SET_DOMAINNAME); |
| 65 | + GetCommandLineOption('a', SET_ARGUMENTS); |
| 66 | + |
| 67 | + SET_HIDDEN := CommandLineOptionExists('h'); |
| 68 | + |
| 69 | + { |
| 70 | + Run Program |
| 71 | + } |
| 72 | + LRet := CreateProcessAsUser( |
| 73 | + SET_PROGRAM, |
| 74 | + SET_ARGUMENTS, |
| 75 | + SET_USERNAME, |
| 76 | + SET_PASSWORD, |
| 77 | + SET_DOMAINNAME, |
| 78 | + (NOT SET_HIDDEN) |
| 79 | + ); |
| 80 | + |
| 81 | + case LRet of |
| 82 | + { |
| 83 | + Access Denied |
| 84 | + } |
| 85 | + 5 : begin |
| 86 | + Debug('Generally this error is related to file access permission. You should place the file you are trying to execute into a folder accessible by any user. (Ex: C:\ProgramData\)', dlWarning); |
| 87 | + end; |
| 88 | + end; |
| 89 | + except |
| 90 | + on E: Exception do begin |
| 91 | + if (E.Message <> '') then |
| 92 | + Debug(Format('%s : %s', [E.ClassName, E.Message]), dlError) |
| 93 | + else |
| 94 | + DisplayHelpBanner(); |
| 95 | + end; |
| 96 | + end; |
| 97 | +end. |
0 commit comments