是否有可能伪造 Windows 控制台 api?
我用 C# 编写了一个 ssh 服务器,我认为将 powershell 作为 shell 连接起来会很漂亮。我尝试了两种方法来使其正常工作,但这两种方法都远非完美。这是我尝试过的:
- 启动 powershell.exe 并将其重定向为 std(in/out)。这不 工作良好,因为 powershell.exe 检测到它被重定向,发生变化 这是行为。更重要的是,它期望 stdid 上的输入数据,而不是 命令。所以它使用控制台api来读取命令。
- 在“包装”应用程序中托管 powershell。这样做的好处是 能够为 powershell 提供“控制台”实现(通过 PSHostRawUserInterface)。这效果更好,但您仍然可以调用 命令(主要是真正的控制台应用程序),例如“... | more”,期望 能够使用控制台 api,然后尝试从 包装器进程的控制台。
所以我想做的是用一组函数替换控制台应用程序使用的常规控制台输入/输出函数,这样我就可以处理它们。但这似乎相当激烈,以至于成为一个糟糕的设计理念(我认为)。
现在,我的想法是通过使用 WriteConsoleInput 等本机/Pinvoke 函数发送相关键来操作控制台。我猜想也许可以通过这种方式伪造控制台。但我不知道如何“读取”控制台上发生的情况。
另请记住,它是一项服务,因此最好它不应该生成实际的控制台窗口,尽管可能在 Windows 会话 0 中不会显示并且无关紧要。
I've written a ssh server in c# and I thought it'd be neat to hook up powershell as a shell. I've tried 2 methods to get this to work properly but both are far from perfect. Here's what I've tried:
- Launch powershell.exe and redirect it's std(in/out). This doesn't
work well since powershell.exe detects it is redirected, changes
it's behaviour. What's more, it expects input data on the stdid, not
commands. So it uses the console api to read commands. - Host powershell in a "wrapper" application. This has the advantage of
being able to provide a "console" implementation to powershell (via
the PSHostRawUserInterface). This works better, but you can still invoke
commands (mostly real console applications) like "... | more", that expect
to be able to use the console api, and subsequently try to read from the
console of the wrapper process.
So what I'd like to do is have a set of functions replace the regular console input/output functions that console applications use, so I can handle them. But that seems rather drastic to the point of being a bad design idea (imo).
Right now I am on the idea of manipulating the console by sending the relevant keys with native/Pinvoke functions like WriteConsoleInput. I gather that it might be possible to fake the console that way. But I don't see how I would then "read" what happens on the console.
Also keep in mind, it's a service, so preferably it shouldn't spawn an actual console window, although perhaps in windows session 0 that wouldn't show up and not matter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已获得用于此目的的 PSSession 和 Enter-PSSession CmdLet。使用 Powershell 的 SSH 会做什么而 PSSession 不会做什么?
但如果你想这样做,这里有一个不写任何东西的解决方案: 使用通过 SSH 的 PowerShell
编辑于 2011 年 2 月 11 日
PowerShell 内部提供了另一种无需编写任何内容即可完成此操作的方法(免费供个人使用)。
Host03 示例,也许可以提供基本代码来完成您想做的事情。
You've got PSSession for this purpose and the Enter-PSSession CmdLet. What will your SSH with Powershell do that PSSession is not doing ?
But if you want to do that here is a solution whithout writting anything : Using PowerShell through SSH
Edited 02/11/2011
PowerShell inside provide another way to do it whithout writting anything (free for personal usage).
Host03 sample, can perhaps provide basic code to do what you wat to do.
我按照 JPBlanc 的建议安装了 PowerShellInside,但没有使用很长时间。唯一的联系太有限了,我不喜欢受到限制(特别是如果这种限制是基于利润的,但那是我不应该参与的另一个讨论)。尽管它是原始问题的解决方案,但感觉并不令人满意,因为它没有解决我遇到的编程问题。
然而,我最终确实设法解决了上述问题,实际上是通过在包装器进程中使用 Windows api 调用。因为存在很多陷阱,所以我决定回答我自己的问题,并给其他看到同样问题的人一些指导。基本结构如下:
所有这一切的最终结果是:Windows 控制台 API 的包装应用程序。包装器可以读取/写入重定向的标准输入和标准输出以与世界通信。当然,如果你想变得更奇特,你可以在这里使用任何流(命名管道、tcp/ip 等)。我实现了一些 xterm 控制序列,并设法获得一个完全工作的终端包装器,它应该能够包装任何 Windows 控制台进程,将 xterm 输入转换为目标应用程序控制台输入上的输入,并将应用程序的输出处理为 xterm 控制序列。我什至让鼠标可以工作。现在,将 powershell.exe 作为子进程启动可以解决在 ssh 会话中运行 powershell 的原始问题。 Cmd.exe 也可以工作。如果有人感兴趣,我会在某处发布完整的代码。
I installed PowerShellInside as suggested by JPBlanc, but didn't use it for very long. The one connection thing is just too limiting, and I don't like being limited (especially if that limitation is profit based but thats a whole other discussion i shouldn't get into). And despite being a solution to the original problem, it feels unsatisfactory because it doesn't solve the programming problem I ran into.
However, I did eventually manage to solve said problem, indeed by using the windows api calls in a wrapper process. Because there are quite a few pitfalls, I decided to anwser my own question and give others looking at the same problem some pointers. The basic structure is as follows:
The net result of all of this: a wrapper application for the windows console api. The wrapper can read/write the redirected stdin and stdout to communicate with the world. Ofcourse if you want to get fancy you could use any stream here (named pipe, tcp/ip, etc..). I implemented a few xterm control sequences and managed to get a fully working terminal wrapper that should be capable of wrapping any windows console process, translate the xterm input to input on the target application's console input and process the application's output to xterm control sequences. I even got the mouse to work. Starting powershell.exe as sub process now solves the original problem of running powershell in a ssh session. Cmd.exe also works. If anyone is interrested I'll see about posting the full code somewhere.