Linux monitor usb events

Linux monitor usb events

Если только в Венду в виртуалку

  • 3.4 , e2 ( ? ), 01:11, 08/01/2011 [^] [^^] [^^^] [ответить]
  • + / –
    begin
    задать вопрос,
    полубаться,
    ответить на вопрос в стиле КО.
    end
  • 3.6, Michael Shigorin ( ok ), 14:16, 09/01/2011 [^] [^^] [^^^] [ответить]
  • + / –
    Может поддерживаться закрытым, например.
  • 2.5 , User294 ( ok ), 11:32, 09/01/2011 [^] [^^] [^^^] [ответить]
  • + / –
    > Можно проводить реверс-инженеринг для USB устройств не поддерживающихся в Linux.

    Это такой современный вариант pkunzip.zip? 🙂 А то если девайс не поддерживается — что сниффать то предполагается? oO

    3.7, pavlinux ( ok ), 14:22, 09/01/2011 [^] [^^] [^^^] [ответить] + / –
    >> Можно проводить реверс-инженеринг для USB устройств не поддерживающихся в Linux.
    > Это такой современный вариант pkunzip.zip? 🙂 А то если девайс не поддерживается
    > — что сниффать то предполагается? oO

    Из отпуска что ль вернулся? Как там в Альпах, лыжи катят?!

    Источник

    usbmonВ¶

    IntroductionВ¶

    The name «usbmon» in lowercase refers to a facility in kernel which is used to collect traces of I/O on the USB bus. This function is analogous to a packet socket used by network monitoring tools such as tcpdump(1) or Ethereal. Similarly, it is expected that a tool such as usbdump or USBMon (with uppercase letters) is used to examine raw traces produced by usbmon.

    The usbmon reports requests made by peripheral-specific drivers to Host Controller Drivers (HCD). So, if HCD is buggy, the traces reported by usbmon may not correspond to bus transactions precisely. This is the same situation as with tcpdump.

    Two APIs are currently implemented: «text» and «binary». The binary API is available through a character device in /dev namespace and is an ABI. The text API is deprecated since 2.6.35, but available for convenience.

    How to use usbmon to collect raw text tracesВ¶

    Unlike the packet socket, usbmon has an interface which provides traces in a text format. This is used for two purposes. First, it serves as a common trace exchange format for tools while more sophisticated formats are finalized. Second, humans can read it in case tools are not available.

    To collect a raw text trace, execute following steps.

    1. PrepareВ¶

    Mount debugfs (it has to be enabled in your kernel configuration), and load the usbmon module (if built as module). The second step is skipped if usbmon is built into the kernel:

    Verify that bus sockets are present:

    Now you can choose to either use the socket ‘0u’ (to capture packets on all buses), and skip to step #3, or find the bus used by your device with step #2. This allows to filter away annoying devices that talk continuously.

    2. Find which bus connects to the desired deviceВ¶

    Run «cat /sys/kernel/debug/usb/devices», and find the T-line which corresponds to the device. Usually you do it by looking for the vendor string. If you have many similar devices, unplug one and compare the two /sys/kernel/debug/usb/devices outputs. The T-line will have a bus number.

    «Bus=03» means it’s bus 3. Alternatively, you can look at the output from «lsusb» and get the bus number from the appropriate line. Example:

    Bus 003 Device 002: ID 0557:2004 ATEN UC100KM V2.00

    3. Start ‘cat’В¶

    to listen on a single bus, otherwise, to listen on all buses, type:

    This process will read until it is killed. Naturally, the output can be redirected to a desirable location. This is preferred, because it is going to be quite long.

    4. Perform the desired operation on the USB busВ¶

    This is where you do something that creates the traffic: plug in a flash key, copy files, control a webcam, etc.

    5. Kill catВ¶

    Usually it’s done with a keyboard interrupt (Control-C).

    At this point the output file (/tmp/1.mon.out in this example) can be saved, sent by e-mail, or inspected with a text editor. In the last case make sure that the file size is not excessive for your favourite editor.

    Raw text data formatВ¶

    Two formats are supported currently: the original, or ‘1t’ format, and the ‘1u’ format. The ‘1t’ format is deprecated in kernel 2.6.21. The ‘1u’ format adds a few fields, such as ISO frame descriptors, interval, etc. It produces slightly longer lines, but otherwise is a perfect superset of ‘1t’ format.

    If it is desired to recognize one from the other in a program, look at the «address» word (see below), where ‘1u’ format adds a bus number. If 2 colons are present, it’s the ‘1t’ format, otherwise ‘1u’.

    Any text format data consists of a stream of events, such as URB submission, URB callback, submission error. Every event is a text line, which consists of whitespace separated words. The number or position of words may depend on the event type, but there is a set of words, common for all types.

    Here is the list of words, from left to right:

    URB Tag. This is used to identify URBs, and is normally an in-kernel address of the URB structure in hexadecimal, but can be a sequence number or any other unique string, within reason.

    Timestamp in microseconds, a decimal number. The timestamp’s resolution depends on available clock, and so it can be much worse than a microsecond (if the implementation uses jiffies, for example).

    Event Type. This type refers to the format of the event, not URB type. Available types are: S — submission, C — callback, E — submission error.

    «Address» word (formerly a «pipe»). It consists of four fields, separated by colons: URB type and direction, Bus number, Device address, Endpoint number. Type and direction are encoded with two bytes in the following manner:

    Ci Co Control input and output
    Zi Zo Isochronous input and output
    Ii Io Interrupt input and output
    Bi Bo Bulk input and output

    Bus number, Device address, and Endpoint are decimal numbers, but they may have leading zeros, for the sake of human readers.

    URB Status word. This is either a letter, or several numbers separated by colons: URB status, interval, start frame, and error count. Unlike the «address» word, all fields save the status are optional. Interval is printed only for interrupt and isochronous URBs. Start frame is printed only for isochronous URBs. Error count is printed only for isochronous callback events.

    The status field is a decimal number, sometimes negative, which represents a «status» field of the URB. This field makes no sense for submissions, but is present anyway to help scripts with parsing. When an error occurs, the field contains the error code.

    In case of a submission of a Control packet, this field contains a Setup Tag instead of an group of numbers. It is easy to tell whether the Setup Tag is present because it is never a number. Thus if scripts find a set of numbers in this word, they proceed to read Data Length (except for isochronous URBs). If they find something else, like a letter, they read the setup packet before reading the Data Length or isochronous descriptors.

    Setup packet, if present, consists of 5 words: one of each for bmRequestType, bRequest, wValue, wIndex, wLength, as specified by the USB Specification 2.0. These words are safe to decode if Setup Tag was ‘s’. Otherwise, the setup packet was present, but not captured, and the fields contain filler.

    Number of isochronous frame descriptors and descriptors themselves. If an Isochronous transfer event has a set of descriptors, a total number of them in an URB is printed first, then a word per descriptor, up to a total of 5. The word consists of 3 colon-separated decimal numbers for status, offset, and length respectively. For submissions, initial length is reported. For callbacks, actual length is reported.

    Data Length. For submissions, this is the requested length. For callbacks, this is the actual length.

    Data tag. The usbmon may not always capture data, even if length is nonzero. The data words are present only if this tag is ‘=’.

    Data words follow, in big endian hexadecimal format. Notice that they are not machine words, but really just a byte stream split into words to make it easier to read. Thus, the last word may contain from one to four bytes. The length of collected data is limited and can be less than the data length reported in the Data Length word. In the case of an Isochronous input (Zi) completion where the received data is sparse in the buffer, the length of the collected data can be greater than the Data Length value (because Data Length counts only the bytes that were received whereas the Data words contain the entire transfer buffer).

    An input control transfer to get a port status:

    An output bulk transfer to send a SCSI command 0x28 (READ_10) in a 31-byte Bulk wrapper to a storage device at address 5:

    Raw binary format and APIВ¶

    The overall architecture of the API is about the same as the one above, only the events are delivered in binary format. Each event is sent in the following structure (its name is made up, so that we can refer to it):

    These events can be received from a character device by reading with read(2), with an ioctl(2), or by accessing the buffer with mmap. However, read(2) only returns first 48 bytes for compatibility reasons.

    The character device is usually called /dev/usbmonN, where N is the USB bus number. Number zero (/dev/usbmon0) is special and means «all buses». Note that specific naming policy is set by your Linux distribution.

    If you create /dev/usbmon0 by hand, make sure that it is owned by root and has mode 0600. Otherwise, unprivileged users will be able to snoop keyboard traffic.

    The following ioctl calls are available, with MON_IOC_MAGIC 0x92:

    This call returns the length of data in the next event. Note that majority of events contain no data, so if this call returns zero, it does not mean that no events are available.

    The argument is a pointer to the following structure:

    The member «queued» refers to the number of events currently queued in the buffer (and not to the number of events processed since the last reset).

    The member «dropped» is the number of events lost since the last call to MON_IOCG_STATS.

    This call sets the buffer size. The argument is the size in bytes. The size may be rounded down to the next chunk (or page). If the requested size is out of [unspecified] bounds for this kernel, the call fails with -EINVAL.

    This call returns the current size of the buffer in bytes.

    These calls wait for events to arrive if none were in the kernel buffer, then return the first event. The argument is a pointer to the following structure:

    Before the call, hdr, data, and alloc should be filled. Upon return, the area pointed by hdr contains the next event structure, and the data buffer contains the data, if any. The event is removed from the kernel buffer.

    The MON_IOCX_GET copies 48 bytes to hdr area, MON_IOCX_GETX copies 64 bytes.

    This ioctl is primarily used when the application accesses the buffer with mmap(2). Its argument is a pointer to the following structure:

    The ioctl operates in 3 stages.

    First, it removes and discards up to nflush events from the kernel buffer. The actual number of events discarded is returned in nflush.

    Second, it waits for an event to be present in the buffer, unless the pseudo- device is open with O_NONBLOCK.

    Third, it extracts up to nfetch offsets into the mmap buffer, and stores them into the offvec. The actual number of event offsets is stored into the nfetch.

    This call removes a number of events from the kernel buffer. Its argument is the number of events to remove. If the buffer contains fewer events than requested, all events present are removed, and no error is reported. This works when no events are available too.

    The ioctl FIONBIO may be implemented in the future, if there’s a need.

    In addition to ioctl(2) and read(2), the special file of binary API can be polled with select(2) and poll(2). But lseek(2) does not work.

    • Memory-mapped access of the kernel buffer for the binary API

    The basic idea is simple:

    To prepare, map the buffer by getting the current size, then using mmap(2). Then, execute a loop similar to the one written in pseudo-code below:

    Thus, the main idea is to execute only one ioctl per N events.

    Although the buffer is circular, the returned headers and data do not cross the end of the buffer, so the above pseudo-code does not need any gathering.

    © Copyright The kernel development community.

    Источник

    Linux-форензика в лице трекинга истории подключений USB-устройств

    В рамках погружения в одну из дисциплин (в процессе обучения по специальности компбеза) я работал над одним занимательным проектом, который бы мне не хотелось просто похоронить в недрах папки «Универ» на внешнем винчестере.

    Сей проект носит название usbrip и представляет собой небольшую консольную опенсорс утилиту для Linux-форензики, а именно для работы с историей подключений USB-устройств. Программа написана на чистом Python 3 (с использованием некоторых сторонних модулей) и не требует зависимостей помимо Python 3.x интерпретатора и пары строк из requirements.txt, разрешающихся одной строкой с помощью pip.

    В этом посте я опишу некоторые возможности данного софта и оставлю краткий мануал со ссылкой на источник загрузки.

    Снято! (… в смысле Cut!)

    Примечание. Описываемый в статье функционал актуален для первой версии утилиты. За последней версией с множеством новых плюшек предлагаю перейти в репозиторий.

    Скриншоты

    Получение истории подключений съёмных USB-устройств:

    Поиск дополнительной информации о USB-устройстве по идентификатору модели (PID’у):

    Описание

    Как известно, операционные системы на базе GNU/Linux очень трепетно относятся к логированию разного рода событий, и подключение/отключение USB-устройств не является исключением. В совокупности с одним из пунктов UNIX-философии о «текстовых потоках, как универсальных интерфейсах» информацию об артефактах таких событий (с разной степенью подробности) в зависимости от дистрибутива можно обнаружить в одном или нескольких из следующих текстовых файлов:

    • /var/log/kern.log* ;
    • /var/log/syslog* ;
    • /var/log/messages* ;
    • /var/log/dmesg* ;
    • /var/log/daemon.log* .

    FORENSIC-PROOF даже показывает нам такую картинку на этот счет (немного неполную, но неважно):

    Для своей работы usbrip находит универсальные для всех сборок Linux, основанных на Debian (Ubuntu, Linux Mint и др.) и RPM (CentOS, Fedora, openSUSE и др.), лог-файлы, а именно: /var/log/syslog* или /var/log/messages* , парсит их в поисках нужной информации и обликает найденные следы подключений USB-устройств в красивые таблички (или списки — как угодно).

    Также usbrip умеет:

    • создавать списки авторизированных (доверенных) устройств в виде JSON-файлов;
    • искать «события-нарушителей» на основе списка доверенных устройств: такие события (подключение/отключение USB), в которых участвовали USB-устройства, не отмеченные как доверенные;
    • искать дополнительную информацию о USB-устройстве по его VID (Vendor ID) и/или PID (Product ID).

    Справка

    Получить список доступных модулей:

    Получить список доступных подмодулей для конкретного модуля:

    Получить список доступных опций для конкретного подмодуля:

    Синтаксис

    Опции

    Примеры использования

    Показать историю подключений всех USB-устройств, опуская баннер, информационные (зелёные) сообщения, а также не задавая вопросы по ходу выполнения ( -q, —quite ), сформировав вывод в виде списка ( -l, —list ), включив в него 100 последних найденных событий ( -n КОЛИЧЕСТВО_СОБЫТИЙ, —number КОЛИЧЕСТВО_СОБЫТИЙ ):

    Показать историю подключений съёмных USB-устройств ( -e, —external ), сформировав вывод в форме таблицы с полями (столбцами) «Connected», «VID», «PID», «Disconnected» и «Serial Number» ( -c СТОЛБЕЦ [СТОЛБЕЦ . ], —column СТОЛБЕЦ [СТОЛБЕЦ . ] ), отфильтровав поиск по датам ( -d ДАТА [ДАТА . ], —date ДАТА [ДАТА . ] ), взяв при этом информацию из внешних лог-файлов ( -f ЛОГ_ФАЙЛ [ЛОГ_ФАЙЛ . ], —file ЛОГ_ФАЙЛ [ЛОГ_ФАЙЛ . ] ):

    Построить таблицу истории подключений всех USB-устройств и перенаправить вывод в файл для дальнейшего анализа. В том случае, если поток вывода не стандартный stdout ( «|» либо «>» к примеру) в выходных данных не будут присутствовать спец. символы, отвечающие за цвет шрифта в терминале, поэтому текстовый файл не будет замусорен нечитаемыми символами. Также нужно отметить, что usbrip использует некоторые UNICODE-константы, поэтому было бы неплохо сразу конвертировать кодировку создаваемого файла в UTF-8 для их корректного отображения (например с помощью encov ), а также использовать символы новой строки в Windows-стиле для лучшей переносимости (например с помощью awk ):

    Примечание: избавиться от спец. символов, отвечающих за цвет можно и в том случае, если вывод уже был направлен в stdout. Для этого необходимо скопировать полученные данные в новый файл usbrip.txt и добавить еще одну awk -инструкцию:

    Создать список доверенных устройств в виде JSON-файла ( trusted/auth.json ), содержащего поля «VID» и «PID» первых трех устройств, подключенных 26-го сентября:

    Найти среди истории подключений съёмных USB-устройств «события-нарушители» на основе списка доверенных устройств ( trusted/auth.json ) по полю «PID» и сформировать вывод в виде таблицы с полями «Connected», «VID» и «PID»:

    Найти дополнительную информацию о USB-устройстве на основе его VID’а и PID’а:

    Загрузить/обновить базу данных идентификаторов USB-устройств (источник здесь):

    Ссылки и постскриптум

    Забрать утилиту можно с Гитхаба, все зависимости для запуска и корректной работы указаны в requirements.txt.

    Благодарю за внимание!

    P. S. да, стиль баннера и информационных сообщений вдохновлен проектом sqlmap (*^.^*)

    UPD 13.06.2018. Появилось расследование реального (?) кейса с участием моей утилиты от человека с Codeby (сам в шоке, если честно).

    Источник

    Читайте также:  Dan windows and doors
    Оцените статью