# Converting your GO bins to Shellcode and Using them in C\#

With release of Go1.15 a new "buildmode" flag has been released. **-buildmode=pie**&#x20;

Lets do a simple demo of converting a go binary to shellcode and injecting it to other processes&#x20;

### Building Go Binary&#x20;

I am going to build a simple golang program which launches calc&#x20;

{% code title="calc.go" %}

```go
package main

import(
    "fmt"
    "os/exec"
)

func main(){    
    c := exec.Command("calc.exe")

    if err := c.Run(); err != nil { 
        fmt.Println("Error: ", err)
    }   
}
```

{% endcode %}

Now lets build the program. I am using Windows 10 amd64 machine. You may need to specify other parameters if you are cross compiling&#x20;

```bash
go build -buildmode=pie -o calc.exe calc.go
```

The command will generate a static binary **calc.exe.**&#x20;

### **Converting Binary to Shellcode**

Here we will use TheWover's [*Donut* ](https://github.com/TheWover/donut)to convert the calc.exe to shellcode. The command is quite simple&#x20;

```bash
donut.exe calc.exe -o calc.bin
```

### Using DonutTest&#x20;

[DonutTest ](https://github.com/TheWover/donut/tree/master/DonutTest)is a subproject of Donut repo. DonutTest provides a test harness to test your generated Shellcode.

To use our calc.bin inside donut test we need to convert it into base64&#x20;

```
[Convert]::ToBase64String([IO.File]::ReadAllBytes("./calc.bin")) | clip
```

Now paste the shellcode in DonutTest  and compile. Your program should run as expected and you should see a calc pop

```
DonutTest.exe <pid> 
```

### **Credits:**

<https://twitter.com/rkervell>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.redteam.cafe/red-team/golang/converting-your-go-bins-to-shellcode-and-using-them-in-c.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
