├── README.md ├── RunAs.dpr ├── RunAs.dproj ├── RunAs.dproj.local ├── RunAs.res ├── Units └── UntFunctions.pas ├── Win32 └── Release │ ├── RunAs.exe │ └── UntFunctions.dcu └── Win64 └── Release ├── RunAs.exe └── UntFunctions.dcu /README.md: -------------------------------------------------------------------------------- 1 | # RunAs (Microsoft Windows) - 32bit / 64bit. 2 | 3 | ![Example](https://i.ibb.co/vdwBk1R/Screenshot-2020-05-18-at-18-11-41.png) 4 | 5 | This program is an example about how to easily run any programs as any user. 6 | 7 | ## Usage 8 | 9 | ### Mandatory 10 | 11 | * `-u ` : Launch program as defined username. 12 | * `-p ` : Password associated to username account. 13 | * `-e ` : Executable path (Ex: notepad.exe). 14 | 15 | ### Optional 16 | 17 | * `-d ` : Optional domain name (default %USERDOMAIN% environment variable value) for user authentication. 18 | * `-a`` : Arguments to pass to executable. 19 | * `-h` : Run program without showing window. (Hidden mode) 20 | 21 | ## Notes 22 | 23 | Target program must be accessible by target user otherwise you will receive an Access Denied Error(5). 24 | It is recommended to place target program in a folder accessible by any users such as "C:\ProgramData". 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /RunAs.dpr: -------------------------------------------------------------------------------- 1 | (******************************************************************************* 2 | 3 | Jean-Pierre LESUEUR (@DarkCoderSc) 4 | https://www.phrozen.io/ 5 | jplesueur@phrozen.io 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 -p -e [-d ] [-a ] [-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. 98 | -------------------------------------------------------------------------------- /RunAs.dproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | {5E4372AD-AA12-4908-94F2-489C07CFF3D0} 4 | 18.8 5 | None 6 | RunAs.dpr 7 | True 8 | Release 9 | Win64 10 | 3 11 | Console 12 | 13 | 14 | true 15 | 16 | 17 | true 18 | Base 19 | true 20 | 21 | 22 | true 23 | Base 24 | true 25 | 26 | 27 | true 28 | Base 29 | true 30 | 31 | 32 | true 33 | Base 34 | true 35 | 36 | 37 | true 38 | Base 39 | true 40 | 41 | 42 | true 43 | Base 44 | true 45 | 46 | 47 | true 48 | Base 49 | true 50 | 51 | 52 | true 53 | Cfg_1 54 | true 55 | true 56 | 57 | 58 | true 59 | Base 60 | true 61 | 62 | 63 | .\$(Platform)\$(Config) 64 | .\$(Platform)\$(Config) 65 | false 66 | false 67 | false 68 | false 69 | false 70 | System;Xml;Data;Datasnap;Web;Soap;$(DCC_Namespace) 71 | RunAs 72 | 73 | 74 | $(BDS)\bin\Artwork\Android\FM_LauncherIcon_36x36.png 75 | $(BDS)\bin\Artwork\Android\FM_LauncherIcon_48x48.png 76 | $(BDS)\bin\Artwork\Android\FM_LauncherIcon_72x72.png 77 | $(BDS)\bin\Artwork\Android\FM_LauncherIcon_96x96.png 78 | $(BDS)\bin\Artwork\Android\FM_LauncherIcon_144x144.png 79 | $(BDS)\bin\Artwork\Android\FM_SplashImage_426x320.png 80 | $(BDS)\bin\Artwork\Android\FM_SplashImage_470x320.png 81 | $(BDS)\bin\Artwork\Android\FM_SplashImage_640x480.png 82 | $(BDS)\bin\Artwork\Android\FM_SplashImage_960x720.png 83 | $(BDS)\bin\Artwork\Android\FM_NotificationIcon_24x24.png 84 | $(BDS)\bin\Artwork\Android\FM_NotificationIcon_36x36.png 85 | $(BDS)\bin\Artwork\Android\FM_NotificationIcon_48x48.png 86 | $(BDS)\bin\Artwork\Android\FM_NotificationIcon_72x72.png 87 | $(BDS)\bin\Artwork\Android\FM_NotificationIcon_96x96.png 88 | 89 | 90 | $(BDS)\bin\Artwork\Android\FM_LauncherIcon_36x36.png 91 | $(BDS)\bin\Artwork\Android\FM_LauncherIcon_48x48.png 92 | $(BDS)\bin\Artwork\Android\FM_LauncherIcon_72x72.png 93 | $(BDS)\bin\Artwork\Android\FM_LauncherIcon_96x96.png 94 | $(BDS)\bin\Artwork\Android\FM_LauncherIcon_144x144.png 95 | $(BDS)\bin\Artwork\Android\FM_SplashImage_426x320.png 96 | $(BDS)\bin\Artwork\Android\FM_SplashImage_470x320.png 97 | $(BDS)\bin\Artwork\Android\FM_SplashImage_640x480.png 98 | $(BDS)\bin\Artwork\Android\FM_SplashImage_960x720.png 99 | $(BDS)\bin\Artwork\Android\FM_NotificationIcon_24x24.png 100 | $(BDS)\bin\Artwork\Android\FM_NotificationIcon_36x36.png 101 | $(BDS)\bin\Artwork\Android\FM_NotificationIcon_48x48.png 102 | $(BDS)\bin\Artwork\Android\FM_NotificationIcon_72x72.png 103 | $(BDS)\bin\Artwork\Android\FM_NotificationIcon_96x96.png 104 | 105 | 106 | DBXSqliteDriver;bindcompdbx;IndyIPCommon;RESTComponents;DBXInterBaseDriver;IndyIPServer;IndySystem;tethering;fmxFireDAC;FireDAC;bindcompfmx;FireDACSqliteDriver;FireDACPgDriver;ibmonitor;inetdb;FMXTee;soaprtl;DbxCommonDriver;FmxTeeUI;ibxpress;FireDACIBDriver;fmx;fmxdae;xmlrtl;soapmidas;ibxbindings;fmxobj;rtl;DbxClientDriver;CustomIPTransport;dbexpress;IndyCore;bindcomp;dsnap;FireDACCommon;IndyIPClient;RESTBackendComponents;soapserver;dbxcds;bindengine;DBXMySQLDriver;CloudService;dsnapxml;FireDACMySQLDriver;dbrtl;inetdbxpress;IndyProtocols;FireDACCommonODBC;FireDACCommonDriver;inet;fmxase;$(DCC_UsePackage) 107 | true 108 | 109 | 110 | DBXSqliteDriver;bindcompdbx;IndyIPCommon;RESTComponents;DBXInterBaseDriver;IndyIPServer;IndySystem;tethering;fmxFireDAC;FireDAC;bindcompfmx;FireDACSqliteDriver;FireDACPgDriver;ibmonitor;inetdb;FMXTee;soaprtl;DbxCommonDriver;FmxTeeUI;ibxpress;FireDACIBDriver;fmx;fmxdae;xmlrtl;soapmidas;ibxbindings;fmxobj;rtl;DbxClientDriver;CustomIPTransport;dbexpress;IndyCore;bindcomp;dsnap;FireDACCommon;IndyIPClient;RESTBackendComponents;soapserver;dbxcds;bindengine;DBXMySQLDriver;CloudService;dsnapxml;FireDACMySQLDriver;dbrtl;inetdbxpress;IndyProtocols;FireDACCommonODBC;FireDACCommonDriver;inet;fmxase;$(DCC_UsePackage) 111 | true 112 | 113 | 114 | DBXSqliteDriver;bindcompdbx;IndyIPCommon;RESTComponents;DBXInterBaseDriver;vcl;IndyIPServer;vclactnband;vclFireDAC;IndySystem;tethering;svnui;dsnapcon;FireDACADSDriver;madExcept_;VirtualTreesR;PhrozenEngine;FireDACMSAccDriver;fmxFireDAC;vclimg;madBasic_;TeeDB;FireDAC;vcltouch;vcldb;bindcompfmx;svn;FireDACSqliteDriver;FireDACPgDriver;ibmonitor;inetdb;ChromeTabs_R;FMXTee;soaprtl;DbxCommonDriver;FmxTeeUI;ibxpress;FireDACIBDriver;fmx;fmxdae;xmlrtl;soapmidas;ibxbindings;fmxobj;vclwinx;vclib;rtl;Tee;DbxClientDriver;madDisAsm_;S7C;CustomIPTransport;vcldsnap;dbexpress;IndyCore;vclx;OMultiPanel_Design;bindcomp;appanalytics;dsnap;FireDACCommon;IndyIPClient;bindcompvcl;RESTBackendComponents;TeeUI;VCLRESTComponents;soapserver;dbxcds;VclSmp;adortl;vclie;bindengine;DBXMySQLDriver;CloudService;dsnapxml;FireDACMySQLDriver;dbrtl;inetdbxpress;IndyProtocols;FireDACCommonODBC;FireDACCommonDriver;PhrozenSystem;inet;fmxase;$(DCC_UsePackage) 115 | Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;Bde;$(DCC_Namespace) 116 | Debug 117 | CompanyName=;FileDescription=$(MSBuildProjectName);FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments= 118 | 1033 119 | true 120 | $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_44.png 121 | $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_150.png 122 | 123 | 124 | DBXSqliteDriver;bindcompdbx;IndyIPCommon;RESTComponents;DBXInterBaseDriver;vcl;IndyIPServer;vclactnband;vclFireDAC;IndySystem;tethering;dsnapcon;FireDACADSDriver;VirtualTreesR;FireDACMSAccDriver;fmxFireDAC;vclimg;TeeDB;FireDAC;vcltouch;vcldb;bindcompfmx;FireDACSqliteDriver;FireDACPgDriver;ibmonitor;inetdb;FMXTee;soaprtl;DbxCommonDriver;FmxTeeUI;ibxpress;FireDACIBDriver;fmx;fmxdae;xmlrtl;soapmidas;ibxbindings;fmxobj;vclwinx;vclib;rtl;Tee;DbxClientDriver;CustomIPTransport;vcldsnap;dbexpress;IndyCore;vclx;bindcomp;appanalytics;dsnap;FireDACCommon;IndyIPClient;bindcompvcl;RESTBackendComponents;TeeUI;VCLRESTComponents;soapserver;dbxcds;VclSmp;adortl;vclie;bindengine;DBXMySQLDriver;CloudService;dsnapxml;FireDACMySQLDriver;dbrtl;inetdbxpress;IndyProtocols;FireDACCommonODBC;FireDACCommonDriver;inet;fmxase;$(DCC_UsePackage) 125 | true 126 | $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_44.png 127 | $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_150.png 128 | Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;$(DCC_Namespace) 129 | Debug 130 | CompanyName=;FileDescription=$(MSBuildProjectName);FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments= 131 | 1033 132 | 133 | 134 | DEBUG;$(DCC_Define) 135 | true 136 | false 137 | true 138 | true 139 | true 140 | 141 | 142 | false 143 | 144 | 145 | false 146 | RELEASE;$(DCC_Define) 147 | 0 148 | 0 149 | 150 | 151 | 152 | MainSource 153 | 154 | 155 | 156 | Cfg_2 157 | Base 158 | 159 | 160 | Base 161 | 162 | 163 | Cfg_1 164 | Base 165 | 166 | 167 | 168 | Delphi.Personality.12 169 | Application 170 | 171 | 172 | 173 | RunAs.dpr 174 | 175 | 176 | 177 | 178 | 179 | true 180 | 181 | 182 | 183 | 184 | true 185 | 186 | 187 | 188 | 189 | true 190 | 191 | 192 | 193 | 194 | RunAs.exe 195 | true 196 | 197 | 198 | 199 | 200 | 1 201 | 202 | 203 | Contents\MacOS 204 | 1 205 | 206 | 207 | 0 208 | 209 | 210 | 211 | 212 | classes 213 | 1 214 | 215 | 216 | classes 217 | 1 218 | 219 | 220 | 221 | 222 | res\xml 223 | 1 224 | 225 | 226 | res\xml 227 | 1 228 | 229 | 230 | 231 | 232 | library\lib\armeabi-v7a 233 | 1 234 | 235 | 236 | 237 | 238 | library\lib\armeabi 239 | 1 240 | 241 | 242 | library\lib\armeabi 243 | 1 244 | 245 | 246 | 247 | 248 | library\lib\armeabi-v7a 249 | 1 250 | 251 | 252 | 253 | 254 | library\lib\mips 255 | 1 256 | 257 | 258 | library\lib\mips 259 | 1 260 | 261 | 262 | 263 | 264 | library\lib\armeabi-v7a 265 | 1 266 | 267 | 268 | library\lib\arm64-v8a 269 | 1 270 | 271 | 272 | 273 | 274 | library\lib\armeabi-v7a 275 | 1 276 | 277 | 278 | 279 | 280 | res\drawable 281 | 1 282 | 283 | 284 | res\drawable 285 | 1 286 | 287 | 288 | 289 | 290 | res\values 291 | 1 292 | 293 | 294 | res\values 295 | 1 296 | 297 | 298 | 299 | 300 | res\values-v21 301 | 1 302 | 303 | 304 | res\values-v21 305 | 1 306 | 307 | 308 | 309 | 310 | res\values 311 | 1 312 | 313 | 314 | res\values 315 | 1 316 | 317 | 318 | 319 | 320 | res\drawable 321 | 1 322 | 323 | 324 | res\drawable 325 | 1 326 | 327 | 328 | 329 | 330 | res\drawable-xxhdpi 331 | 1 332 | 333 | 334 | res\drawable-xxhdpi 335 | 1 336 | 337 | 338 | 339 | 340 | res\drawable-ldpi 341 | 1 342 | 343 | 344 | res\drawable-ldpi 345 | 1 346 | 347 | 348 | 349 | 350 | res\drawable-mdpi 351 | 1 352 | 353 | 354 | res\drawable-mdpi 355 | 1 356 | 357 | 358 | 359 | 360 | res\drawable-hdpi 361 | 1 362 | 363 | 364 | res\drawable-hdpi 365 | 1 366 | 367 | 368 | 369 | 370 | res\drawable-xhdpi 371 | 1 372 | 373 | 374 | res\drawable-xhdpi 375 | 1 376 | 377 | 378 | 379 | 380 | res\drawable-mdpi 381 | 1 382 | 383 | 384 | res\drawable-mdpi 385 | 1 386 | 387 | 388 | 389 | 390 | res\drawable-hdpi 391 | 1 392 | 393 | 394 | res\drawable-hdpi 395 | 1 396 | 397 | 398 | 399 | 400 | res\drawable-xhdpi 401 | 1 402 | 403 | 404 | res\drawable-xhdpi 405 | 1 406 | 407 | 408 | 409 | 410 | res\drawable-xxhdpi 411 | 1 412 | 413 | 414 | res\drawable-xxhdpi 415 | 1 416 | 417 | 418 | 419 | 420 | res\drawable-xxxhdpi 421 | 1 422 | 423 | 424 | res\drawable-xxxhdpi 425 | 1 426 | 427 | 428 | 429 | 430 | res\drawable-small 431 | 1 432 | 433 | 434 | res\drawable-small 435 | 1 436 | 437 | 438 | 439 | 440 | res\drawable-normal 441 | 1 442 | 443 | 444 | res\drawable-normal 445 | 1 446 | 447 | 448 | 449 | 450 | res\drawable-large 451 | 1 452 | 453 | 454 | res\drawable-large 455 | 1 456 | 457 | 458 | 459 | 460 | res\drawable-xlarge 461 | 1 462 | 463 | 464 | res\drawable-xlarge 465 | 1 466 | 467 | 468 | 469 | 470 | res\values 471 | 1 472 | 473 | 474 | res\values 475 | 1 476 | 477 | 478 | 479 | 480 | 1 481 | 482 | 483 | Contents\MacOS 484 | 1 485 | 486 | 487 | 0 488 | 489 | 490 | 491 | 492 | Contents\MacOS 493 | 1 494 | .framework 495 | 496 | 497 | Contents\MacOS 498 | 1 499 | .framework 500 | 501 | 502 | 0 503 | 504 | 505 | 506 | 507 | 1 508 | .dylib 509 | 510 | 511 | 1 512 | .dylib 513 | 514 | 515 | 1 516 | .dylib 517 | 518 | 519 | Contents\MacOS 520 | 1 521 | .dylib 522 | 523 | 524 | Contents\MacOS 525 | 1 526 | .dylib 527 | 528 | 529 | 0 530 | .dll;.bpl 531 | 532 | 533 | 534 | 535 | 1 536 | .dylib 537 | 538 | 539 | 1 540 | .dylib 541 | 542 | 543 | 1 544 | .dylib 545 | 546 | 547 | Contents\MacOS 548 | 1 549 | .dylib 550 | 551 | 552 | Contents\MacOS 553 | 1 554 | .dylib 555 | 556 | 557 | 0 558 | .bpl 559 | 560 | 561 | 562 | 563 | 0 564 | 565 | 566 | 0 567 | 568 | 569 | 0 570 | 571 | 572 | 0 573 | 574 | 575 | 0 576 | 577 | 578 | Contents\Resources\StartUp\ 579 | 0 580 | 581 | 582 | Contents\Resources\StartUp\ 583 | 0 584 | 585 | 586 | 0 587 | 588 | 589 | 590 | 591 | 1 592 | 593 | 594 | 1 595 | 596 | 597 | 1 598 | 599 | 600 | 601 | 602 | 1 603 | 604 | 605 | 1 606 | 607 | 608 | 1 609 | 610 | 611 | 612 | 613 | 1 614 | 615 | 616 | 1 617 | 618 | 619 | 1 620 | 621 | 622 | 623 | 624 | 1 625 | 626 | 627 | 1 628 | 629 | 630 | 1 631 | 632 | 633 | 634 | 635 | 1 636 | 637 | 638 | 1 639 | 640 | 641 | 1 642 | 643 | 644 | 645 | 646 | 1 647 | 648 | 649 | 1 650 | 651 | 652 | 1 653 | 654 | 655 | 656 | 657 | 1 658 | 659 | 660 | 1 661 | 662 | 663 | 1 664 | 665 | 666 | 667 | 668 | 1 669 | 670 | 671 | 1 672 | 673 | 674 | 1 675 | 676 | 677 | 678 | 679 | 1 680 | 681 | 682 | 1 683 | 684 | 685 | 1 686 | 687 | 688 | 689 | 690 | 1 691 | 692 | 693 | 1 694 | 695 | 696 | 1 697 | 698 | 699 | 700 | 701 | 1 702 | 703 | 704 | 1 705 | 706 | 707 | 1 708 | 709 | 710 | 711 | 712 | 1 713 | 714 | 715 | 1 716 | 717 | 718 | 1 719 | 720 | 721 | 722 | 723 | 1 724 | 725 | 726 | 1 727 | 728 | 729 | 1 730 | 731 | 732 | 733 | 734 | 1 735 | 736 | 737 | 1 738 | 739 | 740 | 1 741 | 742 | 743 | 744 | 745 | 1 746 | 747 | 748 | 1 749 | 750 | 751 | 1 752 | 753 | 754 | 755 | 756 | 1 757 | 758 | 759 | 1 760 | 761 | 762 | 1 763 | 764 | 765 | 766 | 767 | 1 768 | 769 | 770 | 1 771 | 772 | 773 | 1 774 | 775 | 776 | 777 | 778 | 1 779 | 780 | 781 | 1 782 | 783 | 784 | 1 785 | 786 | 787 | 788 | 789 | 1 790 | 791 | 792 | 1 793 | 794 | 795 | 1 796 | 797 | 798 | 799 | 800 | 1 801 | 802 | 803 | 1 804 | 805 | 806 | 1 807 | 808 | 809 | 810 | 811 | 1 812 | 813 | 814 | 1 815 | 816 | 817 | 1 818 | 819 | 820 | 821 | 822 | 1 823 | 824 | 825 | 1 826 | 827 | 828 | 1 829 | 830 | 831 | 832 | 833 | 1 834 | 835 | 836 | 1 837 | 838 | 839 | 1 840 | 841 | 842 | 843 | 844 | 1 845 | 846 | 847 | 1 848 | 849 | 850 | 1 851 | 852 | 853 | 854 | 855 | 1 856 | 857 | 858 | 1 859 | 860 | 861 | 862 | 863 | ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF 864 | 1 865 | 866 | 867 | ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF 868 | 1 869 | 870 | 871 | 872 | 873 | 1 874 | 875 | 876 | 1 877 | 878 | 879 | 880 | 881 | ..\ 882 | 1 883 | 884 | 885 | ..\ 886 | 1 887 | 888 | 889 | 890 | 891 | 1 892 | 893 | 894 | 1 895 | 896 | 897 | 1 898 | 899 | 900 | 901 | 902 | 1 903 | 904 | 905 | 1 906 | 907 | 908 | 1 909 | 910 | 911 | 912 | 913 | ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF 914 | 1 915 | 916 | 917 | 918 | 919 | ..\ 920 | 1 921 | 922 | 923 | ..\ 924 | 1 925 | 926 | 927 | 928 | 929 | Contents 930 | 1 931 | 932 | 933 | Contents 934 | 1 935 | 936 | 937 | 938 | 939 | Contents\Resources 940 | 1 941 | 942 | 943 | Contents\Resources 944 | 1 945 | 946 | 947 | 948 | 949 | library\lib\armeabi-v7a 950 | 1 951 | 952 | 953 | library\lib\arm64-v8a 954 | 1 955 | 956 | 957 | 1 958 | 959 | 960 | 1 961 | 962 | 963 | 1 964 | 965 | 966 | 1 967 | 968 | 969 | Contents\MacOS 970 | 1 971 | 972 | 973 | Contents\MacOS 974 | 1 975 | 976 | 977 | 0 978 | 979 | 980 | 981 | 982 | library\lib\armeabi-v7a 983 | 1 984 | 985 | 986 | 987 | 988 | 1 989 | 990 | 991 | 1 992 | 993 | 994 | 995 | 996 | Assets 997 | 1 998 | 999 | 1000 | Assets 1001 | 1 1002 | 1003 | 1004 | 1005 | 1006 | Assets 1007 | 1 1008 | 1009 | 1010 | Assets 1011 | 1 1012 | 1013 | 1014 | 1015 | 1016 | 1017 | 1018 | 1019 | 1020 | 1021 | 1022 | 1023 | 1024 | 1025 | 1026 | False 1027 | False 1028 | False 1029 | False 1030 | True 1031 | True 1032 | 1033 | 1034 | 12 1035 | 1036 | 1037 | 1038 | 1039 | 1040 | -------------------------------------------------------------------------------- /RunAs.dproj.local: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 2020/03/03 11:06:06.000.717,=C:\Users\PhrozenLab\Documents\Embarcadero\Studio\Projects\Unit1.pas 5 | 2020/04/30 15:03:58.000.912,=C:\Users\PhrozenLab\Documents\Embarcadero\Studio\Projects\Unit1.pas 6 | 2020/05/18 17:25:24.921,C:\Users\PhrozenLab\Desktop\Open Source Projects\Git\run-as\RunAs.dproj=C:\Users\PhrozenLab\Documents\Embarcadero\Studio\Projects\Project2.dproj 7 | 2020/05/18 17:29:47.499,=C:\Users\PhrozenLab\Desktop\Open Source Projects\Git\run-as\Unit1.pas 8 | 2020/05/18 17:29:59.546,C:\Users\PhrozenLab\Desktop\Open Source Projects\Git\run-as\Units\UntFunctions.pas=C:\Users\PhrozenLab\Desktop\Open Source Projects\Git\run-as\Unit1.pas 9 | 10 | 11 | -------------------------------------------------------------------------------- /RunAs.res: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DarkCoderSc/run-as/12c1f4111200655f4e949d635317fc18cc75b29e/RunAs.res -------------------------------------------------------------------------------- /Units/UntFunctions.pas: -------------------------------------------------------------------------------- 1 | (******************************************************************************* 2 | 3 | Jean-Pierre LESUEUR (@DarkCoderSc) 4 | https://www.phrozen.io/ 5 | jplesueur@phrozen.io 6 | 7 | License : MIT 8 | 9 | *******************************************************************************) 10 | 11 | unit UntFunctions; 12 | 13 | interface 14 | 15 | uses Windows, SysUtils; 16 | 17 | type 18 | TDebugLevel = ( 19 | dlInfo, 20 | dlSuccess, 21 | dlWarning, 22 | dlError 23 | ); 24 | 25 | function CreateProcessAsUser(AProgram : String; ACommandLine : String; AUserName, APassword : String; ADomain : String = ''; AVisible : Boolean = True) : Integer; 26 | procedure DumpLastError(APrefix : String = ''); 27 | procedure Debug(AMessage : String; ADebugLevel : TDebugLevel = dlInfo); 28 | function GetCommandLineOption(AOption : String; var AValue : String; ACommandLine : String = '') : Boolean; overload; 29 | function GetCommandLineOption(AOption : String; var AValue : String; var AOptionExists : Boolean; ACommandLine : String = '') : Boolean; overload; 30 | function UpdateConsoleAttributes(AConsoleAttributes : Word) : Word; 31 | procedure WriteColoredWord(AString : String); 32 | function CommandLineOptionExists(AOption : String; ACommandLine : String = '') : Boolean; 33 | 34 | implementation 35 | 36 | {------------------------------------------------------------------------------- 37 | Check if commandline option is set 38 | -------------------------------------------------------------------------------} 39 | function CommandLineOptionExists(AOption : String; ACommandLine : String = '') : Boolean; 40 | var ADummy : String; 41 | begin 42 | GetCommandLineOption(AOption, ADummy, result, ACommandLine); 43 | end; 44 | 45 | {------------------------------------------------------------------------------- 46 | Write colored word(s) on current console 47 | -------------------------------------------------------------------------------} 48 | procedure WriteColoredWord(AString : String); 49 | var AOldAttributes : Word; 50 | begin 51 | AOldAttributes := UpdateConsoleAttributes(FOREGROUND_INTENSITY or FOREGROUND_GREEN); 52 | 53 | Write(AString); 54 | 55 | UpdateConsoleAttributes(AOldAttributes); 56 | end; 57 | 58 | {------------------------------------------------------------------------------- 59 | Update Console Attributes (Changing color for example) 60 | 61 | Returns previous attributes. 62 | -------------------------------------------------------------------------------} 63 | function UpdateConsoleAttributes(AConsoleAttributes : Word) : Word; 64 | var AConsoleHandle : THandle; 65 | AConsoleScreenBufInfo : TConsoleScreenBufferInfo; 66 | b : Boolean; 67 | begin 68 | result := 0; 69 | /// 70 | 71 | AConsoleHandle := GetStdHandle(STD_OUTPUT_HANDLE); 72 | if (AConsoleHandle = INVALID_HANDLE_VALUE) then 73 | Exit(); 74 | /// 75 | 76 | b := GetConsoleScreenBufferInfo(AConsoleHandle, AConsoleScreenBufInfo); 77 | 78 | if b then begin 79 | SetConsoleTextAttribute(AConsoleHandle, AConsoleAttributes); 80 | 81 | /// 82 | result := AConsoleScreenBufInfo.wAttributes; 83 | end; 84 | end; 85 | 86 | {------------------------------------------------------------------------------- 87 | Command Line Parser 88 | 89 | AOption : Search for specific option Ex: -c. 90 | AValue : Next argument string if option is found. 91 | AOptionExists : Set to true if option is found in command line string. 92 | ACommandLine : Command Line String to parse, by default, actual program command line. 93 | -------------------------------------------------------------------------------} 94 | function GetCommandLineOption(AOption : String; var AValue : String; var AOptionExists : Boolean; ACommandLine : String = '') : Boolean; 95 | var ACount : Integer; 96 | pElements : Pointer; 97 | I : Integer; 98 | ACurArg : String; 99 | hShell32 : THandle; 100 | 101 | CommandLineToArgvW : function(lpCmdLine : LPCWSTR; var pNumArgs : Integer) : LPWSTR; stdcall; 102 | type 103 | TArgv = array[0..0] of PWideChar; 104 | begin 105 | result := False; 106 | /// 107 | 108 | AOptionExists := False; 109 | 110 | hShell32 := LoadLibrary('SHELL32.DLL'); 111 | if (hShell32 = 0) then begin 112 | Debug('Could load Shell32.dll Library.', dlError); 113 | Exit(); 114 | end; 115 | try 116 | @CommandLineToArgvW := GetProcAddress(hShell32, 'CommandLineToArgvW'); 117 | if NOT Assigned(CommandLineToArgvW) then begin 118 | Debug('Could load CommandLineToArgvW API.', dlError); 119 | Exit(); 120 | end; 121 | 122 | if (ACommandLine = '') then begin 123 | ACommandLine := GetCommandLineW(); 124 | end; 125 | 126 | pElements := CommandLineToArgvW(PWideChar(ACommandLine), ACount); 127 | 128 | if NOT Assigned(pElements) then 129 | Exit(); 130 | 131 | AOption := '-' + AOption; 132 | 133 | if (Length(AOption) > 2) then 134 | AOption := '-' + AOption; 135 | 136 | for I := 0 to ACount -1 do begin 137 | ACurArg := UnicodeString((TArgv(pElements^)[I])); 138 | /// 139 | 140 | if (ACurArg <> AOption) then 141 | continue; 142 | 143 | AOptionExists := True; 144 | 145 | // Retrieve Next Arg 146 | if I <> (ACount -1) then begin 147 | AValue := UnicodeString((TArgv(pElements^)[I+1])); 148 | 149 | /// 150 | result := True; 151 | end; 152 | end; 153 | finally 154 | FreeLibrary(hShell32); 155 | end; 156 | end; 157 | 158 | function GetCommandLineOption(AOption : String; var AValue : String; ACommandLine : String = '') : Boolean; 159 | var AExists : Boolean; 160 | begin 161 | result := GetCommandLineOption(AOption, AValue, AExists, ACommandLine); 162 | end; 163 | 164 | {------------------------------------------------------------------------------- 165 | Spawn a process as another user. 166 | -------------------------------------------------------------------------------} 167 | function CreateProcessAsUser(AProgram : String; ACommandLine : String; AUserName, APassword : String; ADomain : String = ''; AVisible : Boolean = True) : Integer; 168 | var AStartupInfo : TStartupInfo; 169 | AProcessInfo : TProcessInformation; 170 | hAdvApi32 : THandle; 171 | b : Boolean; 172 | 173 | CreateProcessWithLogonW : function( 174 | lpUsername, lpDomain, lpPassword: LPCWSTR; 175 | dwLogonFlags: DWORD; 176 | lpApplicationName: LPCWSTR; 177 | lpCommandLine: LPWSTR; 178 | dwCreationFlags: DWORD; 179 | lpEnvironment: LPVOID; 180 | lpCurrentDirectory: LPCWSTR; 181 | const lpStartupInfo: STARTUPINFOW; 182 | var lpProcessInformation: PROCESS_INFORMATION 183 | ): BOOL; stdcall; 184 | begin 185 | result := 0; 186 | /// 187 | 188 | hAdvapi32 := LoadLibrary('ADVAPI32.DLL'); 189 | if (hAdvapi32 = 0) then begin 190 | Debug('Could load Advapi32.dll Library.', dlError); 191 | 192 | Exit(); 193 | end; 194 | try 195 | @CreateProcessWithLogonW := GetProcAddress(hAdvapi32, 'CreateProcessWithLogonW'); 196 | 197 | if NOT Assigned(CreateProcessWithLogonW) then begin 198 | Debug('Could load CreateProcessWithLogonW API.', dlError); 199 | Exit(); 200 | end; 201 | /// 202 | 203 | if (ADomain = '') then 204 | ADomain := GetEnvironmentVariable('USERDOMAIN'); 205 | /// 206 | 207 | ACommandLine := Format('%s %s', [AProgram, ACommandLine]); 208 | 209 | UniqueString(ACommandLine); 210 | UniqueString(AUserName); 211 | UniqueString(APassword); 212 | UniqueString(ADomain); 213 | /// 214 | 215 | ZeroMemory(@AProcessInfo, SizeOf(TProcessInformation)); 216 | ZeroMemory(@AStartupInfo, Sizeof(TStartupInfo)); 217 | 218 | AStartupInfo.cb := SizeOf(TStartupInfo); 219 | 220 | if AVisible then 221 | AStartupInfo.wShowWindow := SW_SHOW 222 | else 223 | AStartupInfo.wShowWindow := SW_HIDE; 224 | 225 | AStartupInfo.dwFlags := (STARTF_USESHOWWINDOW); 226 | 227 | b := CreateProcessWithLogonW( 228 | PWideChar(AUserName), 229 | PWideChar(ADomain), 230 | PWideChar(APassword), 231 | 0, 232 | nil, 233 | PWideChar(ACommandLine), 234 | 0, 235 | nil, 236 | nil, 237 | AStartupInfo, 238 | AProcessInfo 239 | ); 240 | 241 | result := GetLastError(); 242 | 243 | if (NOT b) then 244 | DumpLastError('CreateProcessWithLogonW') 245 | else 246 | Debug(Format('Process spawned as user=[%s], ProcessId=[%d] and ProcessHandle=[%d].', [AUserName, AProcessInfo.dwProcessId, AProcessInfo.hProcess]), dlSuccess); 247 | finally 248 | FreeLibrary(hAdvApi32); 249 | end; 250 | end; 251 | 252 | {------------------------------------------------------------------------------- 253 | Debug Defs 254 | -------------------------------------------------------------------------------} 255 | procedure Debug(AMessage : String; ADebugLevel : TDebugLevel = dlInfo); 256 | var AConsoleHandle : THandle; 257 | AConsoleScreenBufInfo : TConsoleScreenBufferInfo; 258 | b : Boolean; 259 | AStatus : String; 260 | AColor : Integer; 261 | begin 262 | AConsoleHandle := GetStdHandle(STD_OUTPUT_HANDLE); 263 | if (AConsoleHandle = INVALID_HANDLE_VALUE) then 264 | Exit(); 265 | /// 266 | 267 | b := GetConsoleScreenBufferInfo(AConsoleHandle, AConsoleScreenBufInfo); 268 | 269 | case ADebugLevel of 270 | dlSuccess : begin 271 | AStatus := #32 + 'OK' + #32; 272 | AColor := FOREGROUND_GREEN; 273 | end; 274 | 275 | dlWarning : begin 276 | AStatus := #32 + '!!' + #32; 277 | AColor := (FOREGROUND_RED or FOREGROUND_GREEN); 278 | end; 279 | 280 | dlError : begin 281 | AStatus := #32 + 'KO' + #32; 282 | AColor := FOREGROUND_RED; 283 | end; 284 | 285 | else begin 286 | AStatus := 'INFO'; 287 | AColor := FOREGROUND_BLUE; 288 | end; 289 | end; 290 | 291 | Write('['); 292 | if b then 293 | b := SetConsoleTextAttribute(AConsoleHandle, FOREGROUND_INTENSITY or (AColor)); 294 | try 295 | Write(AStatus); 296 | finally 297 | if b then 298 | SetConsoleTextAttribute(AConsoleHandle, AConsoleScreenBufInfo.wAttributes); 299 | end; 300 | Write(']' + #32); 301 | 302 | /// 303 | WriteLn(AMessage); 304 | end; 305 | 306 | procedure DumpLastError(APrefix : String = ''); 307 | var ACode : Integer; 308 | AFinalMessage : String; 309 | begin 310 | ACode := GetLastError(); 311 | 312 | AFinalMessage := ''; 313 | 314 | if (ACode <> 0) then begin 315 | AFinalMessage := Format('Error_Msg=[%s], Error_Code=[%d]', [SysErrorMessage(ACode), ACode]); 316 | 317 | if (APrefix <> '') then 318 | AFinalMessage := Format('%s: %s', [APrefix, AFinalMessage]); 319 | 320 | /// 321 | Debug(AFinalMessage, dlError); 322 | end; 323 | end; 324 | 325 | end. 326 | -------------------------------------------------------------------------------- /Win32/Release/RunAs.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DarkCoderSc/run-as/12c1f4111200655f4e949d635317fc18cc75b29e/Win32/Release/RunAs.exe -------------------------------------------------------------------------------- /Win32/Release/UntFunctions.dcu: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DarkCoderSc/run-as/12c1f4111200655f4e949d635317fc18cc75b29e/Win32/Release/UntFunctions.dcu -------------------------------------------------------------------------------- /Win64/Release/RunAs.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DarkCoderSc/run-as/12c1f4111200655f4e949d635317fc18cc75b29e/Win64/Release/RunAs.exe -------------------------------------------------------------------------------- /Win64/Release/UntFunctions.dcu: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DarkCoderSc/run-as/12c1f4111200655f4e949d635317fc18cc75b29e/Win64/Release/UntFunctions.dcu --------------------------------------------------------------------------------