| 1 | package hidscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/ |
| 2 | |
| 3 | import ( |
| 4 | "fmtscip-go gomod github.com/golang/go/src go1.16 fmt/" |
| 5 | |
| 6 | "github.com/bettercap/bettercap/networkscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/" |
| 7 | ) |
| 8 | |
| 9 | type MicrosoftBuilderscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/MicrosoftBuilder# struct { |
| 10 | seqnscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/MicrosoftBuilder#seqn. uint16 |
| 11 | } |
| 12 | |
| 13 | func (b MicrosoftBuilderscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/MicrosoftBuilder#) frameForscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/MicrosoftBuilder#frameFor().(template []byte, cmd *Commandscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#) []byte { |
| 14 | data := make([]byte, len(template)) |
| 15 | copy(data, template) |
| 16 | |
| 17 | data[4] = byte(b.seqnscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/MicrosoftBuilder#seqn. & 0xff) |
| 18 | data[5] = byte((b.seqnscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/MicrosoftBuilder#seqn. >> 8) & 0xff) |
| 19 | data[7] = cmd.Modescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#Mode. |
| 20 | data[9] = cmd.HIDscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#HID. |
| 21 | // MS checksum algorithm - as per KeyKeriki paper |
| 22 | sum := byte(0) |
| 23 | last := len(data) - 1 |
| 24 | for i := 0; i < last; i++ { |
| 25 | sum ^= data[i] |
| 26 | } |
| 27 | sum = ^sum & 0xff |
| 28 | data[last] = sum |
| 29 | |
| 30 | b.seqnscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/MicrosoftBuilder#seqn.++ |
| 31 | |
| 32 | return data |
| 33 | } |
| 34 | |
| 35 | func (b MicrosoftBuilderscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/MicrosoftBuilder#) BuildFramesscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/MicrosoftBuilder#BuildFrames().(dev *networkscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/.HIDDevicescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/HIDDevice#, commands []*Commandscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#) error { |
| 36 | if dev == nil { |
| 37 | return fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("the microsoft frame injection requires the device to be visible") |
| 38 | } |
| 39 | |
| 40 | tpl := ([]byte)(nil) |
| 41 | dev.EachPayloadscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/HIDDevice#EachPayload().(func(p []byte) bool { |
| 42 | if len(p) == 19 { |
| 43 | tpl = p |
| 44 | return true |
| 45 | } |
| 46 | return false |
| 47 | }) |
| 48 | |
| 49 | if tpl == nil { |
| 50 | return fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("at least one packet of 19 bytes needed to hijack microsoft devices, try to hid.sniff the device first") |
| 51 | } |
| 52 | |
| 53 | last := len(commands) - 1 |
| 54 | for i, cmd := range commands { |
| 55 | next := (*Commandscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#)(nil) |
| 56 | if i < last { |
| 57 | next = commands[i+1] |
| 58 | } |
| 59 | |
| 60 | if cmd.IsHIDscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#IsHID().() { |
| 61 | cmd.AddFramescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#AddFrame().(b.frameForscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/MicrosoftBuilder#frameFor().(tpl, cmd), 5) |
| 62 | if next == nil || cmd.HIDscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#HID. == next.HIDscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#HID. || next.IsSleepscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#IsSleep().() { |
| 63 | cmd.AddFramescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#AddFrame().(b.frameForscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/MicrosoftBuilder#frameFor().(tpl, &Commandscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#{}), 0) |
| 64 | } |
| 65 | } else if cmd.IsSleepscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#IsSleep().() { |
| 66 | for i, num := 0, cmd.Sleepscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#Sleep./10; i < num; i++ { |
| 67 | cmd.AddFramescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#AddFrame().(b.frameForscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/MicrosoftBuilder#frameFor().(tpl, &Commandscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#{}), 0) |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return nil |
| 73 | } |