如何取得文件路径

robinwjb 2007-05-25 04:21:44
取得其他应用程序正打开着的文件的路径。
比如,现在运行着Word和notepad,分别打开着a.doc和b.txt,那么得到a.doc和b.txt。
我已经试过下面2种方法,可以办到一部分,但都有缺陷。
第一是读取目标进程PEB中的命令行字符串以获取1.txt这个文件名,第二是通过枚举目标进程中的内核对象(句柄)来找到此进程打开的文件。
第一个方法,通过打开1.txt而启动记事本,继而在这个记事本中重新打开2.txt,这时候记事本打开的文件是2.txt,但是PEB中的命令行仍是1.txt。
第二个方法,很多种情况下,比如notepad打开一个文件之后,将数据读到内存,然后就关闭文件,释放句柄。这样我就无法通过句柄得到现在打开的文件了。

请高手指教,分无所谓。
...全文
1231 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
rsp19801226 2007-08-22
  • 打赏
  • 举报
回复
先记下
linuxpgy 2007-08-21
  • 打赏
  • 举报
回复
但是正如我所说:这种方法只适合于,有一个文件句柄存在的情况。
而例如记事本当前在编辑一个叫1.txt的文件。但这时系统中并没有这个文件的句柄。
你可以随意删掉1.txt。因为这个文件只存在于内存。记事本打开它读入内存后就将句柄关掉了。

------------------------------------------------------------------------
呵呵,这个问题有意思。 所以说要得到打开的路径,必须在他打开的那一瞬间得到……

一个工具filemon可以监控系统对文件操作 .

或者你可以试试用户层的APIHook : http://waxb.blog.com.cn/archives/2007/APIHook4.shtml

systemthink 2007-08-20
  • 打赏
  • 举报
回复
進來看看
Yofoo 2007-06-22
  • 打赏
  • 举报
回复
你的思路有问题
取得其他应用程序正打开着的文件的路径。

正打开着的: 有几种情况
1.用户操作菜单打开文件
2.程序自己需要的数据而打开的文件

按你说的应该是指第一种情况, 这对于不同的软件肯定就不一样了
根本就不太现实
龙凤呈祥焱 2007-06-20
  • 打赏
  • 举报
回复
郁闷了.
写驱动你觉得杀鸡用牛刀.
查询句柄你觉得效率低.
不知道你想要什么好方法??

直接进行API HOOK.如何??
robinwjb 2007-06-20
  • 打赏
  • 举报
回复
Monitor any file by driver level,
杀鸡用牛刀了。。。

or query kernel handles by timer.
如果次数频繁,那么性能太差。
次数少,则不是很准确。

自己再顶。
taianmonkey 2007-06-20
  • 打赏
  • 举报
回复
要是通过IFS驱动来处理肯定是可以的,但是有点杀鸡用了宰牛刀的感觉
yjgx007 2007-05-26
  • 打赏
  • 举报
回复
Monitor any file by driver level, or query kernel handles by timer.
robinwjb 2007-05-25
  • 打赏
  • 举报
回复
我最近一周3个问题都没人回答。。。
robinwjb 2007-05-25
  • 打赏
  • 举报
回复
快来人啊
robinwjb 2007-05-25
  • 打赏
  • 举报
回复
TO:yjgx007

HackCleaner是你的SW吧。呵呵。
再次谢谢你的回复。不过关于NtQueryInformationFile这个方法我已经完全实现:
包括waitable thread,得到volume name等等。

但是正如我所说:这种方法只适合于,有一个文件句柄存在的情况。
而例如记事本当前在编辑一个叫1.txt的文件。但这时系统中并没有这个文件的句柄。
你可以随意删掉1.txt。因为这个文件只存在于内存。记事本打开它读入内存后就将句柄关掉了。

还有其他方法吗?望不吝赐教。。。
yjgx007 2007-05-25
  • 打赏
  • 举报
回复
Further, you get the path associated with the file handle using the Native API - NtQueryInformationFile(..., FileNameInformation), before doing this, you should first duplicate a handle from the file handle using DuplicateHandle API, because the file handle is in different process space.
When you use NtQueryInformationFile to query file name, However, you should first create a waitable thread, the working is in the thread.
Finally, you get the the file's path name, an important question is what the file path doesn't contain volume information, so you have to further match volume name with the file path, Generally, the volume name is from A ~ Z, then try to CreateFile(GENERIC_READ, ...), if failed here, you shoule get the GetLastError that will be 32 means the file is being used by other process, so the full path name should be what you want to get.

About above mentioned, if you are not clear, search it in Google.
robinwjb 2007-05-25
  • 打赏
  • 举报
回复
Searching the Native API - NtQuerySystemInformation on Google, or refers to <<Undocmented Native API>>.
Then try to get all system handles(i.e, Undocumented struct - SystemHandleInfo), then you should filter all handlers to retain the file handlers..., In the struct SystemHandleInfo, each handle is assoicated with certain process.

谢谢,不过我所说的第二个方法就是这种方法。

再重复一下这种方法的缺陷:
很多种情况下,比如notepad打开一个文件之后,将数据读到内存,然后就关闭文件,释放句柄。这样我就无法通过句柄得到现在打开的文件了。
除了记事本,excel打开只读文件时也是同样。
或者EM,UE打开文本文件也是同样。
yjgx007 2007-05-25
  • 打赏
  • 举报
回复
Searching the Native API - NtQuerySystemInformation on Google, or refers to <<Undocmented Native API>>.
Then try to get all system handles(i.e, Undocumented struct - SystemHandleInfo), then you should filter all handlers to retain the file handlers..., In the struct SystemHandleInfo, each handle is assoicated with certain process.
robinwjb 2007-05-25
  • 打赏
  • 举报
回复
顶下,无人知道?
robinwjb 2007-05-25
  • 打赏
  • 举报
回复
除了记事本,excel打开只读文件时也是同样。
或者EM,UE打开文本文件也是同样。
wangk 2007-05-25
  • 打赏
  • 举报
回复
记事本比较特殊,大部分文档类型程序不会立即把文件关闭。
要不试试用API钩子?钩GetOpenFileName一下函数?

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧