Debian Codegraph @

modules/hid/duckyparser.go

raw symbols SCIP
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 "strconvscip-go gomod github.com/golang/go/src go1.16 strconv/"
6 "stringsscip-go gomod github.com/golang/go/src go1.16 strings/"
7
8 "github.com/evilsocket/islazy/fsscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/fs`/"
9 )
10
11 type DuckyParserscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser# struct {
12 modscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#mod. *HIDReconscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/HIDRecon#
13 }
14
15 func (p DuckyParserscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#) parseLiteralscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#parseLiteral().(what string, kmap KeyMapscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/KeyMap#) (*Commandscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#, error) {
16 // get reference command from the layout
17 ref, found := kmap[what]
18 if found == false {
19 return nil, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("can't find '%s' in current keymap", what)
20 }
21 return &Commandscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#{
22 HIDscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#HID.: ref.HIDscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#HID.,
23 Modescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#Mode.: ref.Modescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#Mode.,
24 }, nil
25 }
26
27 func (p DuckyParserscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#) parseModifierscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#parseModifier().(line string, kmap KeyMapscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/KeyMap#, modMask byte) (*Commandscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#, error) {
28 // get optional key after the modifier
29 ch := ""
30 if idx := stringsscip-go gomod github.com/golang/go/src go1.16 strings/.IndexRunescip-go gomod github.com/golang/go/src go1.16 strings/IndexRune().(line, ' '); idx != -1 {
31 ch = line[idx+1:]
32 }
33 cmd, err := p.parseLiteralscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#parseLiteral().(ch, kmap)
34 if err != nil {
35 return nil, err
36 }
37 // apply modifier mask
38 cmd.Modescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#Mode. |= modMask
39 return cmd, nil
40 }
41
42 func (p DuckyParserscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#) parseNumberscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#parseNumber().(from string) (int, error) {
43 idx := stringsscip-go gomod github.com/golang/go/src go1.16 strings/.IndexRunescip-go gomod github.com/golang/go/src go1.16 strings/IndexRune().(from, ' ')
44 if idx == -1 {
45 return 0, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("can't parse number from '%s'", from)
46 }
47
48 num, err := strconvscip-go gomod github.com/golang/go/src go1.16 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.16 strconv/Atoi().(from[idx+1:])
49 if err != nil {
50 return 0, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("can't parse number from '%s': %v", from, err)
51 }
52
53 return num, nil
54 }
55
56 func (p DuckyParserscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#) parseStringscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#parseString().(from string) (string, error) {
57 idx := stringsscip-go gomod github.com/golang/go/src go1.16 strings/.IndexRunescip-go gomod github.com/golang/go/src go1.16 strings/IndexRune().(from, ' ')
58 if idx == -1 {
59 return "", fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("can't parse string from '%s'", from)
60 }
61 return from[idx+1:], nil
62 }
63
64 func (p DuckyParserscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#) lineIsscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#lineIs().(line string, tokens ...string) bool {
65 for _, tok := range tokens {
66 if stringsscip-go gomod github.com/golang/go/src go1.16 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.16 strings/HasPrefix().(line, tok) {
67 return true
68 }
69 }
70 return false
71 }
72
73 func (p DuckyParserscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#) Parsescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#Parse().(kmap KeyMapscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/KeyMap#, path string) (cmds []*Commandscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#, err error) {
74 lines := []string{}
75 source := []string{}
76 reader := (chan string)(nil)
77
78 if reader, err = fsscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/fs`/.LineReaderscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/fs`/LineReader().(path); err != nil {
79 return
80 } else {
81 for line := range reader {
82 lines = append(lines, line)
83 }
84 }
85
86 // preprocessing
87 for lineno, line := range lines {
88 if p.lineIsscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#lineIs().(line, "REPEAT") {
89 if lineno == 0 {
90 err = fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("error on line %d: REPEAT instruction at the beginning of the script", lineno+1)
91 return
92 }
93 times := 1
94 times, err = p.parseNumberscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#parseNumber().(line)
95 if err != nil {
96 return
97 }
98
99 for i := 0; i < times; i++ {
100 source = append(source, lines[lineno-1])
101 }
102 } else {
103 source = append(source, line)
104 }
105 }
106
107 cmds = make([]*Commandscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#, 0)
108 for _, line := range source {
109 cmd := &Commandscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#{}
110 if p.lineIsscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#lineIs().(line, "CTRL", "CONTROL") {
111 if cmd, err = p.parseModifierscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#parseModifier().(line, kmap, 1); err != nil {
112 return
113 }
114 } else if p.lineIsscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#lineIs().(line, "SHIFT") {
115 if cmd, err = p.parseModifierscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#parseModifier().(line, kmap, 2); err != nil {
116 return
117 }
118 } else if p.lineIsscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#lineIs().(line, "ALT") {
119 if cmd, err = p.parseModifierscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#parseModifier().(line, kmap, 4); err != nil {
120 return
121 }
122 } else if p.lineIsscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#lineIs().(line, "GUI", "WINDOWS", "COMMAND") {
123 if cmd, err = p.parseModifierscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#parseModifier().(line, kmap, 8); err != nil {
124 return
125 }
126 } else if p.lineIsscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#lineIs().(line, "CTRL-ALT", "CONTROL-ALT") {
127 if cmd, err = p.parseModifierscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#parseModifier().(line, kmap, 4|1); err != nil {
128 return
129 }
130 } else if p.lineIsscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#lineIs().(line, "CTRL-SHIFT", "CONTROL-SHIFT") {
131 if cmd, err = p.parseModifierscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#parseModifier().(line, kmap, 1|2); err != nil {
132 return
133 }
134 } else if p.lineIsscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#lineIs().(line, "ESC", "ESCAPE", "APP") {
135 if cmd, err = p.parseLiteralscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#parseLiteral().("ESCAPE", kmap); err != nil {
136 return
137 }
138 } else if p.lineIsscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#lineIs().(line, "ENTER") {
139 if cmd, err = p.parseLiteralscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#parseLiteral().("ENTER", kmap); err != nil {
140 return
141 }
142 } else if p.lineIsscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#lineIs().(line, "UP", "UPARROW") {
143 if cmd, err = p.parseLiteralscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#parseLiteral().("UP", kmap); err != nil {
144 return
145 }
146 } else if p.lineIsscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#lineIs().(line, "DOWN", "DOWNARROW") {
147 if cmd, err = p.parseLiteralscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#parseLiteral().("DOWN", kmap); err != nil {
148 return
149 }
150 } else if p.lineIsscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#lineIs().(line, "LEFT", "LEFTARROW") {
151 if cmd, err = p.parseLiteralscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#parseLiteral().("LEFT", kmap); err != nil {
152 return
153 }
154 } else if p.lineIsscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#lineIs().(line, "RIGHT", "RIGHTARROW") {
155 if cmd, err = p.parseLiteralscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#parseLiteral().("RIGHT", kmap); err != nil {
156 return
157 }
158 } else if p.lineIsscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#lineIs().(line, "DELAY", "SLEEP") {
159 secs := 0
160 if secs, err = p.parseNumberscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#parseNumber().(line); err != nil {
161 return
162 }
163 cmd = &Commandscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#{Sleepscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/Command#Sleep.: secs}
164 } else if p.lineIsscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#lineIs().(line, "STRING", "STR") {
165 str := ""
166 if str, err = p.parseStringscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#parseString().(line); err != nil {
167 return
168 }
169
170 for _, c := range str {
171 if cmd, err = p.parseLiteralscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#parseLiteral().(string(c), kmap); err != nil {
172 return
173 }
174 cmds = append(cmds, cmd)
175 }
176
177 continue
178 } else if cmd, err = p.parseLiteralscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/hid`/DuckyParser#parseLiteral().(line, kmap); err != nil {
179 err = fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("error parsing '%s': %s", line, err)
180 return
181 }
182
183 cmds = append(cmds, cmd)
184 }
185
186 return
187 }