Class: WinReg

Inherits:
Object
  • Object
show all
Defined in:
lib/winreg.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, debug = false) ⇒ WinReg



10
11
12
13
14
15
16
17
18
# File 'lib/winreg.rb', line 10

def initialize(file,debug=false)
  @file = file
  @debug = debug
  # Check that file exists
  if not File.exists? @file
    raise "ERROR: File #{@file} does not exist"
  end
  # FIXME: Check that we have the chntpw command
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



8
9
10
# File 'lib/winreg.rb', line 8

def debug
  @debug
end

#fileObject

Returns the value of attribute file.



8
9
10
# File 'lib/winreg.rb', line 8

def file
  @file
end

#verboseObject

Returns the value of attribute verbose.



8
9
10
# File 'lib/winreg.rb', line 8

def verbose
  @verbose
end

Instance Method Details

#read_key(key) ⇒ Object

Read a key from hive



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/winreg.rb', line 21

def read_key(key)
  @value = nil

  PTY.spawn("chntpw -e #{@file}") do |read,write,pid|
    $expect_verbose = @debug
    write.sync = true

    # If 30 seconds pass and the expected text is not found, the
    # response object will be nil.
    read.expect(/^>/, 5) do |response|
      raise unless response
      write.print "cat " + key + "\n" if response
    end

    read.expect(/^>/, 5) do |response|
      raise unless response
      write.print "q" + "\n" if response
      # response is a string array
      @found=false
      response[0].split(/\r\n/).each do |line|
        if @found
          @value=line
          break
        end
        @found = true if line =~ /^Value/
      end
    end
  end

  return @value
end

#write_key(key, value) ⇒ Object

Write a key, value pair to hive



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/winreg.rb', line 54

def write_key(key,value)

  PTY.spawn("chntpw -e #{@file}") do |read,write,pid|
    $expect_verbose = @debug
    write.sync = true

    # If 30 seconds pass and the expected text is not found, the
    # response object will be nil.
    read.expect(/^>/, 5) do |response|
      raise unless response
      write.print "ed " + key + "\n" if response
    end

    read.expect(/^->/, 5) do |response|
      raise unless response
      write.print value + "\n" if response
    end

    read.expect(/^>/, 5) do |response|
      raise unless response
      write.print "q" + "\n" if response
    end

    read.expect(/^Write hive files?/, 5) do |response|
      raise unless response
      write.print "y" + "\n" if response
    end
  end
end

#write_keys(pairs) ⇒ Object

Write an array of key,value pairs to hive



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/winreg.rb', line 85

def write_keys(pairs)

  PTY.spawn("chntpw -e #{@file}") do |read,write,pid|
    $expect_verbose = @debug
    write.sync = true

    pairs.each do |key|
      # If 30 seconds pass and the expected text is not found, the
      # response object will be nil.
      read.expect(/^>/, 5) do |response|
        raise unless response
        write.print "ed " + key[:name] + "\n" if response
      end

      read.expect(/^->/, 5) do |response|
        raise unless response
        write.print key[:value] + "\n" if response
      end
    end

    read.expect(/^>/, 5) do |response|
      raise unless response
      write.print "q" + "\n" if response
    end

    read.expect(/^Write hive files?/, 5) do |response|
      raise unless response
      write.print "y" + "\n" if response
    end
  end
end