-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegHiveParser.cs
173 lines (159 loc) · 7.09 KB
/
RegHiveParser.cs
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using Microsoft.Win32;
using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows.Threading;
namespace RegFineViewer
{
class RegHiveParser : BaseParser
{
// ------------------------------------------------------------------
// Constructeur (on repasse les paramètres à la classe de base)
// ------------------------------------------------------------------
public RegHiveParser(ObservableCollection<RegistryItem> registrytree, KeyUnitDictionnary dictionnary)
: base(registrytree, dictionnary)
{
}
// ------------------------------------------------------------------
// Parcourt de toute la base de registres
// Enregistre l'arborescence dans un RegistryTree
// Enregistre la liste des Nodes dans un Dictionnaire
// ------------------------------------------------------------------
public void ParseHive(string rootPath)
{
// Si le chemein commence par HKLM, on l'enlève (pour les appels aux fonctions Microsoft)
if (rootPath.StartsWith(@"HKLM\")) rootPath = rootPath.Substring(5, rootPath.Length - 5);
// On commence par vider la collection et le dictionnaire
InitParser();
// On cree le node Racine
RegistryItem RootNode = this.CreateRootNode(rootPath);
// On parcourt le subtree de la base de registres, en commençant par le Node Racine
this.CreateChildNodes(RootNode, rootPath);
}
// ------------------------------------------------------------------
// Cree un Item dans le RegistryTree pour cette Value
// ------------------------------------------------------------------
private RegistryItem CreateRegistryKeyFromHive(string keyName, string keyKind, string keyValue)
{
string KeyDType;
if (keyKind.Equals("String", StringComparison.CurrentCultureIgnoreCase))
KeyDType = "SZ";
else if (keyKind.Equals("MultiString", StringComparison.CurrentCultureIgnoreCase))
KeyDType = "MULTI_SZ";
else if (keyKind.Equals("DWord", StringComparison.CurrentCultureIgnoreCase))
KeyDType = "DWORD";
else if (keyKind.Equals("Binary", StringComparison.CurrentCultureIgnoreCase))
{
KeyDType = "HEX";
keyValue = "HEX VALUE";
}
else
{
KeyDType = keyKind;
keyValue = "unrecognized type";
}
KeyDType = "REG_" + KeyDType;
// On cree la Key
RegistryItem newKey = base.CreateRegistryKey(keyName, KeyDType, keyValue);
return newKey;
}
// ------------------------------------------------------------------
// Cree le node racine dans le RegistryTree.
// Retourne Null si la racine est un WrongNode (sans enfants)
// ------------------------------------------------------------------
private RegistryItem CreateRootNode(string rootpath)
{
if (rootpath == string.Empty)
{
// Vérification : HKLM
RegistryItem WrongNode = new RegistryItem("HKLM is not allowed. Try a smaller subtree.", "node");
RegistryTree.Add(WrongNode);
return null;
}
else if (rootpath.Equals("SECURITY"))
{
// Vérification : HKLM\SECURITY
RegistryItem WrongNode = new RegistryItem("SECURITY subtree is not accessible (reserved access).", "node");
RegistryTree.Add(WrongNode);
return null;
}
else
{
// Les vérifications sont OK: On crée le node Racine
RegistryItem RacineNode = new RegistryItem(rootpath, "node");
RegistryTree.Add(RacineNode);
this.AddToNodeTable(RacineNode, RacineNode.Name);
// On memorise le Level de ce Node
base.RacineNodeLevel = rootpath.Split('\\').Length;
return RacineNode;
}
}
// ------------------------------------------------------------------
// Cree les Nodes SubKeys et les Values du Node donné
// C'est un peut redondant de donner le node et le path, mais ça évite de le recalculer à chaque fois.
// ------------------------------------------------------------------
private void CreateChildNodes(RegistryItem ParentNode, string ParentPath)
{
RegistryKey rk;
if (ParentNode == null) return;
try
{
rk = Registry.LocalMachine.OpenSubKey(ParentPath);
}
catch (Exception ex)
{
RegistryItem WrongNode = new RegistryItem("This registry cannot be read (" + ex.Message + ").", "node");
RegistryTree.Add(WrongNode);
return;
}
if (rk == null)
{
RegistryItem WrongNode = new RegistryItem("This registry was found null (" + ParentPath + ").", "node");
RegistryTree.Add(WrongNode);
return;
}
string[] SubKeysArray = rk.GetSubKeyNames();
string[] ValuesArray = rk.GetValueNames();
// Traitement des Subkeys: on les ajoute dans l'arborescence
foreach (string SubKeyName in SubKeysArray)
{
// on cree un nouveau node pour chaque SubKey
string nodePath = ParentPath + @"\" + SubKeyName;
RegistryItem NewNode = base.CreateRegistryNode(nodePath);
// On met le node dans le dictionnaire
base.AddToNodeTable(NewNode, SubKeyName);
// On le rattache à son parent
base.AttachToParentNode(NewNode, ParentNode);
// On traite ses enfants (recursivité)
this.CreateChildNodes(NewNode, nodePath);
}
// Traitement des Values: on les ajoute dans l'arborescence
foreach (string ValueName in ValuesArray)
{
// On recupère toutes les infos sur cette value
string ValueKind = string.Empty;
string Value = string.Empty;
try
{
ValueKind = rk.GetValueKind(ValueName).ToString();
}
catch (NullReferenceException)
{
ValueKind = string.Empty;
}
try
{
Value = rk.GetValue(ValueName).ToString();
}
catch (NullReferenceException)
{
Value = string.Empty;
}
// On cree une Key
RegistryItem newKey = this.CreateRegistryKeyFromHive(ValueName, ValueKind, Value);
// On la rattache à son parent
base.AttachToParentNode(newKey, ParentNode);
}
}
}
}