ホーム › System.Antimalware › AmsiNotifyOperation
AmsiNotifyOperation
関数操作内容をAMSIへ通知し脅威判定を受ける。
シグネチャ
// Amsi.dll
#include <windows.h>
HRESULT AmsiNotifyOperation(
HAMSICONTEXT amsiContext,
void* buffer,
DWORD length,
LPCWSTR contentName, // optional
AMSI_RESULT* result
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| amsiContext | HAMSICONTEXT | in | 通知に使うAMSIコンテキストハンドル。 |
| buffer | void* | in | 通知する操作内容を格納したバッファへのポインタ。 |
| length | DWORD | in | bufferのバイト長を示すDWORD値。 |
| contentName | LPCWSTR | inoptional | 操作対象の名前を示す広域文字列。NULL可。 |
| result | AMSI_RESULT* | out | 判定結果を受け取るAMSI_RESULT列挙ポインタ。 |
戻り値の型: HRESULT
各言語での呼び出し定義
// Amsi.dll
#include <windows.h>
HRESULT AmsiNotifyOperation(
HAMSICONTEXT amsiContext,
void* buffer,
DWORD length,
LPCWSTR contentName, // optional
AMSI_RESULT* result
);[DllImport("Amsi.dll", ExactSpelling = true)]
static extern int AmsiNotifyOperation(
IntPtr amsiContext, // HAMSICONTEXT
IntPtr buffer, // void*
uint length, // DWORD
[MarshalAs(UnmanagedType.LPWStr)] string contentName, // LPCWSTR optional
out int result // AMSI_RESULT* out
);<DllImport("Amsi.dll", ExactSpelling:=True)>
Public Shared Function AmsiNotifyOperation(
amsiContext As IntPtr, ' HAMSICONTEXT
buffer As IntPtr, ' void*
length As UInteger, ' DWORD
<MarshalAs(UnmanagedType.LPWStr)> contentName As String, ' LPCWSTR optional
<Out> ByRef result As Integer ' AMSI_RESULT* out
) As Integer
End Function' amsiContext : HAMSICONTEXT
' buffer : void*
' length : DWORD
' contentName : LPCWSTR optional
' result : AMSI_RESULT* out
Declare PtrSafe Function AmsiNotifyOperation Lib "amsi" ( _
ByVal amsiContext As LongPtr, _
ByVal buffer As LongPtr, _
ByVal length As Long, _
ByVal contentName As LongPtr, _
ByRef result As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
AmsiNotifyOperation = ctypes.windll.amsi.AmsiNotifyOperation
AmsiNotifyOperation.restype = ctypes.c_int
AmsiNotifyOperation.argtypes = [
wintypes.HANDLE, # amsiContext : HAMSICONTEXT
ctypes.POINTER(None), # buffer : void*
wintypes.DWORD, # length : DWORD
wintypes.LPCWSTR, # contentName : LPCWSTR optional
ctypes.c_void_p, # result : AMSI_RESULT* out
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('Amsi.dll')
AmsiNotifyOperation = Fiddle::Function.new(
lib['AmsiNotifyOperation'],
[
Fiddle::TYPE_VOIDP, # amsiContext : HAMSICONTEXT
Fiddle::TYPE_VOIDP, # buffer : void*
-Fiddle::TYPE_INT, # length : DWORD
Fiddle::TYPE_VOIDP, # contentName : LPCWSTR optional
Fiddle::TYPE_VOIDP, # result : AMSI_RESULT* out
],
Fiddle::TYPE_INT)#[link(name = "amsi")]
extern "system" {
fn AmsiNotifyOperation(
amsiContext: *mut core::ffi::c_void, // HAMSICONTEXT
buffer: *mut (), // void*
length: u32, // DWORD
contentName: *const u16, // LPCWSTR optional
result: *mut i32 // AMSI_RESULT* out
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("Amsi.dll")]
public static extern int AmsiNotifyOperation(IntPtr amsiContext, IntPtr buffer, uint length, [MarshalAs(UnmanagedType.LPWStr)] string contentName, out int result);
"@
$api = Add-Type -MemberDefinition $sig -Name 'Amsi_AmsiNotifyOperation' -Namespace Win32 -PassThru
# $api::AmsiNotifyOperation(amsiContext, buffer, length, contentName, result)#uselib "Amsi.dll"
#func global AmsiNotifyOperation "AmsiNotifyOperation" sptr, sptr, sptr, sptr, sptr
; AmsiNotifyOperation amsiContext, buffer, length, contentName, varptr(result) ; 戻り値は stat
; amsiContext : HAMSICONTEXT -> "sptr"
; buffer : void* -> "sptr"
; length : DWORD -> "sptr"
; contentName : LPCWSTR optional -> "sptr"
; result : AMSI_RESULT* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "Amsi.dll" #cfunc global AmsiNotifyOperation "AmsiNotifyOperation" sptr, sptr, int, wstr, var ; res = AmsiNotifyOperation(amsiContext, buffer, length, contentName, result) ; amsiContext : HAMSICONTEXT -> "sptr" ; buffer : void* -> "sptr" ; length : DWORD -> "int" ; contentName : LPCWSTR optional -> "wstr" ; result : AMSI_RESULT* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "Amsi.dll" #cfunc global AmsiNotifyOperation "AmsiNotifyOperation" sptr, sptr, int, wstr, sptr ; res = AmsiNotifyOperation(amsiContext, buffer, length, contentName, varptr(result)) ; amsiContext : HAMSICONTEXT -> "sptr" ; buffer : void* -> "sptr" ; length : DWORD -> "int" ; contentName : LPCWSTR optional -> "wstr" ; result : AMSI_RESULT* out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; HRESULT AmsiNotifyOperation(HAMSICONTEXT amsiContext, void* buffer, DWORD length, LPCWSTR contentName, AMSI_RESULT* result) #uselib "Amsi.dll" #cfunc global AmsiNotifyOperation "AmsiNotifyOperation" intptr, intptr, int, wstr, var ; res = AmsiNotifyOperation(amsiContext, buffer, length, contentName, result) ; amsiContext : HAMSICONTEXT -> "intptr" ; buffer : void* -> "intptr" ; length : DWORD -> "int" ; contentName : LPCWSTR optional -> "wstr" ; result : AMSI_RESULT* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; HRESULT AmsiNotifyOperation(HAMSICONTEXT amsiContext, void* buffer, DWORD length, LPCWSTR contentName, AMSI_RESULT* result) #uselib "Amsi.dll" #cfunc global AmsiNotifyOperation "AmsiNotifyOperation" intptr, intptr, int, wstr, intptr ; res = AmsiNotifyOperation(amsiContext, buffer, length, contentName, varptr(result)) ; amsiContext : HAMSICONTEXT -> "intptr" ; buffer : void* -> "intptr" ; length : DWORD -> "int" ; contentName : LPCWSTR optional -> "wstr" ; result : AMSI_RESULT* out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
amsi = windows.NewLazySystemDLL("Amsi.dll")
procAmsiNotifyOperation = amsi.NewProc("AmsiNotifyOperation")
)
// amsiContext (HAMSICONTEXT), buffer (void*), length (DWORD), contentName (LPCWSTR optional), result (AMSI_RESULT* out)
r1, _, err := procAmsiNotifyOperation.Call(
uintptr(amsiContext),
uintptr(buffer),
uintptr(length),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(contentName))),
uintptr(result),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // HRESULTfunction AmsiNotifyOperation(
amsiContext: THandle; // HAMSICONTEXT
buffer: Pointer; // void*
length: DWORD; // DWORD
contentName: PWideChar; // LPCWSTR optional
result: Pointer // AMSI_RESULT* out
): Integer; stdcall;
external 'Amsi.dll' name 'AmsiNotifyOperation';result := DllCall("Amsi\AmsiNotifyOperation"
, "Ptr", amsiContext ; HAMSICONTEXT
, "Ptr", buffer ; void*
, "UInt", length ; DWORD
, "WStr", contentName ; LPCWSTR optional
, "Ptr", result ; AMSI_RESULT* out
, "Int") ; return: HRESULT●AmsiNotifyOperation(amsiContext, buffer, length, contentName, result) = DLL("Amsi.dll", "int AmsiNotifyOperation(void*, void*, dword, char*, void*)")
# 呼び出し: AmsiNotifyOperation(amsiContext, buffer, length, contentName, result)
# amsiContext : HAMSICONTEXT -> "void*"
# buffer : void* -> "void*"
# length : DWORD -> "dword"
# contentName : LPCWSTR optional -> "char*"
# result : AMSI_RESULT* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "amsi" fn AmsiNotifyOperation(
amsiContext: ?*anyopaque, // HAMSICONTEXT
buffer: ?*anyopaque, // void*
length: u32, // DWORD
contentName: [*c]const u16, // LPCWSTR optional
result: [*c]i32 // AMSI_RESULT* out
) callconv(std.os.windows.WINAPI) i32;proc AmsiNotifyOperation(
amsiContext: pointer, # HAMSICONTEXT
buffer: pointer, # void*
length: uint32, # DWORD
contentName: WideCString, # LPCWSTR optional
result: ptr int32 # AMSI_RESULT* out
): int32 {.importc: "AmsiNotifyOperation", stdcall, dynlib: "Amsi.dll".}pragma(lib, "amsi");
extern(Windows)
int AmsiNotifyOperation(
void* amsiContext, // HAMSICONTEXT
void* buffer, // void*
uint length, // DWORD
const(wchar)* contentName, // LPCWSTR optional
int* result // AMSI_RESULT* out
);ccall((:AmsiNotifyOperation, "Amsi.dll"), stdcall, Int32,
(Ptr{Cvoid}, Ptr{Cvoid}, UInt32, Cwstring, Ptr{Int32}),
amsiContext, buffer, length, contentName, result)
# amsiContext : HAMSICONTEXT -> Ptr{Cvoid}
# buffer : void* -> Ptr{Cvoid}
# length : DWORD -> UInt32
# contentName : LPCWSTR optional -> Cwstring
# result : AMSI_RESULT* out -> Ptr{Int32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t AmsiNotifyOperation(
void* amsiContext,
void* buffer,
uint32_t length,
const uint16_t* contentName,
int32_t* result);
]]
local amsi = ffi.load("amsi")
-- amsi.AmsiNotifyOperation(amsiContext, buffer, length, contentName, result)
-- amsiContext : HAMSICONTEXT
-- buffer : void*
-- length : DWORD
-- contentName : LPCWSTR optional
-- result : AMSI_RESULT* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('Amsi.dll');
const AmsiNotifyOperation = lib.func('__stdcall', 'AmsiNotifyOperation', 'int32_t', ['void *', 'void *', 'uint32_t', 'str16', 'int32_t *']);
// AmsiNotifyOperation(amsiContext, buffer, length, contentName, result)
// amsiContext : HAMSICONTEXT -> 'void *'
// buffer : void* -> 'void *'
// length : DWORD -> 'uint32_t'
// contentName : LPCWSTR optional -> 'str16'
// result : AMSI_RESULT* out -> 'int32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("Amsi.dll", {
AmsiNotifyOperation: { parameters: ["pointer", "pointer", "u32", "buffer", "pointer"], result: "i32" },
});
// lib.symbols.AmsiNotifyOperation(amsiContext, buffer, length, contentName, result)
// amsiContext : HAMSICONTEXT -> "pointer"
// buffer : void* -> "pointer"
// length : DWORD -> "u32"
// contentName : LPCWSTR optional -> "buffer"
// result : AMSI_RESULT* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t AmsiNotifyOperation(
void* amsiContext,
void* buffer,
uint32_t length,
const uint16_t* contentName,
int32_t* result);
C, "Amsi.dll");
// $ffi->AmsiNotifyOperation(amsiContext, buffer, length, contentName, result);
// amsiContext : HAMSICONTEXT
// buffer : void*
// length : DWORD
// contentName : LPCWSTR optional
// result : AMSI_RESULT* out
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public interface Amsi extends StdCallLibrary {
Amsi INSTANCE = Native.load("amsi", Amsi.class);
int AmsiNotifyOperation(
Pointer amsiContext, // HAMSICONTEXT
Pointer buffer, // void*
int length, // DWORD
WString contentName, // LPCWSTR optional
IntByReference result // AMSI_RESULT* out
);
}@[Link("amsi")]
lib LibAmsi
fun AmsiNotifyOperation = AmsiNotifyOperation(
amsiContext : Void*, # HAMSICONTEXT
buffer : Void*, # void*
length : UInt32, # DWORD
contentName : UInt16*, # LPCWSTR optional
result : Int32* # AMSI_RESULT* out
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef AmsiNotifyOperationNative = Int32 Function(Pointer<Void>, Pointer<Void>, Uint32, Pointer<Utf16>, Pointer<Int32>);
typedef AmsiNotifyOperationDart = int Function(Pointer<Void>, Pointer<Void>, int, Pointer<Utf16>, Pointer<Int32>);
final AmsiNotifyOperation = DynamicLibrary.open('Amsi.dll')
.lookupFunction<AmsiNotifyOperationNative, AmsiNotifyOperationDart>('AmsiNotifyOperation');
// amsiContext : HAMSICONTEXT -> Pointer<Void>
// buffer : void* -> Pointer<Void>
// length : DWORD -> Uint32
// contentName : LPCWSTR optional -> Pointer<Utf16>
// result : AMSI_RESULT* out -> Pointer<Int32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function AmsiNotifyOperation(
amsiContext: THandle; // HAMSICONTEXT
buffer: Pointer; // void*
length: DWORD; // DWORD
contentName: PWideChar; // LPCWSTR optional
result: Pointer // AMSI_RESULT* out
): Integer; stdcall;
external 'Amsi.dll' name 'AmsiNotifyOperation';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "AmsiNotifyOperation"
c_AmsiNotifyOperation :: Ptr () -> Ptr () -> Word32 -> CWString -> Ptr Int32 -> IO Int32
-- amsiContext : HAMSICONTEXT -> Ptr ()
-- buffer : void* -> Ptr ()
-- length : DWORD -> Word32
-- contentName : LPCWSTR optional -> CWString
-- result : AMSI_RESULT* out -> Ptr Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let amsinotifyoperation =
foreign "AmsiNotifyOperation"
((ptr void) @-> (ptr void) @-> uint32_t @-> (ptr uint16_t) @-> (ptr int32_t) @-> returning int32_t)
(* amsiContext : HAMSICONTEXT -> (ptr void) *)
(* buffer : void* -> (ptr void) *)
(* length : DWORD -> uint32_t *)
(* contentName : LPCWSTR optional -> (ptr uint16_t) *)
(* result : AMSI_RESULT* out -> (ptr int32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library amsi (t "Amsi.dll"))
(cffi:use-foreign-library amsi)
(cffi:defcfun ("AmsiNotifyOperation" amsi-notify-operation :convention :stdcall) :int32
(amsi-context :pointer) ; HAMSICONTEXT
(buffer :pointer) ; void*
(length :uint32) ; DWORD
(content-name (:string :encoding :utf-16le)) ; LPCWSTR optional
(result :pointer)) ; AMSI_RESULT* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $AmsiNotifyOperation = Win32::API::More->new('Amsi',
'int AmsiNotifyOperation(HANDLE amsiContext, LPVOID buffer, DWORD length, LPCWSTR contentName, LPVOID result)');
# my $ret = $AmsiNotifyOperation->Call($amsiContext, $buffer, $length, $contentName, $result);
# amsiContext : HAMSICONTEXT -> HANDLE
# buffer : void* -> LPVOID
# length : DWORD -> DWORD
# contentName : LPCWSTR optional -> LPCWSTR
# result : AMSI_RESULT* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
使用する型