943,147 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 480
  • C++ RSS
Nov 23rd, 2009
0

Hey, need help with code edit plz

Expand Post »
Hi, I'm new to this site and also to c++. I am currently in the process of learning c++ and would like the ability to read certain portions of a windows-based registry hive. I have found the source code for and compiled a working program that outputs all values within a specified registry file. What I am looking for is to limit the output to find one certain value within the key located in the registry hive file. I thought this would be as simple of a task as adding an if() to check if the key/values match the ones that i want before outputting.

(eg. Software\Microsoft\Windows NT\CurrentVersion\ProductId)

Here is the source code of the original program:
C++ Syntax (Toggle Plain Text)
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5.  
  6. struct offsets {
  7. long block_size;
  8. char block_type[2]; // "lf" "il" "ri"
  9. short count;
  10. long first;
  11. long hash;
  12. };
  13.  
  14. struct key_block {
  15. long block_size;
  16. char block_type[2]; // "nk"
  17. char dummya[18];
  18. int subkey_count;
  19. char dummyb[4];
  20. int subkeys;
  21. char dummyc[4];
  22. int value_count;
  23. int offsets;
  24. char dummyd[28];
  25. short len;
  26. short du;
  27. char name;
  28. };
  29.  
  30. struct value_block {
  31. long block_size;
  32. char block_type[2]; // "vk"
  33. short name_len;
  34. long size;
  35. long offset;
  36. long value_type;
  37. short flags;
  38. short dummy;
  39. char name;
  40. };
  41.  
  42. void walk ( char* path, key_block* key ) {
  43. static char* root=(char*)key-0x20, *full=path;
  44.  
  45. // add current key name to printed path
  46. memcpy(path++,"/",2); memcpy(path,&key->name,key->len); path+=key->len;
  47.  
  48. // print all contained values
  49. for(int o=0;o<key->value_count;o++){ //
  50. value_block* val = (value_block*)(((int*)(key->offsets+root+4))[o]+root);
  51.  
  52. // we skip nodes without values
  53. if(!val->offset) continue;
  54.  
  55. // data are usually in separate blocks without types
  56. char* data = root+val->offset+4;
  57. // but for small values MS added optimization where
  58. // if bit 31 is set data are contained wihin the key itself to save space
  59. if(val->size&1<<31) {
  60. data = (char*)&val->offset;
  61. }
  62. // notice that we use memcopy for key/value names everywhere instead of strcat
  63. // reason is that malware/wiruses often write non nulterminated strings to
  64. // hide from win32 api
  65. *path='/'; if(!val->name_len) *path=' ';
  66. memcpy(path+1,&val->name,val->name_len); path[val->name_len+1]=0;
  67.  
  68. printf("%s [%d] = ",full,val->value_type);
  69.  
  70. for(int i=0;i<(val->size&0xffff);i++) {
  71. // print types 1 and 7 as unicode strings
  72. if(val->value_type==1||val->value_type==7) {
  73. if(data[i]) putchar(data[i]);
  74. // and rest dump as binary data
  75. } else {
  76. printf("%02X",data[i]);
  77. }
  78. }
  79. }
  80.  
  81. // for simplicity we can imagine keys as directories in filesystem and values
  82. // as files.
  83. // and since we already dumped values for this dir we will now iterate
  84. // thru subdirectories in the same way
  85.  
  86. offsets* item = (offsets*)(root+key->subkeys);
  87. for(int i=0;i<item->count;i++){
  88. // in case of too many subkeys this list contain just other lists
  89. offsets* subitem = (offsets*)((&item->first)[i]+root);
  90.  
  91. // usual directory traversal
  92. if(item->block_type[1]=='f'||item->block_type[1]=='h') {
  93. // for now we skip hash codes (used by regedit for faster search)
  94. walk(path,(key_block*)((&item->first)[i*2]+root));
  95. } else for(int j=0;j<subitem->count;j++) {
  96. // also ms had chosen to skip hashes altogether in this case
  97. walk(path,(key_block*)((&subitem->first)[item->block_type[1]=='i'?j*2:j]+root));
  98. }
  99. }
  100. }
  101.  
  102. int main(int argc, char** argv) {
  103. char path[0x1000]={0}, *data; FILE* f; int size;
  104.  
  105. if(argc<2||!(f=fopen(argv[1],"rb"))) return printf("hive path err");
  106.  
  107. fseek(f,0,SEEK_END);
  108. if(!(size=ftell(f))) return printf("empty file");
  109.  
  110. rewind(f); data=(char*)malloc(size);
  111. fread(data,size,1,f);
  112. fclose(f);
  113.  
  114. // we just skip 1k header and start walking root key tree
  115. walk(path,(key_block*)(data+0x1020));
  116. free(data);
  117. return 0;
  118. }

I thank you in advance for any help you can offer.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
FanatiK is offline Offline
9 posts
since Nov 2009
Nov 23rd, 2009
0
Re: Hey, need help with code edit plz
actually after testing the program its giving errors with the source code i put up.. i must have forgot to change something back when i was working on it. i will try to find what went wrong and will post back with the better code.. srry about that
Reputation Points: 10
Solved Threads: 0
Newbie Poster
FanatiK is offline Offline
9 posts
since Nov 2009
Nov 23rd, 2009
0
Re: Hey, need help with code edit plz
Click to Expand / Collapse  Quote originally posted by FanatiK ...
actually after testing the program its giving errors with the source code i put up.. i must have forgot to change something back when i was working on it. i will try to find what went wrong and will post back with the better code.. srry about that
k i found the original site that i found the code on, the link is here
Reputation Points: 10
Solved Threads: 0
Newbie Poster
FanatiK is offline Offline
9 posts
since Nov 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Tic-Tac-DOH!!
Next Thread in C++ Forum Timeline: Sorting a vector





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC