CreateFile(“CONIN$”..) 的作用是什么?
我正在破解 plink 的源代码来制作它一致兼容。
如果你不知道,unison是一个文件同步工具,它运行一个“ssh”命令来连接到远程服务器,但是Windows没有ssh.exe; 有 plink,它非常接近,但还不够接近(它的行为不像 unison 期望的那样),所以人们通常围绕它制作包装器,喜欢这个。
问题之一是一致期望密码提示打印到stderr(但plink将其打印到stdout,并导致一致混淆),所以我想,好吧,应该足够简单,破解我的plink代码并使其将提示打印到标准输出。 所以我就想办法做到了。
下一个问题:我无法响应提示! 无论我输入什么,都没有任何效果。
获取输入的代码大致是这样的:
hin = GetStdHandle(STD_INPUT_HANDLE);
....
r = ReadFile(hin, .....);
我不知道为什么要这样做,但我不是设计Windows命令行工具的专家,所以我知道什么! 但我认为设置输入句柄时缺少一些东西。
我查看了上述包装工具的源代码,我看到了这个: hconin=CreateFile("CONIN$",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,0,OPEN_EXISTING,0,0)
我尝试了它(只是为了好玩)
hin=CreateFile("CONIN$",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,0,OPEN_EXISTING,0,0);
....
r = ReadFile( hin ...... )
,令人惊讶的是它有效! 我现在可以响应提示了!
为什么是这样? 什么是“CONIN$”? 为什么它与 STD_INPUT_HANDLE
不同?
我可以“猜测” FILE_SHARE_READ
和 OPEN_EXISTING
在这方面发挥了作用(因为 ssh 是从另一个进程中运行的),但我想了解发生了什么在这里,并确保此代码没有一些不需要的副作用或安全漏洞或类似的可怕的东西!
I was hacking away the source code for plink to make it compatible with unison.
If you don't know, unison is a file synchronization tool, it runs an "ssh" command to connect to a remote server, but there's no ssh.exe for windows; there's plink, which is very close but not close enough (it doesn't behave like unison expects it to), so people usually make wrappers around it, like this one.
one of the problems is that unison expects the password prompt to print to stderr (but plink prints it to stdout, and causes unison to be confused), so I thought, well, should be simple enough, hack my thru plink's code and make it print the prompt to stdout. so I hacked my way through and did that.
Next problem: I can't respond to the prompt!! no matter what I type, it has no effect.
the code for getting input is roughly like this:
hin = GetStdHandle(STD_INPUT_HANDLE);
....
r = ReadFile(hin, .....);
I'm not sure why it's done this way, but I'm not an expert in designing command line tools for windows, so what do I know! But I figure something is missing in setting up the input handle.
I looked at the source code for the above wrapper tool and I see this:hconin=CreateFile("CONIN$",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,0,OPEN_EXISTING,0,0)
and I try it (just for the heck of it)
hin=CreateFile("CONIN$",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,0,OPEN_EXISTING,0,0);
....
r = ReadFile( hin ...... )
and surprisingly it works! I can now respond to the prompt!
Why is this? what is "CONIN$"? and why is it different from the STD_INPUT_HANDLE
?
I can sort of "guess" that FILE_SHARE_READ
and OPEN_EXISTING
are playing a role in this (since ssh is being run from within another process), but I want to understand what's going on here, and make sure that this code doesn't have some unwanted side effects or security holes or something scary like that!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CONIN$
是控制台输入设备。 通常,stdin 是一个打开的文件句柄,但如果 stdin 由于某种原因被重定向,那么使用 CONIN$ 将允许您访问控制台,尽管有重定向。 参考。CONIN$
is the console input device. Normally, stdin is an open file handle to this, but if stdin is redirected for some reason, then usingCONIN$
will allow you to get access to the console despite the redirection. Reference.