douluozhan4370 2016-05-21 03:10
浏览 73
已采纳

使用Golang读取随机内存位置

Good evening,

I've been trying to build a golang application which scans values in memory but am struggling trying to understand how to address specific memory locations. I know that when accessing memory within the application you can use *variablename to deference and get the address location, but how would I provide an address location and print the value to the screen or grab the next allocated object of any size from RAM and print it's value?

Thanks in advance for any help you may be willing to share

  • 写回答

1条回答 默认 最新

  • duanguanya3052 2017-11-06 16:35
    关注

    I don't know how much useful this will be, but here is a sample code.

    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("
    ")
        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("
    ")
    }
    

    Using "unsafe" package is not a good idea, also you can not read any arbitrary location I believe.

    For me when I tried some other random locations where, most probably, I didn't have read access, it threw me error like this:

    unexpected fault address 0xc41ff8f780
    fatal error: fault
    [signal SIGBUS: bus error code=0x2 addr=0xc41ff8f780 pc=0x1093ec0]
    

    But hopefully, it can be of some value to you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)