-
Notifications
You must be signed in to change notification settings - Fork 43
/
registry.js
155 lines (137 loc) · 6.26 KB
/
registry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
44
45
46
47
48
49
50
51
52
53
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
83
84
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* global Buffer */
'use strict';
var ffi = require('ffi'),
types = require('./types'),
advApi = require('./native/adv_api'),
Key = require('./key'),
ref = require('ref'),
error = require('./error'),
windef = require('./windef'),
debug = require('debug')('windows-registry');
var api = {
openKeyFromPredefined: function (preDefinedKey, subKeyName, accessLevel) {
if (preDefinedKey < 0x80000000 || preDefinedKey > 0x80000006) {
throw 'The key ' + preDefinedKey + ' is not valid. Use the windef module for the list of predefined keys';
}
var pHkey = ref.alloc(types.PHKEY, new Buffer(ref.sizeof.pointer));
debug('PHKEY LENGTH: ' + pHkey.deref().length);
var result = advApi.RegOpenKeyExA(preDefinedKey, subKeyName, 0, accessLevel, pHkey);
debug('result:' + result);
if (result !== 0) {
throw 'Failed to open key error: ' + error[result];
}
return new Key(pHkey, subKeyName);
},
openKeyFromKeyObject: function (keyObject, subKeyName, accessLevel) {
var pHkey = ref.alloc(types.PHKEY, new Buffer(ref.sizeof.pointer));
// RegOpenKeyEx can also take an HKEY in addition to a predefined value
var advApi2 = ffi.Library('Advapi32', {
RegOpenKeyExA: ['long', [types.HKEY, 'string', types.DWORD, types.REGSAM, types.PHKEY]]
});
var result = advApi2.RegOpenKeyExA(keyObject.handle.deref(), subKeyName, 0, accessLevel, pHkey);
if (result !== 0) {
throw 'Failed to open key error: ' + error[result];
}
return new Key(pHkey, subKeyName);
},
queryValueForKeyObject: function (key, valueName) {
var pKeyDataLength = ref.alloc(types.LPDWORD, new Buffer(ref.sizeof.pointer)),
pKeyType = ref.alloc(types.LPDWORD, new Buffer(ref.sizeof.pointer));
// QUERY FOR VALUE SIZE & TYPE
var result = advApi.RegQueryValueExA(key.handle.deref(), valueName, null, pKeyType, null, pKeyDataLength);
// READ VALUE
var value = new Buffer(pKeyDataLength.readUInt32LE()),
valueType = pKeyType.readUInt32LE();
switch (valueType) {
case windef.REG_VALUE_TYPE.REG_SZ:
case windef.REG_VALUE_TYPE.REG_EXPAND_SZ:
case windef.REG_VALUE_TYPE.REG_LINK:
value.type = types.LPCTSR;
break;
case windef.REG_VALUE_TYPE.REG_BINARY:
value.type = types.PVOID;
break;
case windef.REG_VALUE_TYPE.REG_DWORD:
case windef.REG_VALUE_TYPE.REG_DWORD_BIG_ENDIAN:
case windef.REG_VALUE_TYPE.REG_DWORD_LITTLE_ENDIAN:
value.type = types.DWORD;
break;
default:
throw 'The Value Type: ' + valueType + ' is currently unsupported';
}
// READ VALUE
result = advApi.RegQueryValueExA(key.handle.deref(), valueName, null, pKeyType, value, pKeyDataLength);
if (result !== 0) {
throw 'Failed to open key error: ' + error[result];
}
if (value.type === types.LPTSR) {
// TODO not sure why buffer's utf8 parsing leaves in the unicode null
// escape sequence. This is a work-around (at least on node 4.1)
value = value.toString().replace('\u0000', '');
}
return value;
},
setValueForKeyObject: function (key, valueName, valueType, value) {
if (valueType < 1 || valueType > 8) {
throw 'Invalid valueType parameter: ' + valueType + ' use values from windef.REG_VALUE_TYPE';
}
var buffer,
byte,
result;
switch (valueType) {
case windef.REG_VALUE_TYPE.REG_SZ:
case windef.REG_VALUE_TYPE.REG_EXPAND_SZ:
case windef.REG_VALUE_TYPE.REG_LINK:
buffer = new Buffer(value, 'utf8');
byte = ref.alloc(types.LPBYTE, buffer);
debug('content length:' + Buffer.byteLength(value, 'utf8'));
debug(value);
debug(buffer.length);
result = advApi.RegSetValueExA(key.handle.deref(), valueName, null, valueType, byte.deref(), Buffer.byteLength(value, 'utf8'));
break;
case windef.REG_VALUE_TYPE.REG_BINARY:
// we assume that the value is a buffer since it should be binary data
buffer = value;
byte = ref.alloc(types.LPBYTE, buffer);
result = advApi.RegSetValueExA(key.handle.deref(), valueName, null, valueType, byte.deref(), buffer.length);
break;
case windef.REG_VALUE_TYPE.REG_DWORD:
case windef.REG_VALUE_TYPE.REG_DWORD_BIG_ENDIAN:
case windef.REG_VALUE_TYPE.REG_DWORD_LITTLE_ENDIAN:
buffer = new Buffer(4, value);
result = advApi.RegSetValueExA(key.handle.deref(), valueName, null, valueType, byte.deref(), buffer.length);
break;
default:
throw 'The type ' + valueType + ' is currently unsupported';
}
if (result !== 0) {
throw 'Failed to open key error: ' + error[result];
}
},
createKey: function (key, subKeyName, accessLevel) {
var pHkey = ref.alloc(types.PHKEY, new Buffer(ref.sizeof.pointer));
var result = advApi.RegCreateKeyExA(key.handle.deref(), subKeyName, null, null, windef.REG_OPTION_NON_VOLATILE, accessLevel, null, pHkey, null);
if (result !== 0) {
throw 'Failed to open key error: ' + error[result];
}
},
deleteKey: function (key, subKeyName) {
var result = advApi.RegDeleteTreeA(key.handle.deref(), subKeyName);
if (result !== 0) {
throw 'Failed to open key error ' + result + ':' + error[result];
}
},
deleteValue: function (key, value) {
var result = advApi.RegDeleteValueA(key.handle.deref(), value);
if (result !== 0) {
throw 'Failed to delete value error ' + result + ':' + error[result];
}
},
closeKey: function (key) {
var result = advApi.RegCloseKey(key.handle.deref());
if (result !== 0) {
throw 'Failed to open key error ' + result + ':' + error[result];
}
}
};
module.exports = api;