Skip to content

Commit

Permalink
Add adapted appendedzip implementation
Browse files Browse the repository at this point in the history
This is from [Spyre](https://github.com/spyre-project/spyre), with
crypto zip support removed and a function renamed for consistency
  • Loading branch information
hillu committed Dec 23, 2021
1 parent c13fda0 commit e6e463d
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 1 deletion.
55 changes: 55 additions & 0 deletions appendedzip/appendedzip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package appendedzip

import (
"archive/zip"
"bytes"
"errors"
"io"
"os"
)

func OpenFile(file string) (*zip.Reader, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
fi, err := f.Stat()
if err != nil {
return nil, err
}
return NewReader(f, fi.Size())
}

// OpenReader searches for ZIP beginning-of-file signatures ('P' 'K'
// 03 04) in r and tries to read the file starting at that offset
// using an encryption-enabled archive/zip, returning a *zip.Reader
// for the first valid entry, or an error.
func NewReader(r io.ReaderAt, size int64) (*zip.Reader, error) {
const BUFSIZE = 4096
var buf [BUFSIZE + 4]byte
for i := int64(0); (i-1)*BUFSIZE < size; i++ {
len, err := r.ReadAt(buf[:], i*BUFSIZE)
if err != nil && err != io.EOF {
break
}

n := 0
for {
m := bytes.Index(buf[n:len], []byte("PK\x03\x04"))
if m == -1 {
break
}
off := i*BUFSIZE + int64(n+m)
ssize := size - int64(off)
sr := io.NewSectionReader(r, int64(off), ssize)
if zr, ze := zip.NewReader(sr, ssize+1); ze == nil {
return zr, nil
}
n += m + 1
}
if err == io.EOF {
break
}
}
return nil, errors.New("No zip file found")
}
39 changes: 39 additions & 0 deletions appendedzip/appendedzip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package appendedzip

import (
"archive/zip"
"os"
"testing"
)

func TestAppendedZip(t *testing.T) {
for _, sample := range []string{
"data512",
"data8192",
"data8190",
"doublezip",
"fakeheader8192",
"fakeheader512",
"regular",
} {
t.Log("Reading " + sample + " ...")
f, err := os.Open("testdata/" + sample)
if _, err := zip.OpenReader("testdata/" + sample); err == nil {
t.Logf("Note: %s can be opened by archive/zip", sample)
}
if err != nil {
t.Fatal(err)
}
fi, err := f.Stat()
if err != nil {
t.Fatal(err)
}
zr, err := NewReader(f, fi.Size())
if err != nil {
t.Error(err)
} else {
t.Log("found zip in " + sample)
t.Logf("%+v", zr)
}
}
}
Binary file added appendedzip/testdata/data512
Binary file not shown.
Binary file added appendedzip/testdata/data8190
Binary file not shown.
Binary file added appendedzip/testdata/data8192
Binary file not shown.
Binary file added appendedzip/testdata/doublezip
Binary file not shown.
Binary file added appendedzip/testdata/fakeheader512
Binary file not shown.
Binary file added appendedzip/testdata/fakeheader8192
Binary file not shown.
Binary file added appendedzip/testdata/regular
Binary file not shown.
2 changes: 1 addition & 1 deletion scanner/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"archive/zip"
"bytes"
"flag"
"fmt"
Expand All @@ -11,6 +10,7 @@ import (
"path/filepath"
"strings"

zip "github.com/hillu/local-log4j-vuln-scanner/appendedzip"
"github.com/hillu/local-log4j-vuln-scanner/filter"
)

Expand Down

0 comments on commit e6e463d

Please sign in to comment.