Skip to content

Commit f468b2e

Browse files
added check for TLS too short error
1 parent 767579f commit f468b2e

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

layers/tls.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const (
1515
ServerHelloTLSVal = 0x02
1616
)
1717

18+
var TLSTooShortErr = fmt.Errorf("tls message too short")
19+
1820
type TLSVersion struct {
1921
Value uint16
2022
Desc string
@@ -117,7 +119,7 @@ type TLSClientHello struct {
117119
TypeDesc string
118120
Length int // 3 bytes int(uint(b[2]) | uint(b[1])<<8 | uint(b[0])<<16))
119121
Version *TLSVersion
120-
Random []byte //32 bytes
122+
Random []byte // 32 bytes
121123
SessionIDLength uint8 // if 0 no session follows
122124
SessionID string
123125
CipherSuitesLength uint16
@@ -178,12 +180,12 @@ func (tch *TLSClientHello) String() string {
178180
- Random: %s
179181
- SessionIDLength: %d
180182
- SessionID: %s
181-
- CipherSuitesLength: %d
183+
- CipherSuitesLength: %d
182184
- CipherSuites: %v
183185
- CmprMethodsLength: %d
184-
- CmprMethods: %v
186+
- CmprMethods: %v
185187
- ExtensionLength: %d
186-
- Extensions: %v
188+
- Extensions: %v
187189
- %s
188190
- ALPN: %v
189191
`,
@@ -233,7 +235,7 @@ func (tch *TLSClientHello) ParseHS(data []byte) error {
233235
}
234236
csl := binary.BigEndian.Uint16(data[sid : sid+2]) // data[71:73] suites count * 2 bytes
235237
tch.CipherSuitesLength = csl
236-
offset := uint16(sid + 2) //73
238+
offset := uint16(sid + 2) // 73
237239
cmproffset := csl + offset // 107
238240
css := make([]*CipherSuite, 0, csl/2)
239241
var i uint16
@@ -285,8 +287,8 @@ func (tch *TLSClientHello) ParseHS(data []byte) error {
285287
return err
286288
}
287289
tch.ServerName = sn
288-
case 16: //ALPN
289-
//skip data[i+4:i+6] alpn extension length
290+
case 16: // ALPN
291+
// skip data[i+4:i+6] alpn extension length
290292
if len(data) < int(i+6) {
291293
return nil
292294
}
@@ -318,7 +320,7 @@ type TLSServerHello struct {
318320
TypeDesc string
319321
Length int // 3 bytes int(uint(b[2]) | uint(b[1])<<8 | uint(b[0])<<16))
320322
Version *TLSVersion
321-
Random []byte //32 bytes
323+
Random []byte // 32 bytes
322324
SessionIDLength uint8 // if 0 no session follows
323325
SessionID string
324326
CipherSuite *CipherSuite
@@ -374,10 +376,10 @@ func (tsh *TLSServerHello) String() string {
374376
- SessionIDLength: %d
375377
- SessionID: %s
376378
- CipherSuite: %s
377-
- CmprMethod: %d
379+
- CmprMethod: %d
378380
- ExtensionLength: %d
379-
- Extensions: %v
380-
- Supported Version: %s
381+
- Extensions: %v
382+
- Supported Version: %s
381383
`,
382384
tsh.TypeDesc,
383385
tsh.Type,
@@ -539,7 +541,7 @@ func (t *TLSMessage) printRecords() string {
539541
func (t *TLSMessage) Parse(data []byte) error {
540542
t.Records = make([]*Record, 0, 5)
541543
if len(data) < headerSizeTLS {
542-
return nil
544+
return TLSTooShortErr
543545
}
544546
for len(data) > 0 {
545547
ctype := data[0]
@@ -1388,6 +1390,7 @@ func csuitedesc(csuite uint16) string {
13881390
}
13891391
return csuitedesc
13901392
}
1393+
13911394
func extdesc(ext uint16) string {
13921395
var extdesc string
13931396
switch ext {

mshark.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ func NewWriter(w io.Writer, verbose bool) *Writer {
5151
return &Writer{
5252
w: w,
5353
stdout: w == os.Stdout,
54-
verbose: verbose}
54+
verbose: verbose,
55+
}
5556
}
5657

5758
// printPacket prints a layer packet to the writer. If the writer is an instance of os.Stdout,
@@ -161,7 +162,6 @@ func InterfaceByName(name string) (*net.Interface, error) {
161162
// OpenLive opens a live capture based on the given configuration and writes
162163
// all captured packets to the given PacketWriters.
163164
func OpenLive(conf *Config, pw ...PacketWriter) error {
164-
165165
packetcfg := packet.Config{}
166166

167167
// setting up filter
@@ -237,7 +237,7 @@ func OpenLive(conf *Config, pw ...PacketWriter) error {
237237
return fmt.Errorf("failed to read Ethernet frame: %v", err)
238238
}
239239
for _, w := range pw {
240-
if err := w.WritePacket(time.Now().UTC(), b[:n]); err != nil {
240+
if err := w.WritePacket(time.Now().UTC(), b[:n]); err != nil && !errors.Is(err, layers.TLSTooShortErr) {
241241
return err
242242
}
243243
}

0 commit comments

Comments
 (0)