-
Notifications
You must be signed in to change notification settings - Fork 43
/
parseT3.go
171 lines (161 loc) · 3.5 KB
/
parseT3.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package t3
import (
"bufio"
"bytes"
"fmt"
"github.com/yaklang/yaklang/common/yserx"
"io/ioutil"
"os"
"reflect"
)
type T3Header struct {
cmd byte
qos byte
flags byte
hasJVMIDs bool
hasTX bool
hasTrace bool
resopnseId int
invokableId int
abbrevOffset int
}
type T3Request struct {
Header T3Header
}
type reader struct {
buf []byte
pos int
chunkPos int
}
type InboundMsgAbbrev struct {
obj [][]yserx.JavaSerializable
n int
}
func (i *InboundMsgAbbrev) push(obj []yserx.JavaSerializable) {
i.obj = append(i.obj, obj)
i.n += 1
}
func (i *InboundMsgAbbrev) get() []yserx.JavaSerializable {
i.n -= 1
return i.obj[i.n+1]
}
func NewReader(b []byte) *reader {
return &reader{chunkPos: 0, buf: b, pos: 0}
}
func (r *reader) ReadByte() byte {
b := r.buf[r.chunkPos]
r.chunkPos += 1
return b
}
func (r *reader) ReadByteN(n int) []byte {
pre := r.chunkPos
r.chunkPos += n
return r.buf[pre:r.chunkPos]
}
func (r *reader) ReadLength() int {
var b byte
b = r.ReadByte()
len := int(b & 255)
if len < 254 {
} else if len == 254 {
chs := r.ReadByteN(2)
ch1 := int(chs[0] & 255)
ch2 := int(chs[1] & 255)
len = (ch1 << 8) + ch2
} else {
bs := r.ReadByteN(4)
ch1 := int(bs[0] & 255)
ch2 := int(bs[1] & 255)
ch3 := int(bs[2] & 255)
ch4 := int(bs[3] & 255)
len = (ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)
}
return len
}
func (r *reader) ReadInt() int {
chs := r.ReadByteN(4)
ch1 := int(chs[0]&255) << 24
ch2 := int(chs[1]&255) << 16
ch3 := int(chs[2]&255) << 8
ch4 := int(chs[3]&255) << 0
ch := ch1 + ch2 + ch3 + ch4
if ch == 255 {
ch = -1
}
return ch
}
func (r *reader) Skip(n int) error {
r.chunkPos += n
return nil
}
func readObject(buf *reader) []yserx.JavaSerializable {
buf.ReadByte()
bfs := buf.buf[buf.chunkPos:]
l := len(bfs)
checkn := 5
for i := l - checkn; i < l; i++ {
if bfs[i] == 254 {
bfs = bfs[:i]
break
}
}
//println(codec.EncodeToHex(bfs))
br := bytes.NewReader(bfs)
bfr := bufio.NewReader(br)
obj, _ := yserx.ParseJavaSerializedEx(bfr, ioutil.Discard)
if obj == nil {
println("parse object error")
os.Exit(0)
}
elem := reflect.ValueOf(bfr).Elem()
r := elem.FieldByName("r")
bufAdd := int(r.Int())
if bufAdd == 0 {
bufAdd = len(bfs) + 1
}
buf.chunkPos += int(bufAdd)
return obj
}
func ParseT3(data []byte) *T3Request {
headerLen := 19
var extentbyte []byte
t3 := &T3Request{Header: T3Header{}}
buf := NewReader(data)
buf.Skip(4)
t3.Header.cmd = buf.ReadByte()
t3.Header.qos = buf.ReadByte()
t3.Header.flags = buf.ReadByte()
t3.Header.flags = t3.Header.flags & 255
t3.Header.hasJVMIDs = t3.Header.flags&1 != 0
t3.Header.hasTX = t3.Header.flags&2 != 0
t3.Header.hasTrace = t3.Header.flags&4 != 0
t3.Header.resopnseId = buf.ReadInt()
t3.Header.invokableId = buf.ReadInt()
t3.Header.abbrevOffset = buf.ReadInt()
buf.Skip(headerLen - 19)
s := t3.Header.abbrevOffset - buf.chunkPos
extentbyte = buf.buf[headerLen:s]
_ = extentbyte
buf.Skip(s)
len := buf.ReadLength()
abbrevs := InboundMsgAbbrev{n: 0}
res := ""
for i := 0; i < len; i++ {
abbrev := buf.ReadLength()
if abbrev > 255 {
obj := readObject(buf)
json, err := yserx.ToJson(obj)
if err != nil {
println(err.Error())
return nil
}
res += fmt.Sprintf("contextObj%d=`%s`\n\n", i, string(json))
abbrevs.push(obj)
} else {
res += fmt.Sprintf("contextObj%d=nil\n\n", i)
abbrevs.push(nil)
}
}
//ioutil.WriteFile(fmt.Sprintf("/Users/z3/Downloads/contextObjs.json"), []byte(res), 0666)
return nil
}