Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: krosty123 en 2 Noviembre 2010, 02:03 am



Título: Ayuda win32.beginupdateresource(), win32.updateresource(), no funciona.
Publicado por: krosty123 en 2 Noviembre 2010, 02:03 am
estuve probando varias maneras de cambiar los rescursos de un exe, al final lo que ando usando es:

Código
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. using Microsoft.Win32;
  11.  
  12. namespace WindowsFormsApplication1
  13. {
  14.    static class Program
  15.    {
  16.        /// <summary>
  17.        /// The main entry point for the application.
  18.        /// </summary>
  19.        [STAThread]
  20.        [DllImport("kernel32.dll", SetLastError = true)]
  21.        static extern IntPtr BeginUpdateResource(string pFileName, bool bDeleteExistingResources);
  22.        [DllImport("kernel32.dll", SetLastError = true)]
  23.        static extern bool UpdateResource(IntPtr hUpdate, string lpType, string lpName, ushort wLanguage, IntPtr lpData, uint cbData);
  24.        [DllImport("kernel32.dll", SetLastError = true)]
  25.        static extern bool EndUpdateResource(IntPtr hUpdate, bool fDiscard);
  26.        static unsafe void Main()
  27.        {
  28.            string path = "c:\\users\\sean\\desktop\\resourcer.exe";
  29.  
  30.            IntPtr hResource = BeginUpdateResource(path, false);
  31.            if (hResource.ToInt32() == 0)
  32.                throw new Exception("File Not Found");
  33.  
  34.            string newMessage = File.ReadAllText("c:\\users\\sean\\desktop\\newMessage.txt");
  35.            Byte[] bytes = new ASCIIEncoding().GetBytes(newMessage);
  36.            GCHandle bHandle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
  37.            IntPtr ptr = bHandle.AddrOfPinnedObject();
  38.  
  39.            ushort id = (ushort)Language.MakeLanguageID();
  40.  
  41.            if (UpdateResource(hResource, "FILE", "eva.txt", id, ptr, (uint)bytes.Length))
  42.                EndUpdateResource(hResource, false);
  43.            else
  44.                MessageBox.Show("Did not update", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  45.        }
  46.    }
  47.    public static class Language
  48.    {
  49.  
  50.        public static int MakeLanguageID()
  51.        {
  52.            CultureInfo currentCulture = CultureInfo.CurrentCulture;
  53.            int pid = GetPid(currentCulture.LCID);
  54.            int sid = GetSid(currentCulture.LCID);
  55.            return (((ushort)pid) << 10) | ((ushort)sid);
  56.        }
  57.  
  58.  
  59.        public static int GetPid(int lcid)
  60.        { return ((ushort)lcid) & 0x3ff; }
  61.  
  62.  
  63.        public static int GetSid(int lcid)
  64.        { return ((ushort)lcid) >> 10; }
  65.  
  66.    }
  67. }
  68.  


Para ver si funciona lo que hago es "descomprimir" el recurso del exe modificado.
Supuestamente deberia mostrar el nuevo .txt, pero no... cuando lo abro contiene el texto viejo.

Lo que hago para descomprimir los recursos del exe modificado es:
Código
  1.  
  2.      string strNewPathToSave = "new.txt";
  3.  
  4.      Assembly assembly = Assembly.GetExecutingAssembly();
  5.  
  6.      Stream resourceStream = assembly.GetManifestResourceStream("WindowsFormsApplication2.Resources.eva.txt");
  7.  
  8.      System.IO.FileStream fs = System.IO.File.OpenWrite(strNewPathToSave);
  9.  
  10.      try
  11.  
  12.      {
  13.  
  14.      // Save the File...
  15.  
  16.      byte[] buffer = new byte[resourceStream.Length];
  17.  
  18.      resourceStream.Read(buffer, 0, (int)resourceStream.Length);
  19.  
  20.      fs.Write(buffer, 0, buffer.Length);
  21.  
  22.      }
  23.  
  24.      finally
  25.  
  26.      {
  27.  
  28.      fs.Close();
  29.  
  30.      }

Redondeando....
Con exe1 cambio un recurso de exe2 (un .txt). Despues abro el exe2 y descomprimo el recurso .txt . Este en lugar de mostrar el nuevo texto, muestra el texto del recurso original.
Alguna idea?

Saludos


Título: Re: Ayuda win32.beginupdateresource(), win32.updateresource(), no funciona.
Publicado por: MANULOMM en 2 Noviembre 2010, 20:31 pm
NO entiendo que intentas hacer y por que usas las llamadas a la API de Windows. los exe's que contienen los recursos en que estan hechos?...

Atentamente,

Juan Manuel Lombana
Medellín - Colombia


Título: Re: Ayuda win32.beginupdateresource(), win32.updateresource(), no funciona.
Publicado por: krosty123 en 6 Noviembre 2010, 04:10 am
Llamo a la api, para poder modificar el recurso de una aplicacion desde otra, es la unica manera que encontre.
Los exes estan hechos en c# ambos.
Saludos