-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathpipes_notwin.go
84 lines (70 loc) · 1.73 KB
/
pipes_notwin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// +build !windows
package main
import (
"bytes"
"fmt"
"io"
"log"
"os"
"syscall"
"github.com/Binject/binjection/bj"
)
// MakePipe - Create a named pipe
func MakePipe(pipename string) string {
if _, err := os.Stat(pipename); os.IsNotExist(err) {
// Create named pipe
syscall.Mkfifo(pipename, 0600)
} else if err != nil {
log.Fatal(err)
}
return pipename
}
// ListenPipeDry - Handle events on the dry pipe (dry=not yet injected)
func ListenPipeDry(namedPipe string, config *bj.BinjectConfig) {
MakePipe(namedPipe)
// Open named pipe for reading
fmt.Println("Opening named pipe for reading")
for {
var buff bytes.Buffer
stdout, err := os.OpenFile(namedPipe, os.O_RDONLY, 0600)
if err != nil {
log.Fatalf("Open(%s) failed: %v", namedPipe, err)
}
io.Copy(&buff, stdout)
stdout.Close()
go handleDryConnection(buff, config)
}
}
// ListenPipeWet - Handle events on the wet pipe (wet=injected)
func ListenPipeWet(namedPipe string) {
MakePipe(namedPipe)
// Open named pipe for writing
fmt.Println("Opening named pipe for writing")
for {
if lastBytes != nil {
stdout, err := os.OpenFile(namedPipe, os.O_WRONLY, 0600)
if err != nil {
log.Fatalf("Open(%s) failed: %v", namedPipe, err)
}
_, err = io.Copy(stdout, bytes.NewReader(lastBytes))
stdout.Close()
log.Println("Wrote wet bytes: ", len(lastBytes))
if err != nil {
log.Fatalf("Error on writing to pipe: %v", err)
}
lastBytes = nil
}
}
}
var lastBytes []byte
func handleDryConnection(buff bytes.Buffer, config *bj.BinjectConfig) {
i, err := Inject(&buff, config)
if err != nil {
log.Printf("Error injecting: %v\n", err)
return
}
if i != nil {
lastBytes = i.Bytes()
log.Println("Set lastBytes: ", len(lastBytes))
}
}