CreateFile("CONIN $" ..)做什么? [英] What does CreateFile("CONIN$" ..) do?

查看:233
本文介绍了CreateFile("CONIN $" ..)做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在破解 plink 的源代码使其与统一兼容.

I was hacking away the source code for plink to make it compatible with unison.

如果您不知道,统一是一种文件同步工具,它将运行"ssh"命令以连接到远程服务器,但是Windows没有ssh.exe;有一个plink,它非常接近但不够紧密(它的行为不像统一预期的那样),因此人们通常在它周围包装,

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.

问题之一是,统一要求密码提示打印到stderr(但是plink将密码提示打印到stdout,并导致统一混淆),所以我认为,应该足够简单,通过plink的代码来破解我的代码并将其打印到stdout的提示.所以我闯了进去,然后做到了.

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, .....);

我不确定为什么要这样做,但是我不是为Windows设计命令行工具的专家,所以我知道些什么!但是我认为设置输入句柄时缺少某些东西.

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.

我查看了包装工具上方的源代码,我看到了: hconin=CreateFile("CONIN$",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,0,OPEN_EXISTING,0,0)

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!

这是为什么?什么是"CONIN $"?为何与STD_INPUT_HANDLE不同?

Why is this? what is "CONIN$"? and why is it different from the STD_INPUT_HANDLE?

我可以猜测" FILE_SHARE_READOPEN_EXISTING在其中起作用(因为ssh从另一个进程中运行),但是我想了解这里发生的事情,并确保该代码没有任何有害的副作用,安全漏洞或类似的可怕内容!

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!

推荐答案

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 using CONIN$ will allow you to get access to the console despite the redirection. Reference.

这篇关于CreateFile("CONIN $" ..)做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆