OpenGFW/analyzer/udp/openvpn.go

385 lines
8.7 KiB
Go
Raw Normal View History

2024-03-29 20:29:30 +08:00
package udp
import (
"github.com/apernet/OpenGFW/analyzer"
"github.com/apernet/OpenGFW/analyzer/utils"
)
2024-03-31 04:23:46 +08:00
var (
2024-03-31 04:29:19 +08:00
_ analyzer.UDPAnalyzer = (*OpenVPNAnalyzer)(nil)
_ analyzer.TCPAnalyzer = (*OpenVPNAnalyzer)(nil)
2024-03-31 04:23:46 +08:00
)
2024-03-31 04:23:46 +08:00
var (
2024-03-31 04:29:19 +08:00
_ analyzer.UDPStream = (*openvpnUDPStream)(nil)
_ analyzer.TCPStream = (*openvpnTCPStream)(nil)
2024-03-31 04:23:46 +08:00
)
// Ref paper:
// https://www.usenix.org/system/files/sec22fall_xue-diwen.pdf
// OpenVPN Opcodes definitions from:
// https://github.com/OpenVPN/openvpn/blob/master/src/openvpn/ssl_pkt.h
const (
2024-03-31 04:29:19 +08:00
OpenVPNControlHardResetClientV1 = 1
OpenVPNControlHardResetServerV1 = 2
OpenVPNControlSoftResetV1 = 3
OpenVPNControlV1 = 4
OpenVPNAckV1 = 5
OpenVPNDataV1 = 6
OpenVPNControlHardResetClientV2 = 7
OpenVPNControlHardResetServerV2 = 8
OpenVPNDataV2 = 9
OpenVPNControlHardResetClientV3 = 10
OpenVPNControlWkcV1 = 11
)
const (
2024-03-31 04:29:19 +08:00
OpenVPNMinPktLen = 6
OpenVPNTCPPktDefaultLimit = 256
OpenVPNUDPPktDefaultLimit = 256
)
2024-03-29 20:29:30 +08:00
2024-03-31 04:29:19 +08:00
type OpenVPNAnalyzer struct{}
2024-03-29 20:29:30 +08:00
2024-03-31 04:29:19 +08:00
func (a *OpenVPNAnalyzer) Name() string {
return "openvpn"
2024-03-29 20:29:30 +08:00
}
2024-03-31 04:29:19 +08:00
func (a *OpenVPNAnalyzer) Limit() int {
2024-03-29 20:29:30 +08:00
return 0
}
2024-03-31 04:29:19 +08:00
func (a *OpenVPNAnalyzer) NewUDP(info analyzer.UDPInfo, logger analyzer.Logger) analyzer.UDPStream {
return newOpenVPNUDPStream(logger)
}
2024-03-31 04:29:19 +08:00
func (a *OpenVPNAnalyzer) NewTCP(info analyzer.TCPInfo, logger analyzer.Logger) analyzer.TCPStream {
return newOpenVPNTCPStream(logger)
}
2024-03-31 04:29:19 +08:00
type openvpnPkt struct {
pktLen uint16 // 16 bits, TCP proto only
opcode byte // 5 bits
_keyId byte // 3 bits, not used
// We don't care about the rest of the packet
// payload []byte
2024-03-29 20:29:30 +08:00
}
2024-03-31 04:29:19 +08:00
type openvpnStream struct {
2024-03-29 20:29:30 +08:00
logger analyzer.Logger
reqUpdated bool
reqLSM *utils.LinearStateMachine
reqDone bool
respUpdated bool
respLSM *utils.LinearStateMachine
respDone bool
rxPktCnt int
txPktCnt int
pktLimit int
2024-03-31 04:29:19 +08:00
reqPktParse func() (*openvpnPkt, utils.LSMAction)
respPktParse func() (*openvpnPkt, utils.LSMAction)
2024-03-29 20:29:30 +08:00
lastOpcode byte
}
2024-03-31 04:29:19 +08:00
func (o *openvpnStream) parseCtlHardResetClient() utils.LSMAction {
pkt, action := o.reqPktParse()
if action != utils.LSMActionNext {
return action
}
2024-03-29 20:29:30 +08:00
2024-03-31 04:29:19 +08:00
if pkt.opcode != OpenVPNControlHardResetClientV1 &&
pkt.opcode != OpenVPNControlHardResetClientV2 &&
pkt.opcode != OpenVPNControlHardResetClientV3 {
return utils.LSMActionCancel
}
o.lastOpcode = pkt.opcode
return utils.LSMActionNext
2024-03-29 20:29:30 +08:00
}
2024-03-31 04:29:19 +08:00
func (o *openvpnStream) parseCtlHardResetServer() utils.LSMAction {
if o.lastOpcode != OpenVPNControlHardResetClientV1 &&
o.lastOpcode != OpenVPNControlHardResetClientV2 &&
o.lastOpcode != OpenVPNControlHardResetClientV3 {
return utils.LSMActionCancel
2024-03-29 20:29:30 +08:00
}
pkt, action := o.respPktParse()
if action != utils.LSMActionNext {
return action
}
2024-03-31 04:29:19 +08:00
if pkt.opcode != OpenVPNControlHardResetServerV1 &&
pkt.opcode != OpenVPNControlHardResetServerV2 {
return utils.LSMActionCancel
}
o.lastOpcode = pkt.opcode
return utils.LSMActionNext
}
2024-03-31 04:29:19 +08:00
func (o *openvpnStream) parseReq() utils.LSMAction {
pkt, action := o.reqPktParse()
if action != utils.LSMActionNext {
return action
}
2024-03-31 04:29:19 +08:00
if pkt.opcode != OpenVPNControlSoftResetV1 &&
pkt.opcode != OpenVPNControlV1 &&
pkt.opcode != OpenVPNAckV1 &&
pkt.opcode != OpenVPNDataV1 &&
pkt.opcode != OpenVPNDataV2 &&
pkt.opcode != OpenVPNControlWkcV1 {
return utils.LSMActionCancel
}
o.txPktCnt += 1
o.reqUpdated = true
return utils.LSMActionPause
}
2024-03-31 04:29:19 +08:00
func (o *openvpnStream) parseResp() utils.LSMAction {
pkt, action := o.respPktParse()
if action != utils.LSMActionNext {
return action
}
2024-03-31 04:29:19 +08:00
if pkt.opcode != OpenVPNControlSoftResetV1 &&
pkt.opcode != OpenVPNControlV1 &&
pkt.opcode != OpenVPNAckV1 &&
pkt.opcode != OpenVPNDataV1 &&
pkt.opcode != OpenVPNDataV2 &&
pkt.opcode != OpenVPNControlWkcV1 {
return utils.LSMActionCancel
}
o.rxPktCnt += 1
o.respUpdated = true
return utils.LSMActionPause
}
2024-03-31 04:29:19 +08:00
type openvpnUDPStream struct {
openvpnStream
curPkt []byte
// We don't introduce `invalidCount` here to decrease the false positive rate
// invalidCount int
}
2024-03-31 04:29:19 +08:00
func newOpenVPNUDPStream(logger analyzer.Logger) *openvpnUDPStream {
s := &openvpnUDPStream{
openvpnStream: openvpnStream{
logger: logger,
2024-03-31 04:29:19 +08:00
pktLimit: OpenVPNUDPPktDefaultLimit,
},
}
s.respPktParse = s.parsePkt
s.reqPktParse = s.parsePkt
2024-03-29 20:29:30 +08:00
s.reqLSM = utils.NewLinearStateMachine(
s.parseCtlHardResetClient,
s.parseReq,
)
s.respLSM = utils.NewLinearStateMachine(
s.parseCtlHardResetServer,
s.parseResp,
)
return s
}
2024-03-31 04:29:19 +08:00
func (o *openvpnUDPStream) Feed(rev bool, data []byte) (u *analyzer.PropUpdate, d bool) {
2024-03-29 20:29:30 +08:00
if len(data) == 0 {
return nil, false
}
var update *analyzer.PropUpdate
var cancelled bool
o.curPkt = data
if rev {
o.respUpdated = false
cancelled, o.respDone = o.respLSM.Run()
if o.respUpdated {
update = &analyzer.PropUpdate{
Type: analyzer.PropUpdateReplace,
M: analyzer.PropMap{"rx_pkt_cnt": o.rxPktCnt, "tx_pkt_cnt": o.txPktCnt},
2024-03-29 20:29:30 +08:00
}
o.respUpdated = false
}
} else {
o.reqUpdated = false
cancelled, o.reqDone = o.reqLSM.Run()
if o.reqUpdated {
update = &analyzer.PropUpdate{
Type: analyzer.PropUpdateReplace,
M: analyzer.PropMap{"rx_pkt_cnt": o.rxPktCnt, "tx_pkt_cnt": o.txPktCnt},
2024-03-29 20:29:30 +08:00
}
o.reqUpdated = false
}
}
return update, cancelled || (o.reqDone && o.respDone) || o.rxPktCnt+o.txPktCnt > o.pktLimit
}
2024-03-31 04:29:19 +08:00
func (o *openvpnUDPStream) Close(limited bool) *analyzer.PropUpdate {
2024-03-29 20:29:30 +08:00
return nil
}
2024-03-31 04:29:19 +08:00
// Parse OpenVPN UDP packet.
func (o *openvpnUDPStream) parsePkt() (p *openvpnPkt, action utils.LSMAction) {
if o.curPkt == nil {
return nil, utils.LSMActionPause
2024-03-29 20:29:30 +08:00
}
2024-03-31 04:29:19 +08:00
if !OpenVPNCheckForValidOpcode(o.curPkt[0] >> 3) {
return nil, utils.LSMActionCancel
2024-03-29 20:29:30 +08:00
}
// Parse packet header
2024-03-31 04:29:19 +08:00
p = &openvpnPkt{}
p.opcode = o.curPkt[0] >> 3
p._keyId = o.curPkt[0] & 0x07
o.curPkt = nil
return p, utils.LSMActionNext
2024-03-29 20:29:30 +08:00
}
2024-03-31 04:29:19 +08:00
type openvpnTCPStream struct {
openvpnStream
reqBuf *utils.ByteBuffer
respBuf *utils.ByteBuffer
}
2024-03-29 20:29:30 +08:00
2024-03-31 04:29:19 +08:00
func newOpenVPNTCPStream(logger analyzer.Logger) *openvpnTCPStream {
s := &openvpnTCPStream{
openvpnStream: openvpnStream{
logger: logger,
2024-03-31 04:29:19 +08:00
pktLimit: OpenVPNTCPPktDefaultLimit,
},
reqBuf: &utils.ByteBuffer{},
respBuf: &utils.ByteBuffer{},
2024-03-29 20:29:30 +08:00
}
2024-03-31 04:29:19 +08:00
s.respPktParse = func() (*openvpnPkt, utils.LSMAction) {
return s.parsePkt(true)
2024-03-29 20:29:30 +08:00
}
2024-03-31 04:29:19 +08:00
s.reqPktParse = func() (*openvpnPkt, utils.LSMAction) {
return s.parsePkt(false)
2024-03-29 20:29:30 +08:00
}
s.reqLSM = utils.NewLinearStateMachine(
s.parseCtlHardResetClient,
s.parseReq,
)
s.respLSM = utils.NewLinearStateMachine(
s.parseCtlHardResetServer,
s.parseResp,
)
return s
2024-03-29 20:29:30 +08:00
}
2024-03-31 04:29:19 +08:00
func (o *openvpnTCPStream) Feed(rev, start, end bool, skip int, data []byte) (u *analyzer.PropUpdate, d bool) {
if skip != 0 {
return nil, true
2024-03-29 20:29:30 +08:00
}
if len(data) == 0 {
return nil, false
}
var update *analyzer.PropUpdate
var cancelled bool
if rev {
o.respBuf.Append(data)
o.respUpdated = false
cancelled, o.respDone = o.respLSM.Run()
if o.respUpdated {
update = &analyzer.PropUpdate{
Type: analyzer.PropUpdateReplace,
M: analyzer.PropMap{"rx_pkt_cnt": o.rxPktCnt, "tx_pkt_cnt": o.txPktCnt},
}
o.respUpdated = false
}
} else {
o.reqBuf.Append(data)
o.reqUpdated = false
cancelled, o.reqDone = o.reqLSM.Run()
if o.reqUpdated {
update = &analyzer.PropUpdate{
Type: analyzer.PropUpdateMerge,
M: analyzer.PropMap{"rx_pkt_cnt": o.rxPktCnt, "tx_pkt_cnt": o.txPktCnt},
}
o.reqUpdated = false
}
2024-03-29 20:29:30 +08:00
}
return update, cancelled || (o.reqDone && o.respDone) || o.rxPktCnt+o.txPktCnt > o.pktLimit
}
2024-03-29 20:29:30 +08:00
2024-03-31 04:29:19 +08:00
func (o *openvpnTCPStream) Close(limited bool) *analyzer.PropUpdate {
o.reqBuf.Reset()
o.respBuf.Reset()
return nil
2024-03-29 20:29:30 +08:00
}
2024-03-31 04:29:19 +08:00
// Parse OpenVPN TCP packet.
func (o *openvpnTCPStream) parsePkt(rev bool) (p *openvpnPkt, action utils.LSMAction) {
var buffer *utils.ByteBuffer
if rev {
buffer = o.respBuf
} else {
buffer = o.reqBuf
2024-03-29 20:29:30 +08:00
}
// Parse packet length
pktLen, ok := buffer.GetUint16(false, false)
if !ok {
return nil, utils.LSMActionPause
2024-03-29 20:29:30 +08:00
}
2024-03-31 04:29:19 +08:00
if pktLen < OpenVPNMinPktLen {
return nil, utils.LSMActionCancel
}
2024-03-29 20:29:30 +08:00
pktOp, ok := buffer.Get(3, false)
if !ok {
2024-03-29 20:29:30 +08:00
return nil, utils.LSMActionPause
}
2024-03-31 04:29:19 +08:00
if !OpenVPNCheckForValidOpcode(pktOp[2] >> 3) {
2024-03-29 20:29:30 +08:00
return nil, utils.LSMActionCancel
}
pkt, ok := buffer.Get(int(pktLen)+2, true)
if !ok {
return nil, utils.LSMActionPause
}
pkt = pkt[2:]
2024-03-29 20:29:30 +08:00
// Parse packet header
2024-03-31 04:29:19 +08:00
p = &openvpnPkt{}
p.pktLen = pktLen
p.opcode = pkt[0] >> 3
p._keyId = pkt[0] & 0x07
2024-03-29 20:29:30 +08:00
return p, utils.LSMActionNext
}
2024-03-31 04:29:19 +08:00
func OpenVPNCheckForValidOpcode(opcode byte) bool {
switch opcode {
2024-03-31 04:29:19 +08:00
case OpenVPNControlHardResetClientV1,
OpenVPNControlHardResetServerV1,
OpenVPNControlSoftResetV1,
OpenVPNControlV1,
OpenVPNAckV1,
OpenVPNDataV1,
OpenVPNControlHardResetClientV2,
OpenVPNControlHardResetServerV2,
OpenVPNDataV2,
OpenVPNControlHardResetClientV3,
OpenVPNControlWkcV1:
return true
}
return false
}