使用 Golang 读取随机内存位置
Read Random Memory Locations with Golang
晚上好,
我一直在尝试构建一个 golang 应用程序来扫描内存中的值,但我正在努力了解如何寻址特定的内存位置。我知道在应用程序中访问内存时,您可以使用 *variablename
来引用并获取地址位置,但是我将如何提供地址位置并将值打印到屏幕或从中获取任何大小的下一个分配对象RAM 并打印它的值?
提前感谢您愿意分享的任何帮助
我不知道这会有多大用处,但这里有一个示例代码。
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")
}
使用 "unsafe" 包不是一个好主意,我相信你也不能读取任意位置。
对我来说,当我尝试其他一些随机位置时,很可能我没有读取权限,它给我带来了这样的错误:
unexpected fault address 0xc41ff8f780
fatal error: fault
[signal SIGBUS: bus error code=0x2 addr=0xc41ff8f780 pc=0x1093ec0]
但希望它能对您有所帮助。
晚上好,
我一直在尝试构建一个 golang 应用程序来扫描内存中的值,但我正在努力了解如何寻址特定的内存位置。我知道在应用程序中访问内存时,您可以使用 *variablename
来引用并获取地址位置,但是我将如何提供地址位置并将值打印到屏幕或从中获取任何大小的下一个分配对象RAM 并打印它的值?
提前感谢您愿意分享的任何帮助
我不知道这会有多大用处,但这里有一个示例代码。
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")
}
使用 "unsafe" 包不是一个好主意,我相信你也不能读取任意位置。
对我来说,当我尝试其他一些随机位置时,很可能我没有读取权限,它给我带来了这样的错误:
unexpected fault address 0xc41ff8f780
fatal error: fault
[signal SIGBUS: bus error code=0x2 addr=0xc41ff8f780 pc=0x1093ec0]
但希望它能对您有所帮助。