new: implemented can.inject

This commit is contained in:
evilsocket 2024-08-09 16:19:35 +02:00
commit fd05df613e
3 changed files with 37 additions and 0 deletions

View file

@ -1,6 +1,7 @@
package can
import (
"errors"
"net"
"github.com/bettercap/bettercap/session"
@ -18,6 +19,7 @@ type CANModule struct {
conn net.Conn
recv *socketcan.Receiver
send *socketcan.Transmitter
}
func NewCanModule(s *session.Session) *CANModule {
@ -68,6 +70,15 @@ func NewCanModule(s *session.Session) *CANModule {
return mod.Show()
}))
mod.AddHandler(session.NewModuleHandler("can.inject FRAME_EXPRESSION", `(?i)^can\.inject\s+([a-fA-F0-9#R]+)$`,
"Parse FRAME_EXPRESSION as 'id#data' and inject it as a CAN frame.",
func(args []string) error {
if !mod.Running() {
return errors.New("can module not running")
}
return mod.Inject(args[0])
}))
return mod
}

24
modules/can/can_inject.go Normal file
View file

@ -0,0 +1,24 @@
package can
import (
"context"
"github.com/dustin/go-humanize"
"go.einride.tech/can"
)
func (mod *CANModule) Inject(expr string) (err error) {
frame := can.Frame{}
if err := frame.UnmarshalString(expr); err != nil {
return err
}
mod.Info("injecting %s of CAN frame %d ...",
humanize.Bytes(uint64(frame.Length)), frame.ID)
if err := mod.send.TransmitFrame(context.Background(), frame); err != nil {
return err
}
return
}

View file

@ -62,6 +62,7 @@ func (mod *CANModule) Configure() error {
}
mod.recv = socketcan.NewReceiver(mod.conn)
mod.send = socketcan.NewTransmitter(mod.conn)
return nil
}
@ -124,6 +125,7 @@ func (mod *CANModule) Stop() error {
mod.conn.Close()
mod.conn = nil
mod.recv = nil
mod.send = nil
mod.dbc = nil
mod.dbcPath = ""
}