Skip to content

Parse java php nodejs binary version #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions pkg/javabinary/exe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Ported from https://github.com/golang/go/blob/e9c96835971044aa4ace37c7787de231bbde05d9/src/cmd/go/internal/version/exe.go

package javabinary

import (
"bytes"
"debug/elf"
"fmt"
"io"
"io/ioutil"
)

// An exe is a generic interface to an OS executable (ELF, Mach-O, PE, XCOFF).
type exe interface {
// ReadData reads and returns up to size byte starting at virtual address addr.
ReadData(addr, size uint64) ([]byte, error)

// DataStart returns the writable data segment start address.
DataStart() (uint64, uint64)
}

// openExe opens file and returns it as an exe.
func openExe(r io.Reader) (exe, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}

br := bytes.NewReader(b)

data := make([]byte, 16)
if _, err := io.ReadFull(br, data); err != nil {
return nil, err
}
br.Seek(0, 0)

if bytes.HasPrefix(data, []byte("\x7FELF")) {
e, err := elf.NewFile(br)
if err != nil {
return nil, err
}
return &elfExe{e}, nil
}

return nil, fmt.Errorf("unrecognized executable format")
}

// elfExe is the ELF implementation of the exe interface.
type elfExe struct {
f *elf.File
}

func (x *elfExe) ReadData(addr, size uint64) ([]byte, error) {
for _, prog := range x.f.Progs {
if prog.Vaddr <= addr && addr <= prog.Vaddr+prog.Filesz-1 {
n := prog.Vaddr + prog.Filesz - addr
if n > size {
n = size
}
data := make([]byte, n)
_, err := prog.ReadAt(data, int64(addr-prog.Vaddr))
if err != nil {
return nil, err
}
return data, nil
}
}
return nil, fmt.Errorf("address not mapped")
}

func (x *elfExe) DataStart() (uint64, uint64) {
for _, s := range x.f.Sections {
if s.Name == ".rodata" {
return s.Addr, s.SectionHeader.Size
}
}
return 0, 0
}
59 changes: 59 additions & 0 deletions pkg/javabinary/parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Ported from https://github.com/golang/go/blob/e9c96835971044aa4ace37c7787de231bbde05d9/src/cmd/go/internal/version/version.go

package javabinary

import (
"bytes"
"io"
"regexp"
"strings"

"github.com/aquasecurity/go-dep-parser/pkg/types"
)

// Parse scans file to try to report the OpenJDK version.
func Parse(r io.Reader) ([]types.Library, error) {
x, err := openExe(r)
if err != nil {
return nil, err
}

vers, mod := findVers(x)
if vers == "" {
return nil, nil
}

var libs []types.Library
libs = append(libs, types.Library{
Name: mod,
Version: vers,
})

return libs, nil
}

// findVers finds and returns the Java version in the executable x.
func findVers(x exe) (vers string, mod string) {
text, size := x.DataStart()
data, err := x.ReadData(text, size)
if err != nil {
return
}

re := regexp.MustCompile(`\d{1,4}\.\d{1,4}\.\d{1,4}`)
// split by null characters
items := bytes.Split(data, []byte("\000"))
for _, s := range items {
// If the string starts with a version number
if re.Match(s) {
vers = string(s)
} else if strings.Contains(string(s), "openjdk") {
mod = "openjdk"
}
}

if mod == "openjdk" {
return vers, mod
}
return
}
62 changes: 62 additions & 0 deletions pkg/javabinary/parse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package javabinary

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/aquasecurity/go-dep-parser/pkg/types"
)

func TestParse(t *testing.T) {
tests := []struct {
name string
inputFile string
want []types.Library
wantErr string
}{
{
name: "ELF",
inputFile: "testdata/java.11.elf",
want: []types.Library{
{
Name: "openjdk",
Version: "11.0.10+9",
},
},
},
{
name: "ELF",
inputFile: "testdata/java.8.elf",
want: []types.Library{
{
Name: "openjdk",
Version: "1.8.0_212-b04",
},
},
},
{
name: "sad path",
inputFile: "testdata/dummy",
wantErr: "unrecognized executable format",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f, err := os.Open(tt.inputFile)
require.NoError(t, err)

got, err := Parse(f)
if tt.wantErr != "" {
require.NotNil(t, err)
assert.Contains(t, err.Error(), tt.wantErr)
return
}

assert.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}
1 change: 1 addition & 0 deletions pkg/javabinary/testdata/dummy
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Binary file added pkg/javabinary/testdata/java.11.elf
Binary file not shown.
Binary file added pkg/javabinary/testdata/java.8.elf
Binary file not shown.
78 changes: 78 additions & 0 deletions pkg/nodejsbinary/exe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Ported from https://github.com/golang/go/blob/e9c96835971044aa4ace37c7787de231bbde05d9/src/cmd/go/internal/version/exe.go

package nodejsbinary

import (
"bytes"
"debug/elf"
"fmt"
"io"
"io/ioutil"
)

// An exe is a generic interface to an OS executable (ELF, Mach-O, PE, XCOFF).
type exe interface {
// ReadData reads and returns up to size byte starting at virtual address addr.
ReadData(addr, size uint64) ([]byte, error)

// DataStart returns the writable data segment start address.
DataStart() (uint64, uint64)
}

// openExe opens file and returns it as an exe.
func openExe(r io.Reader) (exe, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}

br := bytes.NewReader(b)

data := make([]byte, 16)
if _, err := io.ReadFull(br, data); err != nil {
return nil, err
}
br.Seek(0, 0)

if bytes.HasPrefix(data, []byte("\x7FELF")) {
e, err := elf.NewFile(br)
if err != nil {
return nil, err
}
return &elfExe{e}, nil
}

return nil, fmt.Errorf("unrecognized executable format")
}

// elfExe is the ELF implementation of the exe interface.
type elfExe struct {
f *elf.File
}

func (x *elfExe) ReadData(addr, size uint64) ([]byte, error) {
for _, prog := range x.f.Progs {
if prog.Vaddr <= addr && addr <= prog.Vaddr+prog.Filesz-1 {
n := prog.Vaddr + prog.Filesz - addr
if n > size {
n = size
}
data := make([]byte, n)
_, err := prog.ReadAt(data, int64(addr-prog.Vaddr))
if err != nil {
return nil, err
}
return data, nil
}
}
return nil, fmt.Errorf("address not mapped")
}

func (x *elfExe) DataStart() (uint64, uint64) {
for _, s := range x.f.Sections {
if s.Name == ".rodata" {
return s.Addr, s.SectionHeader.Size
}
}
return 0, 0
}
55 changes: 55 additions & 0 deletions pkg/nodejsbinary/parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Ported from https://github.com/golang/go/blob/e9c96835971044aa4ace37c7787de231bbde05d9/src/cmd/go/internal/version/version.go

package nodejsbinary

import (
"bytes"
"io"
"regexp"

"github.com/aquasecurity/go-dep-parser/pkg/types"
)

// Parse scans file to try to report the OpenJDK version.
func Parse(r io.Reader) ([]types.Library, error) {
x, err := openExe(r)
if err != nil {
return nil, err
}

vers, mod := findVers(x)
if vers == "" {
return nil, nil
}

var libs []types.Library
libs = append(libs, types.Library{
Name: mod,
Version: vers,
})

return libs, nil
}

// findVers finds and returns the Java version in the executable x.
func findVers(x exe) (vers string, mod string) {
text, size := x.DataStart()
data, err := x.ReadData(text, size)
if err != nil {
return
}

re := regexp.MustCompile(`node\.js\/v(\d{1,3}\.\d{1,3}\.\d{1,3})`)
// split by null characters
items := bytes.Split(data, []byte("\000"))
for _, s := range items {
// Extract the version number
match := re.FindSubmatch(s)
if match != nil {
vers = string(match[1])
break
}
}

return vers, "node.js"
}
52 changes: 52 additions & 0 deletions pkg/nodejsbinary/parse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package nodejsbinary

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/aquasecurity/go-dep-parser/pkg/types"
)

func TestParse(t *testing.T) {
tests := []struct {
name string
inputFile string
want []types.Library
wantErr string
}{
{
name: "ELF",
inputFile: "testdata/node.12.elf",
want: []types.Library{
{
Name: "node.js",
Version: "12.16.3",
},
},
},
{
name: "sad path",
inputFile: "testdata/dummy",
wantErr: "unrecognized executable format",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f, err := os.Open(tt.inputFile)
require.NoError(t, err)

got, err := Parse(f)
if tt.wantErr != "" {
require.NotNil(t, err)
assert.Contains(t, err.Error(), tt.wantErr)
return
}

assert.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}
1 change: 1 addition & 0 deletions pkg/nodejsbinary/testdata/dummy
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Binary file added pkg/nodejsbinary/testdata/node.12.elf
Binary file not shown.
Loading