GithubHelp home page GithubHelp logo

binject / universal Goto Github PK

View Code? Open in Web Editor NEW
211.0 211.0 26.0 117 KB

Universal Shared Library User-space Loader

License: GNU General Public License v3.0

Go 95.11% C++ 2.10% C 2.68% Batchfile 0.11%

universal's People

Contributors

awgh avatar lesnuages avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

universal's Issues

signal SIGSEGV - in test Mac M1

The example work, but fails during test
in go 1.18 I get the following message:

user@MACHINE universal % go test
map[_Runme:5037686692]
2022/07/12 11:41:14 334dad5e4 20895d640 
unexpected fault address 0x334dad5e4
fatal error: fault
[signal SIGSEGV: segmentation violation code=0x2 addr=0x334dad5e4 pc=0x334dad5e4]

goroutine 21 [running]:
runtime.throw({0x104316c0e?, 0x40?})
        /Users/user/homebrew/Cellar/go/1.18.3/libexec/src/runtime/panic.go:992 +0x50 fp=0x140000b5dc0 sp=0x140000b5d90 pc=0x10424aa20
runtime.sigpanic()
        /Users/user/homebrew/Cellar/go/1.18.3/libexec/src/runtime/signal_unix.go:825 +0x1a4 fp=0x140000b5df0 sp=0x140000b5dc0 pc=0x104260284
github.com/awgh/cppgo/asmcall/cdecl.call1(0x334dad5e4, 0x7)
        /Users/user/go/pkg/mod/github.com/awgh/[email protected]/asmcall/cdecl/cdecl_posix_arm64.s:13 +0x18 fp=0x140000b5e30 sp=0x140000b5e00 pc=0x104312218
github.com/awgh/cppgo/asmcall/cdecl.Call(0x104316f68?, {0x140000b5ed8?, 0x140000b5f38?, 0x6?})
        /Users/user/go/pkg/mod/github.com/awgh/[email protected]/asmcall/cdecl/cdecl.go:21 +0x44 fp=0x140000b5e80 sp=0x140000b5e30 pc=0x104311fc4
github.com/Binject/universal.Test_Darwin_arm64_1(0x14000083d40)
        /Users/user/go-projects/universal/darwin_test.go:71 +0x208 fp=0x140000b5f60 sp=0x140000b5e80 pc=0x104315fe8
testing.tRunner(0x14000083d40, 0x10437d010)
        /Users/user/homebrew/Cellar/go/1.18.3/libexec/src/testing/testing.go:1439 +0x110 fp=0x140000b5fb0 sp=0x140000b5f60 pc=0x1042ca480
testing.(*T).Run.func1()
        /Users/user/homebrew/Cellar/go/1.18.3/libexec/src/testing/testing.go:1486 +0x30 fp=0x140000b5fd0 sp=0x140000b5fb0 pc=0x1042cb1f0
runtime.goexit()
        /Users/user/homebrew/Cellar/go/1.18.3/libexec/src/runtime/asm_arm64.s:1263 +0x4 fp=0x140000b5fd0 sp=0x140000b5fd0 pc=0x10427a374
created by testing.(*T).Run
        /Users/user/homebrew/Cellar/go/1.18.3/libexec/src/testing/testing.go:1486 +0x300

goroutine 1 [chan receive]:
fatal error: unexpected signal during runtime execution
panic during panic
[signal SIGSEGV: segmentation violation code=0x2 addr=0x118 pc=0x10426f2bc]

runtime stack:
runtime.throw({0x10431f22d?, 0x104423420?})
        /Users/user/homebrew/Cellar/go/1.18.3/libexec/src/runtime/panic.go:992 +0x50
runtime.sigpanic()
        /Users/user/homebrew/Cellar/go/1.18.3/libexec/src/runtime/signal_unix.go:802 +0x1e8
runtime.gentraceback(0x10427ae90?, 0x16bd86bd8?, 0x1?, 0x140000021a0, 0x0, 0x0, 0x64, 0x0, 0x16bd86bd0?, 0x0)
        /Users/user/homebrew/Cellar/go/1.18.3/libexec/src/runtime/traceback.go:242 +0x49c
runtime.traceback1(0x140000021a0?, 0x0?, 0x3?, 0x140000021a0, 0x16bd86da8?)
        /Users/user/homebrew/Cellar/go/1.18.3/libexec/src/runtime/traceback.go:835 +0x14c
runtime.traceback(...)
        /Users/user/homebrew/Cellar/go/1.18.3/libexec/src/runtime/traceback.go:782
runtime.tracebackothers.func1(0x140000021a0)
        /Users/user/homebrew/Cellar/go/1.18.3/libexec/src/runtime/traceback.go:1051 +0x104
runtime.forEachGRace(0x16bd86df8)
        /Users/user/homebrew/Cellar/go/1.18.3/libexec/src/runtime/proc.go:590 +0x64
runtime.tracebackothers(0x14000083a00?)
        /Users/user/homebrew/Cellar/go/1.18.3/libexec/src/runtime/traceback.go:1037 +0xd0
runtime.dopanic_m(0x14000083a00, 0x104277dbc?, 0x104316c0e?)
        /Users/user/homebrew/Cellar/go/1.18.3/libexec/src/runtime/panic.go:1192 +0x25c
runtime.fatalthrow.func1()
        /Users/user/homebrew/Cellar/go/1.18.3/libexec/src/runtime/panic.go:1047 +0x48
runtime.systemstack()
        /Users/user/homebrew/Cellar/go/1.18.3/libexec/src/runtime/asm_arm64.s:241 +0x6c
exit status 2
FAIL    github.com/Binject/universal    1.349s

error compile 32 bits

go version
go version go1.20.1 windows/amd64

GOARCH: 386
GOOS: windows

Code:

Dll, err := os.ReadFile("C:\Windows\SysWOW64\ntdll.dll")
if err != nil {
	panic(err)
}

Nt, err := universal.NewLoader()
if err != nil {
	panic(err)
}

NtLibrary, err := Nt.LoadLibrary("main", &Dll)
if err != nil {
	panic(err)
}
fmt.Println(NtLibrary.BaseAddress)

Error:

github.com/Binject/universal ..\..\..\go\pkg\mod\github.com\!binject\[email protected]\loader_windows.universal\un.go:119:13: array length ^uigo:82:13: array length ^uint32(0) (constant 4294967295 of type uint32) must be integer bas> ..\..\..\go\pkg\mod\github.com\!binject\[email protected]\loader_windows.go:99:13: array length ^uint32(0) (constant 4294967295 of type uint32) must be integer

https://github.com/Binject/universal/blob/main/loader_windows.go

Line 82:
hbuf := (*[^[^uint32(0)]byte)(unsafe.Pointer(uintptr(loc))))

Line 99:
buf := ([[^uint32(0)]byte)(unsafe.Pointer(uintptr(dst)))

Can't load kernel32 in windows

//go:build windows
// +build windows

package main

import (
	"github.com/Binject/universal"
	"io/ioutil"
	"log"
)

func main() {
	var image []byte
	var err error

	image, err = ioutil.ReadFile("C:\\windows\\system32\\kernel32.dll")

	loader, err := universal.NewLoader()
	if err != nil {
		log.Fatal(err)
	}

	library, err := loader.LoadLibrary("main", &image)
	if err != nil {
		log.Fatal(err)
	}

	val, err := library.Call("Sleep", 3000)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("%+v\n", val)
}


segmentation fault

Hi @awgh,
just trying the darwin_example with a dylib on Mac OS (x86_64) but experiencing segmentation fault. I assume this is due to SIP being enabled. Is this working for you with the SIP being enabled, please?

Found image at 0x01000000
Found image at 0x02b71000
found NSCreateObjectFileImageFromMemory
found NSLinkModule
Found dyld!
unexpected fault address 0xb01dfacedebac1e
fatal error: fault
[signal SIGSEGV: segmentation violation code=0x1 addr=0xb01dfacedebac1e pc=0x2b80432]

goroutine 1 [running]:
runtime.throw(0x10f0048, 0x5)
	/usr/local/opt/go/libexec/src/runtime/panic.go:1117 +0x72 fp=0xc000092bb0 sp=0xc000092b80 pc=0x10328b2
runtime: unexpected return pc for runtime.sigpanic called from 0x2b80432
stack: frame={sp:0xc000092bb0, fp:0xc000092be8} stack=[0xc000092000,0xc000093000)
000000c000092ab0:  000000c000092b00  000000c000000001
000000c000092ac0:  000000000119c020  000000c000092af0
000000c000092ad0:  0000000001050779 <runtime.write1+57>  0000000001065d00 <runtime.write_trampoline+0>
000000c000092ae0:  000000c000092b00  0000000000000001
000000c000092af0:  000000c000092b30  0000000001034225 <runtime.gwrite+165>
000000c000092b00:  0000000000000002  00000000010f8710
000000c000092b10:  0000000000000001  0000000000000001
000000c000092b20:  000000c000092b8f  0000000000000011
000000c000092b30:  000000c000092b80  0000000001034b18 <runtime.printstring+120>
000000c000092b40:  0000000001032a97 <runtime.fatalthrow+87>  000000c000092b50
000000c000092b50:  000000000105e4e0 <runtime.fatalthrow.func1+0>  000000c000000180
000000c000092b60:  00000000010328b2 <runtime.throw+114>  000000c000092b80
000000c000092b70:  000000c000092ba0  00000000010328b2 <runtime.throw+114>
000000c000092b80:  000000c000092b88  000000000105e440 <runtime.throw.func1+0>
000000c000092b90:  00000000010f0048  0000000000000005
000000c000092ba0:  000000c000092bd8  0000000001047716 <runtime.sigpanic+630>
000000c000092bb0: <00000000010f0048  0000000000000005
000000c000092bc0:  0000000001305ae0  0b01dfacedebac1e
000000c000092bd0:  000000c000000180  000000c000092c28
000000c000092be0: !0000000002b80432 >00000000012cf000
000000c000092bf0:  000000c000092e80  0000000000000000
000000c000092c00:  0000000001305ae0  0000000002b8035f
000000c000092c10:  00000000000000f9  00000000010f86f8
000000c000092c20:  0000000000000039  000000c000092c80
000000c000092c30:  00000000010c1b76 <github.com/awgh/cppgo/asmcall/cdecl.call3+22>  00000000010c195e <github.com/awgh/cppgo/asmcall/cdecl.Call+286>
000000c000092c40:  0000000002b8035f  0000000001305ae0
000000c000092c50:  000000c000092e80  000000c000092cf8
000000c000092c60:  0000000000000001  000000c00009d1e0
000000c000092c70:  0000000000000000  0000000000000000
000000c000092c80:  000000c000092e70  00000000010c266e <github.com/Binject/universal.LoadLibraryImpl+1454>
000000c000092c90:  0000000002b8035f  000000c000092d88
000000c000092ca0:  0000000000000003  0000000000000003
000000c000092cb0:  0000000000000001  0000000000000000
000000c000092cc0:  0000000000000000  0000000000000000
000000c000092cd0:  0000000000000000  0000000000000000
000000c000092ce0:  0101010000000003
runtime.sigpanic()
	/usr/local/opt/go/libexec/src/runtime/signal_unix.go:741 +0x276 fp=0xc000092be8 sp=0xc000092bb0 pc=0x1047716
exit status 2```

Call function got crashed

Examples are very simple functions, I wrote a simple function which use printf to print a message, but got crashed, is there anything else I should do?

The lib src:

#include "stdio.h"

void test_go() {
  printf("invoke test_go");
}

Compiled to lib:

gcc -shared testlib/biz.c -o testlib.so

Run with the following go src:

package main

import (
	"fmt"
	"io/ioutil"

	"github.com/Binject/universal"
)

func main() {
	var (
		libPath = "testlib.so"
		fName   = "test_go"
	)

	image, err := ioutil.ReadFile(libPath)
	if err != nil {
		panic(err)
	}

	loader, err := universal.NewLoader()
	if err != nil {
		panic(err)
	}

	lib, err := loader.LoadLibrary("main", &image)
	if err != nil {
		panic(err)
	}

	for k := range lib.Exports {
		fmt.Printf("exported function: %s\n", k)
	}

	addr, ok := lib.FindProc(fName)
	if ok {
		fmt.Printf("function address of %s: %x\n", fName, addr)
	} else {
		panic("function not found")
	}

	val, err := lib.Call(fName)
	if err != nil {
		panic(err)
	}

	fmt.Printf("returned value: %v\n", val)
}

But got a sad crash:

image

Go version: go version go1.17.1 linux/amd64

GCC version: gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)

System: WSL1, and ubuntu20.04 also fails.

load with multiple args failed

i want to load https://github.com/trustedsec/ELFLoader this program

package main

import (
	"github.com/Binject/universal"
	"io/ioutil"
	"log"
	"syscall"
	"unsafe"
)

func main() {
	image, err := ioutil.ReadFile("libELFLoader.so")

	loader, err := universal.NewLoader()
	if err != nil {
		log.Fatal(err)
	}

	library, err := loader.LoadLibrary("main", &image)
	if err != nil {
		log.Fatal(err)
	}

	g, _ := syscall.BytePtrFromString("go")

	f, _ := ioutil.ReadFile("whoami.o")

	inDataBuff := uintptr(unsafe.Pointer(&f[0]))
	inDataSize := uintptr(uint64(len(f)))

	val, err := library.Call("ELFRunner", uintptr(unsafe.Pointer(g)), inDataBuff, inDataSize, 0, 0)
	if err != nil {
		//log.Fatal(err)
	}
	log.Printf("%+v\n", val)
}

but didn't work

[BUG][WINDOWS] Segfault on COFFSymbols & StringTable bytes exceeding memory committed size

hbuf[index+pefile.FileHeader.PointerToSymbolTable] = b[index]

The combined total size of the COFFSymbols and StringTable buffers can exceed the memory committed size of the ImageSize, thus causing a sefault in trying to write to a memory location not allocated.

Tested on Win7 64bit vm on QubeOS.

Recommendation:
Instead of allocating for the total ImageSize, allocate these sections separately or adopt CopySections function from: https://github.com/fancycode/MemoryModule/blob/master/MemoryModule.c#L176

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.