GithubHelp home page GithubHelp logo

marcelog / simplepcap Goto Github PK

View Code? Open in Web Editor NEW
29.0 4.0 15.0 125 KB

A mini libpcap adapter in C++, with a php module using SWIG

Home Page: http://marcelog.github.com/articles/swig_php_libpcap_module_c++.html

PHP 38.36% C++ 61.64%

simplepcap's Introduction

This is the sourcecode for: http://marcelog.github.com/articles/swig_php_libpcap_module_c++.html

It is a small C++ wrapper around libpcap, that comes with a SWIG interface file 
for PHP. So this will in essence, give you access to libpcap from PHP.

Currently, it can list all devices and sniff packets without using pcap_loop(). It
uses pcap_next(). You can also inject packets, the wrapper will call pcap_inject().

NOTE: It's very possible that you will need root or administrator privileges
to sniff an interface. So, beware.

CI
==
You can get a build for linux x86_64 at:
http://ci.marcelog.name:8080/view/C%20C++/?

USE EXAMPLE
===========

List all devices
----------------
foreach (SimplePcap::findAllDevs() as $name => $description) {
    echo "$name => $description\n";
}

Getting packets
---------------
First, get an instance of the SimplePcap class:
$simplePcap = new SimplePcap($interfaceName, $filter, $snapLen, $timeout);

$interfaceName can be something like "any", or "eth0".
$filter is a libpcap filter, like "port 80".
$snapLen is the maximum amount of bytes to capture per packet.
$timeout specifies the read timeout in milliseconds.

Then, loop!
while(true) {
    // Get a packet
    $packet = $simplePcap->get(); // Returns a 'Packet' Object.

    // Get the data
    $data = $packet->getData(); // Returns a string

    // Get the packet information
    $capturedLen = $packet->getCapturedLen();
    $realLen = $packet->getRealLen();
    $timeSeconds = $packet->getSecondsOffset();
    $timeMicroSeconds = $packet->getMicroSecondsOffset();
}

That's it. Happy sniffing!

Injecting Packets
-----------------
$totalBytesSent = $simplePcap->send($rawPacketData);

Hint: If you want to try it quickly, you can sniff packets and save them to a file. Do
a file_get_contents() on it when calling send(), to reinject it :) If not, you will have
to forge your packet.

Note: Since the SimplePcap constructor calls pcap_open_live(), even if you're opening the
interface just to inject packets (.. wont comment anything on that.. ) you will still need
to pass all the arguments.

Compiling
=========
Edit the GNUmakefile. You will need to specify the SWIG binary location and
the php include directory (for the .h files). Then, just:
$ gmake

The "build" directory will be generated, with the SimplePcap.so file. Include it in your
php.ini file like:
extension=SimplePcap.so

See the example directory, that ships a sample php.ini file that you can use with the -c flag for php:

$ cd example
$ php -c php.ini list.php
any => Pseudo-device that captures on all interfaces
eth0 =>
lo =>

$ php -c php.ini sniff.php eth0 "port 80" 4096 0
...
00080   2e 31 0d 0a 48 6f 73 74    3a 20 77 77 77 2e 67 6f    .1..Host   : www.go
00096   6f 67 6c 65 2e 63 6f 6d    0d 0a 55 73 65 72 2d 41    ogle.com   ..User-A
00112   67 65 6e 74 3a 20 4c 69    6e 6b 73 20 28 32 2e 32    gent: Li   nks (2.2
00128   3b 20 4c 69 6e 75 78 20    32 2e 36 2e 32 39 2d 67    ; Linux    2.6.29-g
00144   65 6e 74 6f 6f 2d 72 31    20 78 38 36 5f 36 34 3b    entoo-r1    x86_64;
00160   20 31 32 36 78 34 34 29    0d 0a 41 63 63 65 70 74     126x44)   ..Accept
00176   3a 20 2a 2f 2a 0d 0a 41    63 63 65 70 74 2d 45 6e    : */*..A   ccept-En
00192   63 6f 64 69 6e 67 3a 20    67 7a 69 70 2c 20 64 65    coding:    gzip, de

simplepcap's People

Contributors

marcelog 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

Watchers

 avatar  avatar  avatar  avatar

simplepcap's Issues

Unable to compile

hi - please can you give me some guidance on what to do here?

[root@dev SimplePcap-master]# head -2 GNUmakefile
PHPINC += /usr/local/include/php
SWIG += /usr/dev/swig/bin/swig
[root@dev SimplePcap-master]# gmake
mkdir -p ./build
g++ -fPIC -I./include -I./build/build -I/usr/local/include/php/Zend -I/usr/local/include/php/main -I/usr/local/include/php/ext -I/usr/local/include/php/TSRM -I/usr/local/include/php -c -o build/SimplePcap_swig.o build/SimplePcap_swig.cpp
build/SimplePcap_swig.cpp:1866:90: error: macro "add_assoc_string" passed 4 arguments, but takes just 3
add_assoc_string(return_value, iter->first.c_str(), (char )iter->second.c_str(), 1);
^
build/SimplePcap_swig.cpp: In function 'void _wrap_SimplePcap_findAllDevs(zend_execute_data
, zval*)':
build/SimplePcap_swig.cpp:1866:7: error: 'add_assoc_string' was not declared in this scope
add_assoc_string(return_value, iter->first.c_str(), (char *)iter->second.c_str(), 1);
^
gmake: *** [build/SimplePcap_swig.o] Error 1

thank you!

README

I think your README file isn't correct.

Why not export it as a simple php extension??

I am learning PHP. I Don't have any idea about php extensions or C thingy.
So, its kind gibberish for me to understand your source code.
Through, I tried to install it but its just so complex.... Moreover, i think i will also need make a C coding environment for installing swig
So, Please, export the source code as a php extension.
Thanks in advance......

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.