hivex: Make node names and value names with embedded null characters accessible
On Windows, there exist at least two APIs for dealing with the Registry: The Win32 API (RegCreateKeyA, RegCreateKeyW, etc.) works with null-terminated ASCII or UTF-16 strings. The native API (ZwCreateKey, etc.), on the other hand works with UTF-16 strings that are stored as buffers+length and may contain null characters. Malware authors have been relying on the Win32 API's inability to properly work with such names for several years. These changes make such names accessible from hivex.
--- lib/hivex-internal.h | 1 + lib/utf16.c | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/hivex-internal.h b/lib/hivex-internal.h index 6bc8638..7f4cc3c 100644 --- a/lib/hivex-internal.h +++ b/lib/hivex-internal.h @@ -277,6 +277,7 @@ extern char * _hivex_recode (const char *input_encoding, _hivex_recode ("LATIN1", _input, _len, "UTF-8", NULL) extern char* _hivex_encode_string(const char *str, size_t *size, int *utf16); extern size_t _hivex_utf16_string_len_in_bytes_max (const char *str, size_t len); +extern size_t _hivex_utf8_strlen (const char* str, size_t len, int utf16); /* util.c */ extern void _hivex_free_strings (char **argv); diff --git a/lib/utf16.c b/lib/utf16.c index 3641580..7dde9e5 100644 --- a/lib/utf16.c +++ b/lib/utf16.c @@ -123,3 +123,13 @@ _hivex_utf16_string_len_in_bytes_max (const char *str, size_t len) return ret; } + +size_t +_hivex_utf8_strlen (const char* str, size_t len, int utf16) +{ + char* encoding = utf16 ? "UTF-16LE" : "LATIN1"; + size_t ret; + char* buf = _hivex_recode(encoding, str, len, "UTF-8", &ret); + free(buf); + return ret; +} -- 1.8.5.2
Since iconv() does not treat null characters as special, this makes it possible to read node names and value keys with embedded null characters. which are not displayed at all by Windows Regedit. --- generator/generator.ml | 19 ++++++++++++++----- lib/node.c | 24 ++++++++++++++++++++++++ lib/value.c | 9 +++++---- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/generator/generator.ml b/generator/generator.ml index 54e5f0f..1c62129 100755 --- a/generator/generator.ml +++ b/generator/generator.ml @@ -179,7 +179,14 @@ C<$$$PROTO.HIV> (other names are possible: it seems to depend on the tool or program that created the hive in the first place). You can only know the \"real\" name of the root node by knowing which registry file this hive originally comes from, which is knowledge that is -outside the scope of this library."; +outside the scope of this library. + +The name is recoded to UTF-8 and may contain embedded NUL characters."; + + "node_name_len", (RSize, [AHive; ANode "node"]), + "return the length of a node's name", + "\ +Return the length of the node name as produced by C<hivex_node_name>."; "node_timestamp", (RInt64, [AHive; ANode "node"]), "return the modification time of the node", @@ -233,13 +240,15 @@ default key."; "value_key_len", (RSize, [AHive; AValue "val"]), "return the length of a value's key", "\ -Return the length of the key (name) of a (key, value) pair. The -length can legitimately be 0, so errno is the necesary mechanism -to check for errors. +Return the length of the key (name) of a (key, value) pair as produced +by C<hivex_value_key>. The length can legitimately be 0, so errno is +the necesary mechanism to check for errors. In the context of Windows Registries, a zero-length name means that this value is the default key for this node in the tree. -This is usually written as C<\"@\">."; +This is usually written as C<\"@\">. + +The key is recoded to UTF-8 and may contain embedded NUL characters."; "value_key", (RString, [AHive; AValue "val"]), "return the key of a (key, value) pair", diff --git a/lib/node.c b/lib/node.c index fcd7442..22d1861 100644 --- a/lib/node.c +++ b/lib/node.c @@ -96,6 +96,30 @@ hivex_node_name (hive_h *h, hive_node_h node) } } +size_t +hivex_node_name_len (hive_h *h, hive_node_h node) +{ + if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) { + SET_ERRNO (EINVAL, "invalid block or not an 'nk' block"); + return 0; + } + struct ntreg_nk_record *nk = + (struct ntreg_nk_record *) ((char *) h->addr + node); + + /* nk->name_len is unsigned, 16 bit, so this is safe ... However + * we have to make sure the length doesn't exceed the block length. + */ + size_t len = le16toh (nk->name_len); + size_t seg_len = block_len (h, node, NULL); + if (sizeof (struct ntreg_nk_record) + len - 1 > seg_len) { + SET_ERRNO (EFAULT, "node name is too long (%zu, %zu)", len, seg_len); + return 0; + } + + return _hivex_utf8_strlen (nk->name, len, ! (le16toh (nk->flags) & 0x20)); +} + + static int64_t timestamp_check (hive_h *h, hive_node_h node, int64_t timestamp) { diff --git a/lib/value.c b/lib/value.c index 3460a8c..65404d7 100644 --- a/lib/value.c +++ b/lib/value.c @@ -186,13 +186,14 @@ hivex_value_key_len (hive_h *h, hive_value_h value) /* vk->name_len is unsigned, 16 bit, so this is safe ... However * we have to make sure the length doesn't exceed the block length. */ - size_t ret = le16toh (vk->name_len); + size_t len = le16toh (vk->name_len); + size_t seg_len = block_len (h, value, NULL); - if (sizeof (struct ntreg_vk_record) + ret - 1 > seg_len) { - SET_ERRNO (EFAULT, "key length is too long (%zu, %zu)", ret, seg_len); + if (sizeof (struct ntreg_vk_record) + len - 1 > seg_len) { + SET_ERRNO (EFAULT, "key length is too long (%zu, %zu)", len, seg_len); return 0; } - return ret; + return _hivex_utf8_strlen (vk->name, len, ! (le16toh (vk->flags) & 0x01)); } char * -- 1.8.5.2
--- generator/generator.ml | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/generator/generator.ml b/generator/generator.ml index 1c62129..6b1eef7 100755 --- a/generator/generator.ml +++ b/generator/generator.ml @@ -380,6 +380,11 @@ new key is added. Key matching is case insensitive. C<node> is the node to modify."; ] +let f_len_exists n = + List.exists + (function (cand, _, _, _) -> cand = (String.concat "" [n; "_len"])) + functions + (* Useful functions. * Note we don't want to use any external OCaml libraries which * makes this a bit harder than it should be. @@ -1839,9 +1844,7 @@ static void raise_closed (const char *) Noreturn; * call, so don't do it. XXX *) (*pr " caml_enter_blocking_section ();\n";*) - pr " r = hivex_%s (%s" name (List.hd c_params); - List.iter (pr ", %s") (List.tl c_params); - pr ");\n"; + pr " r = hivex_%s (%s);\n" name (String.concat ", " c_params); (*pr " caml_leave_blocking_section ();\n";*) pr "\n"; @@ -1890,7 +1893,13 @@ static void raise_closed (const char *) Noreturn; pr " rv = copy_int_array (r);\n"; pr " free (r);\n" | RString -> - pr " rv = caml_copy_string (r);\n"; + if f_len_exists name then ( + pr " size_t sz;\n sz = hivex_%s_len (%s);\n" + name (String.concat ", " c_params); + pr " rv = caml_alloc_string (sz);\n"; + pr " memcpy (String_val (rv), r, sz);\n" + ) else + pr " rv = caml_copy_string (r);\n"; pr " free (r);\n" | RStringList -> pr " rv = caml_copy_string_array ((const char **) r);\n"; @@ -2638,7 +2647,11 @@ DESTROY (h) pr " if (r == NULL)\n"; pr " croak (\"%%s: %%s\", \"%s\", strerror (errno));\n" name; - pr " RETVAL = newSVpv (r, 0);\n"; + if f_len_exists name then + pr " RETVAL = newSVpvn (r, hivex_%s_len (%s));\n" + name (String.concat ", " c_params) + else + pr " RETVAL = newSVpv (r, 0);\n"; pr " free (r);\n"; pr " OUTPUT:\n"; pr " RETVAL\n" @@ -3178,10 +3191,19 @@ put_val_type (char *val, size_t len, hive_type t) | RValue -> pr " py_r = PyLong_FromLongLong (r);\n" | RString -> + if f_len_exists name then + pr " size_t sz = hivex_%s_len (%s);\n" + name (String.concat ", " c_params); pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; - pr " py_r = PyString_FromString (r);\n"; + if f_len_exists name then + pr " py_r = PyString_FromStringAndSize (r, sz);\n" + else + pr " py_r = PyString_FromString (r);\n"; pr "#else\n"; - pr " py_r = PyUnicode_FromString (r);\n"; + if f_len_exists name then + pr " py_r = PyUnicode_FromStringAndSize (r, sz);\n" + else + pr " py_r = PyUnicode_FromString (r);\n"; pr "#endif\n"; pr " free (r);" | RStringList -> @@ -3633,7 +3655,11 @@ get_values (VALUE valuesv, size_t *nr_values) pr " free (r);\n"; pr " return rv;\n" | RString -> - pr " VALUE rv = rb_str_new2 (r);\n"; + if f_len_exists name then ( + pr " size_t sz = hivex_%s_len (%s);\n" name (String.concat ", " c_params); + pr " VALUE rv = rb_str_new (r, sz);\n" + ) else + pr " VALUE rv = rb_str_new2 (r);\n"; pr " free (r);\n"; pr " return rv;\n" | RStringList -> -- 1.8.5.2
On Wed, Jan 08, 2014 at 01:26:23AM +0100, Hilko Bengen wrote:
On Windows, there exist at least two APIs for dealing with the Registry: The Win32 API (RegCreateKeyA, RegCreateKeyW, etc.) works with null-terminated ASCII or UTF-16 strings. The native API (ZwCreateKey, etc.), on the other hand works with UTF-16 strings that are stored as buffers+length and may contain null characters. Malware authors have been relying on the Win32 API's inability to properly work with such names for several years.
These changes make such names accessible from hivex.
ACK to all 3 patches. It be nice to have some sort of test coverage of these. Thanks, Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://people.redhat.com/~rjones/virt-df/
--- images/README | 15 +++++++++++++++ images/mkzero/Makefile | 7 +++++++ images/mkzero/mkzero.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ images/special | Bin 0 -> 8192 bytes 4 files changed, 70 insertions(+) create mode 100644 images/mkzero/Makefile create mode 100644 images/mkzero/mkzero.c create mode 100644 images/special diff --git a/images/README b/images/README index 2131885..34c65f3 100644 --- a/images/README +++ b/images/README @@ -11,3 +11,18 @@ hand-crafted binary blob. tests. - Richard W.M. Jones 2010-02-24. + +'special' was created by importing 'minimal' into a VM running Windows +XP and loading it into HKEY_LOCAL_MACHINE\minimal using regedit.exe +(File/Load Hive...) + +- A subkey 'asdf_äöüß' was created in the root node + - An empty REG_STRING value 'asdf_äöüß' was created within that node. +- A subkey 'weird™' was created in the root node. + - An empty REG_STRING value 'symbols $£₤₧€' (SMALL DOLLAR SIGN, + FULLWIDTH POUND SIGN, PESETA SIGN, EURO SIGN) was created within + that node. +- A subkey 'zero\0key' with an REG_DWORD value 'zero\0val' + was created using the 'mkzero/mkzero.c'. (\0 = zero character) + +- Hilko Bengen 2014-01-10. diff --git a/images/mkzero/Makefile b/images/mkzero/Makefile new file mode 100644 index 0000000..4d33ade --- /dev/null +++ b/images/mkzero/Makefile @@ -0,0 +1,7 @@ +CROSS=i686-w64-mingw32- + +all: mkzero.exe +mkzero.exe: mkzero.o + $(CROSS)gcc -o $@ $< -lntdll +%.o: %.c + $(CROSS)gcc -o $@ -c $< diff --git a/images/mkzero/mkzero.c b/images/mkzero/mkzero.c new file mode 100644 index 0000000..1b50b22 --- /dev/null +++ b/images/mkzero/mkzero.c @@ -0,0 +1,48 @@ +/* use the NT native API to create registry key and value that contain + a zero character */ + +#include <ntdef.h> +#include <stdio.h> +#include <ddk/wdm.h> +#include <windef.h> + +int main (int argc, char **argv) +{ + NTSTATUS rc; + + UNICODE_STRING root_key_name; + RtlInitUnicodeString(&root_key_name, L"\\Registry\\Machine\\minimal"); + OBJECT_ATTRIBUTES root_key_obj; + InitializeObjectAttributes (&root_key_obj, &root_key_name, + OBJ_OPENIF | OBJ_CASE_INSENSITIVE, + NULL, NULL); + HANDLE minimal_key_handle; + rc = ZwCreateKey (&minimal_key_handle, KEY_ALL_ACCESS, &root_key_obj, + 0, NULL, REG_OPTION_NON_VOLATILE, NULL); + if (!NT_SUCCESS (rc)) { + printf("error: CreateKey <HKLM\\minimal>: 0x%08x\n", rc); + exit(1); + } + + UNICODE_STRING key_name = {16, 16, L"zero\0key"}; + OBJECT_ATTRIBUTES key_obj; + InitializeObjectAttributes (&key_obj, &key_name, + OBJ_OPENIF | OBJ_CASE_INSENSITIVE, + minimal_key_handle, NULL); + HANDLE key_handle; + rc = ZwCreateKey (&key_handle, KEY_ALL_ACCESS, &key_obj, + 0, NULL, REG_OPTION_NON_VOLATILE, NULL); + if (!NT_SUCCESS (rc)) { + printf("error: CreateKey: 0x%08x\n", rc); + exit(1); + } + + UNICODE_STRING value_name = {16, 16, L"zero\0val"}; + DWORD value = 0; + rc = ZwSetValueKey (key_handle, &value_name, 0, + REG_DWORD, &value, sizeof(value)); + if (!NT_SUCCESS (rc)) { + printf("error: SetValueKey: 0x%08x\n", rc); + exit(1); + } +} diff --git a/images/special b/images/special new file mode 100644 index 0000000000000000000000000000000000000000..8aa4f2254af7b52f0a79061a7288c2128eee7b63 GIT binary patch literal 8192 zcmeHLziU%b6h28COhb#$Ul4=fYx@MLKL!^^6)M(2Dk4tBk~GnVmnM=(q~Z`n)IUHs zhkyk;I61kvcM)}{1Q$C94muRkP^1{Y@7_GxmnJ$170!YC?z!ijckape-TNS?oV$K) zEF!{jqOm&ic<4<8s>CoT)Lkl$)hjY3XCy0gl9wVzMQ%!2(s-6IPszCCVDruT2G&8` z9AXO0qq1!fXx39_w@ooxh$5f}C<2OrBA^H;0*Zhlpa>`eioicbU^bI4nho&(K9Haf zz;(UVY8CyX^5XN4gX|5^c6x_M9~H=AzX(EF)H&DM^IjS{WDsl5^DbVRygYe);`|l5 zmwvTc6<;J7wSyZRz#}64kJx;0%#I?=7nQG);7gK}{a8it4kYH{F-$#?#6xOZ>|NqV zd&C<%;!nxWP=0qK-r1br*ioH{x3$K0vx<a4z%k7f)M#uy^M+y#w4Hl|^N6ef>#83Y zz+dBbm`$(dcCK8KYPzrn|4Y=<FmVGw%f_)pj&x=wJN>D-{t;aqV!DdB6*xGaxzF8t zo0l4V94u-aKJapH5c6RSisfa^^5XR1CDntMF7XEcF7d{W_^!Oz_^rG+U3f`_u(rH7 zp3Q5UUa@`nU5h8*=k9#*hDN7n{O|m&;rA>@e~6Ft@U?kmJ59(>9P_z7$1d1=Ci3&h zu4c7;&s_Kd0X;?x>>!)>g@(ZILg5jm@Sfd5A1;rMTvkSgX(I!L*{Io*`#r=jSYEha zBJU3JaZr}9uKKCad7-@`^XTPe(B&&Ycg2&Zvf%Qh&>MUwTv>ne?$F)8)E`Hoo%OT6 zp#A~q8|RbR?Kv*Y_Q?e5yt@40bx{9XtY3ZeKG-PmhWd-X8~R?kffF;_Mu=krkIp*C oOB)Kqjk!;LZoXkZJexA9st70oihv@Z2q*%IfFhs>{C^1i23(Tu`2YX_ literal 0 HcmV?d00001 -- 1.8.5.2
On Fri, Jan 10, 2014 at 01:14:51AM +0100, Hilko Bengen wrote:
+ was created using the 'mkzero/mkzero.c'. (\0 = zero character)
Extra whitespace at the end of this ^ line ...
+ InitializeObjectAttributes (&key_obj, &key_name,
.. and this line. --- So there's no problem with adding this test data, but are you planning to also add some tests :-? Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://people.redhat.com/~rjones/virt-df/
* Richard W.M. Jones:
So there's no problem with adding this test data, but are you planning to also add some tests :-?
Sure. Working on it. Good that you asked for tests, btw. Another bugfix patch or two will be on the way. :-) Cheers, -Hilko
--- images/README | 14 ++++++++++++ images/mkzero/Makefile | 9 ++++++++ images/mkzero/mkzero.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ images/special | Bin 0 -> 8192 bytes 4 files changed, 82 insertions(+) create mode 100644 images/mkzero/Makefile create mode 100644 images/mkzero/mkzero.c create mode 100644 images/special diff --git a/images/README b/images/README index 2131885..b01e5a2 100644 --- a/images/README +++ b/images/README @@ -11,3 +11,17 @@ hand-crafted binary blob. tests. - Richard W.M. Jones 2010-02-24. + +'special' was created by importing 'minimal' into a VM running Windows +XP, loading it into HKEY_LOCAL_MACHINE\minimal using regedit.exe +(File/Load Hive...), and running 'mkzero.exe'. + +'mkzero.exe' creates the following keys and values: + +- A key 'zero\0key' containing a REG_DWORD value 'zero\0val' (\0 = zero + character) +- A key 'asdf_äöüß' containing a REG_DWORD value 'asdf_äöüß' +- A key 'weird™' containing a REG_DWORD value 'symbols $£₤₧€' (SMALL + DOLLAR SIGN, FULLWIDTH POUND SIGN, PESETA SIGN, EURO SIGN) + +- Hilko Bengen 2014-01-10. diff --git a/images/mkzero/Makefile b/images/mkzero/Makefile new file mode 100644 index 0000000..affe52b --- /dev/null +++ b/images/mkzero/Makefile @@ -0,0 +1,9 @@ +CROSS=i686-w64-mingw32- +CFLAGS=--std=c99 +all: mkzero.exe +clean: + rm -f *.exe *.o +mkzero.exe: mkzero.o + $(CROSS)gcc -o $@ $< -lntdll +%.o: %.c + $(CROSS)gcc $(CFLAGS) -Wpedantic -Wall -o $@ -c $< diff --git a/images/mkzero/mkzero.c b/images/mkzero/mkzero.c new file mode 100644 index 0000000..a95794a --- /dev/null +++ b/images/mkzero/mkzero.c @@ -0,0 +1,59 @@ +/* use the NT native API to create registry key and value that contain + a zero character */ + +#include <ntdef.h> +#include <stdio.h> +#include <ddk/wdm.h> +#include <windef.h> + +void create_key_value (PHANDLE handle, WCHAR* key, int key_len, WCHAR* val, int val_len) +{ + UNICODE_STRING key_name = { key_len, key_len, key }; + UNICODE_STRING value_name = { val_len, val_len, val }; + OBJECT_ATTRIBUTES key_obj; + InitializeObjectAttributes (&key_obj, &key_name, + OBJ_OPENIF | OBJ_CASE_INSENSITIVE, + *handle, NULL); + HANDLE key_handle; + NTSTATUS rc; + rc = ZwCreateKey (&key_handle, KEY_ALL_ACCESS, &key_obj, + 0, NULL, REG_OPTION_NON_VOLATILE, NULL); + if (!NT_SUCCESS (rc)) { + wprintf(L"error: CreateKey %s: 0x%08x\n", key, rc); + exit(1); + } + DWORD value = 0; + rc = ZwSetValueKey (key_handle, &value_name, 0, + REG_DWORD, &value, sizeof(value)); + if (!NT_SUCCESS (rc)) { + wprintf(L"error: SetValueKey %s: 0x%08x\n", val, rc); + exit(1); + } +} + +int main (int argc, char **argv) +{ + UNICODE_STRING root_key_name; + RtlInitUnicodeString(&root_key_name, L"\\Registry\\Machine\\minimal"); + OBJECT_ATTRIBUTES root_key_obj; + InitializeObjectAttributes (&root_key_obj, &root_key_name, + OBJ_OPENIF | OBJ_CASE_INSENSITIVE, + NULL, NULL); + HANDLE minimal_key_handle; + NTSTATUS rc = ZwCreateKey (&minimal_key_handle, KEY_ALL_ACCESS, &root_key_obj, + 0, NULL, REG_OPTION_NON_VOLATILE, NULL); + if (!NT_SUCCESS (rc)) { + wprintf(L"error: CreateKey <HKLM\\minimal>: 0x%08x\n", rc); + exit(1); + } + WCHAR k1[] = L"zero\0key"; + WCHAR v1[] = L"zero\0val"; + create_key_value (&minimal_key_handle, k1, sizeof (k1)-2, v1, sizeof (v1)-2); + WCHAR k2[] = L"abcd_äöüß"; + WCHAR v2[] = L"abcd_äöüß"; + create_key_value (&minimal_key_handle, k2, sizeof (k2)-2, v2, sizeof (v2)-2); + WCHAR k3[] = L"weird™"; + WCHAR v3[] = L"symbols $£₤₧€"; + create_key_value (&minimal_key_handle, k3, sizeof (k3)-2, v3, sizeof (v3)-2); + return 0; +} diff --git a/images/special b/images/special new file mode 100644 index 0000000000000000000000000000000000000000..8a5ff5f34f32f0e56c1e1e9abf49bd3a8857c5e7 GIT binary patch literal 8192 zcmeHLziU%b6h28CZ9~<EDAeHa+P=XdxC$;R6s%K4L=cN5X`%@)O-drwio+ZH@elY1 z1T2`rMJE?Ghc2Q^L|k-m(@8`sh|ll4H&6SLm(oQjat_>k?mh3^bH4X|uYulX`TDgz zDG`p7(@(o+_rFeIDH#kJ*De>1UAJUG&PYj?r7CrdmfVo06fig77i3n-i1}u}f_;>? zjGP+lsO=gC@|UkX+cjVmh%%rIC<DrXGN2471ImChpbRJj%D_Kmpi->XO#}RYA6aw{ zz<oOm!@8fB{JkG9c{jk=>AfN&T);Y`B1j3jrVo1lAOn3ifxYK>=P%4(oIgHy?h+qw z{0hUCkL!iB9c*wEQ%Xi3^76qk9Yqcyo|Z3?<sq4rBiN;I29|L#O>v!_#3aE$JRyH( zNWO_9|CAhv)p!5qJ6r3UIMA7Vi#223Jxj!Zzcn_rZY<YO+utKz2fTypy6?txaoMqF z)PlzLZB!(`+(*wTH>5K*hWU26*$}_H*6!&MAjUy3vnT>R?oPlTv+vW>W7F#44ACPu zM301g!+S!$i6cKz51T)zhm)X3F2=Fd$nk7ld)bvajCbvvcw4^n+1od>xa5E1yT;F1 zZMMlz4SCknQ#PT23*zbDz<-2a^~b11fNu2s?5!TFg&Ok$bZx`$RPU7pz4=YDHf<cE zP+Tf4e)|6NgNg7jN~BgvO9yAKb|1WY4iGQCdhg#EeB%T7`p;W@vZL?uRsy~p`bYTq zJ8={Dh*jJumSlR05Q(E!8To^64$0m1`;7=Eb7&g|Z!U(H@s_ONF0zPQ${N<Lc=AZr k-N)_|mrtRG{b8{ios|J)Kp9X5lmTTx8Bhk4f&Y|&-#+)~(*OVf literal 0 HcmV?d00001 -- 1.8.5.2
--- lib/value.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lib/value.c b/lib/value.c index 65404d7..e700c84 100644 --- a/lib/value.c +++ b/lib/value.c @@ -207,14 +207,8 @@ hivex_value_key (hive_h *h, hive_value_h value) struct ntreg_vk_record *vk = (struct ntreg_vk_record *) ((char *) h->addr + value); - /* AFAIK the key is always plain ASCII, so no conversion to UTF-8 is - * necessary. However we do need to nul-terminate the string. - */ - errno = 0; - size_t len = hivex_value_key_len (h, value); - if (len == 0 && errno != 0) - return NULL; size_t flags = le16toh (vk->flags); + size_t len = le16toh (vk->name_len); if (flags & 0x01) { return _hivex_windows_latin1_to_utf8 (vk->name, len); } else { -- 1.8.5.2
On Sat, Jan 11, 2014 at 12:12:47AM +0100, Hilko Bengen wrote:
--- lib/value.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-)
diff --git a/lib/value.c b/lib/value.c index 65404d7..e700c84 100644 --- a/lib/value.c +++ b/lib/value.c @@ -207,14 +207,8 @@ hivex_value_key (hive_h *h, hive_value_h value) struct ntreg_vk_record *vk = (struct ntreg_vk_record *) ((char *) h->addr + value);
- /* AFAIK the key is always plain ASCII, so no conversion to UTF-8 is - * necessary. However we do need to nul-terminate the string. - */ - errno = 0; - size_t len = hivex_value_key_len (h, value); - if (len == 0 && errno != 0) - return NULL; size_t flags = le16toh (vk->flags); + size_t len = le16toh (vk->name_len);
I think this effectively removes a check. hivex_value_key_len contains this code: size_t seg_len = block_len (h, value, NULL); if (sizeof (struct ntreg_vk_record) + len - 1 > seg_len) { SET_ERRNO (EFAULT, "key length is too long (%zu, %zu)", len, seg_len); return 0; } but after this change, this would no longer be run, so it would be possible to overrun the registry, or at least overrun the end of a block. Is there a reason for this patch? It seems like just an optimization. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://people.redhat.com/~rjones/virt-df/
On Mon, Jan 13, 2014 at 01:17:52PM +0000, Richard W.M. Jones wrote:
Is there a reason for this patch? It seems like just an optimization.
OK, I see that the reason is to avoid calling _hivex_utf8_strlen which would fail for the name containing a \0 character. However I still think we need to keep that check. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://people.redhat.com/~rjones/virt-df/
--- lib/value.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/value.c b/lib/value.c index 65404d7..7b2e9d3 100644 --- a/lib/value.c +++ b/lib/value.c @@ -207,14 +207,14 @@ hivex_value_key (hive_h *h, hive_value_h value) struct ntreg_vk_record *vk = (struct ntreg_vk_record *) ((char *) h->addr + value); - /* AFAIK the key is always plain ASCII, so no conversion to UTF-8 is - * necessary. However we do need to nul-terminate the string. - */ - errno = 0; - size_t len = hivex_value_key_len (h, value); - if (len == 0 && errno != 0) - return NULL; size_t flags = le16toh (vk->flags); + size_t len = le16toh (vk->name_len); + + size_t seg_len = block_len (h, value, NULL); + if (sizeof (struct ntreg_vk_record) + len - 1 > seg_len) { + SET_ERRNO (EFAULT, "key length is too long (%zu, %zu)", len, seg_len); + return 0; + } if (flags & 0x01) { return _hivex_windows_latin1_to_utf8 (vk->name, len); } else { -- 1.8.5.2
On Mon, Jan 13, 2014 at 04:53:25PM +0100, Hilko Bengen wrote:
--- lib/value.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/lib/value.c b/lib/value.c index 65404d7..7b2e9d3 100644 --- a/lib/value.c +++ b/lib/value.c @@ -207,14 +207,14 @@ hivex_value_key (hive_h *h, hive_value_h value) struct ntreg_vk_record *vk = (struct ntreg_vk_record *) ((char *) h->addr + value);
- /* AFAIK the key is always plain ASCII, so no conversion to UTF-8 is - * necessary. However we do need to nul-terminate the string. - */ - errno = 0; - size_t len = hivex_value_key_len (h, value); - if (len == 0 && errno != 0) - return NULL; size_t flags = le16toh (vk->flags); + size_t len = le16toh (vk->name_len); + + size_t seg_len = block_len (h, value, NULL); + if (sizeof (struct ntreg_vk_record) + len - 1 > seg_len) { + SET_ERRNO (EFAULT, "key length is too long (%zu, %zu)", len, seg_len); + return 0;
It's be nicer if this ^^ was return NULL rather than 0 (although of course the effect is the same). Anyhow, ACK. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-top is 'top' for virtual machines. Tiny program with many powerful monitoring features, net stats, disk stats, logging, etc. http://people.redhat.com/~rjones/virt-top
* Richard W.M. Jones:
On Mon, Jan 13, 2014 at 01:17:52PM +0000, Richard W.M. Jones wrote:
Is there a reason for this patch? It seems like just an optimization.
OK, I see that the reason is to avoid calling _hivex_utf8_strlen which would fail for the name containing a \0 character. However I still think we need to keep that check.
You're right. I have pulled the check into hivex_value_key(). Cheers, -Hilko
--- perl/t/130-special.t | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 perl/t/130-special.t diff --git a/perl/t/130-special.t b/perl/t/130-special.t new file mode 100644 index 0000000..c25af7e --- /dev/null +++ b/perl/t/130-special.t @@ -0,0 +1,34 @@ +# hivex Perl bindings -*- perl -*- + +use strict; +use warnings; +use utf8::all; # so the strings in this file are interpreted correctly. +use Test::More; + +use Win::Hivex; + +my $srcdir = $ENV{srcdir} || "."; +my $h = Win::Hivex->open ("$srcdir/../images/special"); +ok $h, 'hive opened correctly'; +my $root = $h->root; +ok $root, 'root node found'; +my ($node, $value); + +my @nodes = $h->node_children( $root ); + +($node) = grep { $h->node_name($_) eq 'abcd_äöüß' } @nodes; +ok $node, q<'abcd_äöüß' (node) has been found>; +($value) = grep { $h->value_key($_) eq 'abcd_äöüß' } $h->node_values($node); +ok $value, q<'abcd_äöüß\abcd_äöüß' (value) has been found>; + +($node) = grep { $h->node_name($_) eq "zero\0key" } @nodes; +ok $node, 'key has been found'; +($value) = grep { $h->value_key($_) eq "zero\0val" } $h->node_values($node); +ok $value, 'value has been found'; + +($node) = grep { $h->node_name($_) eq 'weird™' } @nodes; +ok $node, q<'weird™' (node) has been found>; +($value) = grep { $h->value_key($_) eq 'symbols $£₤₧€' } $h->node_values($node); +ok $value, q<'weird™\symbols $£₤₧€' (value) has been found>; + +done_testing; -- 1.8.5.2
On Sat, Jan 11, 2014 at 12:12:48AM +0100, Hilko Bengen wrote:
--- perl/t/130-special.t | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 perl/t/130-special.t
diff --git a/perl/t/130-special.t b/perl/t/130-special.t new file mode 100644 index 0000000..c25af7e --- /dev/null +++ b/perl/t/130-special.t @@ -0,0 +1,34 @@ +# hivex Perl bindings -*- perl -*- + +use strict; +use warnings; +use utf8::all; # so the strings in this file are interpreted correctly. +use Test::More; + +use Win::Hivex; + +my $srcdir = $ENV{srcdir} || "."; +my $h = Win::Hivex->open ("$srcdir/../images/special"); +ok $h, 'hive opened correctly'; +my $root = $h->root; +ok $root, 'root node found'; +my ($node, $value); + +my @nodes = $h->node_children( $root ); + +($node) = grep { $h->node_name($_) eq 'abcd_äöüß' } @nodes; +ok $node, q<'abcd_äöüß' (node) has been found>; +($value) = grep { $h->value_key($_) eq 'abcd_äöüß' } $h->node_values($node); +ok $value, q<'abcd_äöüß\abcd_äöüß' (value) has been found>; + +($node) = grep { $h->node_name($_) eq "zero\0key" } @nodes; +ok $node, 'key has been found'; +($value) = grep { $h->value_key($_) eq "zero\0val" } $h->node_values($node); +ok $value, 'value has been found'; + +($node) = grep { $h->node_name($_) eq 'weird™' } @nodes; +ok $node, q<'weird™' (node) has been found>; +($value) = grep { $h->value_key($_) eq 'symbols $£₤₧€' } $h->node_values($node); +ok $value, q<'weird™\symbols $£₤₧€' (value) has been found>; + +done_testing;
ACK. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming blog: http://rwmj.wordpress.com Fedora now supports 80 OCaml packages (the OPEN alternative to F#)
--- python/t/130-special.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100755 python/t/130-special.py diff --git a/python/t/130-special.py b/python/t/130-special.py new file mode 100755 index 0000000..7adb9d5 --- /dev/null +++ b/python/t/130-special.py @@ -0,0 +1,28 @@ +# coding: utf-8 + +import os +import hivex + +srcdir = os.environ["srcdir"] +if not srcdir: + srcdir = "." + +h = hivex.Hivex ("%s/../images/special" % srcdir) +assert h + +root = h.root () +assert root + +ns = [ n for n in h.node_children (root) if h.node_name(n) == u"abcd_äöüß" ] +assert len (ns) == 1 +vs = [ v for v in h.node_values (ns[0]) if h.value_key(v) == u"abcd_äöüß" ] +assert len (vs) == 1 +ns = [ n for n in h.node_children (root) if h.node_name(n) == u"zero\0key" ] +assert len (ns) == 1 +vs = [ v for v in h.node_values (ns[0]) if h.value_key(v) == u"zero\0val" ] +assert len (vs) == 1 +ns = [ n for n in h.node_children (root) if h.node_name(n) == u"weird™" ] +assert len (ns) == 1 +vs = [ v for v in h.node_values (ns[0]) if h.value_key(v) == u"symbols $£₤₧€" ] +assert len (vs) == 1 + -- 1.8.5.2
On Sat, Jan 11, 2014 at 12:12:49AM +0100, Hilko Bengen wrote:
--- python/t/130-special.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100755 python/t/130-special.py
diff --git a/python/t/130-special.py b/python/t/130-special.py new file mode 100755 index 0000000..7adb9d5 --- /dev/null +++ b/python/t/130-special.py @@ -0,0 +1,28 @@ +# coding: utf-8 + +import os +import hivex + +srcdir = os.environ["srcdir"] +if not srcdir: + srcdir = "." + +h = hivex.Hivex ("%s/../images/special" % srcdir) +assert h + +root = h.root () +assert root + +ns = [ n for n in h.node_children (root) if h.node_name(n) == u"abcd_äöüß" ] +assert len (ns) == 1 +vs = [ v for v in h.node_values (ns[0]) if h.value_key(v) == u"abcd_äöüß" ] +assert len (vs) == 1 +ns = [ n for n in h.node_children (root) if h.node_name(n) == u"zero\0key" ] +assert len (ns) == 1 +vs = [ v for v in h.node_values (ns[0]) if h.value_key(v) == u"zero\0val" ] +assert len (vs) == 1 +ns = [ n for n in h.node_children (root) if h.node_name(n) == u"weird™" ] +assert len (ns) == 1 +vs = [ v for v in h.node_values (ns[0]) if h.value_key(v) == u"symbols $£₤₧€" ] +assert len (vs) == 1 + -- 1.8.5.2
ACK. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming blog: http://rwmj.wordpress.com Fedora now supports 80 OCaml packages (the OPEN alternative to F#)
--- generator/generator.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generator/generator.ml b/generator/generator.ml index 6b1eef7..4119b04 100755 --- a/generator/generator.ml +++ b/generator/generator.ml @@ -2648,7 +2648,7 @@ DESTROY (h) pr " croak (\"%%s: %%s\", \"%s\", strerror (errno));\n" name; if f_len_exists name then - pr " RETVAL = newSVpvn (r, hivex_%s_len (%s));\n" + pr " RETVAL = newSVpvn_utf8 (r, hivex_%s_len (%s), 1);\n" name (String.concat ", " c_params) else pr " RETVAL = newSVpv (r, 0);\n"; -- 1.8.5.2
On Sat, Jan 11, 2014 at 12:12:50AM +0100, Hilko Bengen wrote:
--- generator/generator.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/generator/generator.ml b/generator/generator.ml index 6b1eef7..4119b04 100755 --- a/generator/generator.ml +++ b/generator/generator.ml @@ -2648,7 +2648,7 @@ DESTROY (h) pr " croak (\"%%s: %%s\", \"%s\", strerror (errno));\n" name; if f_len_exists name then - pr " RETVAL = newSVpvn (r, hivex_%s_len (%s));\n" + pr " RETVAL = newSVpvn_utf8 (r, hivex_%s_len (%s), 1);\n" name (String.concat ", " c_params) else pr " RETVAL = newSVpv (r, 0);\n"; -- 1.8.5.2
_______________________________________________ Libguestfs mailing list Libguestfs@redhat.com https://www.redhat.com/mailman/listinfo/libguestfs
ACK. I wonder what happens if the registry contains a garbage string (one which is not valid utf-8)? It looks from the source as if newSVpvn_utf8 just sets a flag in the SV, so that would be OK in the sense that Perl programs wouldn't start crashing. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-top is 'top' for virtual machines. Tiny program with many powerful monitoring features, net stats, disk stats, logging, etc. http://people.redhat.com/~rjones/virt-top
* Richard W.M. Jones:
I wonder what happens if the registry contains a garbage string (one which is not valid utf-8)?
This should not happen because libhivex passes those strings through iconv. Cheers, -Hilko
--- python/t/120-rlenvalue.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/t/120-rlenvalue.py b/python/t/120-rlenvalue.py index ebc48f5..a9b2129 100644 --- a/python/t/120-rlenvalue.py +++ b/python/t/120-rlenvalue.py @@ -38,5 +38,5 @@ moderate_value_node = h.node_get_child (root, "ModerateValueParent") moderate_value_value = h.node_get_value (moderate_value_node, "33Bytes") r = h.value_data_cell_offset (moderate_value_value) -assert r[0] == 37L -assert r[1] == 8712L +assert r[0] == 37 +assert r[1] == 8712 -- 1.8.5.2
On Sat, Jan 11, 2014 at 12:12:51AM +0100, Hilko Bengen wrote:
--- python/t/120-rlenvalue.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/python/t/120-rlenvalue.py b/python/t/120-rlenvalue.py index ebc48f5..a9b2129 100644 --- a/python/t/120-rlenvalue.py +++ b/python/t/120-rlenvalue.py @@ -38,5 +38,5 @@ moderate_value_node = h.node_get_child (root, "ModerateValueParent") moderate_value_value = h.node_get_value (moderate_value_node, "33Bytes")
r = h.value_data_cell_offset (moderate_value_value) -assert r[0] == 37L -assert r[1] == 8712L +assert r[0] == 37 +assert r[1] == 8712
As long as this still works on Python 2, ACK. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-top is 'top' for virtual machines. Tiny program with many powerful monitoring features, net stats, disk stats, logging, etc. http://people.redhat.com/~rjones/virt-top
--- python/run-python-tests.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/run-python-tests.in b/python/run-python-tests.in index 90aee03..0df646b 100755 --- a/python/run-python-tests.in +++ b/python/run-python-tests.in @@ -19,7 +19,7 @@ set -e shopt -s nullglob -for f in @srcdir@t/*.py; do +for f in @srcdir@/t/*.py; do basename "$f" $PYTHON "$f" done -- 1.8.5.2
On Sat, Jan 11, 2014 at 12:12:52AM +0100, Hilko Bengen wrote:
--- python/run-python-tests.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/python/run-python-tests.in b/python/run-python-tests.in index 90aee03..0df646b 100755 --- a/python/run-python-tests.in +++ b/python/run-python-tests.in @@ -19,7 +19,7 @@ set -e shopt -s nullglob
-for f in @srcdir@t/*.py; do +for f in @srcdir@/t/*.py; do basename "$f" $PYTHON "$f" done -- 1.8.5.2
ACK (because @srcdir@ always expands to something, usually '.'). Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-top is 'top' for virtual machines. Tiny program with many powerful monitoring features, net stats, disk stats, logging, etc. http://people.redhat.com/~rjones/virt-top
On Sat, Jan 11, 2014 at 12:12:46AM +0100, Hilko Bengen wrote:
--- images/README | 14 ++++++++++++ images/mkzero/Makefile | 9 ++++++++ images/mkzero/mkzero.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ images/special | Bin 0 -> 8192 bytes 4 files changed, 82 insertions(+) create mode 100644 images/mkzero/Makefile create mode 100644 images/mkzero/mkzero.c create mode 100644 images/special
diff --git a/images/README b/images/README index 2131885..b01e5a2 100644 --- a/images/README +++ b/images/README @@ -11,3 +11,17 @@ hand-crafted binary blob. tests.
- Richard W.M. Jones 2010-02-24. + +'special' was created by importing 'minimal' into a VM running Windows +XP, loading it into HKEY_LOCAL_MACHINE\minimal using regedit.exe +(File/Load Hive...), and running 'mkzero.exe'. + +'mkzero.exe' creates the following keys and values: + +- A key 'zero\0key' containing a REG_DWORD value 'zero\0val' (\0 = zero + character) +- A key 'asdf_äöüß' containing a REG_DWORD value 'asdf_äöüß' +- A key 'weird™' containing a REG_DWORD value 'symbols $£₤₧€' (SMALL + DOLLAR SIGN, FULLWIDTH POUND SIGN, PESETA SIGN, EURO SIGN) + +- Hilko Bengen 2014-01-10. diff --git a/images/mkzero/Makefile b/images/mkzero/Makefile new file mode 100644 index 0000000..affe52b --- /dev/null +++ b/images/mkzero/Makefile @@ -0,0 +1,9 @@ +CROSS=i686-w64-mingw32- +CFLAGS=--std=c99 +all: mkzero.exe +clean: + rm -f *.exe *.o +mkzero.exe: mkzero.o + $(CROSS)gcc -o $@ $< -lntdll +%.o: %.c + $(CROSS)gcc $(CFLAGS) -Wpedantic -Wall -o $@ -c $< diff --git a/images/mkzero/mkzero.c b/images/mkzero/mkzero.c new file mode 100644 index 0000000..a95794a --- /dev/null +++ b/images/mkzero/mkzero.c @@ -0,0 +1,59 @@ +/* use the NT native API to create registry key and value that contain + a zero character */ + +#include <ntdef.h> +#include <stdio.h> +#include <ddk/wdm.h> +#include <windef.h> + +void create_key_value (PHANDLE handle, WCHAR* key, int key_len, WCHAR* val, int val_len) +{ + UNICODE_STRING key_name = { key_len, key_len, key }; + UNICODE_STRING value_name = { val_len, val_len, val }; + OBJECT_ATTRIBUTES key_obj; + InitializeObjectAttributes (&key_obj, &key_name, + OBJ_OPENIF | OBJ_CASE_INSENSITIVE, + *handle, NULL); + HANDLE key_handle; + NTSTATUS rc; + rc = ZwCreateKey (&key_handle, KEY_ALL_ACCESS, &key_obj, + 0, NULL, REG_OPTION_NON_VOLATILE, NULL); + if (!NT_SUCCESS (rc)) { + wprintf(L"error: CreateKey %s: 0x%08x\n", key, rc); + exit(1); + } + DWORD value = 0; + rc = ZwSetValueKey (key_handle, &value_name, 0, + REG_DWORD, &value, sizeof(value)); + if (!NT_SUCCESS (rc)) { + wprintf(L"error: SetValueKey %s: 0x%08x\n", val, rc); + exit(1); + } +} + +int main (int argc, char **argv) +{ + UNICODE_STRING root_key_name; + RtlInitUnicodeString(&root_key_name, L"\\Registry\\Machine\\minimal"); + OBJECT_ATTRIBUTES root_key_obj; + InitializeObjectAttributes (&root_key_obj, &root_key_name, + OBJ_OPENIF | OBJ_CASE_INSENSITIVE, + NULL, NULL); + HANDLE minimal_key_handle; + NTSTATUS rc = ZwCreateKey (&minimal_key_handle, KEY_ALL_ACCESS, &root_key_obj, + 0, NULL, REG_OPTION_NON_VOLATILE, NULL); + if (!NT_SUCCESS (rc)) { + wprintf(L"error: CreateKey <HKLM\\minimal>: 0x%08x\n", rc); + exit(1); + } + WCHAR k1[] = L"zero\0key"; + WCHAR v1[] = L"zero\0val"; + create_key_value (&minimal_key_handle, k1, sizeof (k1)-2, v1, sizeof (v1)-2); + WCHAR k2[] = L"abcd_äöüß"; + WCHAR v2[] = L"abcd_äöüß"; + create_key_value (&minimal_key_handle, k2, sizeof (k2)-2, v2, sizeof (v2)-2); + WCHAR k3[] = L"weird™"; + WCHAR v3[] = L"symbols $£₤₧€"; + create_key_value (&minimal_key_handle, k3, sizeof (k3)-2, v3, sizeof (v3)-2); + return 0; +} diff --git a/images/special b/images/special new file mode 100644 index 0000000000000000000000000000000000000000..8a5ff5f34f32f0e56c1e1e9abf49bd3a8857c5e7 GIT binary patch literal 8192 zcmeHLziU%b6h28CZ9~<EDAeHa+P=XdxC$;R6s%K4L=cN5X`%@)O-drwio+ZH@elY1 z1T2`rMJE?Ghc2Q^L|k-m(@8`sh|ll4H&6SLm(oQjat_>k?mh3^bH4X|uYulX`TDgz zDG`p7(@(o+_rFeIDH#kJ*De>1UAJUG&PYj?r7CrdmfVo06fig77i3n-i1}u}f_;>? zjGP+lsO=gC@|UkX+cjVmh%%rIC<DrXGN2471ImChpbRJj%D_Kmpi->XO#}RYA6aw{ zz<oOm!@8fB{JkG9c{jk=>AfN&T);Y`B1j3jrVo1lAOn3ifxYK>=P%4(oIgHy?h+qw z{0hUCkL!iB9c*wEQ%Xi3^76qk9Yqcyo|Z3?<sq4rBiN;I29|L#O>v!_#3aE$JRyH( zNWO_9|CAhv)p!5qJ6r3UIMA7Vi#223Jxj!Zzcn_rZY<YO+utKz2fTypy6?txaoMqF z)PlzLZB!(`+(*wTH>5K*hWU26*$}_H*6!&MAjUy3vnT>R?oPlTv+vW>W7F#44ACPu zM301g!+S!$i6cKz51T)zhm)X3F2=Fd$nk7ld)bvajCbvvcw4^n+1od>xa5E1yT;F1 zZMMlz4SCknQ#PT23*zbDz<-2a^~b11fNu2s?5!TFg&Ok$bZx`$RPU7pz4=YDHf<cE zP+Tf4e)|6NgNg7jN~BgvO9yAKb|1WY4iGQCdhg#EeB%T7`p;W@vZL?uRsy~p`bYTq zJ8={Dh*jJumSlR05Q(E!8To^64$0m1`;7=Eb7&g|Z!U(H@s_ONF0zPQ${N<Lc=AZr k-N)_|mrtRG{b8{ios|J)Kp9X5lmTTx8Bhk4f&Y|&-#+)~(*OVf
literal 0 HcmV?d00001
ACK. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://people.redhat.com/~rjones/virt-df/
Hi Hilko, Thanks a lot for creating this test hive! I think another thing that will trip up Registry programs is value names that include back slashes. For example, under the System hive, the key "\MountedDevices" has child values with names like "\DosDevices\C:" (the leading backslash is a part of the value name). There are many other values that include backslashes. I don't think I have any keys that have backslash-carrying names, but I haven't tested that extensively. Have these values caused enough troubles to warrant another test hive? --Alex On Mon, Jan 13, 2014 at 8:18 AM, Richard W.M. Jones <rjones@redhat.com>wrote:
On Sat, Jan 11, 2014 at 12:12:46AM +0100, Hilko Bengen wrote:
--- images/README | 14 ++++++++++++ images/mkzero/Makefile | 9 ++++++++ images/mkzero/mkzero.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ images/special | Bin 0 -> 8192 bytes 4 files changed, 82 insertions(+) create mode 100644 images/mkzero/Makefile create mode 100644 images/mkzero/mkzero.c create mode 100644 images/special
diff --git a/images/README b/images/README index 2131885..b01e5a2 100644 --- a/images/README +++ b/images/README @@ -11,3 +11,17 @@ hand-crafted binary blob. tests.
- Richard W.M. Jones 2010-02-24. + +'special' was created by importing 'minimal' into a VM running Windows +XP, loading it into HKEY_LOCAL_MACHINE\minimal using regedit.exe +(File/Load Hive...), and running 'mkzero.exe'. + +'mkzero.exe' creates the following keys and values: + +- A key 'zero\0key' containing a REG_DWORD value 'zero\0val' (\0 = zero + character) +- A key 'asdf_äöüß' containing a REG_DWORD value 'asdf_äöüß' +- A key 'weird™' containing a REG_DWORD value 'symbols $£₤₧€' (SMALL + DOLLAR SIGN, FULLWIDTH POUND SIGN, PESETA SIGN, EURO SIGN) + +- Hilko Bengen 2014-01-10. diff --git a/images/mkzero/Makefile b/images/mkzero/Makefile new file mode 100644 index 0000000..affe52b --- /dev/null +++ b/images/mkzero/Makefile @@ -0,0 +1,9 @@ +CROSS=i686-w64-mingw32- +CFLAGS=--std=c99 +all: mkzero.exe +clean: + rm -f *.exe *.o +mkzero.exe: mkzero.o + $(CROSS)gcc -o $@ $< -lntdll +%.o: %.c + $(CROSS)gcc $(CFLAGS) -Wpedantic -Wall -o $@ -c $< diff --git a/images/mkzero/mkzero.c b/images/mkzero/mkzero.c new file mode 100644 index 0000000..a95794a --- /dev/null +++ b/images/mkzero/mkzero.c @@ -0,0 +1,59 @@ +/* use the NT native API to create registry key and value that contain + a zero character */ + +#include <ntdef.h> +#include <stdio.h> +#include <ddk/wdm.h> +#include <windef.h> + +void create_key_value (PHANDLE handle, WCHAR* key, int key_len, WCHAR* val, int val_len) +{ + UNICODE_STRING key_name = { key_len, key_len, key }; + UNICODE_STRING value_name = { val_len, val_len, val }; + OBJECT_ATTRIBUTES key_obj; + InitializeObjectAttributes (&key_obj, &key_name, + OBJ_OPENIF | OBJ_CASE_INSENSITIVE, + *handle, NULL); + HANDLE key_handle; + NTSTATUS rc; + rc = ZwCreateKey (&key_handle, KEY_ALL_ACCESS, &key_obj, + 0, NULL, REG_OPTION_NON_VOLATILE, NULL); + if (!NT_SUCCESS (rc)) { + wprintf(L"error: CreateKey %s: 0x%08x\n", key, rc); + exit(1); + } + DWORD value = 0; + rc = ZwSetValueKey (key_handle, &value_name, 0, + REG_DWORD, &value, sizeof(value)); + if (!NT_SUCCESS (rc)) { + wprintf(L"error: SetValueKey %s: 0x%08x\n", val, rc); + exit(1); + } +} + +int main (int argc, char **argv) +{ + UNICODE_STRING root_key_name; + RtlInitUnicodeString(&root_key_name, L"\\Registry\\Machine\\minimal"); + OBJECT_ATTRIBUTES root_key_obj; + InitializeObjectAttributes (&root_key_obj, &root_key_name, + OBJ_OPENIF | OBJ_CASE_INSENSITIVE, + NULL, NULL); + HANDLE minimal_key_handle; + NTSTATUS rc = ZwCreateKey (&minimal_key_handle, KEY_ALL_ACCESS, &root_key_obj, + 0, NULL, REG_OPTION_NON_VOLATILE, NULL); + if (!NT_SUCCESS (rc)) { + wprintf(L"error: CreateKey <HKLM\\minimal>: 0x%08x\n", rc); + exit(1); + } + WCHAR k1[] = L"zero\0key"; + WCHAR v1[] = L"zero\0val"; + create_key_value (&minimal_key_handle, k1, sizeof (k1)-2, v1, sizeof (v1)-2); + WCHAR k2[] = L"abcd_äöüß"; + WCHAR v2[] = L"abcd_äöüß"; + create_key_value (&minimal_key_handle, k2, sizeof (k2)-2, v2, sizeof (v2)-2); + WCHAR k3[] = L"weird™"; + WCHAR v3[] = L"symbols $£₤₧€"; + create_key_value (&minimal_key_handle, k3, sizeof (k3)-2, v3, sizeof (v3)-2); + return 0; +} diff --git a/images/special b/images/special new file mode 100644 index 0000000000000000000000000000000000000000..8a5ff5f34f32f0e56c1e1e9abf49bd3a8857c5e7 GIT binary patch literal 8192 zcmeHLziU%b6h28CZ9~<EDAeHa+P=XdxC$;R6s%K4L=cN5X`%@)O-drwio+ZH@elY1 z1T2`rMJE?Ghc2Q^L|k-m(@8`sh|ll4H&6SLm(oQjat_>k?mh3^bH4X|uYulX`TDgz zDG`p7(@(o+_rFeIDH#kJ*De>1UAJUG&PYj?r7CrdmfVo06fig77i3n-i1}u}f_;>? zjGP+lsO=gC@|UkX+cjVmh%%rIC<DrXGN2471ImChpbRJj%D_Kmpi->XO#}RYA6aw{ zz<oOm!@8fB{JkG9c{jk=>AfN&T);Y`B1j3jrVo1lAOn3ifxYK>=P%4(oIgHy?h+qw z{0hUCkL!iB9c*wEQ%Xi3^76qk9Yqcyo|Z3?<sq4rBiN;I29|L#O>v!_#3aE$JRyH( zNWO_9|CAhv)p!5qJ6r3UIMA7Vi#223Jxj!Zzcn_rZY<YO+utKz2fTypy6?txaoMqF z)PlzLZB!(`+(*wTH>5K*hWU26*$}_H*6!&MAjUy3vnT>R?oPlTv+vW>W7F#44ACPu zM301g!+S!$i6cKz51T)zhm)X3F2=Fd$nk7ld)bvajCbvvcw4^n+1od>xa5E1yT;F1 zZMMlz4SCknQ#PT23*zbDz<-2a^~b11fNu2s?5!TFg&Ok$bZx`$RPU7pz4=YDHf<cE zP+Tf4e)|6NgNg7jN~BgvO9yAKb|1WY4iGQCdhg#EeB%T7`p;W@vZL?uRsy~p`bYTq zJ8={Dh*jJumSlR05Q(E!8To^64$0m1`;7=Eb7&g|Z!U(H@s_ONF0zPQ${N<Lc=AZr k-N)_|mrtRG{b8{ios|J)Kp9X5lmTTx8Bhk4f&Y|&-#+)~(*OVf
literal 0 HcmV?d00001
ACK.
Rich.
-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://people.redhat.com/~rjones/virt-df/
_______________________________________________ Libguestfs mailing list Libguestfs@redhat.com https://www.redhat.com/mailman/listinfo/libguestfs
On Tue, Jan 14, 2014 at 12:10:59PM -0500, Alex Nelson wrote:
Hi Hilko,
Thanks a lot for creating this test hive!
I think another thing that will trip up Registry programs is value names that include back slashes. For example, under the System hive, the key "\MountedDevices" has child values with names like "\DosDevices\C:" (the leading backslash is a part of the value name). There are many other values that include backslashes. I don't think I have any keys that have backslash-carrying names, but I haven't tested that extensively.
Have these values caused enough troubles to warrant another test hive?
Backslash characters aren't special for the C hivex API. If you use the Perl Win::Hivex::Regedit API you'll probably have trouble trying to import or export them. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://people.redhat.com/~rjones/virt-df/
On Tue, Jan 14, 2014 at 12:10:59PM -0500, Alex Nelson wrote:
Have these values caused enough troubles to warrant another test hive?
Another thing to say is that we cannot add proprietary test hives to the main repository for license reasons. "minimal" and "special" are both on the edge of what is permitted. We have another repository for test hives: http://git.annexia.org/?p=hivex-test-data.git;a=summary This is not part of the hivex project, but you can run the tests from it using hivex, see the README file. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming blog: http://rwmj.wordpress.com Fedora now supports 80 OCaml packages (the OPEN alternative to F#)
--- configure.ac | 1 + ruby/run-ruby-tests | 27 --------------------------- ruby/run-ruby-tests.in | 27 +++++++++++++++++++++++++++ ruby/tests/tc_010_load.rb | 2 -- ruby/tests/tc_021_close.rb | 2 -- ruby/tests/tc_120_rlenvalue.rb | 4 +--- ruby/tests/tc_200_write.rb | 2 -- ruby/tests/tc_210_setvalue.rb | 2 -- run.in | 4 ++-- 9 files changed, 31 insertions(+), 40 deletions(-) delete mode 100755 ruby/run-ruby-tests create mode 100755 ruby/run-ruby-tests.in diff --git a/configure.ac b/configure.ac index 4066ca5..6785037 100644 --- a/configure.ac +++ b/configure.ac @@ -564,6 +564,7 @@ AC_CONFIG_FILES([Makefile sh/Makefile xml/Makefile]) AC_CONFIG_FILES([python/run-python-tests], [chmod +x python/run-python-tests]) +AC_CONFIG_FILES([ruby/run-ruby-tests], [chmod +x ruby/run-ruby-tests]) AC_CONFIG_FILES([run], [chmod +x,-w run]) AC_OUTPUT diff --git a/ruby/run-ruby-tests b/ruby/run-ruby-tests deleted file mode 100755 index 2efd890..0000000 --- a/ruby/run-ruby-tests +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/sh - -# hivex Ruby bindings -# Copyright (C) 2009-2011 Red Hat Inc. -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - -set -e - -# Run them one at a time, otherwise rake runs them in parallel (which -# is bound to fail because they all use a single test image file). - -for f in tests/tc_*.rb; do - echo $RAKE test "$@" TEST="$f" - $RAKE test "$@" TEST="$f" -done diff --git a/ruby/run-ruby-tests.in b/ruby/run-ruby-tests.in new file mode 100755 index 0000000..b61699e --- /dev/null +++ b/ruby/run-ruby-tests.in @@ -0,0 +1,27 @@ +#!/bin/sh - +# hivex Ruby bindings +# Copyright (C) 2009-2011 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +set -e + +# Run them one at a time, otherwise rake runs them in parallel (which +# is bound to fail because they all use a single test image file). +export abs_srcdir=@abs_srcdir@ +for f in @srcdir@/tests/tc_*.rb; do + echo $RAKE test "$@" TEST="$f" + $RAKE test "$@" TEST="$f" +done diff --git a/ruby/tests/tc_010_load.rb b/ruby/tests/tc_010_load.rb index 113ab69..8a8ae51 100644 --- a/ruby/tests/tc_010_load.rb +++ b/ruby/tests/tc_010_load.rb @@ -16,8 +16,6 @@ # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. require 'test/unit' -$:.unshift(File::join(File::dirname(__FILE__), "..", "lib")) -$:.unshift(File::join(File::dirname(__FILE__), "..", "ext", "hivex")) require 'hivex' class TestLoad < Test::Unit::TestCase diff --git a/ruby/tests/tc_021_close.rb b/ruby/tests/tc_021_close.rb index a089cf3..9c5ea65 100644 --- a/ruby/tests/tc_021_close.rb +++ b/ruby/tests/tc_021_close.rb @@ -16,8 +16,6 @@ # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. require 'test/unit' -$:.unshift(File::join(File::dirname(__FILE__), "..", "lib")) -$:.unshift(File::join(File::dirname(__FILE__), "..", "ext", "hivex")) require 'hivex' class TestClose < Test::Unit::TestCase diff --git a/ruby/tests/tc_120_rlenvalue.rb b/ruby/tests/tc_120_rlenvalue.rb index 368cb19..5cb0a80 100644 --- a/ruby/tests/tc_120_rlenvalue.rb +++ b/ruby/tests/tc_120_rlenvalue.rb @@ -22,13 +22,11 @@ # 8712. require 'test/unit' -$:.unshift(File::join(File::dirname(__FILE__), "..", "lib")) -$:.unshift(File::join(File::dirname(__FILE__), "..", "ext", "hivex")) require 'hivex' class TestRLenValue < Test::Unit::TestCase def test_RLenValue - h = Hivex::open("../images/rlenvalue_test_hive", {}) + h = Hivex::open(File::join(ENV['abs_srcdir'], '..', 'images', 'rlenvalue_test_hive'), {}) assert_not_nil(h) root = h.root() diff --git a/ruby/tests/tc_200_write.rb b/ruby/tests/tc_200_write.rb index b46dc7b..6ae239e 100644 --- a/ruby/tests/tc_200_write.rb +++ b/ruby/tests/tc_200_write.rb @@ -16,8 +16,6 @@ # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. require 'test/unit' -$:.unshift(File::join(File::dirname(__FILE__), "..", "lib")) -$:.unshift(File::join(File::dirname(__FILE__), "..", "ext", "hivex")) require 'hivex' class TestWrite < Test::Unit::TestCase diff --git a/ruby/tests/tc_210_setvalue.rb b/ruby/tests/tc_210_setvalue.rb index e55e5fe..4d1aa7e 100644 --- a/ruby/tests/tc_210_setvalue.rb +++ b/ruby/tests/tc_210_setvalue.rb @@ -16,8 +16,6 @@ # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. require 'test/unit' -$:.unshift(File::join(File::dirname(__FILE__), "..", "lib")) -$:.unshift(File::join(File::dirname(__FILE__), "..", "ext", "hivex")) require 'hivex' class TestSetValue < Test::Unit::TestCase diff --git a/run.in b/run.in index 9592e38..46b3df6 100755 --- a/run.in +++ b/run.in @@ -70,9 +70,9 @@ export PYTHONPATH export RUBY=@RUBY@ export RAKE=@RAKE@ if [ -z "$RUBYLIB" ]; then - RUBYLIB="$b/ruby/lib:$b/ruby/ext/hivex" + RUBYLIB="$s/ruby/lib:$b/ruby/ext/hivex" else - RUBYLIB="$b/ruby/lib:$b/ruby/ext/hivex:$RUBYLIB" + RUBYLIB="$s/ruby/lib:$b/ruby/ext/hivex:$RUBYLIB" fi export RUBYLIB export LD_LIBRARY_PATH="$b/ruby/ext/hivex:$LD_LIBRARY_PATH" -- 1.8.5.2
--- generator/generator.ml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/generator/generator.ml b/generator/generator.ml index 4119b04..991e3ac 100755 --- a/generator/generator.ml +++ b/generator/generator.ml @@ -3367,6 +3367,7 @@ and generate_ruby_c () = #include <stdint.h> #include <ruby.h> +#include <ruby/encoding.h> #include \"hivex.h\" @@ -3657,7 +3658,8 @@ get_values (VALUE valuesv, size_t *nr_values) | RString -> if f_len_exists name then ( pr " size_t sz = hivex_%s_len (%s);\n" name (String.concat ", " c_params); - pr " VALUE rv = rb_str_new (r, sz);\n" + pr " VALUE rv = rb_str_new (r, sz);\n"; + pr " rb_enc_associate (rv, rb_utf8_encoding ());"; ) else pr " VALUE rv = rb_str_new2 (r);\n"; pr " free (r);\n"; -- 1.8.5.2
--- ruby/tests/tc_130_special.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 ruby/tests/tc_130_special.rb diff --git a/ruby/tests/tc_130_special.rb b/ruby/tests/tc_130_special.rb new file mode 100644 index 0000000..0623f4d --- /dev/null +++ b/ruby/tests/tc_130_special.rb @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +# -*- ruby -*- + +require 'test/unit' +require 'hivex' + +class TestRLenValue < Test::Unit::TestCase + def test_RLenValue + h = Hivex::open(File::join(ENV['abs_srcdir'], '..', 'images', 'special'), {}) + assert_not_nil(h) + + root = h.root() + assert_not_nil(root) + + nodes = h.node_children (root) + node = nodes.find { |n| h.node_name(n) == "abcd_äöüß" } + assert node != nil + value = h.node_values(node).find { |v| h.value_key(v) == "abcd_äöüß" } + assert value != nil + node = nodes.find { |n| h.node_name(n) == "zero\0key" } + assert node != nil + value = h.node_values(node).find { |v| h.value_key(v) == "zero\0val" } + assert value != nil + node = nodes.find { |n| h.node_name(n) == "weird™" } + assert node != nil + value = h.node_values(node).find { |v| h.value_key(v) == "symbols $£₤₧€" } + assert value != nil + + end +end -- 1.8.5.2
On Mon, Jan 13, 2014 at 01:07:13PM +0100, Hilko Bengen wrote:
--- ruby/tests/tc_130_special.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 ruby/tests/tc_130_special.rb
diff --git a/ruby/tests/tc_130_special.rb b/ruby/tests/tc_130_special.rb new file mode 100644 index 0000000..0623f4d --- /dev/null +++ b/ruby/tests/tc_130_special.rb @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +# -*- ruby -*- + +require 'test/unit' +require 'hivex' + +class TestRLenValue < Test::Unit::TestCase + def test_RLenValue + h = Hivex::open(File::join(ENV['abs_srcdir'], '..', 'images', 'special'), {}) + assert_not_nil(h) + + root = h.root() + assert_not_nil(root) + + nodes = h.node_children (root) + node = nodes.find { |n| h.node_name(n) == "abcd_äöüß" } + assert node != nil + value = h.node_values(node).find { |v| h.value_key(v) == "abcd_äöüß" } + assert value != nil + node = nodes.find { |n| h.node_name(n) == "zero\0key" } + assert node != nil + value = h.node_values(node).find { |v| h.value_key(v) == "zero\0val" } + assert value != nil + node = nodes.find { |n| h.node_name(n) == "weird™" } + assert node != nil + value = h.node_values(node).find { |v| h.value_key(v) == "symbols $£₤₧€" } + assert value != nil + + end +end -- 1.8.5.2
ACK to all three of these ruby patches. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into KVM guests. http://libguestfs.org/virt-v2v
participants (3)
-
Alex Nelson -
Hilko Bengen -
Richard W.M. Jones