如何将某些按键转换为其他按键?

发布于 2024-08-07 23:00:15 字数 654 浏览 10 评论 0原文

我有一个关于考试作弊的好主意。我的学校使用非常古老的 IDE(例如 Turbo Pascal、Turbo C++ 和其他 80 年代的 IDE),我想做的是:

  • 在后台启动我的程序

  • 拦截按键,而不是将它们直接发送到屏幕,我想从预先配置的文本文件中读取字符,并将其作为按下的键发送。这样,无论您要写什么,该文件中的文本都会写在屏幕上。

我找到了 Stephen Toub 的关于记录密钥的文章,并且我认为这将成为构建这个“工具”的良好开端。有没有比 SetWindowsHookEx 更好的替代方案来拦截系统中按下的所有按键?该代码是否会被防病毒软件标记为可疑程序?如果是这样,我可以使用其他什么方法来完成此操作而不被防病毒软件标记吗?需要管理员权限吗?

我知道你们中的一些人会说,如果我对学习的兴趣和避免学习的兴趣一样多,我会做得很好,但我想尝试一下。

编辑:我添加了赏金,我对一些捕获击键的技术感兴趣(我对低级挂钩或高级东西不感兴趣 - 基本的就可以了),主要是方法名称和一些文档链接。我还想知道它们是否会被防病毒软件视为恶意软件。

I have a great idea about cheating on exams. My school uses very old IDE's ( think Turbo Pascal, Turbo C++ , and other 80's ones ), and the thing I'd like to do is this :

  • start my program in the background

  • intercept the keypresses, and instead of sending them directly to the screen, I'd like to read a character from a pre-configured text-file, and send that as the pressed key. This way, no matter what you'll write, the text from that file will get written on the screen.

I found Stephen Toub's article about logging keys, and I think it will serve as a good start on building this "tool". Is there a better alternative to intercepting all the keys pressed in the system than SetWindowsHookEx? Will the code be flagged by the antivirus as a suspicious program? If so, is there anything else I can use to accomplish this without being flagged by the antivirus? Will administrator priviledges be required ?

I know some of you guys will say that If I'd put as much interest in learning as I do in avoiding learning, I'd do great, but I'd like to try this out.

EDIT: I've added a bounty, I'm interested in some techniques for capturing keystrokes ( I'm not interested in low-level hooking or advanced stuff - basic ones are fine ), mainly method names and some links to documentation. I'd also like to know if they would appear as malware to an antivirus.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尐偏执 2024-08-14 23:00:15

上帝!前几天我刚刚找到了完美的课程..花了我一段时间才找到原始来源:

http://www.codeproject.com/KB/cs/globalhook.aspx

我单独使用了这个(和 SendKeys)来满足您的需要,您需要使用 Handle=true 来阻止原始关键消息。

SendKeys 有点难看...如果您愿意,您可以寻找替代品...但 SendKeys 会为您工作。

对于 SendKeys,它类似于:

    private void HookManager_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.D){
            e.Handled = true;
            SendKeys.Send("F");
        }
    }

我实际上也使用此方法来确定哪个进程的窗口处于焦点,因此如果您在另一个应用程序上,该应用程序将不执行任何操作:

    /// <summary>
    /// The GetForegroundWindow function returns a handle to the foreground window.
    /// </summary>
    [DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();

    static bool IsProcessFocused(string processname)
    {
        if (processname == null || processname.Length == 0)
        {
            throw new ArgumentNullException("processname");
        }

        Process[] runninProcesses = Process.GetProcessesByName(processname);
        IntPtr activeWindowHandle = GetForegroundWindow();

        foreach (Process process in runninProcesses)
        {
            if (process.MainWindowHandle.Equals(activeWindowHandle))
            {
                return true;
            }
        }

        // Process was not found or didn't had the focus.
        return false;
    }

GOD! I've just found the perfect class for this the other day.. took me a while to find the original source:

http://www.codeproject.com/KB/cs/globalhook.aspx

I used this (and SendKeys) alone exactly for what you need, you'll need to use Handle=true to block the original key message.

SendKeys is a bit ugly... you can look for a replacement if you'd like.. but SendKeys will work for you.

With SendKeys it's something like:

    private void HookManager_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.D){
            e.Handled = true;
            SendKeys.Send("F");
        }
    }

I actually also use this method to determined what prccess' window is in focus, so the app will do nothing if you are on another application:

    /// <summary>
    /// The GetForegroundWindow function returns a handle to the foreground window.
    /// </summary>
    [DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();

    static bool IsProcessFocused(string processname)
    {
        if (processname == null || processname.Length == 0)
        {
            throw new ArgumentNullException("processname");
        }

        Process[] runninProcesses = Process.GetProcessesByName(processname);
        IntPtr activeWindowHandle = GetForegroundWindow();

        foreach (Process process in runninProcesses)
        {
            if (process.MainWindowHandle.Equals(activeWindowHandle))
            {
                return true;
            }
        }

        // Process was not found or didn't had the focus.
        return false;
    }
未央 2024-08-14 23:00:15

SetWindowsHookEx 是最好的选择。有人告诉我,一些防病毒程序会标记它,但我在使用它后创建了一个小应用程序,并使用 AVG 扫描它,没有返回任何病毒警报。

SetWindowsHookEx is the best option. I've been told that some antivirus programs will flag it, but I created a little app once using it and scanned it with AVG which returned no virus alerts.

莫言歌 2024-08-14 23:00:15

我并不是说作弊是好事,但我们理解您的请求。我认为不进入 API 挂钩是不可能的,因为 .Net 包装了 WIN32 API,我认为他们没有包装您想要的方法。

为了完整起见,我们只添加一些细节:

不幸的是,您必须进入 WIN32 API:http://msdn.microsoft.com/en-us/library/ms645530(VS.85).aspx

WM_SYSKEYDOWN 通知可用于获取按键,但它需要监视窗口的句柄,因此需要使用 *GetWindow** API 来获取要监视的窗口的句柄。

尝试 SendInputkeybd_event [http://msdn.microsoft.com/en-us/library/ms646304(VS.85).aspx] 函数来模拟击键。

Google“WIN API Viewer”是一个用于轻松搜索和查看所述 API 的实用程序,也有一个针对 .Net 代码的版本。

这是需要大量试验和错误的事情之一,我希望能为您提供更明确的答案。

Not that I'm saying cheating is good, but we understand your plea. I dont' think it's possible without getting into API hooks, since .Net wraps WIN32 API's I don't think they have wrapped methods for what you want.

For completeness, let's just add some details:

Unfortunately you have to get into the WIN32 API's: http://msdn.microsoft.com/en-us/library/ms645530(VS.85).aspx

The WM_SYSKEYDOWN notification can be used to get the key strokes, but it takes the handle of the window to watch, so it would involve the *GetWindow** API's to get the handle of the window you want to monitor.

Try the SendInput or keybd_event [http://msdn.microsoft.com/en-us/library/ms646304(VS.85).aspx] functions to simulate keystrokes.

Google "WIN API Viewer" for a utility to easily search for and view said API's, there is a version for .Net code too.

It's one of those things that will take a lot of trial and error, and I wish I had a more definitive answer for you.

人间☆小暴躁 2024-08-14 23:00:15

据我所知,最可靠的键盘记录方法是设置系统范围的中断(我认为 INT17h?),但考虑到您的帖子标记为 C#,这是相当低的水平。去搜索相关工具......你会发现很多......

As far as I can say, the most reliable way to keylog is to set up a system wide interrupt (I think INT17h?), but that's pretty low level, considering that your post is tagged C#. Go search for a relevant tool... you'll find many...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文