Jump to content

Paul

Administrators
  • Posts

    1741
  • Joined

  • Last visited

Posts posted by Paul

  1. Here is the code that gets your public IP address of the current computer:

    public string GetPublicIPAddress()
    {
        var request = (HttpWebRequest) WebRequest.Create("http://checkip.dyndns.org");
        request.Method = "GET";
        WebResponse response = null;
        StreamReader reader = null;
        try
        {
            response = request.GetResponse();
            // ReSharper disable AssignNullToNotNullAttribute
            reader = new StreamReader(response.GetResponseStream());
            // ReSharper restore AssignNullToNotNullAttribute
            var result = reader.ReadToEnd();
            result = result.Remove(0, result.IndexOf("Address:", StringComparison.Ordinal) + 9);
            return result.Remove(result.IndexOf("</body>", StringComparison.Ordinal));
        }
        catch
        {
            return "127.0.0.1";
        }
        finally
        {
            if (reader != null)
            {
                reader.Close();
                reader.Dispose();
            }
           if (response != null)
               response.Close();
           request.Abort();
       }
    }
    

    Note: You may need some using statements (try pressing CTRL + . (dot) ) on the red types, or right click -> resolve -> select namespace.

     

    This code gets the IP address using a free online service that returns your IP address (http://checkip.dyndns.org). It does not return the IP address of other monitored computers :). It will always be the IP address of the machine the code executes from.

     

    Good Luck,

    Paul.

  2. External IP is not exposed by the pc monitor api. Btw your code to execute a process was fine but microsoft decided to make a developer's life a pain while working under a windows service. If you can wait 9 hours until I wake up and I am at work I will post a method that returns your public IP address.

  3. While reading your replies made me realize you want a plugin on a computer change the bahavior and execute a process on the client computer / device. This is not supported and it is very dangerous if it would be. What if I share with you a computer that has a script that can download and execute a rootkit on your computer just by clicking on my plugin? Mobile PC Monitor doesn't do any code injection on client devices / computers. Plugins can only execute on the machines they are installed.

  4. Hello Kurt,

     

    Add this class to your project:

    using System;
    using System.Runtime.InteropServices;
    
    namespace Plugin1
    {
        public static class Win32
        {
            [StructLayout(LayoutKind.Sequential)]
            private struct Security_Attributes
            {
                public Int32 Length;
                public IntPtr lpSecurityDescriptor;
                public Boolean bInheritHandle;
            }
    
            private enum Token_Type
            {
                TOKEN_PRIMARY = 1,
                TOKEN_IMPERSONATION = 2
            }
    
            [StructLayout(LayoutKind.Sequential)]
            private struct Startupinfo
            {
                public Int32 cb;
                public String lpReserved;
                public String lpDesktop;
                public String lpTitle;
                public UInt32 dwX;
                public UInt32 dwY;
                public UInt32 dwXSize;
                public UInt32 dwYSize;
                public UInt32 dwXCountChars;
                public UInt32 dwYCountChars;
                public UInt32 dwFillAttribute;
                public UInt32 dwFlags;
                public short wShowWindow;
                public short cbReserved2;
                public IntPtr lpReserved2;
                public IntPtr hStdInput;
                public IntPtr hStdOutput;
                public IntPtr hStdError;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            private struct Process_Information
            {
                public readonly IntPtr hProcess;
                public readonly IntPtr hThread;
                public readonly UInt32 dwProcessId;
                public readonly UInt32 dwThreadId;
            }
    
            private enum Security_Impersonation_Level
            {
                SECURITY_ANONYMOUS = 0,
                SECURITY_IDENTIFICATION = 1,
                SECURITY_IMPERSONATION = 2,
                SECURITY_DELEGATION = 3,
            }
    
            private const UInt32 MAXIMUM_ALLOWED = 0x2000000;
            private const Int32 CREATE_UNICODE_ENVIRONMENT = 0x00000400;
            private const Int32 NORMAL_PRIORITY_CLASS = 0x20;
            private const Int32 CREATE_NEW_CONSOLE = 0x00000010;
    
            [DllImport("kernel32.dll", SetLastError = true)]
            private static extern Boolean CloseHandle(IntPtr hSnapshot);
    
            [DllImport("kernel32.dll")]
            public static extern UInt32 WTSGetActiveConsoleSessionId();
    
            [DllImport("Wtsapi32.dll")]
            private static extern UInt32 WTSQueryUserToken(UInt32 sessionId, ref IntPtr phToken);
    
            [DllImport("advapi32.dll", EntryPoint = "CreateProcessAsUser", SetLastError = true, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            private static extern Boolean CreateProcessAsUser(
                IntPtr hToken,
                String lpApplicationName,
                String lpCommandLine,
                ref Security_Attributes lpProcessAttributes,
                ref Security_Attributes lpThreadAttributes,
                Boolean bInheritHandle,
                Int32 dwCreationFlags,
                IntPtr lpEnvironment,
                String lpCurrentDirectory,
                ref Startupinfo lpStartupInfo,
                out Process_Information lpProcessInformation);
    
            [DllImport("advapi32.dll", EntryPoint = "DuplicateTokenEx")]
            private static extern Boolean DuplicateTokenEx(
                IntPtr existingTokenHandle,
                UInt32 dwDesiredAccess,
                ref Security_Attributes lpThreadAttributes,
                Int32 tokenType,
                Int32 impersonationLevel,
                ref IntPtr duplicateTokenHandle);
    
            [DllImport("userenv.dll", SetLastError = true)]
            private static extern Boolean CreateEnvironmentBlock(
                ref IntPtr lpEnvironment,
                IntPtr hToken,
                Boolean bInherit);
    
            [DllImport("userenv.dll", SetLastError = true)]
            private static extern Boolean DestroyEnvironmentBlock(IntPtr lpEnvironment);
    
            /// <summary>
            /// Creates the process in the interactive desktop with credentials of the logged in user.
            /// </summary>
            public static void CreateProcessAsUser(String commandLine, String workingDirectory = null)
            {
                var dwSessionId = WTSGetActiveConsoleSessionId();
                var hUserToken = IntPtr.Zero;
                WTSQueryUserToken(dwSessionId, ref hUserToken);
    
                var sa = new Security_Attributes();
                sa.Length = Marshal.SizeOf(sa);
    
                var hUserTokenDup = IntPtr.Zero;
                DuplicateTokenEx(
                    hUserToken,
                    (Int32) MAXIMUM_ALLOWED,
                    ref sa,
                    (Int32) Security_Impersonation_Level.SECURITY_IDENTIFICATION,
                    (Int32) Token_Type.TOKEN_PRIMARY,
                    ref hUserTokenDup);
    
    
                if (hUserTokenDup != IntPtr.Zero)
                {
                    var dwCreationFlags = NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE;
    
                    var pEnv = IntPtr.Zero;
                    if (CreateEnvironmentBlock(ref pEnv, hUserTokenDup, true))
                        dwCreationFlags |= CREATE_UNICODE_ENVIRONMENT;
                    else
                        pEnv = IntPtr.Zero;
    
                    // Launch the process in the client's logon session.
                    Process_Information pi;
    
                    var si = new Startupinfo();
                    si.cb = Marshal.SizeOf(si);
                    si.lpDesktop = "winsta0\\default";
    
                    CreateProcessAsUser(hUserTokenDup, // client's access token
                                        null, // file to execute
                                        commandLine, // command line
                                        ref sa, // pointer to process SECURITY_ATTRIBUTES
                                        ref sa, // pointer to thread SECURITY_ATTRIBUTES
                                        false, // handles are not inheritable
                                        dwCreationFlags, // creation flags
                                        pEnv, // pointer to new environment block 
                                        workingDirectory, // name of current directory 
                                        ref si, // pointer to STARTUPINFO structure
                                        out pi // receives information about new process
                        );
                    DestroyEnvironmentBlock(pEnv);
                }
                CloseHandle(hUserTokenDup);
                CloseHandle(hUserToken);
            }
        }
    }
    

    Then in your application delete the page command received with command received (Only use page command received for commands received from a PageItem).

     

    Use this code:

            public override void CommandReceived(int commandId)
            {
                if (commandId == 1)
                    Win32.CreateProcessAsUser(@"C:\Windows\System32\mstsc /v:10.0.8.5", @"C:\Windows\System32");
            }
    

    I didn't test this code on a machine with multiple sessions. I have no idea how it will manifest then.

     

    Let me know if you need further help.

     

    Paul.

  5. Hello Kurt,

     

    Your code is correct however you missed two things:

     

    1. You should use shell execute on your processes to 'unbind' them from the parent process.
    2. Starting from windows vista a windows service cannot touch a user session unless some hardcore impersonation API calls.

    Basically you need to ask yourself the following questions:

    • On which user sessions your service is supposed to run the application?
    • What if no user is logged in?

    Consult these posts:

     

    Let me know if you get stuck again.

     

    Good luck mate.

    Paul.

  6. It could be possible on a future version.

     

     

    Hello Paul,

    I've come to another question: ... when you install a plugin, it comes at the bottom of the page ... No problem here ... But how do I get to open (for example IE to the ip-address of that server)

    So 1. How to make an clickable item or button or whatsoever ...

    And 2. directly open IE to ip-address

     

    Thanks

     

    Kind Regards

     

    Kurt De Jaeger

  7. Yes if you have a predefined bat file with something like this:

    ftp -i -s:"%~f0"
    open 192.168.1.55
    FTP USERNAME
    FTP PASSWORD
    !:--- FTP commands below here ---
    lcd .
    cd  data/
    ascii
    mput "Data\h\*.txt"
    disconnect
    bye
    

    Then you just invoke the bat file.

  8. Hello kurtdejaeger,

     

    Welcome to the world of programming. I understand you are a beginner so I have converted your vb script into a valid PC Monitor plugin in two (visual studio compatible) programming languages so that you can see the differences between them and maybe learn a bit from them.

     

    I hope that you will continue researching programming after you finish your first plugin. Also if you ever need help on creating a plugin please ask here and I will do my best to help you.

     

    You will find a Release folder that contains both of the plugin versions in binary format. Both of them do the same thing.

     

    Also check out API documentation that will help you get started on writing plugins. Click here.

     

    Good Luck,

    Paul.

  9. Hello,

     

    The tricky part with GPU monitoring is that a Windows Service cannot query the status of a GPU Adapter as far as I know. I believe this is one of the main reasons why PC Monitor doesn't currently offer support for GPU monitoring. How many render farms you got? Maybe I can find you a workaround.

  10. Hello,

     

    We currently use a netduino ( an arduino based io board ) which allows you to use .NET code to write logic and a cheap temperature sensors. Estimated costs < 50 USD + some time on the code ( I can help you with the code if you need ).

     

    Or you can use a product that does all that with a web interface ~140 USD. Link: http://www.controlbyweb.com/temperature .

     

    Or you can contact a home automation specialist which can setup an enterprise temperature monitoring system which can alert you via sms or email ( the ones with email cost more ). The price for these vary on the size of the project however for a small server room it can't cost more than 300 USD.

     

    Paul.

  11. By any chance you changed your username and password of the account you made the PC Monitor Service impersonate? Because it's the only possible way. Just make the PC Monitor Service run as SYSTEM as default and make sure SYSTEM has access to the PC Monitor installation directory.

     

    Source.

×
×
  • Create New...