Skip to content

Commit

Permalink
fix marshaling issue in coap
Browse files Browse the repository at this point in the history
  • Loading branch information
RicYaben committed Jul 24, 2024
1 parent fd887a6 commit 2ed5d17
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
18 changes: 10 additions & 8 deletions modules/coap/message/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
)

const (
OptionBlock1 = 27 // CoAP Block1 Option Number
OptionBlock2 = 23 // CoAP Block2 Option Number
OptionContentFormat = 12 // CoAP Content-Format Option Number
OptionBlock1 uint16 = 27 // CoAP Block1 Option Number
OptionBlock2 uint16 = 23 // CoAP Block2 Option Number
OptionContentFormat uint16 = 12 // CoAP Content-Format Option Number
)

var (
Expand Down Expand Up @@ -43,12 +43,14 @@ func (d *decoder) Decode(data []byte, msg *Message) error {
}
msg.Options = opts

var payload = new(Payload)
cType := uint16(opts[OptionContentFormat].Value[0])
if err := payload.Unmarshal(buf, cType); err != nil {
return err
if cOpt, ok := opts[OptionContentFormat]; ok {
var payload = new(Payload)
cType := uint16(cOpt.Value[0])
if err := payload.Unmarshal(buf, cType); err != nil {
return err
}
msg.Payload = payload
}
msg.Payload = payload

for _, oID := range []uint16{OptionBlock1, OptionBlock2} {
if opt, ok := opts[oID]; ok {
Expand Down
14 changes: 7 additions & 7 deletions modules/coap/message/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
)

Expand Down Expand Up @@ -99,32 +100,31 @@ var optionDefinitions = map[uint16]OptDef{

type Options map[uint16]*Option

func (o *Options) Unmarshal(buf *bytes.Buffer) (err error) {
func (o *Options) Unmarshal(buf *bytes.Buffer) error {
prevOpt := 0
for buf.Len() > 0 {
b, err := buf.ReadByte()
if err == io.EOF {
break
return err
}

delta, length := int(b>>4), int(b&0x0F)
if delta == 0x0F && length == 0x0F {
break
return fmt.Errorf("invalid delta")
}

delta, err = o.parseExtOpt(buf, delta)
if err != nil {
break
return err
}

length, err = o.parseExtOpt(buf, length)
if err != nil {
break
return err
}

if buf.Len() < length {
err = ErrSmallBuffer
break
return ErrSmallBuffer
}
data := buf.Next(length)
optID := prevOpt + delta
Expand Down
10 changes: 8 additions & 2 deletions modules/coap/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/csv"
"log"
"strings"
"time"

"github.com/zmap/zgrab2"
)
Expand Down Expand Up @@ -102,6 +103,11 @@ func (scanner *Scanner) Init(flags zgrab2.ScanFlags) error {
return err
}
scanner.paths = paths

if fl.Timeout <= 0 {
fl.Timeout = 10 * time.Second
}

scanner.probe = newProbeBuilder(fl.Timeout)

return nil
Expand Down Expand Up @@ -139,9 +145,9 @@ func (scanner *Scanner) Scan(t zgrab2.ScanTarget) (zgrab2.ScanStatus, interface{
scan := scanner.newCoAPscan(&t)
err := scan.Grab()
if err != nil {
return err.Unpack(&scan.results)
return err.Unpack(scan.results)
}
return zgrab2.SCAN_SUCCESS, &scan.results, nil
return zgrab2.SCAN_SUCCESS, scan.results, nil
}

// RegisterModule is called by modules/coap.go to register this module with the
Expand Down

0 comments on commit 2ed5d17

Please sign in to comment.