Skip to content

Commit

Permalink
fix: fix commons stream
Browse files Browse the repository at this point in the history
  • Loading branch information
yywing authored and yang.yu committed Jan 23, 2024
1 parent b842d91 commit 13e10aa
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 107 deletions.
66 changes: 10 additions & 56 deletions commons/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,39 @@ package commons

import (
"bytes"
"errors"
"io"
)

type CommonStream interface {
io.ReadSeeker
ReadN(n int) (bs []byte, err error)
PeekN(n int) (bs []byte, err error)
EOF() bool
CurrentIndex() int64
}

type Stream struct {
io.ReadSeeker
current int64
err error
}

func (s *Stream) Read(b []byte) (n int, err error) {
if _, err = s.Seek(s.current, io.SeekStart); err != nil {
s.err = err
return
}
n, err = s.ReadSeeker.Read(b)
if err != nil {
s.err = err
return
}
s.current += int64(n)
return
}

func (s *Stream) ReadN(n int) (bs []byte, err error) {
oldCurrent := s.current
bs = make([]byte, n)
_, err = io.ReadFull(s, bs)
if err != nil {
s.current = oldCurrent
bs = nil
}
return
}

func (s *Stream) PeekN(n int) (bs []byte, err error) {
oldOffset := s.current
bs, err = s.ReadN(n)
s.current = oldOffset
return
}

func (s *Stream) EOF() bool {
return s.err != nil
}

func (s *Stream) CurrentIndex() int64 {
return s.current
n, _ := s.Seek(0, io.SeekCurrent)
return n
}

func (s *Stream) Seek(offset int64, whence int) (int64, error) {
var abs int64
switch whence {
case io.SeekStart:
abs = offset
case io.SeekCurrent:
abs = s.current + offset
default:
return 0, errors.New("bytes.Reader.Seek: invalid whence")
func (s *Stream) PeekN(n int) (bs []byte, err error) {
bs, err = s.ReadN(n)
if err != nil {
return
}
if abs < 0 {
return 0, errors.New("bytes.Reader.Seek: negative position")
_, err = s.Seek(int64(-n), io.SeekCurrent)
if err != nil {
return
}
s.current = abs
return s.ReadSeeker.Seek(abs, whence)
return
}

func NewStreamFromReadSeeker(rs io.ReadSeeker) *Stream {
return &Stream{
ReadSeeker: rs,
current: 0,
}
}

Expand Down
34 changes: 16 additions & 18 deletions commons/stream_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package commons

import (
"github.com/stretchr/testify/require"
"io"
"testing"

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

func TestStream_Read(t *testing.T) {
Expand Down Expand Up @@ -35,12 +36,8 @@ func TestStream_ReadN(t *testing.T) {
bs, err = s.ReadN(5)
require.Nil(t, err)
require.Equal(t, []byte("11111"), bs)
bs, err = s.ReadN(23)
_, err = s.ReadN(23)
require.Equal(t, io.ErrUnexpectedEOF, err)
require.Nil(t, bs)
bs, err = s.ReadN(10)
require.Nil(t, err)
require.Equal(t, []byte("2222233333"), bs)
}

func TestStream_PeekN(t *testing.T) {
Expand All @@ -50,20 +47,21 @@ func TestStream_PeekN(t *testing.T) {
bs, err = s.PeekN(5)
require.Nil(t, err)
require.Equal(t, []byte("11111"), bs)
bs, err = s.PeekN(25)
_, err = s.PeekN(25)
require.Nil(t, err)
require.Equal(t, []byte("1111122222333334444455555"), bs)
bs, err = s.PeekN(30)
require.Equal(t, io.ErrUnexpectedEOF, err)
require.Nil(t, bs)
_, _ = s.ReadN(10)
}

func TestStream_CurrentIndex(t *testing.T) {
var bs []byte
var err error
s := NewStream([]byte("1111122222333334444455555"))
bs, err = s.PeekN(5)
require.Nil(t, err)
require.Equal(t, []byte("33333"), bs)
bs, err = s.PeekN(10)
require.Equal(t, []byte("11111"), bs)
require.Equal(t, int64(0), s.CurrentIndex())

bs, err = s.ReadN(5)
require.Nil(t, err)
require.Equal(t, []byte("3333344444"), bs)
bs, err = s.PeekN(20)
require.Equal(t, io.ErrUnexpectedEOF, err)
require.Nil(t, bs)
require.Equal(t, []byte("11111"), bs)
require.Equal(t, int64(5), s.CurrentIndex())
}
17 changes: 9 additions & 8 deletions serz/buffer.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package serz

import (
"github.com/phith0n/zkar/commons"
"io"

"github.com/phith0n/zkar/commons"
)

type ObjectStream struct {
commons.CommonStream
*commons.Stream
handler uint32
references map[uint32]Object
}

func NewObjectStream(bs []byte) *ObjectStream {
return &ObjectStream{
CommonStream: commons.NewStream(bs),
handler: JAVA_BASE_WRITE_HANDLE,
references: make(map[uint32]Object),
Stream: commons.NewStream(bs),
handler: JAVA_BASE_WRITE_HANDLE,
references: make(map[uint32]Object),
}
}

func NewObjectStreamFromReadSeeker(r io.ReadSeeker) *ObjectStream {
return &ObjectStream{
CommonStream: commons.NewStreamFromReadSeeker(r),
handler: JAVA_BASE_WRITE_HANDLE,
references: make(map[uint32]Object),
Stream: commons.NewStreamFromReadSeeker(r),
handler: JAVA_BASE_WRITE_HANDLE,
references: make(map[uint32]Object),
}
}

Expand Down
13 changes: 2 additions & 11 deletions serz/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package serz
import (
"bytes"
"fmt"
"github.com/phith0n/zkar/commons"
"io"
"os"

"github.com/phith0n/zkar/commons"
)

type Object interface {
Expand Down Expand Up @@ -66,16 +67,6 @@ func FromJDK8u20Bytes(data []byte) (*Serialization, error) {
return FromBytes(data)
}

func FromJDK8u20ReadSeeker(data []byte) (*Serialization, error) {
data = bytes.Replace(
data,
[]byte{0x00, 0x7e, 0x00, 0x09},
[]byte{0x00, 0x7e, 0x00, 0x09, JAVA_TC_ENDBLOCKDATA},
1,
)
return FromReadSeeker(bytes.NewReader(data))
}

func (ois *Serialization) ToString() string {
var b = commons.NewPrinter()
b.Printf("@Magic - %s", commons.Hexify(ois.MagicNumber))
Expand Down
16 changes: 2 additions & 14 deletions serz/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package serz
import (
"bytes"
"fmt"
"github.com/stretchr/testify/require"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"time"

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

const existsFlag = "✅"
Expand All @@ -27,19 +28,6 @@ func extractPackage(name string) string {
return name
}

func TestJDK8u20FromReadSeeker(t *testing.T) {
var filename = "../testcases/pwntester/JDK8u20.ser"
data, err := ioutil.ReadFile(filename)
require.Nil(t, err)

ser, err := FromJDK8u20ReadSeeker(data)
require.Nilf(t, err, "an error is occurred in file %v", filename)
serFromBytes, err := FromJDK8u20Bytes(data)
require.Equal(t, ser, serFromBytes)
require.Nilf(t, err, "an error is occurred in file %v", filename)
require.Truef(t, bytes.Equal(data, ser.ToJDK8u20Bytes()), "original data is different from generation data in file %v", filename)
}

func TestYsoserial(t *testing.T) {
walkAndTest("../testcases/ysoserial/*.ser", t, func(filename string, data []byte, ser *Serialization) {
require.Truef(
Expand Down

0 comments on commit 13e10aa

Please sign in to comment.