OPENVPN interactiveservice C#

-1

As a side project I gave myself the challenge to create a custom GUI for our department in C#. So far I have managed to connect to the VPN through starting the openvpn.exe process from my app.

The problem I have encountered is that openvpn.exe requests admin rights every single time the process is started, which is a no-go

After some googling I have found the "openVPN interactive service" which is designed to resolve this exact problem. Unfortunately I am not able to figure out how to make it run the openvpn.exe for me. Source: https://community.openvpn.net/openvpn/wiki/OpenVPNInteractiveService

I have managed to call the function CreateFile but when I call the SetNamedPipeHandleState function I get the following error:

Attempted to read or write protected memory. This is often an indication that other memory

I am not able to get past this error...

So far I have tried the following:

  • enable native code debugging
  • Uncheck option "Suppress JIT optimization on module load"
  • setting the debugger to run on a x86 platform

My code:

        private void button7_Click(object sender, EventArgs e)
    {
        try
        {
            UnmanagedFileLoader loader = new UnmanagedFileLoader(("\\\\.\\pipe\\openvpn\\service"));
        }
        catch (Exception er)
        {
            Console.WriteLine(er);
        }
        Console.ReadLine();
    }
class UnmanagedFileLoader
{

    public const short FILE_ATTRIBUTE_NORMAL = 0x80;
    public const short INVALID_HANDLE_VALUE = -1;
    public const uint GENERIC_READ = 0x80000000;
    public const uint GENERIC_WRITE = 0x40000000;
    public const uint FILE_FLAG_OVERLAPPED = 0x40000000;
    public const uint CREATE_NEW = 1;
    public const uint CREATE_ALWAYS = 2;
    public const uint OPEN_EXISTING = 3;
    public const uint dwMode = 0x00000002;

        // Use interop to call the CreateFile function.
        // For more information about CreateFile,
        // see the unmanaged MSDN reference library.
    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    static extern SafeFileHandle CreateFile(string lpFileName, uint dwDesiredAccess,
    uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,
    uint dwFlagsAndAttributes, IntPtr hTemplateFile);

    [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool SetNamedPipeHandleState(SafeFileHandle hNamedPipe, IntPtr lpMode,
    IntPtr lpMaxCollectionCount, IntPtr lpCollectDataTimeout);


    private SafeFileHandle handleValue = null;

    public UnmanagedFileLoader(string Path)
    {
        Load(Path);
    }

    public void Load(string Path)
    {
        if (Path == null || Path.Length == 0)
        {
            throw new ArgumentNullException("Path");
        }

       handleValue = CreateFile(
                Path,
                GENERIC_READ | GENERIC_WRITE ,
                0,
                IntPtr.Zero,
                OPEN_EXISTING,
                FILE_FLAG_OVERLAPPED,
                IntPtr.Zero);

        if (handleValue.IsInvalid)
        {
            Console.Write("pipe_error");
            Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
        }
            IntPtr iptr = (IntPtr)0x00000002;
        if (!SetNamedPipeHandleState(handleValue, iptr, IntPtr.Zero, IntPtr.Zero))
        {

        }
    }

Edit: Fixed link, Added code

c#
pipe
openvpn
asked on Stack Overflow Oct 4, 2020 by Nick Drobec • edited Oct 4, 2020 by Nick Drobec

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0