亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Go 中使用 NtCreateSection?

如何在 Go 中使用 NtCreateSection?

Go
Cats萌萌 2022-07-04 16:44:54
我正在嘗試學習如何在 Go 中使用一些 Windows API 調用,特別是一些較低級別的、未記錄的函數。我在這里關注一個 C++ 示例并嘗試跟隨 Go。下面是我正在嘗試做的輸出和精簡示例:c0000005:操作成功完成。package mainimport (    "fmt"    "golang.org/x/sys/windows")// Why are these not defined in the windows pkg?!const SEC_COMMIT = 0x08000000const SECTION_WRITE = 0x2const SECTION_READ = 0x4const SECTION_EXECUTE = 0x8const SECTION_RWX = SECTION_WRITE | SECTION_READ | SECTION_EXECUTEfunc createSection() error {    var e error    var err uintptr    var ntdll *windows.LazyDLL    var section uintptr    // Load DLL    ntdll = windows.NewLazySystemDLL("ntdll")    // FIXME Allocate section    err, _, e = ntdll.NewProc("NtCreateSection").Call(        section,        SECTION_RWX,        0,        uintptr(512),        windows.PAGE_EXECUTE_READWRITE,        SEC_COMMIT,        0,    )    if err != 0 {        return fmt.Errorf("%0x: %s", uint32(err), e.Error())    } else if section == 0 {        return fmt.Errorf("NtCreateSection failed for unknown reason")    }    fmt.Printf("%0x\n", section)    return nil}func main() {    var e error    if e = createSection(); e != nil {        fmt.Println(e.Error())    }}據我所知,c0000005這是一個Access Violation錯誤。C++ 中的相同代碼似乎可以工作(參見鏈接博客中的第 25 行),但是數據類型略有不同。可能我使用var section uintptr不當。C++ 使用對 a 的引用HANDLE,我認為它也稱為PHANDLE. 圍棋似乎改為使用uintptr。任何幫助將不勝感激!
查看完整描述

1 回答

?
滄海一幻覺

TA貢獻1824條經驗 獲得超5個贊

您的問題是該函數采用節句柄和大小的指針參數。


這似乎有效:


package main


import (

    "fmt"

    "unsafe"


    "golang.org/x/sys/windows"

)


// Why are these not defined in the windows pkg?!

const SEC_COMMIT = 0x8000000

const SECTION_WRITE = 0x2

const SECTION_READ = 0x4

const SECTION_EXECUTE = 0x8

const SECTION_RWX = SECTION_WRITE | SECTION_READ | SECTION_EXECUTE


func createSection() error {

    var e error

    var err uintptr

    var ntdll *windows.LazyDLL

    var section uintptr


    // Load DLL

    ntdll = windows.NewLazySystemDLL("ntdll")

    size := int64(4096)

    // FIXME Allocate section

    err, _, e = ntdll.NewProc("NtCreateSection").Call(

        uintptr(unsafe.Pointer(&section)),

        SECTION_RWX,

        0,

        uintptr(unsafe.Pointer(&size)),

        windows.PAGE_EXECUTE_READWRITE,

        SEC_COMMIT,

        0,

    )

    if err != 0 {

        return fmt.Errorf("%0x: %s", uint32(err), e.Error())

    } else if section == 0 {

        return fmt.Errorf("NtCreateSection failed for unknown reason")

    }

    fmt.Printf("%0x\n", section)


    return nil

}


func main() {

    var e error


    if e = createSection(); e != nil {

        fmt.Println(e.Error())

    }

}


查看完整回答
反對 回復 2022-07-04
  • 1 回答
  • 0 關注
  • 204 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號