Coder Social home page Coder Social logo

pywintrace's Introduction

ETW (Event Tracing for Windows)

ETW is a tracing facility that allows a user to log events to a file or buffer. An overview of ETW can be found here. The basic architecture includes an Provider, Controller, and a Consumer. The controller defines and controls a capture session. This includes what providers are in the as well as starting and stopping the session. The provider, specified using a GUID (Globally Unique Identifier), logs events to a series of buffers. The Consumer receives messages either from a buffer or a file and processes them in chronological order.

This module is an entirely Python-based ctypes wrapper around the Win32 APIs necessary for for controlling ETW sessions and processing message data. The module is very flexible and can set pre or post capture filters.

Usage

To use this module import etw and create an instance of the ETW class by passing in a list of ProviderInfo instances for the provider(s) you wish to capture data from. To process data returned from ETW you will need to specify a callback.


import etw


def some_func():
    # define capture provider info
    providers = [etw.ProviderInfo('Some Provider', etw.GUID("{11111111-1111-1111-1111-111111111111}"))]

    # create instance of ETW and start capture
    with etw.ETW(providers=providers, event_callback=etw.on_event_callback):
        # run capture
        etw.run('etw')

Below is an example using the module to perform a capture using a custom callback.


import time
import etw


def some_func():
    # define capture provider info
    providers = [etw.ProviderInfo('Some Provider', etw.GUID("{11111111-1111-1111-1111-111111111111}"))]
    # create instance of ETW class
    job = etw.ETW(providers=providers, event_callback=lambda x: print(x))
    # start capture
    job.start()

    # wait some time
    time.sleep(5)

    # stop capture
    job.stop()

Subclassing is another handy way to define ETW capture classes.


import time
import etw


class MyETW(etw.ETW):

    def __init__(self, event_callback):
        # define capture provider info
        providers = [etw.ProviderInfo('Some Provider', etw.GUID("{11111111-1111-1111-1111-111111111111}"))]
        super().__init__(providers=providers, event_callback=event_callback)

    def start(self):
        # do pre-capture setup
        self.do_capture_setup()
        super().start()

    def stop(self):
        super().stop()
        # do post-capture teardown
        self.do_capture_teardown()

    def do_capture_setup(self):
        # do whatever setup for capture here
        pass

    def do_capture_teardown(self):
        # do whatever for capture teardown here
        pass


def my_capture():
    # instantiate class
    capture = MyETW(lambda x: print(x))
    # start capture
    capture.start()
    # wait some time to capture data
    time.sleep(5)
    # stop capture
    capture.stop()

For more examples see examples.

pywintrace's People

Contributors

abergl avatar piggum avatar tbeadle avatar yazgoo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pywintrace's Issues

No event data received problem

I am using this module to capture the info of the dns server.At the beginning,everything is ok and I can capture the log,but when I stop the process once,I can't capture the info of this provider unless I reboot the windows,but the other provider can capture as usual.When I use it in product,I can't reboot system at any time,so how could I solve the problem?The following is my code.Thanks!

import json


def some_func(name, guid):
    # define capture provider info "{11111111-1111-1111-1111-111111111111}"
    providers = [etw.ProviderInfo(name, etw.GUID("{"+guid+"}"))]
    # create instance of ETW class
    job = etw.ETW(providers=providers, event_callback=lambda x: print(str(x).replace("'","\"")))

    # start capture
    job.start()

    # wait some time
    #time.sleep(5)

    while True:
        url = "http://127.0.0.1:8093/query"
        d = [
            {
                "Provider": guid
            }
        ]
        try:
            r = requests.post(url, json.dumps(d))
            response = r.text

            if response == "no":
                # stop capture
                job.stop()
                break
            time.sleep(10)
        except Exception as e:
            print("dead")
            job.stop()
            break


if __name__ == '__main__':
    name = sys.argv[1]
    guid = sys.argv[2]
    some_func(name, guid)

Exception in ProcessTrace function

Hey,
First of all, it's a great library. It's helped me a lot.
I ported the library to python 2.7, and maybe this is part of the problem (but it does not look like this is the problem).
The library is working good when its standalone code, but when I try to use it within an exists project there is problems.
In some cases there is an exception on:

if tdh.ERROR_SUCCESS != et.ProcessTrace(ct.byref(trace_handle), 1, None, None):
within _run function inside etw.py

according to MSDN this function not supposed to raise exceptions so I think it's from ctypes.
The exception is not constant but the most common exceptions are "access violation writing 0xE09269EA" (or another address) or "access violation reading 0x00000000."

OSError: [WinError 1450] Insufficient system resources exist to complete the requested service - when running PyWinTrace

I'm trying to run a Python script based on your Pywintrace code (firstly, thank you so much for creating this awesome solution!)

Initially I was able to run the script ~5 times without a problem, but since then I keep getting the following error:
OSError: [WinError 1450] Insufficient system resources exist to complete the requested service.

I don't know why it stopped working because nothing obvious changed about my environment. Also, I've tried to run the script on two separate machines (Windows 10) as well as on a Windows 7 VM, but I always get the same error.

I found a potential solution for the 1450 error on two separate questions on StackOverflow, followed the steps for all machines, however, the problem persisted. Those steps included changing the registry entries "PoolUsageMax" and "PagedPoolSize" under memory management.

(The steps followed are from the answers on the following two questions:
https://stackoverflow.com/questions/53752487/oserror-winerror-1450-insufficient-system-resources-exist-to-complete-the-req

https://stackoverflow.com/questions/19845580/system-error-1450-has-occurred-insufficient-system-resources-exist-to-complete)

I realise this question is not specific to your product, but I can't find much else out there so I was wondering if you might have had a similar experience in the past and/or have any clues as to how I can fix this?

Thank you in advance.

Best,
Ana

request add a consumer demo

Thanks for you for your wonderful project!

According to "The OpenTrace / ProcessTrace / CloseTrace functions allow developers to consume events from either a real-time trace session or from an ETL log file", I try to call the consumer to parse a
windows .etl log file (e.g. ShutdownCKCL.etl ). But I encountered a trouble when I try to call the consumer, because I haven't find an right way about using class EventConsumer in details.

Could you provide a demo as a reference for me?

module 'etw' has no attribute 'ProviderInfo'.

When I ran the example code, it returns an attribute error:
providers = [etw.ProviderInfo('Some Provider', etw.GUID("{94335EB3-79EA-44D5-8EA9-306F49B3A041}"))] AttributeError: module 'etw' has no attribute 'ProviderInfo'.

What's going on?

Major slowdown when tracing CLR provider live, not seen with perfview

When I run the code below to trace the GC keyword from the CLR Runtime provider (https://docs.microsoft.com/en-us/dotnet/framework/performance/clr-etw-providers), I see a large slowdown in all managed code on my machine - eg. running a build of a C# project in VS goes from 1m30s to >6 minutes. However, when I run perfview /GCCollectOnly, which based on its source code appears to enable a larger set of keywords on this and other providers, and uses the same level (Informational), I don't see this slowdown.

I guess it might be because perfview is tracing direct to a file rather than using a callback, but I'm surprised that would make such a huge difference to the instrumented code. I thought the whole point of ETW was to be minimally intrusive. I don't see any additional filtering available on this provider besides the keywords and level.

import etw

providers = [
    etw.ProviderInfo('CLR', etw.GUID('{E13C0D23-CCBC-4E12-931B-D9CC2EEE27E4}'), any_keywords=[1]),
]

with etw.ETW(providers=providers, event_callback=lambda evt: ()):
    etw.run('etw')

Perfview has

            if (GCCollectOnly)
            {
                // TODO this logic is cloned.  We need it in only one place.  If you update it do the other location as well
                // The process events are so we get process names.  The ImageLoad events are so that we get version information about the DLLs 
                KernelEvents = KernelTraceEventParser.Keywords.Process | KernelTraceEventParser.Keywords.ImageLoad;
                ClrEvents = ClrTraceEventParser.Keywords.GC | ClrTraceEventParser.Keywords.Exception;
                ClrEventLevel = TraceEventLevel.Informational;
                TplEvents = TplEtwProviderTraceEventParser.Keywords.None;
                NoRundown = true;
                CommandProcessor.s_UserModeSessionName = "PerfViewGCSession";
                DataFile = "PerfViewGCCollectOnly.etl";
            }

make consumer thread a daemon thread

In case the main thread crash because of bad coding from the user (or something unforeseen happens) , the interpreter is not stopped because the consumer thread is still running.
To avoid that, one could make the consumer thread a daemon thread which wouldn't block the program from exiting in case of an error :

in etw.py (line 302):

self.process_thread = threading.Thread(target=self._run, args=(self.trace_handle, self.end_capture))
self.process_thread.daemon = True
self.process_thread.start()

Warning: ETW sessions are expensive

This library does not follow best practices for ETW sessions, resulting in excessive memory usage. In addition, this library can cause substantial problems for the target system but does not appear to have any warnings that would alert users to the possible problems that might be caused by this library.

References:

Issues:

  • Even not counting memory usage, ETW sessions are a limited resource (usually a system-wide limit of 64 sessions). See the "IMPORTANT" note at the top of the StartTrace documentation.
  • The minimum memory usage of a normal ETW session is BufferSize x 2 x CpuCount.
  • Since your default BufferSize is 1MB, the minimum memory usage of the pywintrace session is 1MB x 2 x CpuCount, and this is NON-PAGED memory. Even on a small 8-core system, that's 16MB of non-paged memory. Since it's non-paged, the memory must be allocated even if it is never used.
  • If the python program exits without closing the session (e.g. if the program crashes or is killed), the session will continue running and will continue using CPU, memory, and (since the events get written to disk if the consumer gets behind) disk space.

Users of this library need to be made aware of these issues and given guidance on how to minimize their impact on the target system.

In addition, it would be very helpful to improve the defaults to use less memory, and to provide additional ways for users to avoid using too much memory.

  • Lower the default buffer size. This library defaults to 1024KB buffers, but this is almost always too large and is almost always very wasteful of non-paged memory. As described in the documentation for EVENT_TRACE_PROPERTIES, most trace sessions should use a buffer size of 64KB or less. There is almost never any reason to use buffer sizes larger than 128KB. (If your user needs more memory allocated, larger memory allocations should occur via a larger MaximumBuffers count, not via a larger BufferSize.)
  • Provide an option (maybe the default?) for setting the EVENT_TRACE_NO_PER_PROCESSOR_BUFFERING flag. This flag is appropriate for low-event-rate sessions (less than a few hundred events per second) and significantly reduces the memory usage (you don't need separate buffers for each CPU).
  • Provide an option for setting the EVENT_TRACE_USE_PAGED_MEMORY flag. By default, ETW sessions use non-paged memory so that they can receive events from the kernel. Non-paged memory means you're reserving the memory even if it is never used. If the session user knows they won't receive any events from the kernel, they should set EVENT_TRACE_USE_PAGED_MEMORY so that the memory can be paged-out.
  • For reliability, your session should probably just always set EVENT_TRACE_INDEPENDENT_SESSION_MODE. This just opts-in to a better ETW behavior that should have been the default.

Need Event tracing USB4 device and HUB by using ETW logging or Pywintrace.

Hello,
Im working on USB4 device event tracing by using ETW logging and pywintrace.
but except USB4 I have captured and successfully traced by pywintrace.

Here I will provide for USB4 provider name and guid.
Microsoft-Windows-USB-USB4DeviceRouter-EventLogs {D07E8C3F-78FB-4C22-B77C-2203D00BFDF3}

how to update this issue in source code.

Fetching additional data

Hi,

Never mind, it was my mistake.

I'm pretty new to ETW and trying to log network traffic from a specific process.

I was able to get the network activity using the "Microsoft-Windows-Winsock-AFD" provider. however, I'm having trouble getting additional data such as remote address and the number of bytes sent/received.

Any help would be appreciated. I know that this is not technically an issue, but the official email didn't work.

Thanks.

Connot capture all events on windows kernel trace

I'm using Provider Windows Kernel Trace {9E814AAD-3204-11D2-9A82-006008A86939} to capture the behavior of a PE, whose function is search some files and move them to other directory. But not all events can be captured. Is it because of the value of ring_buf_size? I have tried to increase the ring_buf_size to 10240, but the problem is still unsolved.

multiple providers

hello,

trying to use multiple providers using add_provider, second provider added does not seem to work as no events from second provider

using:

providers = [etw.ProviderInfo('Some Provider', etw.GUID("{11111111-1111-1111-1111-111111111111}"))]
provider2 = [etw.ProviderInfo('Some Provider2', etw.GUID("{11111111-1111-1111-1111-111111111112}"))]

job = etw.ETW(providers=providers, event_callback=something)

job.add_provider(provider2)

thanks!

bad format UserData

I've try to capture provider Active Directory Domain Service: Core's events. The UserData is bad formatted like below:
"Description": "닼ᲃ쁏ᇑﲊ쀀쉏ᐙ瀪맔檘ᇒႷ쀀륏ꊘ"

I've also use tracerpt tool to process the events and save it into a CSV file, and it convert properly.

Event Name,       Type,     Event ID,    Version,    Channel,      Level,     Opcode,       Task,            Keyword,        PID,        TID,     Processor Number,  Instance ID,   Parent Instance ID,                              Activity ID,                      Related Activity ID,           Clock-Time, Kernel(ms),   User(ms), User Data
DsDirSearch,      Start,            0,          4,          0,          0,          1,          0, 0x0000000000000000, 0x000001D8, 0x000004EC,                    0,             ,                     ,   {00000000-0000-0000-0000-000000000000},                                         ,   131644278080567528,          0,          0, "DS", 4, 6, 1141178432, 27086592, "127.0.0.1:54449", "base", "CN=***CommonName***,CN=***CommonName*** ,CN=***CommonName*** ,CN=***CommonName*** ,CN=***CommonName*** ,CN=***CommonName*** ,DC=***CommonName*** ,DC=***CommonName*** ", " (objectClass=*) ", "options", "", "", "",  0x0000160000000000

The System is Windows 2008 R2 Server, Active Directory Domain Server is enabled.

The issue also occour on windows 10 with provider Active Directory Domain Service: SAM

Windows 10 Microsoft-Windows-Kernel-Process Parse Error

When parsing events from Microsoft-Windows-Kernel-Process ({22FB2CD6-0E7B-422B-A0C7-2FAD1FD0E716}) on Windows 10 it looks like PROCESSSTART events lead to the exception below.

I believe it's process start events causing this because just adding a 'try catch pass' block around the exception lets me view process start events, whereas without that error handling I do not see any start events.

Python 3.6.

ValueError: invalid literal for int() with base 10: b''
Traceback (most recent call last):
File "_ctypes/callbacks.c", line 234, in 'calling callback function'
File "C:\Users\Colin\Documents\win_events\pywinevent\etw\etw.py", line 662, in _processEvent
out.update(self._unpackSimpleType(record, info, property_array[i]))
File "C:\Users\Colin\Documents\win_events\pywinevent\etw\etw.py", line 549, in _unpackSimpleType
data = tdh.TDH_CONVERTER_LOOKUPout_type

This is the code I used to trigger the bug:

import time
import etw

def handle_res(res):
    n, event = res
    if event['Task Name'] == 'PROCESSSTART':
        print(res)
    if event['Task Name'] == 'PROCESSSTOP':
        print(res)

if __name__ == '__main__':
    guid = {'Microsoft-Windows-Kernel-Process': etw.GUID("{22FB2CD6-0E7B-422B-A0C7-2FAD1FD0E716}")}
    job = etw.ETW(guid)
    job.start(handle_res)

    time.sleep(15)
    job.stop()

how to find the process

thanks:
I am new to the etw with 3 questions.

  1. how to log the result with windows10? the result are print on the command and I wonder how can I log it
  2. the description was print in byte? how could convert it to utf-8 because there is nothing I can read
  3. When I monitor the dns request, how could I defind which process launch the dns Or is the anyother way to locate which dns launched by the process

Failed to get data field for AssemblyFlags, incrementing by reported size

On a Win10 x64 box, in an Admin cmd window I am running the python script from the article https://www.countercept.com/blog/detecting-malicious-use-of-net-part-1/ (https://gist.github.com/countercept/7765ba05ad00255bcf6a4a26d7647f6e). I am running it with the --high-risk-only flag. It gets a lot of "Failed to get data field for AssemblyFlags, incrementing by reported size" error messages.

What would cause this? Is this normal or a bug? How can I fix it or suppress these messages?

Importing etw causes log level to be set

I am using the 0.2.0 package from PyPi.

When I 'import etw' logging is configured (in common.py), this caused a problem because my own call to logging.basicConfig happened later and most of my logging disappears.

I have modified this issue based on my later findings, as basicConfig is called when the first log call is made I suggest removing the call to logging.basicConfig in etw/common.py but am happy to hear alternative suggestions.

Do you know how to simulate win10 `perfmon.exe` to monitor the disk operation of a process?

image

I don't know how to get the information of the field "File".
Do you have any good Suggestions?

Aim

The 3dsmax process did not officially display a progress bar when loading the large model.
So I needed to implement an external progress bar myself.

  1. Get the open files path to get the total file size;
  2. Get Read file IO speed/s.

To implement the progress bar.

Can use EWT implement it?

Publishing to PyPI

Are there any plans to put this project on PyPI? I love the project and it would be awesome to be able to run

pip install pywintrace

It seems like most of the work has been done already (i.e. setup.py exists) but it is just a matter of building and pushing to PyPI. I am willing to help out in whatever way possible!

Callback functions stop returning anything

The provider below was returning stuff a few hours ago when I was testing it. But it stopped returing anything after I rerun it a few hours later, any thoughts?

def some_func():

    providers = [etw.ProviderInfo('Microsoft-Windows-Win32k', etw.GUID("{8C416C79-D49B-4F01-A467-E56D3AA8234C}"))]

    job = etw.ETW( providers=providers, event_callback=lambda x: print(x))

    job.start()

    time.sleep(10)

    job.stop()

how to close the warning?

hello,author. I have a question. I have installed the package and Run successfully, but there are so many warnings in the pycharm console.like this.
image
I have saved the information of the event_callback to the .txt, but there are still warnings in the pycharm console.
image
Do you know how to close this? Thanks in advance.

Nothing happens when I run the example.

image

I don't know much about ETW,
so providers = [etw.ProviderInfo('Some Provider', etw.GUID("{11111111-1111-1111-11111111}"))] how do I find this GUID ({11111111-1111-1111-11111111})?

No events captured

I am testing the module and when I fire up the browser and connect to some website, nothing seems to be logged. When I enable the EWT from the event log, event are logged in the event log.
nothing-logged

add provider on the fly

Hi guys,

I'm trying to add providers on the fly after the session is started. I tried different ways but I can't do it.

The next code is a part about what I'm trying to do but It doesn't work

providers = [etw.ProviderInfo('Microsoft-Windows-Kernel-Process', etw.GUID("{22FB2CD6-0E7B-422B-A0C7-2FAD1FD0E716}"))]
job = etw.ETW(providers=providers, event_callback=lambda x: print(x))
job.start()
job.add_provider(etw.ProviderInfo('Microsoft-Windows-Kernel-File', etw.GUID("{EDD08927-9CC4-4E65-B970-C2560FB5C289}")))

However, if I add the provider before starting the job, it is added.

providers = [etw.ProviderInfo('Microsoft-Windows-Kernel-Process', etw.GUID("{22FB2CD6-0E7B-422B-A0C7-2FAD1FD0E716}"))]
job = etw.ETW(providers=providers, event_callback=lambda x: print(x))
job.add_provider(etw.ProviderInfo('Microsoft-Windows-Kernel-File', etw.GUID("{EDD08927-9CC4-4E65-B970-C2560FB5C289}")))
job.start()

Am I doing something wrong?

Thanks in advance :)

PS: I've tried also with job.query() and job.update() methods without success.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.