diff --git a/keyemu.cs b/keyemu.cs new file mode 100644 index 0000000..5b0f50f --- /dev/null +++ b/keyemu.cs @@ -0,0 +1,233 @@ +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; + +public static class KeyEmu { + static string charArr_payload = null; + + public static void Main(String[] args){ + parseArgs(); + } + + static void parseArgs() { + String[] args = Environment.GetCommandLineArgs(); + for (int i = 1; i < args.Length - 1; i = i + 2) { + switch (args[i].ToLower()) { + case "-text": + if (args[i + 1].Length > 0){ + charArr_payload = args[i + 1]; + foreach (char c in charArr_payload){ + string primary_charset_data = "abcdefghijklmnopqrstuvwxyz1234567890`-=[]\\;',./"; + List primary_charset_list = new List(); + primary_charset_list.AddRange(primary_charset_data); + if (primary_charset_list.Contains(c)){ + SendFirstKey(c); + }else{ + SendSecondKey(c); + } + } + } + Environment.Exit(2); + break; + case "-key": + if (args[i + 1] == "RETURN"){ + SendScanCode(0x0D); + } + Environment.Exit(2); + break; + default: + Console.WriteLine("Wrong arguments"); + Environment.Exit(4); + break; + } + } + } + + public enum InputType : uint { + INPUT_MOUSE = 0, + INPUT_KEYBOARD = 1, + INPUT_HARDWARE = 3 + } + + [Flags] + internal enum KEYEVENTF : uint + { + KEYDOWN = 0x0, + EXTENDEDKEY = 0x0001, + KEYUP = 0x0002, + SCANCODE = 0x0008, + UNICODE = 0x0004 + } + + [Flags] + internal enum MOUSEEVENTF : uint + { + ABSOLUTE = 0x8000, + HWHEEL = 0x01000, + MOVE = 0x0001, + MOVE_NOCOALESCE = 0x2000, + LEFTDOWN = 0x0002, + LEFTUP = 0x0004, + RIGHTDOWN = 0x0008, + RIGHTUP = 0x0010, + MIDDLEDOWN = 0x0020, + MIDDLEUP = 0x0040, + VIRTUALDESK = 0x4000, + WHEEL = 0x0800, + XDOWN = 0x0080, + XUP = 0x0100 + } + + // Master Input structure + [StructLayout(LayoutKind.Sequential)] + public struct lpInput { + internal InputType type; + internal InputUnion Data; + internal static int Size { get { return Marshal.SizeOf(typeof(lpInput)); } } + } + + // Union structure + [StructLayout(LayoutKind.Explicit)] + internal struct InputUnion { + [FieldOffset(0)] + internal MOUSEINPUT mi; + [FieldOffset(0)] + internal KEYBDINPUT ki; + [FieldOffset(0)] + internal HARDWAREINPUT hi; + } + + // Input Types + [StructLayout(LayoutKind.Sequential)] + internal struct MOUSEINPUT + { + internal int dx; + internal int dy; + internal int mouseData; + internal MOUSEEVENTF dwFlags; + internal uint time; + internal UIntPtr dwExtraInfo; + } + + [StructLayout(LayoutKind.Sequential)] + internal struct KEYBDINPUT + { + internal short wVk; + internal short wScan; + internal KEYEVENTF dwFlags; + internal int time; + internal UIntPtr dwExtraInfo; + } + + [StructLayout(LayoutKind.Sequential)] + internal struct HARDWAREINPUT + { + internal int uMsg; + internal short wParamL; + internal short wParamH; + } + + private class unmanaged { + [DllImport("user32.dll", SetLastError = true)] + internal static extern uint SendInput ( + uint cInputs, + [MarshalAs(UnmanagedType.LPArray)] + lpInput[] inputs, + int cbSize + ); + + [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + public static extern short VkKeyScan(char ch); + } + + internal static short VkKeyScan(char ch) { + return unmanaged.VkKeyScan(ch); + } + + internal static uint SendInput(uint cInputs, lpInput[] inputs, int cbSize) { + return unmanaged.SendInput(cInputs, inputs, cbSize); + } + + public static void SendScanCode(short scanCode) { + lpInput[] KeyInputs = new lpInput[1]; + lpInput KeyInput = new lpInput(); + // Generic Keyboard Event + KeyInput.type = InputType.INPUT_KEYBOARD; + KeyInput.Data.ki.wScan = 0; + KeyInput.Data.ki.time = 0; + KeyInput.Data.ki.dwExtraInfo = UIntPtr.Zero; + + + // Push the correct key + KeyInput.Data.ki.wVk = scanCode; + KeyInput.Data.ki.dwFlags = KEYEVENTF.KEYDOWN; + KeyInputs[0] = KeyInput; + SendInput(1, KeyInputs, lpInput.Size); + + // Release the key + KeyInput.Data.ki.dwFlags = KEYEVENTF.KEYUP; + KeyInputs[0] = KeyInput; + SendInput(1, KeyInputs, lpInput.Size); + + return; + } + + public static void SendFirstKey(char ch) { + lpInput[] KeyInputs = new lpInput[1]; + lpInput KeyInput = new lpInput(); + // Generic Keyboard Event + KeyInput.type = InputType.INPUT_KEYBOARD; + KeyInput.Data.ki.wScan = 0; + KeyInput.Data.ki.time = 0; + KeyInput.Data.ki.dwExtraInfo = UIntPtr.Zero; + + // Push the correct key + KeyInput.Data.ki.wVk = VkKeyScan(ch); + KeyInput.Data.ki.dwFlags = KEYEVENTF.KEYDOWN; + KeyInputs[0] = KeyInput; + SendInput(1, KeyInputs, lpInput.Size); + + // Release the key + KeyInput.Data.ki.dwFlags = KEYEVENTF.KEYUP; + KeyInputs[0] = KeyInput; + SendInput(1, KeyInputs, lpInput.Size); + + return; + } + + public static void SendSecondKey(char ch) { + lpInput[] KeyInputs = new lpInput[1]; + lpInput KeyInput = new lpInput(); + // Generic Keyboard Event + KeyInput.type = InputType.INPUT_KEYBOARD; + KeyInput.Data.ki.wScan = 0; + KeyInput.Data.ki.time = 0; + KeyInput.Data.ki.dwExtraInfo = UIntPtr.Zero; + + //Shift Down + KeyInput.Data.ki.wVk = 0x10; + KeyInput.Data.ki.dwFlags = KEYEVENTF.KEYDOWN; + KeyInputs[0] = KeyInput; + SendInput(1, KeyInputs, lpInput.Size); + + // Push the correct key + KeyInput.Data.ki.wVk = VkKeyScan(ch); + KeyInput.Data.ki.dwFlags = KEYEVENTF.KEYDOWN; + KeyInputs[0] = KeyInput; + SendInput(1, KeyInputs, lpInput.Size); + + // Release the key + KeyInput.Data.ki.wVk = 0x10; + KeyInput.Data.ki.dwFlags = KEYEVENTF.KEYUP; + KeyInputs[0] = KeyInput; + SendInput(1, KeyInputs, lpInput.Size); + + // Release the key + KeyInput.Data.ki.wVk = VkKeyScan(ch); + KeyInput.Data.ki.dwFlags = KEYEVENTF.KEYUP; + KeyInputs[0] = KeyInput; + SendInput(1, KeyInputs, lpInput.Size); + + return; + } +}