OpenGnsys  1.1.1
Registry.lib
Ir a la documentación de este archivo.
1 #!/bin/bash
2 #
12 # Función ficticia para lanzar chntpw con timeout de 5 s., evitando cuelgues del programa.
13 function chntpw ()
14 {
15 local CHNTPW
16 CHNTPW=$(which drbl-chntpw)
17 CHNTPW=${CHNTPW:-$(which chntpw)}
18 timeout --foreground 5s $CHNTPW -e "$@"
19 }
20 
21 
22 # ##
38 function ogAddRegistryKey ()
39 {
40 # Variables locales.
41 local FILE
42 
43 # Si se solicita, mostrar ayuda.
44 if [ "$*" == "help" ]; then
45  ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_hive str_key" \
46  "$FUNCNAME /mnt/sda1 SOFTWARE '\Microsoft\NewKey'"
47  return
48 fi
49 # Error si no se reciben 3 parámetros.
50 [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
51 # Camino del fichero de registro.
52 FILE=$(ogGetHivePath "$1" "$2") || return $?
53 
54 # Añadir nueva clave.
55 chntpw "$FILE" << EOT &> /dev/null
56 cd ${3%\\*}
57 nk ${3##*\\}
58 q
59 y
60 EOT
61 }
62 
63 # ##
81 function ogAddRegistryValue ()
82 {
83 # Variables locales.
84 local FILE TYPE
85 
86 # Si se solicita, mostrar ayuda.
87 if [ "$*" == "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
92 fi
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.
96 FILE=$(ogGetHivePath "$1" "$2") || return $?
97 case "${4^^}" in
98  STRING|"") TYPE=1 ;;
99  BINARY) TYPE=3 ;;
100  DWORD) TYPE=4 ;;
101  *) ogRaiseError $OG_ERR_OUTOFLIMIT "$4"
102  return $? ;;
103 esac
104 
105 # Devolver el dato del valor de registro.
106 # /* (comentario Doxygen)
107 chntpw "$FILE" << EOT &> /dev/null
108 cd ${3%\\*}
109 nv $TYPE ${3##*\\}
110 q
111 y
112 EOT
113 # (comentario Doxygen) */
114 }
115 
116 
117 # ##
135 {
136 # Variables locales.
137 local FILE
138 
139 # Si se solicita, mostrar ayuda.
140 if [ "$*" == "help" ]; then
141  ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_hive str_key" \
142  "$FUNCNAME /mnt/sda1 SOFTWARE '\Microsoft\NewKey'"
143  return
144 fi
145 # Error si no se reciben 3 parámetros.
146 [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
147 # Camino del fichero de registro.
148 FILE=$(ogGetHivePath "$1" "$2") || return $?
149 
150 # Añadir nueva clave.
151 chntpw "$FILE" << EOT &> /dev/null
152 cd ${3%\\*}
153 dk ${3##*\\}
154 q
155 y
156 EOT
157 }
158 
159 
160 # ##
177 {
178 # Variables locales.
179 local FILE
180 
181 # Si se solicita, mostrar ayuda.
182 if [ "$*" == "help" ]; then
183  ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_hive str_valuename" \
184  "$FUNCNAME /mnt/sda1 SOFTWARE '\Microsoft\NewKey\Value1'"
185  return
186 fi
187 # Error si no se reciben 3 parámetros.
188 [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
189 # Camino del fichero de registro.
190 FILE=$(ogGetHivePath "$1" "$2") || return $?
191 
192 # Devolver el dato del valor de registro.
193 # /* (comentario Doxygen)
194 chntpw "$FILE" << EOT &> /dev/null
195 cd ${3%\\*}
196 dv ${3##*\\}
197 q
198 y
199 EOT
200 # (comentario Doxygen) */
201 }
202 
203 
204 # ##
221 function ogGetHivePath ()
222 {
223 # Variables locales.
224 local FILE HIVE
225 
226 # Si se solicita, mostrar ayuda.
227 if [ "$*" == "help" ]; then
228  ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint [str_hive|str_user]" \
229  "$FUNCNAME /mnt/sda1 SOFTWARE => /mnt/sda1/WINDOWS/System32/config/SOFTWARE" \
230  "$FUNCNAME /mnt/sda1 user1 => /mnt/sda1/Users/user1/NTUSER.DAT"
231  return
232 fi
233 # Error si no se reciben 2 parámetros.
234 [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
235 
236 # Camino del fichero de registro de usuario o de sistema (de menor a mayor prioridad).
237 FILE="$(ogGetPath "/$1/Windows/System32/config/$2")"
238 [ -z "$FILE" ] && FILE="$(ogGetPath "/$1/Users/$2/NTUSER.DAT")"
239 [ -z "$FILE" ] && FILE="$(ogGetPath "/$1/winnt/system32/config/$2")"
240 [ -z "$FILE" ] && FILE="$(ogGetPath "/$1/Documents and Settings/$2/NTUSER.DAT")"
241 [ -f "$FILE" ] && echo "$FILE" || ogRaiseError $OG_ERR_NOTFOUND "$1 $2" || return $?
242 }
243 
244 
245 # ##
265 {
266 # Variables locales.
267 local FILE
268 
269 # Si se solicita, mostrar ayuda.
270 if [ "$*" == "help" ]; then
271  ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_hive str_valuename" \
272  "$FUNCNAME /mnt/sda1 SOFTWARE '\Microsoft\NewKey\Value1' ==> 1"
273  return
274 fi
275 # Error si no se reciben 3 parámetros.
276 [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
277 # Camino del fichero de registro.
278 FILE=$(ogGetHivePath "$1" "$2") || return $?
279 
280 # Devolver el dato del valor de registro.
281 # /* (comentario Doxygen)
282 chntpw "$FILE" << EOT 2> /dev/null | awk '/> Value/ {if (index($0, "REG_BINARY") > 0)
283  {data=""}
284  else
285  {getline; data=$0;} }
286  /^:[0-9A-F]+ / {data=data""substr($0, 9, 48);}
287  END {print data;}'
288 cd ${3%\\*}
289 cat ${3##*\\}
290 q
291 EOT
292 # (comentario Doxygen) */
293 }
294 
295 
296 # ##
313 {
314 # Variables locales.
315 local FILE
316 
317 # Si se solicita, mostrar ayuda.
318 if [ "$*" == "help" ]; then
319  ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_hive str_key" \
320  "$FUNCNAME /mnt/sda1 SOFTWARE '\Microsoft\Windows\CurrentVersion'"
321  return
322 fi
323 # Error si no se reciben 3 parámetros.
324 [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
325 
326 # Camino del fichero de registro.
327 FILE=$(ogGetHivePath "$1" "$2") || return $?
328 
329 # Devolver la lista de claves de registro.
330 chntpw "$FILE" << EOT 2> /dev/null | awk 'BEGIN {FS="[<>]"} $1~/^ $/ {print $2}'
331 ls $3
332 q
333 EOT
334 }
335 
336 
337 # ##
354 {
355 # Variables locales.
356 local FILE
357 
358 # Si se solicita, mostrar ayuda.
359 if [ "$*" == "help" ]; then
360  ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_hive str_key" \
361  "$FUNCNAME /mnt/sda1 SOFTWARE '\Microsoft\Windows\CurrentVersion'"
362  return
363 fi
364 # Error si no se reciben 3 parámetros.
365 [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
366 # Camino del fichero de registro.
367 FILE=$(ogGetHivePath "$1" "$2") || return $?
368 
369 # Devolver la lista de claves de registro.
370 chntpw "$FILE" << EOT 2> /dev/null | awk 'BEGIN {FS="[<>]"} $1~/REG_/ {print $2}'
371 ls $3
372 q
373 EOT
374 }
375 
376 
377 # ##
398 {
399 # Variables locales.
400 local FILE i n tmpfile
401 
402 # Si se solicita, mostrar ayuda.
403 if [ "$*" == "help" ]; then
404  ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_hive str_valuename str_data" \
405  "$FUNCNAME /mnt/sda1 SOFTWARE '\Key\SubKey\StringValue' \"Abcde Fghij\"" \
406  "$FUNCNAME /mnt/sda1 SOFTWARE '\Key\SubKey\DwordValue' 1" \
407  "$FUNCNAME /mnt/sda1 SOFTWARE '\Key\SubKey\BinaryValue' \"04 08 0C 10\""
408  return
409 fi
410 # Error si no se reciben 4 parámetros.
411 [ $# == 4 ] || ogRaiseError $OG_ERR_FORMAT || return $?
412 # Camino del fichero de registro.
413 FILE=$(ogGetHivePath "$1" "$2") || return $?
414 
415 # Fichero temporal para componer la entrada al comando "chntpw".
416 tmpfile=/tmp/chntpw$$
417 trap "rm -f $tmpfile" 1 2 3 9 15
418 
419 # Comprobar tipo de datos del valor del registro.
420 cat << EOT >$tmpfile
421 ls ${3%\\*}
422 q
423 EOT
424 if [ -n "$(chntpw "$FILE" < $tmpfile 2> /dev/null | grep "BINARY.*<${3##*\\}>")" ]; then
425  # Procesar tipo binario (incluir nº de bytes y líneas de 16 parejas hexadecimales).
426  [[ "$4 " =~ ^([0-9A-F]{2} )*$ ]] || ogRaiseError $OG_ERR_FORMAT "\"$4\"" || return $?
427  let n=${#4}+1
428  cat << EOT >$tmpfile
429 cd ${3%\\*}
430 ed ${3##*\\}
431 $[n/3]
432 EOT
433  # Formato de líneas hexadecimales: :OFFSET XX YY ZZ ... (hasta 16 parejas).
434  for (( i=0; i<n; i+=48 )); do
435  printf ":%05x %s\n" $[i/3] "${4:$i:48}" >> $tmpfile
436  done
437  echo -e "s\nq\ny" >> $tmpfile
438 else
439  # Cambiar el dato del valor de registro para cadenas y bytes.
440  cat << EOT >$tmpfile
441 cd ${3%\\*}
442 ed ${3##*\\}
443 $4
444 q
445 y
446 EOT
447 
448 fi
449 
450 # Aplicar cambios.
451 chntpw "$FILE" < $tmpfile &> /dev/null
452 rm -f $tmpfile
453 }
454 
455 
function ogGetHivePath()
Función básica que devuelve el camino del fichero con una sección del registro. #.
Definition: Registry.lib:221
function ogAddRegistryKey()
Añade una nueva clave al registro de Windows. #.
Definition: Registry.lib:38
function ogListRegistryValues()
Lista los nombres de valores de una determinada clave del registro de Windows. #. ...
Definition: Registry.lib:353
function ogRaiseError()
Devuelve el mensaje y el código de error correspondiente. #.
Definition: System.lib:188
function ogHelp()
Muestra mensaje de ayuda para una función determinda. #.
Definition: System.lib:153
function ogDeleteRegistryValue()
Elimina un valor del registro de Windows. #.
Definition: Registry.lib:176
function ogListRegistryKeys()
Lista los nombres de subclaves de una determinada clave del registro de Windows. #.
Definition: Registry.lib:312
function ogAddRegistryValue()
Añade un nuevo valor al registro de Windows, indicando su tipo de datos. #.
Definition: Registry.lib:81
function ogGetRegistryValue()
Devuelve el dato de un valor del registro de Windows. #.
Definition: Registry.lib:264
function ogDeleteRegistryKey()
Elimina una clave del registro de Windows con todo su contenido. #.
Definition: Registry.lib:134
function chntpw()
Definition: Registry.lib:13
function ogSetRegistryValue()
Establece el dato asociado a un valor del registro de Windows. #.
Definition: Registry.lib:397
function awk()
Definition: ToolsGNU.c:89