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

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

使用 Golang 讀取隨機內存位置

使用 Golang 讀取隨機內存位置

Go
至尊寶的傳說 2022-01-10 17:42:03
晚上好,我一直在嘗試構建一個 golang 應用程序來掃描內存中的值,但我正在努力理解如何解決特定的內存位置。我知道在訪問應用程序中的內存時,您可以使用*variablename來尊重并獲取地址位置,但是我將如何提供地址位置并將值打印到屏幕或從 RAM 中獲取下一個分配的任意大小的對象并打印它的值?提前感謝您可能愿意分享的任何幫助
查看完整描述

1 回答

?
翻過高山走不出你

TA貢獻1875條經驗 獲得超3個贊

我不知道這會有多大用處,但這里有一個示例代碼。


package main


import (

    "fmt"

    "unsafe"

)


func main() {

    var i int = 1

    fmt.Println("Address : ", &i, " Value : ", i)


    var address *int

    address = &i // getting the starting address


    loc := (uintptr)(unsafe.Pointer(address))

    p := unsafe.Pointer(loc)


    // verification - it should print 1

    var val int = *((* int)(p))

    fmt.Println("Location : ", loc, " Val :",val) // it does print !!


    // lets print 1000 bytes starting from address of variable i

    // first memory location contains 1 as expected

    printValueAtMemoryLocation(loc, 1000)


    // now lets test for some arbitrary memory location

    // not so random ! wanted to reduce the diff value also any arbitrary memory location you can't read !!

    memoryToReach := 842350500000

    loc = changeToInputLocation(loc, memoryToReach)

    fmt.Println("Loc is now at : ", loc)

    // lets print 1000 bytes starting from the memory location "memoryToReach"

    printValueAtMemoryLocation(loc, 1000)


}


func changeToInputLocation(location uintptr, locationToreach int) uintptr {

    var diff,i int

    diff = locationToreach - int(location)


    fmt.Println("We need to travel ", diff, " memory locations !")


    if diff < 0 {

        i= diff * -1

        for i > 0 {

            location--

            i--

        }

    } else {

        i= diff

        for i > 0 {

            location++

            i--

        }

    }

    return location

}


func printValueAtMemoryLocation(location uintptr, next int) {

    var v byte

    p := unsafe.Pointer(location)

    fmt.Println("\n")

    for i:=1; i<next; i++ {

        p = unsafe.Pointer(location)

        v = *((*byte)(p))

        fmt.Print(v," ")

        //fmt.Println("Loc : ", loc, " --- Val : ", v)

        location++

    }

    fmt.Println("\n")

}

使用“不安全”包不是一個好主意,我相信你也不能閱讀任何任意位置。


對我來說,當我嘗試其他一些隨機位置時,很可能我沒有讀取權限,它給我帶來了如下錯誤:


unexpected fault address 0xc41ff8f780

fatal error: fault

[signal SIGBUS: bus error code=0x2 addr=0xc41ff8f780 pc=0x1093ec0]

但希望它對你有一些價值。


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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