Ticket #43 (closed defect: fixed)

Opened 3 years ago

Last modified 3 years ago

Hard coded directories in win32 port

Reported by: jUrner(at)arcor.de Owned by: pbi
Priority: minor Milestone:
Component: Scapy Version:
Keywords: Cc:

Description

Just happend to read over scapy and stumbled over some hard coded directories wich should lead into troubles on windows. "TMP" and "system" might be located anywhere and named anyhow. So I would like to suggest some changes.


Near line 176 it should read probably:


if not PCAP:
    #Note testing still
    if WINDOWS:
        #Windows needs TMP and not Temp here on my system
        f = os.popen("!WinDump -V 2> %s" % os.environ!["TMP"])  

Near line 407 the system directory is hard coded to get on the available protocols. There is no easy way to get the system directory on windows, so best is to digg into kernel32 library. Below a version that uses win32all or ctypes (builtin since python 2.5):


def get_windows_system_directory():
    """Returns the windows system directory"""
    
    # win32all version
    def gwsd_win32all():
        import win32api
        return win32api.GetSystemDirectory()
    
    # ctypes ansi version
    def gwsd_ctypes_a():
        from ctypes import windll, sizeof, create_string_buffer, WinError
        kernel32 = windll.kernel32

        nBuffer = kernel32.GetSystemDirectoryA(None, 0)
        if nBuffer:
            p = create_string_buffer(nBuffer)
            if kernel32.GetSystemDirectoryA(p, sizeof(p)):
                return p.value
        raise WinError()
    
    # ctypes unicode version (not used, just in case)
    def gwsd_ctypes_w():
        from ctypes import windll, sizeof, create_unicode_buffer, WinError
        kernel32 = windll.kernel32

        nBuffer = kernel32.GetSystemDirectoryW(None, 0)
        if nBuffer:
            p = create_unicode_buffer(nBuffer)
            if kernel32.GetSystemDirectoryW(p, sizeof(p)):
                return p.value
        raise WinError()
        
    ##
    try:
        return gwsd_win32all()
    except ImportError:
        try:
            return gwsd_ctypes_a()
        except ImportError:
            raise SystemExit('Win32all or ctypes required')

Didn't fiddle along with a ctypes version of WaitForMultipleObjects(), but it should be fairly easy to implement.

regards, Jürgen

Attachments

Change History

Changed 3 years ago by Dirk Loss

  • priority changed from major to minor
  • status changed from new to closed
  • resolution set to fixed

I agree that we should try to avoid hard coded directories and fixed both problems. You'll find the updated code in the scapy-win Mercurial repository.

The hard coded C:\Windows\ path is a problem on Windows 2000 and Windows NT, as the system directory is C:\WINNT on these platforms. I fixed the issue by using the %SystemRoot% environment variable, which should be available on all NT based systems (%TMP% and %TEMP% are based on %SystemRoot% as well).

Currently the Windump call doesn't do any harm, because in the current version of Scapy-win it never will be reached. Scapy-win only works with pcap and it just quits if pcap is not available. So it was a minor issue, nevertheless worth fixing.

After considering your ctypes-based patch or using os.tempnam("", "scapy") I settled on just redirecting the Windump output to NUL: which is the Windows equivalent of /dev/null. If that doesn't work for you, please let me know.

Add/Change #43 (Hard coded directories in win32 port)

Author


E-mail address and user name can be saved in the Preferences.


Change Properties
<Author field>
Action
as closed
Next status will be 'reopened'
 
Note: See TracTickets for help on using tickets.