• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            S.l.e!ep.¢%

            像打了激速一樣,以四倍的速度運轉(zhuǎn),開心的工作
            簡單、開放、平等的公司文化;尊重個性、自由與個人價值;
            posts - 1098, comments - 335, trackbacks - 0, articles - 1
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            The Fast Way to ShutDown the computer

            Posted on 2009-11-04 22:02 S.l.e!ep.¢% 閱讀(311) 評論(0)  編輯 收藏 引用 所屬分類: RootKit

            Demo_Executable

            ?

            Introduction

            Sometimes an application just needs to quickly shutdown Windows. Unfortunately, there is no well-documented method to do this, and Microsoft does not support it. In fact, neither do I. However, I am presenting two methods to do so just as a mere idea.

            Disclaimer: I am not responsible for any unwanted behavior of this program. If your computer doesn't start up again afterwards, it is not my fault. The program is presented merely as a "proof of concept".

            Background

            Many of the Nt/Zw functions inside the Windows kernel are documented, but some are not. The NtShutdownSystem function is documented pretty well here at NTInternals. The NtSetSystemPowerState function, however, is not.

            At the final stages of the Windows shutdown process, NtShutdownSystem is called. It is responsible for shutting down all drivers, flushing Registry hives and the disc cache, clearing the page file, etc. After doing so, it calls the NtSetSystemPowerState function.

            NtSetSystemPowerState then causes all plug-and-play devices to be shut down and the system to be either halted, powered off, or rebooted.

            However, calling these two functions without notifying the system first is extremely dangerous, and may cause instability in the system.

            Using the code

            Both NtShutdownSystem and NtSetSystemPowerState are inside NTDLL.DLL, which is why we have to load it first. We also have to enable the SE_SHUTDOWN_NAME privilege in order to call it:

            Collapse Copy Code
            OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY|TOKEN_ADJUST_PRIVILEGES, &hToken);
            LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
            tkp.PrivilegeCount = 1;
            tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
            AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, NULL, 0);
            CloseHandle(hToken); 
            
            hNTDLL = LoadLibrary("NTDLL.DLL");

            After doing so, we obtain the functions:

            Collapse Copy Code
            				typedef DWORD (WINAPI* lpNtShutdownSystem)(SHUTDOWN_ACTION Action);
            lpNtShutdownSystem NtShutdownSystem = 
               (lpNtShutdownSystem)GetProcAddress(hNTDLL, "NtShutdownSystem");
            
            typedef DWORD (WINAPI* lpNtSetSystemPowerState)(
            IN POWER_ACTION SystemAction,IN SYSTEM_POWER_STATE MinSystemState,IN ULONG Flags);
            lpNtSetSystemPowerState NtSetSystemPowerState =
               (lpNtSetSystemPowerState)GetProcAddress(hNTDLL, "NtSetSystemPowerState");

            As the typedefs show, NtShutdownSystem requires a SHUTDOWN_ACTION parameter, which is documented here. The three options, ShutdownNoReboot, ShutdownReboot, and ShutdownPowerOff are self-explanatory.

            NtSetSystemPowerState is a bit more complicated. Microsoft makes it a bit easier for us by documenting all three parameters. The first two, POWER_ACTION and SYSTEM_POWER_STATE, are documented well. The third, flags, is simply the reason for shutdown, which is also documented here on MSDN.

            Calling the actual functions is rather simple:

            Collapse Copy Code
            				void CEmergency_ShutdownDlg::OnShutdown() 
            {
                if (NtShutdownSystem)
                {
                    DWORD returnval = NtShutdownSystem(ShutdownPowerOff);
                    if (returnval != 0) FormatNtMessage(returnval);
                }
            }
            
            void CEmergency_ShutdownDlg::OnPowerdown() 
            {
                if (NtSetSystemPowerState)
                {
                    DWORD returnval = NtSetSystemPowerState(
                        PowerActionShutdownOff,
                        PowerSystemShutdown,
                        SHTDN_REASON_MAJOR_OTHER | 
                                   SHTDN_REASON_MINOR_OTHER | 
                                   SHTDN_REASON_FLAG_PLANNED
                        );
                    if (returnval != 0) FormatNtMessage(returnval);
                }
            }

            FormatNtMessage is simply a specialized function to display an error, given an NTSTATUS error code. It uses the FormatMessage function.

            Points of interest

            Only one point of interest: save all un-saved work before executing any one of the functions, or else it'd be rather interesting what happens to them.

            History

            I probably won't be updating this, unless there is a critical flaw anywhere in the code.

            • v1.0 - March 16, 2009.

            License

            This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

            About the Author

            hxhl95


            Member
            A random 14 year old guy in Vancouver with an avid interest in how Windows works. Started coding a few years ago, beginning with a bit of game design using GDI+ and some Windows programming. Recently moved onto messing with the Windows kernel and hoping to get into algorithm design soon.

            http://xkcd.com/424/
            Location: Canada Canada

            Other popular Win32/64 SDK & OS articles:

            伊人久久久AV老熟妇色| 精品国产福利久久久| 性做久久久久久免费观看| 国产精品无码久久久久| 久久免费99精品国产自在现线| 武侠古典久久婷婷狼人伊人| 亚洲人成伊人成综合网久久久| 久久电影网2021| 久久国产欧美日韩精品免费| 久久精品国产亚洲AV麻豆网站| 国产亚洲美女精品久久久 | 亚洲va中文字幕无码久久| 亚洲乱亚洲乱淫久久| 欧美伊人久久大香线蕉综合| 人人狠狠综合久久亚洲婷婷| 久久无码AV中文出轨人妻| 伊人色综合久久| 国产精品久久久久AV福利动漫| 青青久久精品国产免费看 | 中文精品久久久久人妻| 久久精品无码一区二区三区| 久久久久99精品成人片试看| 国产一区二区久久久| 久久免费视频一区| 亚洲乱亚洲乱淫久久| 狠狠色丁香婷婷久久综合不卡| 伊人久久大香线蕉成人| 久久强奷乱码老熟女网站| 久久99久久成人免费播放| 人人狠狠综合久久亚洲婷婷| 国产成人精品久久二区二区| 久久久久久久亚洲Av无码| 日本强好片久久久久久AAA| 伊人久久精品无码二区麻豆| 久久精品国产色蜜蜜麻豆| 四虎国产精品成人免费久久| 亚洲第一永久AV网站久久精品男人的天堂AV| 69国产成人综合久久精品| 久久国产高清字幕中文| 久久综合久久久| 2021国产成人精品久久|