473,403 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 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 3150
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.