diff --git a/example/main.go b/example/main.go index f94e95f..8a2a2e3 100644 --- a/example/main.go +++ b/example/main.go @@ -1,36 +1,12 @@ package main import ( + "encoding/hex" "fmt" - "github.com/phith0n/zkar" - "github.com/sanity-io/litter" - "io/ioutil" - "regexp" ) func main() { - var filename = "testcases/ysoserial/URLDNS.ser" - // var filename = "example/object.poc" - data, err := ioutil.ReadFile(filename) - if err != nil { - fmt.Println(err.Error()) - return - } - - ser, err := zkar.FromBytes(data) - if err != nil { - fmt.Println(err.Error()) - return - } - - sq := litter.Options{ - StripPackageNames: false, - HidePrivateFields: true, - FieldExclusions: regexp.MustCompile(`^(XXX_.*)$`), // XXX_ is a prefix of fields generated by protoc-gen-go - Separator: " ", - HideZeroValues: true, - } - sq.Dump(ser) - // ioutil.WriteFile("testcases/ysoserial/C3P01.ser", ser.ToBytes(), fs.FileMode(755)) - // fmt.Println(ser.ToString()) + var s = "\x8c\xA3\x8B" + var bs = []byte(s) + fmt.Println(bs, s, hex.EncodeToString(bs)) } diff --git a/parser.go b/parser.go index df57ea2..013e0c5 100644 --- a/parser.go +++ b/parser.go @@ -51,13 +51,13 @@ func FromBytes(data []byte) (*Serialization, error) { } func (ois *Serialization) ToString() string { - var b = NewPrinter() - b.Printf("@Magic - %s", Hexify(ois.MagicNumber)) - b.Printf("@Version - %s", Hexify(ois.StreamVersion)) - b.Printf("@Contents") - b.IncreaseIndent() + var b = newPrinter() + b.printf("@Magic - %s", Hexify(ois.MagicNumber)) + b.printf("@Version - %s", Hexify(ois.StreamVersion)) + b.printf("@Contents") + b.increaseIndent() for _, content := range ois.Contents { - b.Print(content.ToString()) + b.print(content.ToString()) } return b.String() } diff --git a/printer.go b/printer.go index 3d4464e..d8d77d9 100644 --- a/printer.go +++ b/printer.go @@ -5,44 +5,40 @@ import ( "strings" ) -type Printer struct { +type printer struct { strings.Builder currentIndent int } -func (p *Printer) Printf(msg string, args ...interface{}) { +func (p *printer) printf(msg string, args ...interface{}) { if len(args) > 0 { msg = fmt.Sprintf(msg, args...) } - p.indent(msg) + p.print(msg) } -func (p *Printer) Print(data string) { - p.indent(data) +func (p *printer) print(data string) { + var indent = strings.Repeat(" ", p.currentIndent) + var blocks = strings.Split(data, "\n") + for _, block := range blocks { + if block != "" { + _, _ = p.WriteString(indent + block + "\n") + } + } } -func (p *Printer) IncreaseIndent() { +func (p *printer) increaseIndent() { p.currentIndent += 2 } -func (p *Printer) DecreaseIndent() { +func (p *printer) decreaseIndent() { if p.currentIndent >= 2 { p.currentIndent -= 2 } } -func (p *Printer) indent(data string) { - var indent = strings.Repeat(" ", p.currentIndent) - var blocks = strings.Split(data, "\n") - for _, block := range blocks { - if block != "" { - _, _ = p.WriteString(indent + block + "\n") - } - } -} - -func NewPrinter() *Printer { - return &Printer{ +func newPrinter() *printer { + return &printer{ currentIndent: 0, } } diff --git a/printer_test.go b/printer_test.go index a9c21a6..e7a1756 100644 --- a/printer_test.go +++ b/printer_test.go @@ -6,33 +6,33 @@ import ( ) func TestPrinterIndent(t *testing.T) { - var b = NewPrinter() - b.IncreaseIndent() - b.Printf("a: 1\nb: 2") + var b = newPrinter() + b.increaseIndent() + b.printf("a: 1\nb: 2") require.Equal(t, " a: 1\n b: 2\n", b.String()) } func TestPrinterNoNewline(t *testing.T) { - var b = NewPrinter() - b.Printf("no newline") + var b = newPrinter() + b.printf("no newline") require.Equal(t, "no newline\n", b.String()) } func TestPrinterNewline(t *testing.T) { - var b = NewPrinter() - b.Printf("\n") + var b = newPrinter() + b.printf("\n") require.Equal(t, "", b.String()) } func TestPrinterNewlines(t *testing.T) { - var b = NewPrinter() - b.Printf("\n\n\n\n\n") + var b = newPrinter() + b.printf("\n\n\n\n\n") require.Equal(t, "", b.String()) } func TestPrinterIndentLines(t *testing.T) { - var b = NewPrinter() - b.IncreaseIndent() - b.Printf("\n\n\n") + var b = newPrinter() + b.increaseIndent() + b.printf("\n\n\n") require.Equal(t, "", b.String()) } diff --git a/tc_array.go b/tc_array.go index 2baad1e..5132495 100644 --- a/tc_array.go +++ b/tc_array.go @@ -27,14 +27,14 @@ func (t *TCArray) ToBytes() []byte { } func (t *TCArray) ToString() string { - var b = NewPrinter() - b.Printf("TC_ARRAY - %s", Hexify(JAVA_TC_ARRAY)) - b.IncreaseIndent() - b.Print(t.ClassPointer.ToString()) - b.Printf("@Handler - %v", t.Handler) - b.Printf("@ArraySize - %d - %s", len(t.ArrayData), Hexify(uint32(len(t.ArrayData)))) - b.Printf("[]Values") - b.IncreaseIndent() + var b = newPrinter() + b.printf("TC_ARRAY - %s", Hexify(JAVA_TC_ARRAY)) + b.increaseIndent() + b.print(t.ClassPointer.ToString()) + b.printf("@Handler - %v", t.Handler) + b.printf("@ArraySize - %d - %s", len(t.ArrayData), Hexify(uint32(len(t.ArrayData)))) + b.printf("[]Values") + b.increaseIndent() // check if Array is a bytes, then hexdump the byte array var className = "" @@ -49,10 +49,10 @@ func (t *TCArray) ToString() string { } for index, data := range t.ArrayData { - b.Printf("Index %d", index) - b.IncreaseIndent() - b.Print(data.ToString()) - b.DecreaseIndent() + b.printf("Index %d", index) + b.increaseIndent() + b.print(data.ToString()) + b.decreaseIndent() } return b.String() diff --git a/tc_blockdata.go b/tc_blockdata.go index 75d9839..7499c3e 100644 --- a/tc_blockdata.go +++ b/tc_blockdata.go @@ -23,14 +23,14 @@ func (bd *TCBlockData) ToBytes() []byte { } func (bd *TCBlockData) ToString() string { - var b = NewPrinter() + var b = newPrinter() if len(bd.Data) > 0xFF { - b.Printf("TC_BLOCKDATALONG - %s", Hexify(JAVA_TC_BLOCKDATALONG)) + b.printf("TC_BLOCKDATALONG - %s", Hexify(JAVA_TC_BLOCKDATALONG)) } else { - b.Printf("TC_BLOCKDATA - %s", Hexify(JAVA_TC_BLOCKDATA)) + b.printf("TC_BLOCKDATA - %s", Hexify(JAVA_TC_BLOCKDATA)) } - b.IncreaseIndent() - b.Printf("@Blockdata - %s", Hexify(bd.Data)) + b.increaseIndent() + b.printf("@Blockdata - %s", Hexify(bd.Data)) return b.String() } diff --git a/tc_class.go b/tc_class.go index d7ffd5d..c7282ad 100644 --- a/tc_class.go +++ b/tc_class.go @@ -12,11 +12,11 @@ func (c *TCClass) ToBytes() []byte { } func (c *TCClass) ToString() string { - var b = NewPrinter() - b.Printf("TC_CLASS - %s", Hexify(JAVA_TC_CLASS)) - b.IncreaseIndent() - b.Print(c.ClassPointer.ToString()) - b.Printf("@Handler - %v", c.Handler) + var b = newPrinter() + b.printf("TC_CLASS - %s", Hexify(JAVA_TC_CLASS)) + b.increaseIndent() + b.print(c.ClassPointer.ToString()) + b.printf("@Handler - %v", c.Handler) return b.String() } diff --git a/tc_classdata.go b/tc_classdata.go index 43cb0c5..aed651f 100644 --- a/tc_classdata.go +++ b/tc_classdata.go @@ -35,27 +35,27 @@ func (cd *TCClassData) ToBytes() []byte { } func (cd *TCClassData) ToString() string { - var b = NewPrinter() - b.Printf("@ClassName - %s", cd.ReferenceClass.ClassName) - b.IncreaseIndent() - b.Print("{}Attributes") - b.IncreaseIndent() + var b = newPrinter() + b.printf("@ClassName - %s", cd.ReferenceClass.ClassName) + b.increaseIndent() + b.print("{}Attributes") + b.increaseIndent() for i := 0; i < len(cd.FieldDatas); i++ { - b.Printf("%s", cd.ReferenceClass.Attributes[i]) - b.IncreaseIndent() - b.Print(cd.FieldDatas[i].ToString()) - b.DecreaseIndent() + b.printf("%s", cd.ReferenceClass.Attributes[i]) + b.increaseIndent() + b.print(cd.FieldDatas[i].ToString()) + b.decreaseIndent() } - b.DecreaseIndent() + b.decreaseIndent() if !cd.HasAnnotation { return b.String() } - b.Print("@ObjectAnnotation") - b.IncreaseIndent() + b.print("@ObjectAnnotation") + b.increaseIndent() for _, content := range cd.ObjectAnnotation { - b.Print(content.ToString()) + b.print(content.ToString()) } return b.String() diff --git a/tc_enum.go b/tc_enum.go index ff3c709..562ddeb 100644 --- a/tc_enum.go +++ b/tc_enum.go @@ -14,12 +14,12 @@ func (e *TCEnum) ToBytes() []byte { } func (e *TCEnum) ToString() string { - var b = NewPrinter() - b.Printf("TC_ENUM - %s", Hexify(JAVA_TC_ENUM)) - b.IncreaseIndent() - b.Print(e.ClassPointer.ToString()) - b.Printf("@Handler - %v", e.Handler) - b.Print(e.ConstantName.ToString()) + var b = newPrinter() + b.printf("TC_ENUM - %s", Hexify(JAVA_TC_ENUM)) + b.increaseIndent() + b.print(e.ClassPointer.ToString()) + b.printf("@Handler - %v", e.Handler) + b.print(e.ConstantName.ToString()) return b.String() } diff --git a/tc_fielddesc.go b/tc_fielddesc.go index 587ae21..8f6237b 100644 --- a/tc_fielddesc.go +++ b/tc_fielddesc.go @@ -38,16 +38,16 @@ func (f *TCFieldDesc) ToBytes() []byte { } func (f *TCFieldDesc) ToString() string { - var b = NewPrinter() - b.Printf("%s - %s - %s", typecodeVerbose[f.TypeCode], f.TypeCode, Hexify(f.TypeCode)) - b.Print("@FieldName") - b.IncreaseIndent() - b.Print(f.FieldName.ToString()) - b.DecreaseIndent() + var b = newPrinter() + b.printf("%s - %s - %s", typecodeVerbose[f.TypeCode], f.TypeCode, Hexify(f.TypeCode)) + b.print("@FieldName") + b.increaseIndent() + b.print(f.FieldName.ToString()) + b.decreaseIndent() if f.TypeCode == "L" || f.TypeCode == "[" { - b.Print("@ClassName") - b.IncreaseIndent() - b.Print(f.ClassName.ToString()) + b.print("@ClassName") + b.increaseIndent() + b.print(f.ClassName.ToString()) } return b.String() diff --git a/tc_normalclassdesc.go b/tc_normalclassdesc.go index fa34045..95de521 100644 --- a/tc_normalclassdesc.go +++ b/tc_normalclassdesc.go @@ -35,40 +35,40 @@ func (desc *TCClassDesc) ToBytes() []byte { } func (desc *TCClassDesc) ToString() string { - var b = NewPrinter() - - b.Printf("TC_CLASSDESC - %s", Hexify(JAVA_TC_CLASSDESC)) - b.IncreaseIndent() - b.Print("@ClassName") - b.IncreaseIndent() - b.Print(desc.ClassName.ToString()) - b.DecreaseIndent() - b.Printf("@SerialVersionUID - %v - %s", desc.SerialVersionUID, Hexify(desc.SerialVersionUID)) - b.Printf("@Handler - %v", desc.Handler) - b.Printf("@ClassDescFlags - %s - %s", desc.FlagString(), Hexify(desc.ClassDescFlags)) - b.Printf("@FieldCount - %d - %s", len(desc.Fields), Hexify(uint16(len(desc.Fields)))) - b.Print("[]Fields") - b.IncreaseIndent() + var b = newPrinter() + + b.printf("TC_CLASSDESC - %s", Hexify(JAVA_TC_CLASSDESC)) + b.increaseIndent() + b.print("@ClassName") + b.increaseIndent() + b.print(desc.ClassName.ToString()) + b.decreaseIndent() + b.printf("@SerialVersionUID - %v - %s", desc.SerialVersionUID, Hexify(desc.SerialVersionUID)) + b.printf("@Handler - %v", desc.Handler) + b.printf("@ClassDescFlags - %s - %s", desc.FlagString(), Hexify(desc.ClassDescFlags)) + b.printf("@FieldCount - %d - %s", len(desc.Fields), Hexify(uint16(len(desc.Fields)))) + b.print("[]Fields") + b.increaseIndent() for index, field := range desc.Fields { - b.Printf("Index %d:", index) - b.IncreaseIndent() - b.Print(field.ToString()) - b.DecreaseIndent() - } - b.DecreaseIndent() - b.Print("[]ClassAnnotations") - b.IncreaseIndent() + b.printf("Index %d:", index) + b.increaseIndent() + b.print(field.ToString()) + b.decreaseIndent() + } + b.decreaseIndent() + b.print("[]ClassAnnotations") + b.increaseIndent() for index, content := range desc.ClassAnnotation { - b.Printf("Index %d:", index) - b.IncreaseIndent() - b.Print(content.ToString()) - b.DecreaseIndent() - } - b.Printf("TC_ENDBLOCKDATA - %s", Hexify(JAVA_TC_ENDBLOCKDATA)) - b.DecreaseIndent() - b.Print("@SuperClassDesc") - b.IncreaseIndent() - b.Print(desc.SuperClassPointer.ToString()) + b.printf("Index %d:", index) + b.increaseIndent() + b.print(content.ToString()) + b.decreaseIndent() + } + b.printf("TC_ENDBLOCKDATA - %s", Hexify(JAVA_TC_ENDBLOCKDATA)) + b.decreaseIndent() + b.print("@SuperClassDesc") + b.increaseIndent() + b.print(desc.SuperClassPointer.ToString()) return b.String() } diff --git a/tc_null.go b/tc_null.go index e74809d..f8eda09 100644 --- a/tc_null.go +++ b/tc_null.go @@ -9,8 +9,8 @@ func (n *TCNull) ToBytes() []byte { } func (n *TCNull) ToString() string { - var b = NewPrinter() - b.Printf("TC_NULL - %s", Hexify(JAVA_TC_NULL)) + var b = newPrinter() + b.printf("TC_NULL - %s", Hexify(JAVA_TC_NULL)) return b.String() } diff --git a/tc_object.go b/tc_object.go index e25235b..55dc74e 100644 --- a/tc_object.go +++ b/tc_object.go @@ -17,15 +17,15 @@ func (oo *TCObject) ToBytes() []byte { } func (oo *TCObject) ToString() string { - var b = NewPrinter() - b.Printf("TC_OBJECT - %s", Hexify(JAVA_TC_OBJECT)) - b.IncreaseIndent() - b.Print(oo.ClassPointer.ToString()) - b.Printf("@Handler - %v", oo.Handler) - b.Print("[]ClassData") - b.IncreaseIndent() + var b = newPrinter() + b.printf("TC_OBJECT - %s", Hexify(JAVA_TC_OBJECT)) + b.increaseIndent() + b.print(oo.ClassPointer.ToString()) + b.printf("@Handler - %v", oo.Handler) + b.print("[]ClassData") + b.increaseIndent() for _, data := range oo.ClassDatas { - b.Print(data.ToString()) + b.print(data.ToString()) } return b.String() diff --git a/tc_proxyclassdesc.go b/tc_proxyclassdesc.go index 0711c57..dc9187d 100644 --- a/tc_proxyclassdesc.go +++ b/tc_proxyclassdesc.go @@ -29,34 +29,34 @@ func (pc *TCProxyClassDesc) ToBytes() []byte { } func (pc *TCProxyClassDesc) ToString() string { - var b = NewPrinter() - b.Printf("TC_PROXYCLASSDESC - %s", Hexify(JAVA_TC_PROXYCLASSDESC)) - b.IncreaseIndent() - b.Printf("@Handler - %v", pc.Handler) - b.Printf("@InterfaceCount - %d - %s", len(pc.InterfaceNames), Hexify(uint32(len(pc.InterfaceNames)))) - b.IncreaseIndent() + var b = newPrinter() + b.printf("TC_PROXYCLASSDESC - %s", Hexify(JAVA_TC_PROXYCLASSDESC)) + b.increaseIndent() + b.printf("@Handler - %v", pc.Handler) + b.printf("@InterfaceCount - %d - %s", len(pc.InterfaceNames), Hexify(uint32(len(pc.InterfaceNames)))) + b.increaseIndent() for index, ifce := range pc.InterfaceNames { - b.Printf("Index %d:", index) - b.IncreaseIndent() - b.Print(ifce.ToString()) - b.DecreaseIndent() + b.printf("Index %d:", index) + b.increaseIndent() + b.print(ifce.ToString()) + b.decreaseIndent() } - b.DecreaseIndent() + b.decreaseIndent() - b.Print("@ClassAnnotations") - b.IncreaseIndent() + b.print("@ClassAnnotations") + b.increaseIndent() for index, content := range pc.ClassAnnotation { - b.Printf("Index %d", index) - b.IncreaseIndent() - b.Print(content.ToString()) - b.DecreaseIndent() + b.printf("Index %d", index) + b.increaseIndent() + b.print(content.ToString()) + b.decreaseIndent() } - b.Printf("TC_ENDBLOCKDATA - %s", Hexify(JAVA_TC_ENDBLOCKDATA)) - b.DecreaseIndent() + b.printf("TC_ENDBLOCKDATA - %s", Hexify(JAVA_TC_ENDBLOCKDATA)) + b.decreaseIndent() - b.Print("@SuperClassDesc") - b.IncreaseIndent() - b.Print(pc.SuperClassPointer.ToString()) + b.print("@SuperClassDesc") + b.increaseIndent() + b.print(pc.SuperClassPointer.ToString()) return b.String() } diff --git a/tc_reference.go b/tc_reference.go index 65db62c..101ebba 100644 --- a/tc_reference.go +++ b/tc_reference.go @@ -24,10 +24,10 @@ func (r *TCReference) ToBytes() []byte { } func (r *TCReference) ToString() string { - var b = NewPrinter() - b.Printf("TC_REFERENCE - %s", Hexify(JAVA_TC_REFERENCE)) - b.IncreaseIndent() - b.Printf("@Handler - %v - %s", r.Handler, Hexify(r.Handler)) + var b = newPrinter() + b.printf("TC_REFERENCE - %s", Hexify(JAVA_TC_REFERENCE)) + b.increaseIndent() + b.printf("@Handler - %v - %s", r.Handler, Hexify(r.Handler)) return b.String() } diff --git a/tc_string.go b/tc_string.go index 3b26f1c..98eb098 100644 --- a/tc_string.go +++ b/tc_string.go @@ -22,16 +22,16 @@ func (so *TCString) ToBytes() []byte { } func (so *TCString) ToString() string { - var b = NewPrinter() + var b = newPrinter() var length = len(so.Utf.Data) if length <= 0xFFFF { - b.Printf("TC_STRING - %s", Hexify(JAVA_TC_STRING)) + b.printf("TC_STRING - %s", Hexify(JAVA_TC_STRING)) } else { - b.Printf("TC_LONGSTRING - %s", Hexify(JAVA_TC_LONGSTRING)) + b.printf("TC_LONGSTRING - %s", Hexify(JAVA_TC_LONGSTRING)) } - b.IncreaseIndent() - b.Printf("@Handler - %v", so.Handler) - b.Print(so.Utf.ToString()) + b.increaseIndent() + b.printf("@Handler - %v", so.Handler) + b.print(so.Utf.ToString()) return b.String() } diff --git a/tc_utf.go b/tc_utf.go index 3c6de78..79c3659 100644 --- a/tc_utf.go +++ b/tc_utf.go @@ -22,7 +22,7 @@ func (u *TCUtf) ToBytes() []byte { } func (u *TCUtf) ToString() string { - var b = NewPrinter() + var b = newPrinter() var length = len(u.Data) var bs []byte if length <= 0xFFFF { @@ -31,8 +31,8 @@ func (u *TCUtf) ToString() string { bs = NumberToBytes(uint64(len(u.Data))) } - b.Printf("@Length - %d - %s", len(u.Data), Hexify(bs)) - b.Printf("@Value - %s - %s", u.Data, Hexify(u.Data)) + b.printf("@Length - %d - %s", len(u.Data), Hexify(bs)) + b.printf("@Value - %s - %s", u.Data, Hexify(u.Data)) return b.String() } diff --git a/tc_value.go b/tc_value.go index 160191c..ec02074 100644 --- a/tc_value.go +++ b/tc_value.go @@ -64,26 +64,26 @@ func (t *TCValue) ToBytes() []byte { } func (t *TCValue) ToString() string { - var b = NewPrinter() + var b = newPrinter() switch t.TypeCode { case "B": - b.Printf("(byte)%v - %s", t.Byte, Hexify(t.Byte)) + b.printf("(byte)%v - %s", t.Byte, Hexify(t.Byte)) case "C": - b.Printf("(char)%v - %s", t.Char, Hexify(t.Char)) + b.printf("(char)%v - %s", t.Char, Hexify(t.Char)) case "D": - b.Printf("(double)%v - %s", t.Double, Hexify(t.Double)) + b.printf("(double)%v - %s", t.Double, Hexify(t.Double)) case "F": - b.Printf("(float)%v - %s", t.Float, Hexify(t.Float)) + b.printf("(float)%v - %s", t.Float, Hexify(t.Float)) case "I": - b.Printf("(integer)%v - %s", t.Integer, Hexify(t.Integer)) + b.printf("(integer)%v - %s", t.Integer, Hexify(t.Integer)) case "J": - b.Printf("(long)%v - %s", t.Long, Hexify(t.Long)) + b.printf("(long)%v - %s", t.Long, Hexify(t.Long)) case "S": - b.Printf("(short)%v - %s", t.Short, Hexify(t.Short)) + b.printf("(short)%v - %s", t.Short, Hexify(t.Short)) case "Z": - b.Printf("(boolean)%v - %s", t.Boolean, Hexify(t.Boolean)) + b.printf("(boolean)%v - %s", t.Boolean, Hexify(t.Boolean)) case "L", "[": - b.Print(t.Object.ToString()) + b.print(t.Object.ToString()) } return b.String()