~ruther/csharp-dll-injector

ref: c2de55b90c314ebe6d3df681a35fae1d81519e70 csharp-dll-injector/examples/csharp-dllexport/DllExportInjector/Program.cs -rw-r--r-- 3.3 KiB
c2de55b9 — František Boháček Add InjectedModule::FreeLibrary 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using DllUtils;
using DllUtils.Attributes;
using DllUtils.Memory;
using DllUtils.Modules;
using DllUtils.Process;

namespace DllExportInjector
{
    class Program
    {

        // To pass string in parameter CustomFunctionParams must be used
        // This will work only with cpp, in csharp unsafe struct is needed
        [CustomFunctionParams(EncodingType.ASCII)]
        public struct MyFunctionParams
        {
            [ParamPosition(0)]
            public string Param;

            [ParamPosition(1)]
            public int Number;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public class AddParams
        {
            [MarshalAs(UnmanagedType.I4)]
            public int First;

            [MarshalAs(UnmanagedType.I4)]
            public int Second;
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to CsharpInjector demo for DllExport library");
            Console.WriteLine("Just a small reminder - you cannot inject to every process using this injector");
            Console.WriteLine("Be sure to inject only to processes that match process configuration you use for this project (x86, x64)");

            Console.Write("Put in name (without .exe) or id of the process you want to inject in: ");
            string read = Console.ReadLine();
            RemoteProcessHandle processHandle;

            if (int.TryParse(read, out int processId))
            {
                processHandle = Injector.GetRemoteProcess(processId);
            }
            else
            {
                processHandle = Injector.GetRemoteProcess(read);
            }

            if (processHandle == null)
            {
                Console.WriteLine("Process not found.");
                Console.Read();
                return;
            }

            string dllPath = "DllExportLibrary.dll";
            if (!File.Exists(dllPath))
            {
                Console.WriteLine("Dll to inject not found.");
            }

            InjectedModule injectedModule = processHandle.Inject(dllPath);
            Console.WriteLine("Module injected");

            Console.WriteLine("Calling Main function to alloc console");
            FunctionResult result = injectedModule.ExecuteFunction("Main");
            int mainResult = result.To<int>();

            Console.WriteLine($"Result from main: {mainResult} (should be 1)");

            Console.WriteLine($"Sleeping for 1 second");
            Thread.Sleep(1000);

            // Execute addition function and get its params
            Console.WriteLine("Calling Addition with params of 10 and 5; Expected result: 15");
            result = injectedModule.ExecuteFunction("Add", new AddParams
            {
                First = 5,
                Second = 10
            });

            int additionResult = result.To<int>();
            Console.WriteLine("Result: " + additionResult);

            injectedModule.ExecuteFunction("MyFunction", new MyFunctionParams
            {
                Param = "Injected string!",
                Number = 1000
            });

            injectedModule.Process.Close();
            Console.ReadLine();
        }
    }
}
Do not follow this link