Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have created a c# program which gets new loaded process
C#
startWatch.EventArrived += new ventArrivedEventHandler(startWatch_EventArrived);

and doing stuff when eventHandler fire.

afterwards im using:
C#
foreach (var runningProcess in Process.GetProcessesByName(ShortProcessName))

to get runningProcess.MainModule data for the current process and after that
C#
foreach (System.Diagnostics.ProcessModule module in MYPROCESS.Modules)

to get list of child process and modules.

My next wish is to get an output of files which created \ deleted \ renamed \ changed \ whatever by the process i catched.

What I have tried:

I have tried digging the 'Process' constructure and modules but didn't find anything for that. also tried using filewatcher, but also here cannot get you the parent process responsible for file changes. i guess this cannot be done using high level language such as c# but lower.

Unfortunatly im not familiar with lowers. in the end i want a service \ watcher to hook a process real-time and create an output (file) of which file he has been messing with.
Posted
Updated 9-Oct-16 9:50am
v2
Comments
Suvendu Shekhar Giri 9-Oct-16 12:57pm    
Looking at another very simple approach,
if you already the location (folder path) where the new files are being created then you can get all the information regarding file created/changed/deleted.

Or am I missing something obvious here?
Aner Izraeli 9-Oct-16 15:02pm    
think of a malware approach,
you cant know in which folder the malware will create\delete\rename files.
if i would know the folder path, than i can easly use filewatcher. :)
Kornfeld Eliyahu Peter 9-Oct-16 13:05pm    
Do you talking about a kind of history of file access?
Aner Izraeli 9-Oct-16 15:02pm    
no,
im talking about which file created\deleted\renamed right now, by a process.

If I understand the question correctly you could utilize the idea from Listing Used Files[^]

Another option could be to use an utility like handle.exe. See https://technet.microsoft.com/en-us/sysinternals/handle.aspx[^]
 
Share this answer
 
Comments
Aner Izraeli 9-Oct-16 15:47pm    
Thanks! check those two suggestions.
it`s a solution for current handeled files\modules.
im looking for previous handeled file by a process.
for example:
1. a process helloworld.exe loaded.
2. the process creating a file - "testfile.bat"

I want to know that testfile.bat was created by helloworld.exe.

hope im more clear.
Thanks :)
Wendelius 9-Oct-16 23:27pm    
As far as I know, the information, which program created or modified a file, isn't available in Windows after the process has closed the file. The only way is to make observations when the file is open by a process.
I do not think you can tell why a process opened a file, but can tell the files the process currently holds open...
First of all you have to dig into old WIN32 API
NtQuerySystemInformation[^]
This function is not much documented and the help page will not contain the SystemHandleInformation enumerated value (16) for the first parameter, but still will have to use it...
You will have to call it twice, once the last parameter as return value of the needed buffers size, and a second time with NULL at the last parameter to actually retrieve the values...
The result of this function is a list of all handles (of all types) for all processes...You have to enumerate them and check the type and the process owns the handle...
Using NtQueryObject[^] in a loop (using ObjectNameInformation as second parameter), will provide you with the device based name of the file the handle holds...
To find the user friendly name you have to use QueryDosDevice[^] function - it will map device to letter...
Two things:
1. You have to duplicate the handle into your process before using it. Do it using OpenProcess[^] with PROCESS_DUP_HANDLE, and DuplicateHandle[^] after that.
2. NtQueryObject will hang if the handle points to named pipe (bug), so you should first try to run it in a new thread to check it, and only if the thread does not hand go on and use it in the main thread...
---
And...
There are some ready-made solutions out there, with source code:
HOWTO: Enumerate handles - Sysinternals Forums[^]
Examine Information on Windows NT System Level Primitives[^]
 
Share this answer
 
Comments
Wendelius 10-Oct-16 0:11am    
The OP's comment was most likely meant for you also so if you have ideas please comment.

Thanks! check those two suggestions.
it`s a solution for current handeled files\modules.
im looking for previous handeled file by a process.
for example:
1. a process helloworld.exe loaded.
2. the process creating a file - "testfile.bat"

I want to know that testfile.bat was created by helloworld.exe.

hope im more clear.
Thanks :)
Aner Izraeli 10-Oct-16 3:28am    
Shalom Eliyahu, thanks for replying :)
yes, you can link file to his process.
the great process explorer (sysinternal tool) can do it, so it`s possible.
that`s what im looking for, some Command line tool that`s able to hook a process and tell which files have been created by him.
i even buy one if found one.
Kornfeld Eliyahu Peter 10-Oct-16 11:09am    
I think you missed something... All process explorer from sysinternals tells you is that the there is an active handle-to-file, but no mentioning what the purpose of the handle (new file, delete, edit)...
And even that connection is gone, when the handle closed...
After the handle gone, there is no way to tell what process had the file...
Aner Izraeli 10-Oct-16 11:35am    
Hmmm, it`s actually mentioning the puprpose, for example, the table goes like this:
Process name | PID | Operation | Path
someprocess.exe | 123 | CreateFile | c:\tempfolder\newcreatedfile.bat

i wish i could add screenshot...
Kornfeld Eliyahu Peter 10-Oct-16 12:00pm    
Now I see... You told process explorer but meant process monitor...
To do something like that you have to talk to Mark...
But seriously... It is about a very low level access...
It is about to writing a file system driver (virtual one - VxD) and via that driver attach to the chain of the file system events...
I never done that, so can't help you...
http://www.decuslib.com/decus/vmslt00a/nt/filemon.htm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900