DuplicateToken
関数アクセストークンを複製し偽装用トークンを作成する。
シグネチャ
// ADVAPI32.dll
#include <windows.h>
BOOL DuplicateToken(
HANDLE ExistingTokenHandle,
SECURITY_IMPERSONATION_LEVEL ImpersonationLevel,
HANDLE* DuplicateTokenHandle
);パラメーター
| 名前 | 型 | 方向 |
|---|---|---|
| ExistingTokenHandle | HANDLE | in |
| ImpersonationLevel | SECURITY_IMPERSONATION_LEVEL | in |
| DuplicateTokenHandle | HANDLE* | out |
戻り値の型: BOOL
各言語での呼び出し定義
// ADVAPI32.dll
#include <windows.h>
BOOL DuplicateToken(
HANDLE ExistingTokenHandle,
SECURITY_IMPERSONATION_LEVEL ImpersonationLevel,
HANDLE* DuplicateTokenHandle
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool DuplicateToken(
IntPtr ExistingTokenHandle, // HANDLE
int ImpersonationLevel, // SECURITY_IMPERSONATION_LEVEL
IntPtr DuplicateTokenHandle // HANDLE* out
);<DllImport("ADVAPI32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function DuplicateToken(
ExistingTokenHandle As IntPtr, ' HANDLE
ImpersonationLevel As Integer, ' SECURITY_IMPERSONATION_LEVEL
DuplicateTokenHandle As IntPtr ' HANDLE* out
) As Boolean
End Function' ExistingTokenHandle : HANDLE
' ImpersonationLevel : SECURITY_IMPERSONATION_LEVEL
' DuplicateTokenHandle : HANDLE* out
Declare PtrSafe Function DuplicateToken Lib "advapi32" ( _
ByVal ExistingTokenHandle As LongPtr, _
ByVal ImpersonationLevel As Long, _
ByVal DuplicateTokenHandle As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
DuplicateToken = ctypes.windll.advapi32.DuplicateToken
DuplicateToken.restype = wintypes.BOOL
DuplicateToken.argtypes = [
wintypes.HANDLE, # ExistingTokenHandle : HANDLE
ctypes.c_int, # ImpersonationLevel : SECURITY_IMPERSONATION_LEVEL
ctypes.c_void_p, # DuplicateTokenHandle : HANDLE* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('ADVAPI32.dll')
DuplicateToken = Fiddle::Function.new(
lib['DuplicateToken'],
[
Fiddle::TYPE_VOIDP, # ExistingTokenHandle : HANDLE
Fiddle::TYPE_INT, # ImpersonationLevel : SECURITY_IMPERSONATION_LEVEL
Fiddle::TYPE_VOIDP, # DuplicateTokenHandle : HANDLE* out
],
Fiddle::TYPE_INT)#[link(name = "advapi32")]
extern "system" {
fn DuplicateToken(
ExistingTokenHandle: *mut core::ffi::c_void, // HANDLE
ImpersonationLevel: i32, // SECURITY_IMPERSONATION_LEVEL
DuplicateTokenHandle: *mut *mut core::ffi::c_void // HANDLE* out
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", SetLastError = true)]
public static extern bool DuplicateToken(IntPtr ExistingTokenHandle, int ImpersonationLevel, IntPtr DuplicateTokenHandle);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_DuplicateToken' -Namespace Win32 -PassThru
# $api::DuplicateToken(ExistingTokenHandle, ImpersonationLevel, DuplicateTokenHandle)#uselib "ADVAPI32.dll"
#func global DuplicateToken "DuplicateToken" sptr, sptr, sptr
; DuplicateToken ExistingTokenHandle, ImpersonationLevel, DuplicateTokenHandle ; 戻り値は stat
; ExistingTokenHandle : HANDLE -> "sptr"
; ImpersonationLevel : SECURITY_IMPERSONATION_LEVEL -> "sptr"
; DuplicateTokenHandle : HANDLE* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "ADVAPI32.dll"
#cfunc global DuplicateToken "DuplicateToken" sptr, int, sptr
; res = DuplicateToken(ExistingTokenHandle, ImpersonationLevel, DuplicateTokenHandle)
; ExistingTokenHandle : HANDLE -> "sptr"
; ImpersonationLevel : SECURITY_IMPERSONATION_LEVEL -> "int"
; DuplicateTokenHandle : HANDLE* out -> "sptr"; BOOL DuplicateToken(HANDLE ExistingTokenHandle, SECURITY_IMPERSONATION_LEVEL ImpersonationLevel, HANDLE* DuplicateTokenHandle)
#uselib "ADVAPI32.dll"
#cfunc global DuplicateToken "DuplicateToken" intptr, int, intptr
; res = DuplicateToken(ExistingTokenHandle, ImpersonationLevel, DuplicateTokenHandle)
; ExistingTokenHandle : HANDLE -> "intptr"
; ImpersonationLevel : SECURITY_IMPERSONATION_LEVEL -> "int"
; DuplicateTokenHandle : HANDLE* out -> "intptr"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
procDuplicateToken = advapi32.NewProc("DuplicateToken")
)
// ExistingTokenHandle (HANDLE), ImpersonationLevel (SECURITY_IMPERSONATION_LEVEL), DuplicateTokenHandle (HANDLE* out)
r1, _, err := procDuplicateToken.Call(
uintptr(ExistingTokenHandle),
uintptr(ImpersonationLevel),
uintptr(DuplicateTokenHandle),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction DuplicateToken(
ExistingTokenHandle: THandle; // HANDLE
ImpersonationLevel: Integer; // SECURITY_IMPERSONATION_LEVEL
DuplicateTokenHandle: Pointer // HANDLE* out
): BOOL; stdcall;
external 'ADVAPI32.dll' name 'DuplicateToken';result := DllCall("ADVAPI32\DuplicateToken"
, "Ptr", ExistingTokenHandle ; HANDLE
, "Int", ImpersonationLevel ; SECURITY_IMPERSONATION_LEVEL
, "Ptr", DuplicateTokenHandle ; HANDLE* out
, "Int") ; return: BOOL●DuplicateToken(ExistingTokenHandle, ImpersonationLevel, DuplicateTokenHandle) = DLL("ADVAPI32.dll", "bool DuplicateToken(void*, int, void*)")
# 呼び出し: DuplicateToken(ExistingTokenHandle, ImpersonationLevel, DuplicateTokenHandle)
# ExistingTokenHandle : HANDLE -> "void*"
# ImpersonationLevel : SECURITY_IMPERSONATION_LEVEL -> "int"
# DuplicateTokenHandle : HANDLE* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。