Guest User

blocksleep.asm

a guest
Jan 29th, 2016
113
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; ### Author: Vyacheslav, 2016.
  2. ; ### Use this file as you wish. Comment or write me a PM if you like it. :)
  3.  
  4. .386
  5. .MODEL flat, stdcall
  6. OPTION casemap: none
  7.  
  8. ; ############### Includes & libraries ##################################################
  9.  
  10.   .NOLIST
  11.  
  12.   INCLUDE windows.inc
  13.   INCLUDE kernel32.inc
  14.   INCLUDE shell32.inc
  15.  
  16.   INCLUDELIB kernel32.lib
  17.   INCLUDELIB shell32.lib
  18.   INCLUDELIB user32.lib
  19.   INCLUDELIB shlwapi.lib
  20.  
  21.   .LISTALL
  22.  
  23. ; ############### Constants Section #####################################################
  24.  
  25.   CREATE_DEFAULT_ERROR_MODE EQU 4000000h
  26.  
  27. ; ############### Data Section ##########################################################
  28.  
  29. .CONST
  30.   szAbout       DB      "Block PC sleep while child process is executing.",13,10
  31.             DB      "Usage: blocksleep <exe_filename> [arguments]",13,10,13,10,0
  32.   slAbout       =       $-szAbout
  33.   szErrorSetState   DB      "ERROR: Can't set thread execution state via SetThreadExecutionState WinAPI finction",13,10
  34.             DB      "See https://msdn.microsoft.com/ru-ru/library/windows/desktop/aa373208(v=vs.85).aspx for more info.",13,10,0
  35.   slErrorSetState   =       $-szErrorSetState
  36.   szCreateProcessError  DW      'E','R','R','O','R',':',' ','C','a','n','''','t',' ','s','t','a','r','t',' ','p','r','o','c','e','s','s',' '
  37.             DW      '"','%','s','"',' ','%','s',' ','w','i','t','h',' '
  38.             DW      'C','r','e','a','t','e','P','r','o','c','e','s','s',' ','f','u','n','c','t','i','o','n','.',0
  39.   szCreateProcessAdd    DB      13,10
  40.             DB      "See why this could happen at https://msdn.microsoft.com/ru-ru/library/windows/desktop/ms682425(v=vs.85).aspx",13,10,0
  41.   slCreateProcessAdd    =       $-szCreateProcessAdd
  42.   szWaitForError    DB      "ERROR: WaitForSingleObject function failed with code 0x%08x",13,10,0
  43.   szGetExitCodeError    DB      "ERROR: GetExitCodeProcess function failed.",13,10,0
  44.  
  45. .DATA?
  46.   HInstance     DD      ?
  47.   hStdout       DD      ?
  48.   dwOldState        DD      ?
  49.   stSI          STARTUPINFO <>
  50.   stPI          PROCESS_INFORMATION <>
  51.   szBuffer      DW      8192 DUP(?)
  52.  
  53.  
  54. ; ############### Procedures declarations ###############################################
  55.  
  56.   wsprintfW     PROTO C     :DWORD,:VARARG
  57.   GetCommandLineW   PROTO
  58.   PathGetArgsW      PROTO       :DWORD
  59.   PathRemoveArgsW   PROTO       :DWORD
  60.   PathUnquoteSpacesW    PROTO       :DWORD
  61.   CreateProcessW    PROTO       :DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD
  62.   WriteConsoleW     PROTO       :DWORD,:DWORD,:DWORD,:DWORD,:DWORD
  63.  
  64. ; ############### Module declarations ###################################################
  65. ; ############### Code Section ##########################################################
  66.  
  67. .CODE
  68.  
  69. Move proc USES EDI ESI ECX, Source:DWORD, Dest:DWORD, Count:DWORD
  70.       MOV   ESI, Source
  71.       MOV   EDI, Dest
  72.       MOV   ECX, Count
  73.       CMP   EDI, ESI
  74.       JA    @1
  75.       JE    @2
  76.       SAR   ECX, 2
  77.       JS    @2
  78.       REP   MOVSD
  79.       MOV   ECX, Count
  80.       AND   ECX, 03H
  81.       REP   MOVSB
  82.       JMP   @2
  83.   @1: LEA   ESI, [ESI+ECX-4]
  84.       LEA   EDI, [EDI+ECX-4]
  85.       SAR   ECX, 2
  86.       JS    @2
  87.       STD
  88.       REP   MOVSD
  89.       MOV   ECX, Count
  90.       AND   ECX, 03H
  91.       ADD   ESI, 4-1
  92.       ADD   EDI, 4-1
  93.       REP   MOVSB
  94.       CLD
  95.   @2: RET
  96. Move endp
  97.  
  98. Start:
  99.       MOV   EBX, -1
  100.       INVOKE GetStdHandle, STD_OUTPUT_HANDLE
  101.       MOV   hStdout, EAX
  102.       INVOKE GetCommandLineW
  103.       MOV   ESI, EAX
  104.       INVOKE PathGetArgsW, ESI
  105.       MOV   ESI, EAX
  106.       MOV   AX, WORD PTR [ESI]
  107.       TEST  AX, AX
  108.       JNE   @F
  109.       PUSH  0
  110.       MOV   EAX, ESP
  111.       INVOKE WriteConsoleA, hStdout, OFFSET szAbout, slAbout, EAX, NULL
  112.       POP   EAX
  113.       JMP   Exit
  114.   @@: MOV   EBX, -2
  115.       INVOKE SetThreadExecutionState, ES_CONTINUOUS or ES_SYSTEM_REQUIRED
  116.       MOV   dwOldState, EAX
  117.       TEST  EAX, EAX
  118.       JNE   @F
  119.       PUSH  0
  120.       MOV   EAX, ESP
  121.       INVOKE WriteConsoleA, hStdout, OFFSET szErrorSetState, slErrorSetState, EAX, NULL
  122.       POP   EAX
  123.       JMP   Exit
  124.   @@: MOV   EDI, ESI
  125.       MOV   AX, 0
  126.       XOR   ECX, ECX
  127.       DEC   ECX         ; ECX = -1
  128.       REPNE SCASW
  129.       NEG   ECX
  130.       SHL   ECX, 1
  131.       PUSH  ECX
  132.       INVOKE LocalAlloc, LMEM_FIXED, ECX
  133.       MOV   EDI, EAX
  134.       POP   ECX
  135.       INVOKE Move, ESI, EDI, ECX
  136.      
  137.       INVOKE PathRemoveArgsW, EDI
  138.       INVOKE PathUnquoteSpacesW, EDI
  139.       INVOKE PathGetArgsW, ESI
  140.       MOV   ESI, EAX
  141.  
  142.       MOV   stSI.cb, SIZEOF STARTUPINFO
  143.       MOV   stSI.dwFlags, STARTF_FORCEOFFFEEDBACK
  144.       INVOKE CreateProcessW, EDI, ESI, NULL, NULL, FALSE, CREATE_DEFAULT_ERROR_MODE, NULL, NULL, OFFSET stSI, OFFSET stPI
  145.       PUSH  EAX
  146.       INVOKE LocalFree, EDI
  147.       POP   EAX
  148.       TEST  EAX, EAX
  149.       JNE   @F
  150.       INVOKE wsprintfW, OFFSET szBuffer, OFFSET szCreateProcessError, EDI, ESI
  151.       PUSH  0
  152.       MOV   ECX, ESP
  153.       INVOKE WriteConsoleW, hStdout, OFFSET szBuffer, EAX, ECX, NULL
  154.       MOV   EAX, ESP
  155.       INVOKE WriteConsoleA, hStdout, OFFSET szCreateProcessAdd, slCreateProcessAdd, EAX, NULL
  156.       POP   EAX
  157.       JMP   ExitRetState
  158.   @@: INVOKE CloseHandle, stPI.hThread
  159.       INVOKE WaitForSingleObject, stPI.hProcess, INFINITE
  160.       CMP   EAX, WAIT_OBJECT_0
  161.       JE    @F
  162.       INVOKE wsprintfA, OFFSET szBuffer, OFFSET szWaitForError, EAX
  163.       PUSH  0
  164.       MOV   ECX, ESP
  165.       INVOKE WriteConsoleA, hStdout, OFFSET szBuffer, EAX, ECX, NULL
  166.       POP   EAX
  167.       JMP   ExitCloseProcess
  168.   @@: PUSH  0
  169.       INVOKE GetExitCodeProcess, stPI.hProcess, ESP
  170.       TEST  EAX, EAX
  171.       POP   EAX
  172.       JE    @F
  173.       INVOKE wsprintfA, OFFSET szBuffer, OFFSET szGetExitCodeError, EAX
  174.       PUSH  0
  175.       MOV   ECX, ESP
  176.       INVOKE WriteConsoleA, hStdout, OFFSET szBuffer, EAX, ECX, NULL
  177.       POP   EAX
  178.       JMP   ExitCloseProcess
  179.   @@: MOV   EBX, EAX
  180.   ExitCloseProcess:
  181.       INVOKE CloseHandle, stPI.hProcess
  182.   ExitRetState:
  183.       MOV   EAX, dwOldState
  184.       OR    EAX, ES_CONTINUOUS
  185.       INVOKE SetThreadExecutionState, EAX
  186.   Exit:
  187.       INVOKE ExitProcess, EBX
  188.  
  189. END Start
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×