source: client/engine/Registry.lib @ c03c7c3

918-git-images-111dconfigfileconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacionwebconsole3
Last change on this file since c03c7c3 was 0e24b16, checked in by ramon <ramongomez@…>, 10 years ago

Función ogGetRegistryValue devuelve todo el contenido de un valor binario del registro de Windows.

git-svn-id: https://opengnsys.es/svn/branches/version1.1@4687 a21b9725-9963-47de-94b9-378ad31fedc9

  • Property mode set to 100755
File size: 14.7 KB
Line 
1#!/bin/bash
2#/**
3#@file    Registry.lib
4#@brief   Librería o clase Registry
5#@class   Boot
6#@brief   Funciones para gestión del registro de Windows.
7#@version 1.1.0
8#@warning License: GNU GPLv3+
9#*/
10
11
12# Función ficticia para lanzar chntpw con timeout de 5 s., evitando cuelgues del programa.
13function chntpw ()
14{
15local CHNTPW
16CHNTPW=$(which drbl-chntpw)
17CHNTPW=${CHNTPW:-$(which chntpw)}
18timeout --foreground 5s $CHNTPW "$@"
19}
20
21
22#/**
23#         ogAddRegistryKey path_mountpoint str_hive str_keyname
24#@brief   Añade una nueva clave al registro de Windows.
25#@param   path_mountpoint  directorio donde está montado el sistema Windows
26#@param   str_hive         sección del registro
27#@param   str_keyname      nombre de la clave
28#@return  (nada)
29#@exception OG_ERR_FORMAT    Formato incorrecto.
30#@exception OG_ERR_NOTFOUND  Fichero de registro no encontrado.
31#@note    hive = { default, sam, security, software, system, components }
32#@warning Requisitos: chntpw
33#@warning El sistema de archivos de Windows debe estar montada previamente.
34#@version 1.0.1 - Nueva función
35#@author  Ramon Gomez, ETSII Universidad de Sevilla
36#@date    2011-05-25
37#*/ ##
38function ogAddRegistryKey ()
39{
40# Variables locales.
41local FILE
42
43# Si se solicita, mostrar ayuda.
44if [ "$*" == "help" ]; then
45    ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_hive str_key" \
46           "$FUNCNAME /mnt/sda1 SOFTWARE '\Microsoft\NewKey'"
47    return
48fi
49# Error si no se reciben 3 parámetros.
50[ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
51# Camino del fichero de registro.
52FILE=$(ogGetHivePath "$1" "$2") || return $?
53
54# Añadir nueva clave.
55chntpw "$FILE" << EOT &> /dev/null
56cd ${3%\\*}
57nk ${3##*\\}
58q
59y
60EOT
61}
62
63#/**
64#         ogAddRegistryValue path_mountpoint str_hive str_valuename [str_valuetype]
65#@brief   Añade un nuevo valor al registro de Windows, indicando su tipo de datos.
66#@param   path_mountpoint  directorio donde está montado el sistema Windows
67#@param   str_hive         sección del registro
68#@param   str_valuename    nombre del valor
69#@param   str_valuetype    tipo de datos del valor (opcional)
70#@return  (nada)
71#@exception OG_ERR_FORMAT    Formato incorrecto.
72#@exception OG_ERR_NOTFOUND  Fichero de registro no encontrado.
73#@note    hive = { DEFAULT, SAM, SECURITY, SOFTWARE, SYSTEM, COMPONENTS }
74#@note    valuetype = { STRING, BINARY, DWORD }, por defecto: STRING
75#@warning Requisitos: chntpw
76#@warning El sistema de archivos de Windows debe estar montada previamente.
77#@version 1.0.1 - Nueva función
78#@author  Ramon Gomez, ETSII Universidad de Sevilla
79#@date    2011-05-25
80#*/ ##
81function ogAddRegistryValue ()
82{
83# Variables locales.
84local FILE TYPE
85
86# Si se solicita, mostrar ayuda.
87if [ "$*" == "help" ]; then
88    ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_hive str_valuename [str_valuetype]" \
89           "$FUNCNAME /mnt/sda1 SOFTWARE '\Microsoft\NewKey\Value1'" \
90           "$FUNCNAME /mnt/sda1 SOFTWARE '\Microsoft\NewKey\Value1' DWORD"
91    return
92fi
93# Error si no se reciben 3 o 4 parámetros.
94[ $# == 3 -o $# == 4 ] || ogRaiseError $OG_ERR_FORMAT || return $?
95# Camino del fichero de registro.
96FILE=$(ogGetHivePath "$1" "$2") || return $?
97case "${4^^}" in
98    STRING|"")  TYPE=1 ;;
99    BINARY)     TYPE=3 ;;
100    DWORD)      TYPE=4 ;;
101    *)          ogRaiseError $OG_ERR_OUTOFLIMIT "$4"
102                return $? ;;
103esac
104
105# Devolver el dato del valor de registro.
106# /* (comentario Doxygen)
107chntpw "$FILE" << EOT &> /dev/null
108cd ${3%\\*}
109nv $TYPE ${3##*\\}
110q
111y
112EOT
113# (comentario Doxygen) */
114}
115
116
117#/**
118#         ogDeleteRegistryKey path_mountpoint str_hive str_keyname
119#@brief   Elimina una clave del registro de Windows con todo su contenido.
120#@param   path_mountpoint  directorio donde está montado el sistema Windows
121#@param   str_hive         sección del registro
122#@param   str_keyname      nombre de la clave
123#@return  (nada)
124#@exception OG_ERR_FORMAT    Formato incorrecto.
125#@exception OG_ERR_NOTFOUND  Fichero de registro no encontrado.
126#@note    hive = { default, sam, security, software, system, components }
127#@warning Requisitos: chntpw
128#@warning El sistema de archivos de Windows debe estar montada previamente.
129#@warning La clave debe estar vacía para poder ser borrada.
130#@version 1.0.1 - Nueva función
131#@author  Ramon Gomez, ETSII Universidad de Sevilla
132#@date    2011-05-25
133#*/ ##
134function ogDeleteRegistryKey ()
135{
136# Variables locales.
137local FILE
138
139# Si se solicita, mostrar ayuda.
140if [ "$*" == "help" ]; then
141    ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_hive str_key" \
142           "$FUNCNAME /mnt/sda1 SOFTWARE '\Microsoft\NewKey'"
143    return
144fi
145# Error si no se reciben 3 parámetros.
146[ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
147# Camino del fichero de registro.
148FILE=$(ogGetHivePath "$1" "$2") || return $?
149
150# Añadir nueva clave.
151chntpw "$FILE" << EOT &> /dev/null
152cd ${3%\\*}
153dk ${3##*\\}
154q
155y
156EOT
157}
158
159
160#/**
161#         ogDeleteRegistryValue path_mountpoint str_hive str_valuename
162#@brief   Elimina un valor del registro de Windows.
163#@param   path_mountpoint  directorio donde está montado el sistema Windows
164#@param   str_hive         sección del registro
165#@param   str_valuename    nombre del valor
166#@return  (nada)
167#@exception OG_ERR_FORMAT    Formato incorrecto.
168#@exception OG_ERR_NOTFOUND  Fichero de registro no encontrado.
169#@note    hive = { default, sam, security, software, system, components }
170#@warning Requisitos: chntpw
171#@warning El sistema de archivos de Windows debe estar montada previamente.
172#@version 1.0.1 - Nueva función
173#@author  Ramon Gomez, ETSII Universidad de Sevilla
174#@date    2011-05-25
175#*/ ##
176function ogDeleteRegistryValue ()
177{
178# Variables locales.
179local FILE
180
181# Si se solicita, mostrar ayuda.
182if [ "$*" == "help" ]; then
183    ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_hive str_valuename" \
184           "$FUNCNAME /mnt/sda1 SOFTWARE '\Microsoft\NewKey\Value1'"
185    return
186fi
187# Error si no se reciben 3 parámetros.
188[ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
189# Camino del fichero de registro.
190FILE=$(ogGetHivePath "$1" "$2") || return $?
191
192# Devolver el dato del valor de registro.
193# /* (comentario Doxygen)
194chntpw "$FILE" << EOT &> /dev/null
195cd ${3%\\*}
196dv ${3##*\\}
197q
198y
199EOT
200# (comentario Doxygen) */
201}
202
203
204#/**
205#         ogGetHivePath path_mountpoint str_hive
206#@brief   Función básica que devuelve el camino del fichero con una sección del registro.
207#@param   path_mountpoint  directorio donde está montado el sistema Windows
208#@param   str_hive         sección del registro
209#@return  str_path - camino del fichero de registro
210#@exception OG_ERR_FORMAT    Formato incorrecto.
211#@exception OG_ERR_NOTFOUND  Fichero de registro no encontrado.
212#@note    hive = { DEFAULT, SAM, SECURITY, SOFTWARE, SYSTEM, COMPONENTS }
213#@warning El sistema de archivos de Windows debe estar montada previamente.
214#@version 1.0.1 - Nueva función
215#@author  Ramon Gomez, ETSII Universidad de Sevilla
216#@date    2011-05-18
217#*/ ##
218function ogGetHivePath ()
219{
220# Variables locales.
221local FILE FILENT FILEXP
222
223# Si se solicita, mostrar ayuda.
224if [ "$*" == "help" ]; then
225    ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_hive" \
226           "$FUNCNAME /mnt/sda1 SOFTWARE  => /mnt/sda1/WINDOWS/System32/config/SOFTWARE"
227    return
228fi
229# Error si no se reciben 2 parámetros.
230[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
231
232# Camino del fichero de registro en NT/2000 o en XP y posteriores.
233FILENT=$(ogGetPath "/$1/winnt/system32/config/$2")
234[ -f "$FILENT" ] && FILE="$FILENT"
235FILEXP=$(ogGetPath "/$1/windows/system32/config/$2")
236[ -f "$FILEXP" ] && FILE="$FILEXP"
237[ -f "$FILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$1 $2" || return $?
238
239echo "$FILE"
240}
241
242
243#/**
244#         ogGetRegistryValue path_mountpoint str_hive str_valuename
245#@brief   Devuelve el dato de un valor del registro de Windows.
246#@param   path_mountpoint  directorio donde está montado el sistema Windows
247#@param   str_hive         sección del registro
248#@param   str_valuename    nombre del valor
249#@return  str_valuedata - datos del valor.
250#@exception OG_ERR_FORMAT    Formato incorrecto.
251#@exception OG_ERR_NOTFOUND  Fichero de registro no encontrado.
252#@note    hive = { default, sam, security, software, system, components }
253#@warning Requisitos: chntpw, awk
254#@warning El sistema de archivos de Windows debe estar montado previamente.
255#@version 0.9 - Adaptación para OpenGNSys.
256#@author  Ramon Gomez, ETSII Universidad de Sevilla
257#@date    2009-09-11
258#@version 1.1.0 - Soportar tipos BINARY (parejas hexadecimales separadas por espacio).
259#@author  Ramon Gomez, ETSII Universidad de Sevilla
260#@date    2015-09-28
261#*/ ##
262function ogGetRegistryValue ()
263{
264# Variables locales.
265local FILE
266
267# Si se solicita, mostrar ayuda.
268if [ "$*" == "help" ]; then
269    ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_hive str_valuename" \
270           "$FUNCNAME /mnt/sda1 SOFTWARE '\Microsoft\NewKey\Value1'  ==>  1"
271    return
272fi
273# Error si no se reciben 3 parámetros.
274[ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
275# Camino del fichero de registro.
276FILE=$(ogGetHivePath "$1" "$2") || return $?
277
278# Devolver el dato del valor de registro.
279# /* (comentario Doxygen)
280chntpw "$FILE" << EOT 2> /dev/null | awk '^/> Value/ {if (index($0, "REG_BINARY") > 0)
281                                                          {data=""}
282                                                      else
283                                                          {getline; data=$0;} }
284                                          /^:[0-9A-F]+ / {data=data""substr($0, 9, 48);}
285                                          END {print data;}'
286cd ${3%\\*}
287cat ${3##*\\}
288q
289EOT
290# (comentario Doxygen) */
291}
292
293
294#/**
295#         ogListRegistryKeys path_mountpoint str_hive str_key
296#@brief   Lista los nombres de subclaves de una determinada clave del registro de Windows.
297#@param   path_mountpoint  directorio donde está montado el sistema Windows
298#@param   str_hive         sección del registro
299#@param   str_key          clave de registro
300#@return  str_subkey ... - lista de subclaves
301#@exception OG_ERR_FORMAT    Formato incorrecto.
302#@exception OG_ERR_NOTFOUND  Fichero de registro no encontrado.
303#@note    hive = { default, sam, security, software, system, components }
304#@warning Requisitos: chntpw, awk
305#@warning El sistema de archivos de Windows debe estar montado previamente.
306#@version 0.9 - Adaptación para OpenGNSys.
307#@author  Ramon Gomez, ETSII Universidad de Sevilla
308#@date    2009-09-23
309#*/ ##
310function ogListRegistryKeys ()
311{
312# Variables locales.
313local FILE
314
315# Si se solicita, mostrar ayuda.
316if [ "$*" == "help" ]; then
317    ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_hive str_key" \
318           "$FUNCNAME /mnt/sda1 SOFTWARE '\Microsoft\Windows\CurrentVersion'"
319    return
320fi
321# Error si no se reciben 3 parámetros.
322[ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
323
324# Camino del fichero de registro.
325FILE=$(ogGetHivePath "$1" "$2") || return $?
326
327# Devolver la lista de claves de registro.
328chntpw "$FILE" << EOT 2> /dev/null | awk 'BEGIN {FS="[<>]"} $1~/^  $/ {print $2}'
329ls $3
330q
331EOT
332}
333
334
335#/**
336#         ogListRegistryValues path_mountpoint str_hive str_key
337#@brief   Lista los nombres de valores de una determinada clave del registro de Windows.
338#@param   path_mountpoint  directorio donde está montado el sistema Windows
339#@param   str_hive         sección del registro
340#@param   str_key          clave de registro
341#@return  str_value ... - lista de valores
342#@exception OG_ERR_FORMAT    Formato incorrecto.
343#@exception OG_ERR_NOTFOUND  Fichero de registro no encontrado.
344#@note    hive = { default, sam, security, software, system, components }
345#@warning Requisitos: chntpw, awk
346#@warning El sistema de archivos de Windows debe estar montado previamente.
347#@version 1.0.1 - Nueva función.
348#@author  Ramon Gomez, ETSII Universidad de Sevilla
349#@date    2011-05-26
350#*/ ##
351function ogListRegistryValues ()
352{
353# Variables locales.
354local FILE
355
356# Si se solicita, mostrar ayuda.
357if [ "$*" == "help" ]; then
358    ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_hive str_key" \
359           "$FUNCNAME /mnt/sda1 SOFTWARE '\Microsoft\Windows\CurrentVersion'"
360    return
361fi
362# Error si no se reciben 3 parámetros.
363[ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
364# Camino del fichero de registro.
365FILE=$(ogGetHivePath "$1" "$2") || return $?
366
367# Devolver la lista de claves de registro.
368chntpw "$FILE" << EOT 2> /dev/null | awk 'BEGIN {FS="[<>]"} $1~/REG_/ {print $2}'
369ls $3
370q
371EOT
372}
373
374
375#/**
376#         ogSetRegistryValue path_mountpoint str_hive str_valuename str_valuedata
377#@brief   Establece el dato asociado a un valor del registro de Windows.
378#@param   path_mountpoint  directorio donde está montado el sistema Windows
379#@param   str_hive         sección del registro
380#@param   str_valuename    nombre del valor de registro
381#@param   str_valuedata    dato del valor de registro
382#@return  (nada)
383#@exception OG_ERR_FORMAT    Formato incorrecto.
384#@exception OG_ERR_NOTFOUND  Fichero de registro no encontrado.
385#@note    hive = { default, sam, security, software, system, components }
386#@warning Requisitos: chntpw
387#@warning El sistema de archivos de Windows debe estar montado previamente.
388#@version 0.9 - Adaptación para OpenGNSys.
389#@author  Ramon Gomez, ETSII Universidad de Sevilla
390#@date    2009-09-24
391#@version 1.1.0 - Soportar tipos BINARY (parejas hexadecimales separadas por espacio).
392#@author  Ramon Gomez, ETSII Universidad de Sevilla
393#@date    2015-09-28
394#*/ ##
395function ogSetRegistryValue ()
396{
397# Variables locales.
398local FILE i n tmpfile
399
400# Si se solicita, mostrar ayuda.
401if [ "$*" == "help" ]; then
402    ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_hive str_valuename str_data" \
403           "$FUNCNAME /mnt/sda1 SOFTWARE '\Key\SubKey\StringValue' \"Abcde Fghij\"" \
404           "$FUNCNAME /mnt/sda1 SOFTWARE '\Key\SubKey\DwordValue' 1" \
405           "$FUNCNAME /mnt/sda1 SOFTWARE '\Key\SubKey\BinaryValue' \"04 08 0C 10\""
406    return
407fi
408# Error si no se reciben 4 parámetros.
409[ $# == 4 ] || ogRaiseError $OG_ERR_FORMAT || return $?
410# Camino del fichero de registro.
411FILE=$(ogGetHivePath "$1" "$2") || return $?
412
413# Fichero temporal para componer la entrada al comando "chntpw".
414tmpfile=/tmp/chntpw$$
415trap "rm -f $tmpfile" 1 2 3 9 15
416
417# Comprobar tipo de datos del valor del registro.
418cat << EOT >$tmpfile
419ls ${3%\\*}
420q
421EOT
422if [ -n "$(chntpw "$FILE" < $tmpfile 2> /dev/null | grep "BINARY.*<${3##*\\}>")" ]; then
423    # Procesar tipo binario (incluir nº de bytes y líneas de 16 parejas hexadecimales).
424    [[ "$4 " =~ ^([0-9A-F]{2} )*$ ]] || ogRaiseError $OG_ERR_FORMAT "\"$4\"" || return $?
425    let n=${#4}+1
426    cat << EOT >$tmpfile
427cd ${3%\\*}
428ed ${3##*\\}
429$[n/3]
430EOT
431    # Formato de líneas hexadecimales:   :OFFSET  XX YY ZZ ... (hasta 16 parejas).
432    for (( i=0; i<n; i+=48 )); do
433        printf ":%05x  %s\n" $[i/3] "${4:$i:48}" >> $tmpfile
434    done
435    echo -e "s\nq\ny" >> $tmpfile
436else
437    # Cambiar el dato del valor de registro para cadenas y bytes.
438    cat << EOT >$tmpfile
439cd ${3%\\*}
440ed ${3##*\\}
441$4
442q
443y
444EOT
445
446fi
447
448# Aplicar cambios.
449chntpw "$FILE" < $tmpfile &> /dev/null
450rm -f $tmpfile
451}
452
453
Note: See TracBrowser for help on using the repository browser.