飘云阁

 找回密码
 加入我们

QQ登录

只需一步,快速开始

查看: 3491|回复: 5

【推荐】Win32 汇编 强制关机源码实现

[复制链接]
  • TA的每日心情
    难过
    2022-2-6 09:25
  • 签到天数: 6 天

    [LV.2]偶尔看看I

    发表于 2008-10-4 08:57:55 | 显示全部楼层 |阅读模式
    转 自: ARTeam
    整 理: Playboysen
    原作者:ragdog/Nacho_dj/mia/Gunther



    Before you make shutdown must you set the AdjustToken privilege
    1. ; For NT Type Platforms get the privilege for a Shutdown/Restart.
    2. AdjustToken proc

    3.     LOCAL hdlProcessHandle:DWORD
    4.     LOCAL hdlTokenHandle:DWORD
    5.     LOCAL tmpLuid:LUIDCUST
    6.     LOCAL tkp:TOKEN_PRIVS
    7.     LOCAL tkpNewButIgnored:TOKEN_PRIVS
    8.     LOCAL lBufferNeeded:DWORD
    9.     LOCAL tBuff[32]:BYTE
    10.     LOCAL ptBuff:DWORD

    11.     invoke GetCurrentProcess      ; get the current process handle
    12.     mov hdlProcessHandle,eax      ; save it to hdlProcessHandle

    13.     lea eax, tBuff                ; address of temp buffer into eax
    14.     mov ptBuff, eax               ; set pointer to temp buffer
    15.     mov BYTE PTR [eax], 0         ; initialize the buffer

    16.     invoke OpenProcessToken,hdlProcessHandle,40,ADDR hdlTokenHandle
    17.     invoke LookupPrivilegeValue,ptBuff, SADD("SeShutdownPrivilege"), ADDR tmpLuid

    18.     lea eax, tmpLuid              ; address of tmpLuid into eax

    19.    ; Contents of tmpLuid into ecx:edx
    20.     mov ecx, (LUIDCUST PTR [eax]).usedpart
    21.     mov edx, (LUIDCUST PTR [eax]).ignorehigh32bitpart

    22.     lea eax, tkp                  ; address of tkp into eax

    23.     mov (TOKEN_PRIVS PTR [eax]).privilegecount, 1
    24.     mov (TOKEN_PRIVS PTR [eax]).theluid.usedpart, ecx
    25.     mov (TOKEN_PRIVS PTR [eax]).theluid.ignorehigh32bitpart, edx
    26.     mov (TOKEN_PRIVS PTR [eax]).attributes, 2

    27.     invoke AdjustTokenPrivileges,hdlTokenHandle,0,ADDR tkp,\
    28.         SizeOf tkpNewButIgnored,ADDR tkpNewButIgnored,ADDR lBufferNeeded

    29.     ret

    30. AdjustToken endp



    31. LogoutNOW proc
    32.     invoke ExitWindowsEx,EWX_LOGOFF,0 ; Logout the machine
    33.     ret
    34. LogoutNOW endp

    35. ShutdownNOW proc
    36.     invoke ExitWindowsEx,EWX_SHUTDOWN + EWX_FORCE + EWX_POWEROFF,NULL ; Shutdown the machine
    37.     ret
    38. ShutdownNOW endp

    39. RestartNOW proc
    40.     invoke ExitWindowsEx,EWX_REBOOT,0 ; Restart the machine
    41.     ret
    42. RestartNOW endp
    复制代码
    ***********************************************************************************************************************

    The simply way for code cave can you write this shutdown function in a dll
    1. DllEntryPoint proc hInstDLL:DWORD, reason:DWORD, unused:DWORD

    2.     mov eax,reason
    3.     .if eax == DLL_PROCESS_ATTACH; Called when our dll loaded

    4.     invoke AdjustToken
    5.         invoke ShutdownNOW
    6.     .endif
    7.     ret


    8. DllEntryPoint endp

    9. ; For NT Type Platforms get the privilege for a Shutdown/Restart.
    10. AdjustToken proc

    11.     LOCAL hdlProcessHandle:DWORD
    12.     LOCAL hdlTokenHandle:DWORD
    13.     LOCAL tmpLuid:LUIDCUST
    14.     LOCAL tkp:TOKEN_PRIVS
    15.     LOCAL tkpNewButIgnored:TOKEN_PRIVS
    16.     LOCAL lBufferNeeded:DWORD
    17.     LOCAL tBuff[32]:BYTE
    18.     LOCAL ptBuff:DWORD

    19.     invoke GetCurrentProcess     ; get the current process handle
    20.     mov hdlProcessHandle,eax     ; save it to hdlProcessHandle

    21.     lea eax, tBuff               ; address of temp buffer into eax
    22.     mov ptBuff, eax              ; set pointer to temp buffer
    23.     mov BYTE PTR [eax], 0        ; initialize the buffer

    24.     invoke OpenProcessToken,hdlProcessHandle,40,ADDR hdlTokenHandle
    25.     invoke LookupPrivilegeValue,ptBuff, SADD("SeShutdownPrivilege"), ADDR tmpLuid

    26.     lea eax, tmpLuid             ; address of tmpLuid into eax

    27.   ; Contents of tmpLuid into ecx:edx
    28.     mov ecx, (LUIDCUST PTR [eax]).usedpart
    29.     mov edx, (LUIDCUST PTR [eax]).ignorehigh32bitpart

    30.     lea eax, tkp                 ; address of tkp into eax

    31.     mov (TOKEN_PRIVS PTR [eax]).privilegecount, 1
    32.     mov (TOKEN_PRIVS PTR [eax]).theluid.usedpart, ecx
    33.     mov (TOKEN_PRIVS PTR [eax]).theluid.ignorehigh32bitpart, edx
    34.     mov (TOKEN_PRIVS PTR [eax]).attributes, 2

    35.     invoke AdjustTokenPrivileges,hdlTokenHandle,0,ADDR tkp,\
    36.         SizeOf tkpNewButIgnored,ADDR tkpNewButIgnored,ADDR lBufferNeeded

    37.     ret

    38. AdjustToken endp



    39. LogoutNOW proc
    40.     invoke ExitWindowsEx,EWX_LOGOFF,0; Logout the machine
    41.     ret
    42. LogoutNOW endp

    43. ShutdownNOW proc
    44.     invoke ExitWindowsEx,EWX_SHUTDOWN + EWX_FORCE + EWX_POWEROFF,NULL; Shutdown the machine
    45.     ret
    46. ShutdownNOW endp

    47. RestartNOW proc
    48.     invoke ExitWindowsEx,EWX_REBOOT,0; Restart the machine
    49.     ret
    50. RestartNOW endp
    复制代码
    In the target add this code

    push 00401155
    Call LoadLibraryA

    00401155 . 53 68 75 74 6>ASCII "Shutdown.dll",0

    ***********************************************************************************************************************
    There is one undocumented api in ntdll.dll. Its "NtShutdownSystem", which can be used for really quick(instant) shutdown and restart. It accepts one parameter 0 for shutdown and 1 for restart.

    ***********************************************************************************************************************
    Well...this is from FASM forums...hope it's useful to u.
    1. ;tested only in XP SP2
    2. ;/-----------------------------------------------------------------------------\
    3. ;   File        : shutdown.asm.
    4. ;   Author      : Ancient One.
    5. ;\-----------------------------------------------------------------------------/

    6. define imageBase 0x10000
    7. ShutdownPowerOff=2
    8. SeShutdownPrivilege=0x13
    9. SE_PRIVILEGE_ENABLED=0x2
    10. TOKEN_ADJUST_PRIVILEGES=0x20

    11. NtAdjustPrivilegesToken=011
    12. NtOpenProcessToken=123
    13. NtShutdownSystem=249

    14. use32
    15. dosHeader:
    16. dw 'MZ'
    17. dw 0

    18. ntHeader:
    19. dd 'PE'
    20. dw 0x14c
    21. dw 0
    22. entryPoint:
    23. _12_bytes :
    24.     mov     edi, _sysEnter+imageBase
    25.   ;store 0xCC at _12_bytes
    26.     mov     ebx, esp
    27.     push    ebx
    28.     push    TOKEN_ADJUST_PRIVILEGES
    29.     jmp     _08_bytes_a
    30. dw sizeof.optionalHeader
    31. dw 0x102

    32. optionalHeader:
    33. dw 0x10b

    34. _14_bytes :
    35.     call    edi
    36.     push    ShutdownPowerOff eax SeShutdownPrivilege 1
    37.     mov     ebp, esp
    38.     push    eax
    39.     jmp     _06_bytes
    40. dd entryPoint

    41. _08_bytes_a :
    42.     push    (-1) ebx NtOpenProcessToken
    43.     pop     eax
    44.     jmp     _14_bytes

    45. dd imageBase
    46. dd 4
    47. dd 4

    48. _08_bytes_b :
    49.     push    ebp
    50.     mov     al, NtAdjustPrivilegesToken
    51.     call    edi
    52.     leave
    53.     jmp     _xx_bytes

    54. dw 3

    55. _06_bytes :
    56.     push    eax eax ebp eax
    57.     jmp     _04_bytes

    58. dd sizeof.image
    59. dd sizeof.peHeaders

    60. _04_bytes :
    61.     push    dword [ebx]
    62.     jmp     _08_bytes_b
    63. dw 2
    64. sizeof.optionalHeader = $-optionalHeader
    65. sizeof.peHeaders = sizeof.optionalHeader
    66. _xx_bytes:
    67.     mov     al, NtShutdownSystem
    68. _sysEnter:
    69.     mov     edx, esp
    70.     sysenter
    71. sizeof.image=$


    72. or this one? Hope it helps.

    73. CODE
    74. .686
    75. .model flat,stdcall
    76. include ntdll.inc
    77. include kernel32.inc
    78. .code
    79. start: push esp
    80. invoke RtlAdjustPrivilege,19,2,0,esp
    81. invoke NtShutdownSystem,2
    82. jmp ExitProcess
    83. end start
    复制代码

    [ 本帖最后由 playboysen 于 2008-10-4 08:59 编辑 ]
    PYG19周年生日快乐!
  • TA的每日心情
    难过
    2018-4-20 11:27
  • 签到天数: 22 天

    [LV.4]偶尔看看III

    发表于 2008-10-16 18:36:00 | 显示全部楼层
    看不懂!!!!/:010
    PYG19周年生日快乐!

    该用户从未签到

    发表于 2009-4-28 20:22:36 | 显示全部楼层
    感谢了,努力学习汇编中/:001
    PYG19周年生日快乐!

    该用户从未签到

    发表于 2009-5-1 07:23:57 | 显示全部楼层
    我比你还菜!/:17
    PYG19周年生日快乐!
  • TA的每日心情
    开心
    3 小时前
  • 签到天数: 662 天

    [LV.9]以坛为家II

    发表于 2009-5-23 23:29:49 | 显示全部楼层
    英文的解释,哎!!
    PYG19周年生日快乐!
  • TA的每日心情
    开心
    2017-10-17 17:46
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2009-5-27 09:45:01 | 显示全部楼层
    谢谢楼主发布分享

    学习之
    PYG19周年生日快乐!
    您需要登录后才可以回帖 登录 | 加入我们

    本版积分规则

    快速回复 返回顶部 返回列表