Module: Rubber::Ducky::Encoder

Defined in:
lib/rubber-ducky/encoder.rb

Class Method Summary collapse

Class Method Details

.add_delay(delay_value) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rubber-ducky/encoder.rb', line 74

def self.add_delay(delay_value)
  delay_return = ''

  while delay_value > 0
    if delay_value > 255
      delay_return += '00FF'
      delay_value -= 255
    else
      _delay = delay_value.to_s(16).rjust(2, '0')
      delay_return += "00#{_delay}"
      delay_value = 0
    end
  end

  delay_return
end

.encode(duck_text, language) ⇒ Object



9
10
11
12
13
# File 'lib/rubber-ducky/encoder.rb', line 9

def self.encode(duck_text, language)
  lang_file = Common.load_language(language)
  encoded = parse_text(duck_text, lang_file)
  [encoded].pack('H*')
end

.encode_command(cmd, instruction, lang_file, encoded_file) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rubber-ducky/encoder.rb', line 56

def self.encode_command(cmd, instruction, lang_file, encoded_file)
  if lang_file[cmd]
    elements = lang_file[cmd].split(',')
    elements.map! { |i| i.to_i(16) }

    if instruction && lang_file[instruction]
      param = lang_file[instruction].split(',')
      param.map! { |i| i.to_i(16) }
      elements[0] |= param[0]
      elements[2] |= param[2]
    end

    encoded_file.push(Common.convert_hex(elements[2]), Common.convert_hex(elements[0]))
  else
    puts "Warning: Command '#{cmd}' not found in language file"
  end
end

.encode_string(text, lang_file, encoded_file) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/rubber-ducky/encoder.rb', line 45

def self.encode_string(text, lang_file, encoded_file)
  text.each_char do |char|
    if lang_file[char]
      elements = lang_file[char].split(',')
      encoded_file.push(Common.convert_hex(elements[2].to_i(16)), Common.convert_hex(elements[0].to_i(16)))
    else
      puts "Warning: Character '#{char}' not found in language file"
    end
  end
end

.parse_text(duck_text, lang_file) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubber-ducky/encoder.rb', line 15

def self.parse_text(duck_text, lang_file)
  encoded_file = []
  default_delay = 0

  duck_text.gsub!("\r", '')
  duck_text.split("\n").each do |line|
    line.strip!
    next if line.empty? || line.start_with?('REM', 'rem')

    cmd, instruction = line.split(' ', 2)
    cmd.strip!
    instruction&.strip!

    case cmd.upcase
    when 'DEFAULT_DELAY', 'DEFAULTDELAY'
      default_delay = instruction.to_i
    when 'DELAY'
      encoded_file.push(add_delay(instruction.to_i))
    when 'STRING'
      encode_string(instruction, lang_file, encoded_file)
    else
      encode_command(cmd, instruction, lang_file, encoded_file)
    end

    encoded_file.push(add_delay(default_delay)) if default_delay > 0
  end

  encoded_file.join
end