-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHasPolyverseTaint.go
55 lines (49 loc) · 1.47 KB
/
HasPolyverseTaint.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
package lib
import (
"debug/elf"
"github.com/pkg/errors"
"github.com/polyverse/ropoly/constants"
log "github.com/sirupsen/logrus"
"os"
"strings"
)
// File identification, must be 0x7f + "ELF".
var elfHeader = string([]byte{0x7f, 'E', 'L', 'F'})
func HasPolyverseTaint(path string) (bool, error) {
log.Debugf("Checking file %s for Polyverse taint", path)
rawFile, err := os.Open(path)
if err != nil {
return false, errors.Wrapf(err, "Unable to open file at path %s", path)
}
defer rawFile.Close()
buffer := make([]byte, 4)
count, err := rawFile.Read(buffer)
if err != nil {
return false, errors.Wrapf(err, "Unable to Check whether file is an ELF binary at path %s", path)
}
if count != 4 {
log.Debugf("File %s is not an ELF binary because it does not have a 4-byte header", path)
return false, nil // Not an elf binary
}
if elfHeader != string(buffer) {
log.Debugf("File %s is not an ELF binary because it does not have the expected 4-byte header", path)
return false, nil // Not an elf binary
}
elfFile, err := elf.Open(path)
if err != nil {
return false, errors.Wrapf(err, "Unable to open ELF file at path %s", path)
}
defer elfFile.Close()
for _, section := range elfFile.Sections {
sectionData, err := section.Data()
if err != nil {
continue
}
sectionStr := string(sectionData)
if strings.Contains(sectionStr, constants.PolyverseSignature) {
return true, nil
}
}
log.Debugf("File %s is not Polyverse tainted", path)
return false, nil
}