elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Como proteger una cartera - billetera de Bitcoin


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  Registro Windows desde Java (WinRegistry)
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Registro Windows desde Java (WinRegistry)  (Leído 2,609 veces)
juancaa

Desconectado Desconectado

Mensajes: 140


[ Img^(Img) = Real ]


Ver Perfil
Registro Windows desde Java (WinRegistry)
« en: 4 Febrero 2013, 03:39 am »

Buenas! Pues verán, buscando por internet un método adecuado para acceder al registro de Windows mediante Java encontré un código muy interesante pero que tiene la pega de ser detectado como una amenaza para por el mismísimo Windows Defender lo cual hace que la implementación de dicho codigo en cualquier aplicación no sea posible... Mi pregunta es: ¿Existe alguna manera de modificar el código para que sea posible evadir los mecanismos de detección? O dicho de otra forma, que parte del código es la que es detectada como una amenaza para Windows?

Código
  1. import java.lang.reflect.InvocationTargetException;
  2. import java.lang.reflect.Method;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.prefs.Preferences;
  8.  
  9. public class WinRegistry {
  10.  public static final int HKEY_CURRENT_USER = 0x80000001;
  11.  public static final int HKEY_LOCAL_MACHINE = 0x80000002;
  12.  public static final int REG_SUCCESS = 0;
  13.  public static final int REG_NOTFOUND = 2;
  14.  public static final int REG_ACCESSDENIED = 5;
  15.  
  16.  private static final int KEY_ALL_ACCESS = 0xf003f;
  17.  private static final int KEY_READ = 0x20019;
  18.  private static Preferences userRoot = Preferences.userRoot();
  19.  private static Preferences systemRoot = Preferences.systemRoot();
  20.  private static Class<? extends Preferences> userClass = userRoot.getClass();
  21.  private static Method regOpenKey = null;
  22.  private static Method regCloseKey = null;
  23.  private static Method regQueryValueEx = null;
  24.  private static Method regEnumValue = null;
  25.  private static Method regQueryInfoKey = null;
  26.  private static Method regEnumKeyEx = null;
  27.  private static Method regCreateKeyEx = null;
  28.  private static Method regSetValueEx = null;
  29.  private static Method regDeleteKey = null;
  30.  private static Method regDeleteValue = null;
  31.  
  32.  static {
  33.    try {
  34.      regOpenKey = userClass.getDeclaredMethod("WindowsRegOpenKey",
  35.          new Class[] { int.class, byte[].class, int.class });
  36.      regOpenKey.setAccessible(true);
  37.      regCloseKey = userClass.getDeclaredMethod("WindowsRegCloseKey",
  38.          new Class[] { int.class });
  39.      regCloseKey.setAccessible(true);
  40.      regQueryValueEx = userClass.getDeclaredMethod("WindowsRegQueryValueEx",
  41.          new Class[] { int.class, byte[].class });
  42.      regQueryValueEx.setAccessible(true);
  43.      regEnumValue = userClass.getDeclaredMethod("WindowsRegEnumValue",
  44.          new Class[] { int.class, int.class, int.class });
  45.      regEnumValue.setAccessible(true);
  46.      regQueryInfoKey = userClass.getDeclaredMethod("WindowsRegQueryInfoKey1",
  47.          new Class[] { int.class });
  48.      regQueryInfoKey.setAccessible(true);
  49.      regEnumKeyEx = userClass.getDeclaredMethod(  
  50.          "WindowsRegEnumKeyEx", new Class[] { int.class, int.class,  
  51.              int.class });  
  52.      regEnumKeyEx.setAccessible(true);
  53.      regCreateKeyEx = userClass.getDeclaredMethod(  
  54.          "WindowsRegCreateKeyEx", new Class[] { int.class,  
  55.              byte[].class });  
  56.      regCreateKeyEx.setAccessible(true);  
  57.      regSetValueEx = userClass.getDeclaredMethod(  
  58.          "WindowsRegSetValueEx", new Class[] { int.class,  
  59.              byte[].class, byte[].class });  
  60.      regSetValueEx.setAccessible(true);
  61.      regDeleteValue = userClass.getDeclaredMethod(  
  62.          "WindowsRegDeleteValue", new Class[] { int.class,  
  63.              byte[].class });  
  64.      regDeleteValue.setAccessible(true);
  65.      regDeleteKey = userClass.getDeclaredMethod(  
  66.          "WindowsRegDeleteKey", new Class[] { int.class,  
  67.              byte[].class });  
  68.      regDeleteKey.setAccessible(true);
  69.    }
  70.    catch (Exception e) {
  71.      e.printStackTrace();
  72.    }
  73.  }
  74.  
  75.  private WinRegistry() {  }
  76.  
  77.  /**
  78.    * Read a value from key and value name
  79.    * @param hkey   HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
  80.    * @param key
  81.    * @param valueName
  82.    * @return the value
  83.    * @throws IllegalArgumentException
  84.    * @throws IllegalAccessException
  85.    * @throws InvocationTargetException
  86.    */
  87.  public static String readString(int hkey, String key, String valueName)
  88.  {
  89.    if (hkey == HKEY_LOCAL_MACHINE) {
  90.      return readString(systemRoot, hkey, key, valueName);
  91.    }
  92.    else if (hkey == HKEY_CURRENT_USER) {
  93.      return readString(userRoot, hkey, key, valueName);
  94.    }
  95.    else {
  96.      throw new IllegalArgumentException("hkey=" + hkey);
  97.    }
  98.  }
  99.  
  100.  /**
  101.    * Read value(s) and value name(s) form given key
  102.    * @param hkey  HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
  103.    * @param key
  104.    * @return the value name(s) plus the value(s)
  105.    * @throws IllegalArgumentException
  106.    * @throws IllegalAccessException
  107.    * @throws InvocationTargetException
  108.    */
  109.  public static Map<String, String> readStringValues(int hkey, String key)
  110.  {
  111.    if (hkey == HKEY_LOCAL_MACHINE) {
  112.      return readStringValues(systemRoot, hkey, key);
  113.    }
  114.    else if (hkey == HKEY_CURRENT_USER) {
  115.      return readStringValues(userRoot, hkey, key);
  116.    }
  117.    else {
  118.      throw new IllegalArgumentException("hkey=" + hkey);
  119.    }
  120.  }
  121.  
  122.  /**
  123.    * Read the value name(s) from a given key
  124.    * @param hkey  HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
  125.    * @param key
  126.    * @return the value name(s)
  127.    * @throws IllegalArgumentException
  128.    * @throws IllegalAccessException
  129.    * @throws InvocationTargetException
  130.    */
  131.  public static List<String> readStringSubKeys(int hkey, String key)
  132.  {
  133.    if (hkey == HKEY_LOCAL_MACHINE) {
  134.      return readStringSubKeys(systemRoot, hkey, key);
  135.    }
  136.    else if (hkey == HKEY_CURRENT_USER) {
  137.      return readStringSubKeys(userRoot, hkey, key);
  138.    }
  139.    else {
  140.      throw new IllegalArgumentException("hkey=" + hkey);
  141.    }
  142.  }
  143.  
  144.  /**
  145.    * Create a key
  146.    * @param hkey  HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
  147.    * @param key
  148.    * @throws IllegalArgumentException
  149.    * @throws IllegalAccessException
  150.    * @throws InvocationTargetException
  151.    */
  152.  public static void createKey(int hkey, String key)
  153.  {
  154.    int [] ret;
  155.    if (hkey == HKEY_LOCAL_MACHINE) {
  156.      ret = createKey(systemRoot, hkey, key);
  157.      regCloseKey.invoke(systemRoot, new Object[] { new Integer(ret[0]) });
  158.    }
  159.    else if (hkey == HKEY_CURRENT_USER) {
  160.      ret = createKey(userRoot, hkey, key);
  161.      regCloseKey.invoke(userRoot, new Object[] { new Integer(ret[0]) });
  162.    }
  163.    else {
  164.      throw new IllegalArgumentException("hkey=" + hkey);
  165.    }
  166.    if (ret[1] != REG_SUCCESS) {
  167.      throw new IllegalArgumentException("rc=" + ret[1] + "  key=" + key);
  168.    }
  169.  }
  170.  
  171.  /**
  172.    * Write a value in a given key/value name
  173.    * @param hkey
  174.    * @param key
  175.    * @param valueName
  176.    * @param value
  177.    * @throws IllegalArgumentException
  178.    * @throws IllegalAccessException
  179.    * @throws InvocationTargetException
  180.    */
  181.  public static void writeStringValue
  182.    (int hkey, String key, String valueName, String value)
  183.  {
  184.    if (hkey == HKEY_LOCAL_MACHINE) {
  185.      writeStringValue(systemRoot, hkey, key, valueName, value);
  186.    }
  187.    else if (hkey == HKEY_CURRENT_USER) {
  188.      writeStringValue(userRoot, hkey, key, valueName, value);
  189.    }
  190.    else {
  191.      throw new IllegalArgumentException("hkey=" + hkey);
  192.    }
  193.  }
  194.  
  195.  /**
  196.    * Delete a given key
  197.    * @param hkey
  198.    * @param key
  199.    * @throws IllegalArgumentException
  200.    * @throws IllegalAccessException
  201.    * @throws InvocationTargetException
  202.    */
  203.  public static void deleteKey(int hkey, String key)
  204.  {
  205.    int rc = -1;
  206.    if (hkey == HKEY_LOCAL_MACHINE) {
  207.      rc = deleteKey(systemRoot, hkey, key);
  208.    }
  209.    else if (hkey == HKEY_CURRENT_USER) {
  210.      rc = deleteKey(userRoot, hkey, key);
  211.    }
  212.    if (rc != REG_SUCCESS) {
  213.      throw new IllegalArgumentException("rc=" + rc + "  key=" + key);
  214.    }
  215.  }
  216.  
  217.  /**
  218.    * delete a value from a given key/value name
  219.    * @param hkey
  220.    * @param key
  221.    * @param value
  222.    * @throws IllegalArgumentException
  223.    * @throws IllegalAccessException
  224.    * @throws InvocationTargetException
  225.    */
  226.  public static void deleteValue(int hkey, String key, String value)
  227.  {
  228.    int rc = -1;
  229.    if (hkey == HKEY_LOCAL_MACHINE) {
  230.      rc = deleteValue(systemRoot, hkey, key, value);
  231.    }
  232.    else if (hkey == HKEY_CURRENT_USER) {
  233.      rc = deleteValue(userRoot, hkey, key, value);
  234.    }
  235.    if (rc != REG_SUCCESS) {
  236.      throw new IllegalArgumentException("rc=" + rc + "  key=" + key + "  value=" + value);
  237.    }
  238.  }
  239.  
  240.  // =====================
  241.  
  242.  private static int deleteValue
  243.    (Preferences root, int hkey, String key, String value)
  244.  {
  245.    int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
  246.        new Integer(hkey), toCstr(key), new Integer(KEY_ALL_ACCESS) });
  247.    if (handles[1] != REG_SUCCESS) {
  248.      return handles[1];  // can be REG_NOTFOUND, REG_ACCESSDENIED
  249.    }
  250.    int rc =((Integer) regDeleteValue.invoke(root,  
  251.        new Object[] {
  252.          new Integer(handles[0]), toCstr(value)
  253.          })).intValue();
  254.    regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
  255.    return rc;
  256.  }
  257.  
  258.  private static int deleteKey(Preferences root, int hkey, String key)
  259.  {
  260.    int rc =((Integer) regDeleteKey.invoke(root,  
  261.        new Object[] { new Integer(hkey), toCstr(key) })).intValue();
  262.    return rc;  // can REG_NOTFOUND, REG_ACCESSDENIED, REG_SUCCESS
  263.  }
  264.  
  265.  private static String readString(Preferences root, int hkey, String key, String value)
  266.  {
  267.    int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
  268.        new Integer(hkey), toCstr(key), new Integer(KEY_READ) });
  269.    if (handles[1] != REG_SUCCESS) {
  270.      return null;
  271.    }
  272.    byte[] valb = (byte[]) regQueryValueEx.invoke(root, new Object[] {
  273.        new Integer(handles[0]), toCstr(value) });
  274.    regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
  275.    return (valb != null ? new String(valb).trim() : null);
  276.  }
  277.  
  278.  private static Map<String,String> readStringValues
  279.    (Preferences root, int hkey, String key)
  280.  {
  281.    HashMap<String, String> results = new HashMap<String,String>();
  282.    int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
  283.        new Integer(hkey), toCstr(key), new Integer(KEY_READ) });
  284.    if (handles[1] != REG_SUCCESS) {
  285.      return null;
  286.    }
  287.    int[] info = (int[]) regQueryInfoKey.invoke(root,
  288.        new Object[] { new Integer(handles[0]) });
  289.  
  290.    int count = info[0]; // count  
  291.    int maxlen = info[3]; // value length max
  292.    for(int index=0; index<count; index++)  {
  293.      byte[] name = (byte[]) regEnumValue.invoke(root, new Object[] {
  294.          new Integer
  295.            (handles[0]), new Integer(index), new Integer(maxlen + 1)});
  296.      String value = readString(hkey, key, new String(name));
  297.      results.put(new String(name).trim(), value);
  298.    }
  299.    regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
  300.    return results;
  301.  }
  302.  
  303.  private static List<String> readStringSubKeys
  304.    (Preferences root, int hkey, String key)
  305.  {
  306.    List<String> results = new ArrayList<String>();
  307.    int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
  308.        new Integer(hkey), toCstr(key), new Integer(KEY_READ)
  309.        });
  310.    if (handles[1] != REG_SUCCESS) {
  311.      return null;
  312.    }
  313.    int[] info = (int[]) regQueryInfoKey.invoke(root,
  314.        new Object[] { new Integer(handles[0]) });
  315.  
  316.    int count  = info[0]; // Fix: info[2] was being used here with wrong results. Suggested by davenpcj, confirmed by Petrucio
  317.    int maxlen = info[3]; // value length max
  318.    for(int index=0; index<count; index++)  {
  319.      byte[] name = (byte[]) regEnumKeyEx.invoke(root, new Object[] {
  320.          new Integer
  321.            (handles[0]), new Integer(index), new Integer(maxlen + 1)
  322.          });
  323.      results.add(new String(name).trim());
  324.    }
  325.    regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
  326.    return results;
  327.  }
  328.  
  329.  private static int [] createKey(Preferences root, int hkey, String key)
  330.  {
  331.    return  (int[]) regCreateKeyEx.invoke(root,
  332.        new Object[] { new Integer(hkey), toCstr(key) });
  333.  }
  334.  
  335.  private static void writeStringValue
  336.    (Preferences root, int hkey, String key, String valueName, String value)
  337.  {
  338.    int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
  339.        new Integer(hkey), toCstr(key), new Integer(KEY_ALL_ACCESS) });
  340.  
  341.    regSetValueEx.invoke(root,  
  342.        new Object[] {
  343.          new Integer(handles[0]), toCstr(valueName), toCstr(value)
  344.          });
  345.    regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
  346.  }
  347.  
  348.  // utility
  349.  private static byte[] toCstr(String str) {
  350.    byte[] result = new byte[str.length() + 1];
  351.  
  352.    for (int i = 0; i < str.length(); i++) {
  353.      result[i] = (byte) str.charAt(i);
  354.    }
  355.    result[str.length()] = 0;
  356.    return result;
  357.  }
  358. }


En línea

Que tengas un buen dia!
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
MODIFICANDO EL REGISTRO DESDE VB « 1 2 3 »
Programación Visual Basic
dxr 22 11,002 Último mensaje 6 Diciembre 2008, 15:07 pm
por seba123neo
Sistema de Login - Registro [Usando el registro de Windows]
Programación Visual Basic
ToNy_EsP 2 4,053 Último mensaje 28 Febrero 2009, 18:10 pm
por seba123neo
Modificar registro de Windows desde Ubuntu LiveCD
GNU/Linux
GameAndWatch 8 11,096 Último mensaje 11 Octubre 2012, 22:43 pm
por Squirtle
[Ayuda] Arreglo y registro java
Java
axiotm 2 6,534 Último mensaje 27 Agosto 2013, 12:25 pm
por Zoik
¿ Añadir java al path de windows desde batch ?
Scripting
PedroDJavier 1 2,526 Último mensaje 5 Junio 2014, 03:14 am
por daryo
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines