472,374 Members | 1,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 software developers and data experts.

Hooking file open

Hi,

My goal is to detect all (or most) file dependencies of a script (i.e.
modules, dlls, data files). Currently, after a script is finished
executing I use sys.modules to determine all module dependencies, and I
use win32process.EnumProcessModules to determine DLL dependencies. This
works reasonably well, however I would still like to detect dependencies
from regular file open calls.

I did a little test by modifying __builtins__.open to point to a custom
function that forwards the call to the original open function and saves
the filename to a list. It seemed to work for simple test cases. Here is
the code:

def open_hook(*args,**kwargs):
r = open_real(*args,**kwargs)
saveFileName(args[0])
return r

open_real = __builtins__.open
__builtins__.open = open_hook

Is this a safe approach? Is there a more efficient way of doing this? I
realize that some dependencies will still fall through my checks,
especially file opens from C extensions, which is fine. I just want to
be able to detect the most common use cases. Any other suggestions are
appreciated.

-Farshid
Nov 1 '06 #1
3 3027
You might consider trapping calls to file() too, which is an alias for
open().

Also, I think I'd do my logging before calling the real function. It depends
how you want to deal with exceptions.

Farshid Lashkari wrote:
Hi,

My goal is to detect all (or most) file dependencies of a script (i.e.
modules, dlls, data files). Currently, after a script is finished
executing I use sys.modules to determine all module dependencies, and I
use win32process.EnumProcessModules to determine DLL dependencies. This
works reasonably well, however I would still like to detect dependencies
from regular file open calls.

I did a little test by modifying __builtins__.open to point to a custom
function that forwards the call to the original open function and saves
the filename to a list. It seemed to work for simple test cases. Here is
the code:

def open_hook(*args,**kwargs):
r = open_real(*args,**kwargs)
saveFileName(args[0])
return r

open_real = __builtins__.open
__builtins__.open = open_hook

Is this a safe approach? Is there a more efficient way of doing this? I
realize that some dependencies will still fall through my checks,
especially file opens from C extensions, which is fine. I just want to
be able to detect the most common use cases. Any other suggestions are
appreciated.

-Farshid
--
Dale Strickland-Clark
Riverhall Systems - www.riverhall.co.uk

Nov 1 '06 #2
Dale Strickland-Clark wrote:
You might consider trapping calls to file() too, which is an alias for
open().
Thanks, I didn't know about that.
Also, I think I'd do my logging before calling the real function. It depends
how you want to deal with exceptions.
I placed the logging after the call to the real function so that files
that don't exist will not be logged. However, I'm already checking for
file existence during the post process of the script running, so I guess
it doesn't really make too much of a difference.

Thanks for the help.

-Farshid
Nov 1 '06 #3
Dale Strickland-Clark wrote:
You might consider trapping calls to file() too, which is an alias for
open().

Also, I think I'd do my logging before calling the real function. It depends
how you want to deal with exceptions.
OP should hook into os.open as well. Plus, I don't think the database
modules (dbm, gdbm, bsddb) use Python open, so might want to hook into
those as well. You have be careful with extension modules; they often
open files their own way.
Carl Banks

Nov 2 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matthew Kelly | last post by:
I have pulled together a VB.net project that hooks the keyboard (Ref. Paul Kimmel's hooking program) and allow the user to send "mouse right clicks" via the SendInpuut function (mouse emulation...
79
by: pinkfloydhomer | last post by:
I want to scan a file byte for byte for occurences of the the four byte pattern 0x00000100. I've tried with this: # start import sys numChars = 0 startCode = 0 count = 0
3
by: Rick Strahl [MVP] | last post by:
I'm working on an app that's using the WebBrowser control. I got the control working fine, hooking to the document object. But I've run into a major issue with hooking the Document events....
0
by: webstuff | last post by:
Hi, I have a C# .NET application with lots of strings added through the visual designer (thus stored in the form's .resx file) specifying the application's company name and product name . ...
1
by: kmslick | last post by:
Hello all. Not sure which group this problem best relates to, so I'm posting to both with a follow up. I started learning C# and .NET last august for a project for my employer. The project...
5
by: Ryan Ginstrom | last post by:
Apropos recent threads about GUI editors, coming from a Win32/WTL C++ background, I actually like the idea of being able to (easily) create GUIs programmatically. But I still see a lot of the...
8
by: pigeonrandle | last post by:
Hi, Has anyone had any experience with hooking messages in other application windows (like SPY++). I want to listen for WM_MOVE messages, but can only seem to find examples of Keyboard and Mouse...
0
by: joemango | last post by:
Hi everyone, I was just reading the following artilce titled: "Spy: A Windows CE API Interceptor" by Dmitri Leman in Dr. Dobbs Journal, September 02, 2003 located at ...
1
by: Sean | last post by:
Here is a code I found that notifies if an event has been generated. I still can't find anything that would actually grab the event and export it a file which is what I am trying to do #include...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.