chore: improve case spelling

This commit is contained in:
Toby 2024-03-30 13:29:19 -07:00
parent 98264d9e27
commit ecd60d0ff1
2 changed files with 93 additions and 93 deletions

View File

@ -6,13 +6,13 @@ import (
) )
var ( var (
_ analyzer.UDPAnalyzer = (*OpenVpnAnalyzer)(nil) _ analyzer.UDPAnalyzer = (*OpenVPNAnalyzer)(nil)
_ analyzer.TCPAnalyzer = (*OpenVpnAnalyzer)(nil) _ analyzer.TCPAnalyzer = (*OpenVPNAnalyzer)(nil)
) )
var ( var (
_ analyzer.UDPStream = (*openVpnUdpStream)(nil) _ analyzer.UDPStream = (*openvpnUDPStream)(nil)
_ analyzer.TCPStream = (*openVpnTcpStream)(nil) _ analyzer.TCPStream = (*openvpnTCPStream)(nil)
) )
// Ref paper: // Ref paper:
@ -21,44 +21,44 @@ var (
// OpenVPN Opcodes definitions from: // OpenVPN Opcodes definitions from:
// https://github.com/OpenVPN/openvpn/blob/master/src/openvpn/ssl_pkt.h // https://github.com/OpenVPN/openvpn/blob/master/src/openvpn/ssl_pkt.h
const ( const (
OpenVpnControlHardResetClientV1 = 1 OpenVPNControlHardResetClientV1 = 1
OpenVpnControlHardResetServerV1 = 2 OpenVPNControlHardResetServerV1 = 2
OpenVpnControlSoftResetV1 = 3 OpenVPNControlSoftResetV1 = 3
OpenVpnControlV1 = 4 OpenVPNControlV1 = 4
OpenVpnAckV1 = 5 OpenVPNAckV1 = 5
OpenVpnDataV1 = 6 OpenVPNDataV1 = 6
OpenVpnControlHardResetClientV2 = 7 OpenVPNControlHardResetClientV2 = 7
OpenVpnControlHardResetServerV2 = 8 OpenVPNControlHardResetServerV2 = 8
OpenVpnDataV2 = 9 OpenVPNDataV2 = 9
OpenVpnControlHardResetClientV3 = 10 OpenVPNControlHardResetClientV3 = 10
OpenVpnControlWkcV1 = 11 OpenVPNControlWkcV1 = 11
) )
const ( const (
OpenVpnMinPktLen = 6 OpenVPNMinPktLen = 6
OpenVpnTcpPktDefaultLimit = 256 OpenVPNTCPPktDefaultLimit = 256
OpenVpnUdpPktDefaultLimit = 256 OpenVPNUDPPktDefaultLimit = 256
) )
type OpenVpnAnalyzer struct{} type OpenVPNAnalyzer struct{}
func (a *OpenVpnAnalyzer) Name() string { func (a *OpenVPNAnalyzer) Name() string {
return "openvpn" return "openvpn"
} }
func (a *OpenVpnAnalyzer) Limit() int { func (a *OpenVPNAnalyzer) Limit() int {
return 0 return 0
} }
func (a *OpenVpnAnalyzer) NewUDP(info analyzer.UDPInfo, logger analyzer.Logger) analyzer.UDPStream { func (a *OpenVPNAnalyzer) NewUDP(info analyzer.UDPInfo, logger analyzer.Logger) analyzer.UDPStream {
return newOpenVpnUdpStream(logger) return newOpenVPNUDPStream(logger)
} }
func (a *OpenVpnAnalyzer) NewTCP(info analyzer.TCPInfo, logger analyzer.Logger) analyzer.TCPStream { func (a *OpenVPNAnalyzer) NewTCP(info analyzer.TCPInfo, logger analyzer.Logger) analyzer.TCPStream {
return newOpenVpnTcpStream(logger) return newOpenVPNTCPStream(logger)
} }
type openVpnPkt struct { type openvpnPkt struct {
pktLen uint16 // 16 bits, TCP proto only pktLen uint16 // 16 bits, TCP proto only
opcode byte // 5 bits opcode byte // 5 bits
_keyId byte // 3 bits, not used _keyId byte // 3 bits, not used
@ -67,7 +67,7 @@ type openVpnPkt struct {
// payload []byte // payload []byte
} }
type openVpnStream struct { type openvpnStream struct {
logger analyzer.Logger logger analyzer.Logger
reqUpdated bool reqUpdated bool
@ -82,21 +82,21 @@ type openVpnStream struct {
txPktCnt int txPktCnt int
pktLimit int pktLimit int
reqPktParse func() (*openVpnPkt, utils.LSMAction) reqPktParse func() (*openvpnPkt, utils.LSMAction)
respPktParse func() (*openVpnPkt, utils.LSMAction) respPktParse func() (*openvpnPkt, utils.LSMAction)
lastOpcode byte lastOpcode byte
} }
func (o *openVpnStream) parseCtlHardResetClient() utils.LSMAction { func (o *openvpnStream) parseCtlHardResetClient() utils.LSMAction {
pkt, action := o.reqPktParse() pkt, action := o.reqPktParse()
if action != utils.LSMActionNext { if action != utils.LSMActionNext {
return action return action
} }
if pkt.opcode != OpenVpnControlHardResetClientV1 && if pkt.opcode != OpenVPNControlHardResetClientV1 &&
pkt.opcode != OpenVpnControlHardResetClientV2 && pkt.opcode != OpenVPNControlHardResetClientV2 &&
pkt.opcode != OpenVpnControlHardResetClientV3 { pkt.opcode != OpenVPNControlHardResetClientV3 {
return utils.LSMActionCancel return utils.LSMActionCancel
} }
o.lastOpcode = pkt.opcode o.lastOpcode = pkt.opcode
@ -104,10 +104,10 @@ func (o *openVpnStream) parseCtlHardResetClient() utils.LSMAction {
return utils.LSMActionNext return utils.LSMActionNext
} }
func (o *openVpnStream) parseCtlHardResetServer() utils.LSMAction { func (o *openvpnStream) parseCtlHardResetServer() utils.LSMAction {
if o.lastOpcode != OpenVpnControlHardResetClientV1 && if o.lastOpcode != OpenVPNControlHardResetClientV1 &&
o.lastOpcode != OpenVpnControlHardResetClientV2 && o.lastOpcode != OpenVPNControlHardResetClientV2 &&
o.lastOpcode != OpenVpnControlHardResetClientV3 { o.lastOpcode != OpenVPNControlHardResetClientV3 {
return utils.LSMActionCancel return utils.LSMActionCancel
} }
@ -116,8 +116,8 @@ func (o *openVpnStream) parseCtlHardResetServer() utils.LSMAction {
return action return action
} }
if pkt.opcode != OpenVpnControlHardResetServerV1 && if pkt.opcode != OpenVPNControlHardResetServerV1 &&
pkt.opcode != OpenVpnControlHardResetServerV2 { pkt.opcode != OpenVPNControlHardResetServerV2 {
return utils.LSMActionCancel return utils.LSMActionCancel
} }
o.lastOpcode = pkt.opcode o.lastOpcode = pkt.opcode
@ -125,18 +125,18 @@ func (o *openVpnStream) parseCtlHardResetServer() utils.LSMAction {
return utils.LSMActionNext return utils.LSMActionNext
} }
func (o *openVpnStream) parseReq() utils.LSMAction { func (o *openvpnStream) parseReq() utils.LSMAction {
pkt, action := o.reqPktParse() pkt, action := o.reqPktParse()
if action != utils.LSMActionNext { if action != utils.LSMActionNext {
return action return action
} }
if pkt.opcode != OpenVpnControlSoftResetV1 && if pkt.opcode != OpenVPNControlSoftResetV1 &&
pkt.opcode != OpenVpnControlV1 && pkt.opcode != OpenVPNControlV1 &&
pkt.opcode != OpenVpnAckV1 && pkt.opcode != OpenVPNAckV1 &&
pkt.opcode != OpenVpnDataV1 && pkt.opcode != OpenVPNDataV1 &&
pkt.opcode != OpenVpnDataV2 && pkt.opcode != OpenVPNDataV2 &&
pkt.opcode != OpenVpnControlWkcV1 { pkt.opcode != OpenVPNControlWkcV1 {
return utils.LSMActionCancel return utils.LSMActionCancel
} }
@ -146,18 +146,18 @@ func (o *openVpnStream) parseReq() utils.LSMAction {
return utils.LSMActionPause return utils.LSMActionPause
} }
func (o *openVpnStream) parseResp() utils.LSMAction { func (o *openvpnStream) parseResp() utils.LSMAction {
pkt, action := o.respPktParse() pkt, action := o.respPktParse()
if action != utils.LSMActionNext { if action != utils.LSMActionNext {
return action return action
} }
if pkt.opcode != OpenVpnControlSoftResetV1 && if pkt.opcode != OpenVPNControlSoftResetV1 &&
pkt.opcode != OpenVpnControlV1 && pkt.opcode != OpenVPNControlV1 &&
pkt.opcode != OpenVpnAckV1 && pkt.opcode != OpenVPNAckV1 &&
pkt.opcode != OpenVpnDataV1 && pkt.opcode != OpenVPNDataV1 &&
pkt.opcode != OpenVpnDataV2 && pkt.opcode != OpenVPNDataV2 &&
pkt.opcode != OpenVpnControlWkcV1 { pkt.opcode != OpenVPNControlWkcV1 {
return utils.LSMActionCancel return utils.LSMActionCancel
} }
@ -167,18 +167,18 @@ func (o *openVpnStream) parseResp() utils.LSMAction {
return utils.LSMActionPause return utils.LSMActionPause
} }
type openVpnUdpStream struct { type openvpnUDPStream struct {
openVpnStream openvpnStream
curPkt []byte curPkt []byte
// We don't introduce `invalidCount` here to decrease the false positive rate // We don't introduce `invalidCount` here to decrease the false positive rate
// invalidCount int // invalidCount int
} }
func newOpenVpnUdpStream(logger analyzer.Logger) *openVpnUdpStream { func newOpenVPNUDPStream(logger analyzer.Logger) *openvpnUDPStream {
s := &openVpnUdpStream{ s := &openvpnUDPStream{
openVpnStream: openVpnStream{ openvpnStream: openvpnStream{
logger: logger, logger: logger,
pktLimit: OpenVpnUdpPktDefaultLimit, pktLimit: OpenVPNUDPPktDefaultLimit,
}, },
} }
s.respPktParse = s.parsePkt s.respPktParse = s.parsePkt
@ -194,7 +194,7 @@ func newOpenVpnUdpStream(logger analyzer.Logger) *openVpnUdpStream {
return s return s
} }
func (o *openVpnUdpStream) Feed(rev bool, data []byte) (u *analyzer.PropUpdate, d bool) { func (o *openvpnUDPStream) Feed(rev bool, data []byte) (u *analyzer.PropUpdate, d bool) {
if len(data) == 0 { if len(data) == 0 {
return nil, false return nil, false
} }
@ -226,22 +226,22 @@ func (o *openVpnUdpStream) Feed(rev bool, data []byte) (u *analyzer.PropUpdate,
return update, cancelled || (o.reqDone && o.respDone) || o.rxPktCnt+o.txPktCnt > o.pktLimit return update, cancelled || (o.reqDone && o.respDone) || o.rxPktCnt+o.txPktCnt > o.pktLimit
} }
func (o *openVpnUdpStream) Close(limited bool) *analyzer.PropUpdate { func (o *openvpnUDPStream) Close(limited bool) *analyzer.PropUpdate {
return nil return nil
} }
// Parse OpenVpn UDP packet. // Parse OpenVPN UDP packet.
func (o *openVpnUdpStream) parsePkt() (p *openVpnPkt, action utils.LSMAction) { func (o *openvpnUDPStream) parsePkt() (p *openvpnPkt, action utils.LSMAction) {
if o.curPkt == nil { if o.curPkt == nil {
return nil, utils.LSMActionPause return nil, utils.LSMActionPause
} }
if !OpenVpnCheckForValidOpcode(o.curPkt[0] >> 3) { if !OpenVPNCheckForValidOpcode(o.curPkt[0] >> 3) {
return nil, utils.LSMActionCancel return nil, utils.LSMActionCancel
} }
// Parse packet header // Parse packet header
p = &openVpnPkt{} p = &openvpnPkt{}
p.opcode = o.curPkt[0] >> 3 p.opcode = o.curPkt[0] >> 3
p._keyId = o.curPkt[0] & 0x07 p._keyId = o.curPkt[0] & 0x07
@ -249,25 +249,25 @@ func (o *openVpnUdpStream) parsePkt() (p *openVpnPkt, action utils.LSMAction) {
return p, utils.LSMActionNext return p, utils.LSMActionNext
} }
type openVpnTcpStream struct { type openvpnTCPStream struct {
openVpnStream openvpnStream
reqBuf *utils.ByteBuffer reqBuf *utils.ByteBuffer
respBuf *utils.ByteBuffer respBuf *utils.ByteBuffer
} }
func newOpenVpnTcpStream(logger analyzer.Logger) *openVpnTcpStream { func newOpenVPNTCPStream(logger analyzer.Logger) *openvpnTCPStream {
s := &openVpnTcpStream{ s := &openvpnTCPStream{
openVpnStream: openVpnStream{ openvpnStream: openvpnStream{
logger: logger, logger: logger,
pktLimit: OpenVpnTcpPktDefaultLimit, pktLimit: OpenVPNTCPPktDefaultLimit,
}, },
reqBuf: &utils.ByteBuffer{}, reqBuf: &utils.ByteBuffer{},
respBuf: &utils.ByteBuffer{}, respBuf: &utils.ByteBuffer{},
} }
s.respPktParse = func() (*openVpnPkt, utils.LSMAction) { s.respPktParse = func() (*openvpnPkt, utils.LSMAction) {
return s.parsePkt(true) return s.parsePkt(true)
} }
s.reqPktParse = func() (*openVpnPkt, utils.LSMAction) { s.reqPktParse = func() (*openvpnPkt, utils.LSMAction) {
return s.parsePkt(false) return s.parsePkt(false)
} }
s.reqLSM = utils.NewLinearStateMachine( s.reqLSM = utils.NewLinearStateMachine(
@ -281,7 +281,7 @@ func newOpenVpnTcpStream(logger analyzer.Logger) *openVpnTcpStream {
return s return s
} }
func (o *openVpnTcpStream) Feed(rev, start, end bool, skip int, data []byte) (u *analyzer.PropUpdate, d bool) { func (o *openvpnTCPStream) Feed(rev, start, end bool, skip int, data []byte) (u *analyzer.PropUpdate, d bool) {
if skip != 0 { if skip != 0 {
return nil, true return nil, true
} }
@ -317,14 +317,14 @@ func (o *openVpnTcpStream) Feed(rev, start, end bool, skip int, data []byte) (u
return update, cancelled || (o.reqDone && o.respDone) || o.rxPktCnt+o.txPktCnt > o.pktLimit return update, cancelled || (o.reqDone && o.respDone) || o.rxPktCnt+o.txPktCnt > o.pktLimit
} }
func (o *openVpnTcpStream) Close(limited bool) *analyzer.PropUpdate { func (o *openvpnTCPStream) Close(limited bool) *analyzer.PropUpdate {
o.reqBuf.Reset() o.reqBuf.Reset()
o.respBuf.Reset() o.respBuf.Reset()
return nil return nil
} }
// Parse OpenVpn TCP packet. // Parse OpenVPN TCP packet.
func (o *openVpnTcpStream) parsePkt(rev bool) (p *openVpnPkt, action utils.LSMAction) { func (o *openvpnTCPStream) parsePkt(rev bool) (p *openvpnPkt, action utils.LSMAction) {
var buffer *utils.ByteBuffer var buffer *utils.ByteBuffer
if rev { if rev {
buffer = o.respBuf buffer = o.respBuf
@ -338,7 +338,7 @@ func (o *openVpnTcpStream) parsePkt(rev bool) (p *openVpnPkt, action utils.LSMAc
return nil, utils.LSMActionPause return nil, utils.LSMActionPause
} }
if pktLen < OpenVpnMinPktLen { if pktLen < OpenVPNMinPktLen {
return nil, utils.LSMActionCancel return nil, utils.LSMActionCancel
} }
@ -346,7 +346,7 @@ func (o *openVpnTcpStream) parsePkt(rev bool) (p *openVpnPkt, action utils.LSMAc
if !ok { if !ok {
return nil, utils.LSMActionPause return nil, utils.LSMActionPause
} }
if !OpenVpnCheckForValidOpcode(pktOp[2] >> 3) { if !OpenVPNCheckForValidOpcode(pktOp[2] >> 3) {
return nil, utils.LSMActionCancel return nil, utils.LSMActionCancel
} }
@ -357,7 +357,7 @@ func (o *openVpnTcpStream) parsePkt(rev bool) (p *openVpnPkt, action utils.LSMAc
pkt = pkt[2:] pkt = pkt[2:]
// Parse packet header // Parse packet header
p = &openVpnPkt{} p = &openvpnPkt{}
p.pktLen = pktLen p.pktLen = pktLen
p.opcode = pkt[0] >> 3 p.opcode = pkt[0] >> 3
p._keyId = pkt[0] & 0x07 p._keyId = pkt[0] & 0x07
@ -365,19 +365,19 @@ func (o *openVpnTcpStream) parsePkt(rev bool) (p *openVpnPkt, action utils.LSMAc
return p, utils.LSMActionNext return p, utils.LSMActionNext
} }
func OpenVpnCheckForValidOpcode(opcode byte) bool { func OpenVPNCheckForValidOpcode(opcode byte) bool {
switch opcode { switch opcode {
case OpenVpnControlHardResetClientV1, case OpenVPNControlHardResetClientV1,
OpenVpnControlHardResetServerV1, OpenVPNControlHardResetServerV1,
OpenVpnControlSoftResetV1, OpenVPNControlSoftResetV1,
OpenVpnControlV1, OpenVPNControlV1,
OpenVpnAckV1, OpenVPNAckV1,
OpenVpnDataV1, OpenVPNDataV1,
OpenVpnControlHardResetClientV2, OpenVPNControlHardResetClientV2,
OpenVpnControlHardResetServerV2, OpenVPNControlHardResetServerV2,
OpenVpnDataV2, OpenVPNDataV2,
OpenVpnControlHardResetClientV3, OpenVPNControlHardResetClientV3,
OpenVpnControlWkcV1: OpenVPNControlWkcV1:
return true return true
} }
return false return false

View File

@ -92,7 +92,7 @@ var analyzers = []analyzer.Analyzer{
&tcp.TLSAnalyzer{}, &tcp.TLSAnalyzer{},
&tcp.TrojanAnalyzer{}, &tcp.TrojanAnalyzer{},
&udp.DNSAnalyzer{}, &udp.DNSAnalyzer{},
&udp.OpenVpnAnalyzer{}, &udp.OpenVPNAnalyzer{},
&udp.QUICAnalyzer{}, &udp.QUICAnalyzer{},
&udp.WireGuardAnalyzer{}, &udp.WireGuardAnalyzer{},
} }