GithubHelp home page GithubHelp logo

cyrus-and / fracker Goto Github PK

View Code? Open in Web Editor NEW
240.0 12.0 25.0 1.32 MB

PHP function tracker

Shell 15.29% Makefile 2.46% JavaScript 76.95% PHP 4.73% Dockerfile 0.56%
php function tracking json stacktrace analysis security-assesment white-box pentesting xdebug

fracker's Introduction

Fracker

CI status

Fracker is a suite of tools that allows to easily trace and analyze PHP function calls, its goal is to assist the researcher during manual security assessments of PHP applications.

It consists of:

  • a PHP extension that needs to be installed in the environment of the target web application which sends tracing information to the listener application;

  • a listener application that runs locally and is in charge of receiving the tracing information from the PHP extension and performing some analysis in order to show some meaningful data to the user.

Screenshot

Setup

Install the PHP extension, either by using the deploy script or manually, then install the listener application locally.

Otherwise for a quick test, jump straight to the demo.

Demo

  1. Install the listener application.

  2. Spin a Docker container serving the PHP demo application:

    docker run -d -p 80:80 -v "$PWD/demo/:/var/www/html/" --name hello-fracker php:8.2-apache
  3. Check that the demo PHP application works as expected:

    curl http://localhost/?expression=7*7
    The result is: 49
    
  4. Deploy Fracker to the container using the deploy script:

    ./scripts/deploy.sh hello-fracker
  5. Start Fracker in another terminal, then repeat the above curl command:

    fracker
    +++ │ Listening on 0.0.0.0:6666
    +++ │
    001 │ GET localhost/?expression=7*7
    001 │ {main}() /var/www/html/index.php +0
    001 │ »  is_safe(expression="7*7") /var/www/html/index.php +16
    001 │ »  »  preg_match(pattern="/^[0-9+.\\-*\\/() ]+/", subject="7*7") /var/www/html/index.php +12
    001 │ »  calculate(expression="7*7") /var/www/html/index.php +17
    001 │ »  »  eval("return 7*7;") /var/www/html/index.php +5
    +++ │
    +++ │ Shutting down...
    
  6. Press Ctrl-C to exit Fracker, then run it again with --help, and experiment with other options too, for example, pass the -v option to show the return values too!

  7. Finally stop and remove the container with:

    docker rm -f hello-fracker

Architecture

Every PHP request or command line invocation triggers a TCP connection with the listener. The protocol is merely a stream of newline-terminated JSON objects from the PHP extension to the listener, such objects contain information about the current request, the calls performed, and the return values.

This decoupling allows the users to implement their own tools. Raw JSON objects can be inspected by dumping the stream content to standard output, for example (assuming the default settings):

socat tcp-listen:6666,fork,reuseaddr - | jq

PHP extension

The PHP extension is a modification of Xdebug, hence the installation process is fairly the same, so is the troubleshooting.

The most convenient way to use Fracker is probably to deploy it to the Docker container where the web server resides using the provided deploy script, use the manual setup for a more versatile solution.

Deploy script

This script should work out-of-the-box with Debian-like distributions running Apache:

./scripts/deploy.sh <container> [<port> [<host>]]

It configures the PHP module to connect to specified host on the specified port (defaults to the host running Docker and the default port).

Manual setup

Install the PHP development files and other dependencies. For example, on a Debian-like distribution:

apt-get install php8.2-dev libjson-c-dev pkg-config

Then move into the ./ext/ directory and just run make to fetch Xdebug, apply the patch, and build Fracker.

To check that everything is working fine, start the listener application then run PHP like this:

php -d "zend_extension=$PWD/xdebug/modules/xdebug.so" -r 'var_dump("Hello Fracker!");'

Finally, install the PHP extension the usual way. Briefly:

  1. copy ./ext/xdebug/modules/xdebug.so to the PHP extension directory (e.g., php-config --extension-dir);

  2. place zend_extension=xdebug, along with any other optional custom settings, in some INI file that gets parsed by PHP in the target environment (SAPI) used by the application;

  3. if needed, reload the web server.

At this point the source repository is no more needed.

Settings

The default INI settings should work just fine in most cases, the following aspects can be configured.

Address of the listener application

By default, the PHP extension will try to connect to 127.0.0.1 on the port 6666. This can be changed with:

xdebug.trace_fracker_host = 10.10.10.10
xdebug.trace_fracker_port = 1234

Trace only certain requests

By default, every request will be traced. It is possible to switch to an on-demand behaviour with:

xdebug.start_with_request = trigger
xdebug.trigger_value = FRACKER

In this way, only those requests having XDEBUG_TRACE=FRACKER in their GET, POST, or cookies parameters will be traced by Fracker.

Listener application

The provided listener application is a Node.js package, it is commonly installed locally, but it can resides anywhere, provided that it can be reached by the PHP extension.

Installation

Install the dependencies with:

npm install -C ./app/

Then run Fracker locally with ./app/bin/fracker.js.

Optionally, install the executable globally by creating a symlink to this folder with:

npm install -g ./app/

After that, the fracker command will be globally available.

Uninstall with:

rm -fr ./app/node_modules/
npm uninstall -g fracker

Usage and configuration

Run fracker --help to obtain the full usage.

For convenience some configuration files listing some classes of interesting PHP functions are provided along with this repository. Use them like:

fracker ./app/configs/file-* # ...

License

This product relies on Xdebug, unless explicitly stated otherwise, for the PHP extension itself, the copyright is retained by the original authors.

The listener application instead is released under a different license.

fracker's People

Contributors

ameliabradley avatar cyrus-and 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

fracker's Issues

Show when line is reached (xdebug breakpoint)

I suggest adding a flag to show when a certain file:line is reached, ex.

$ fracker -l 1.php:23
...
Reached line 1.php:23
...

Another useful feature would be a flag to print every PHP line executed (similar to python -m trace 1.py).

This could be very useful for collecting coverage data and debugging complex branching logic inside a single function (ex. parsers).

If we take this idea further, executing custom PHP code when a breakpoint / function call / return is reached would be amazing.

PHP 7.3 Issue with xdebug

Xdebug has fixed a bug recently:

Fixed bug #1734: Segfault with DBGp "source" with a out-of-range start line number

And this is what I'm getting in my computer:

[ 5633.102448] php-fpm[17184]: segfault at 31 ip 00007fe72f9eb815 sp 00007ffc8091ce30 error 4 in xdebug.so[7fe72f9b8000+44000]

Perhaps it's something you should consider fixing.

Thanks

Register time execution each function and file

Hi,
I was using phptrace module for debugging php applications, but I have certains problems with it.
Now I discover your module and is working fine for me on cgi and fpm.

But I miss to know the time that spends each function and file like phptrace does.
Is it possible to add it in further updates?

Here I attach you a comparassion of same code execution in both modules.
Thanks in advance.

trace_phptrace
trace_fracker

Attempting 'manual setup', missing json.h file

$ phpize
$ ./configure
$ make

The make command results in:

fracker/ext/xdebug_trace_fracker.c:17:10: fatal error: json.h: No such file or directory
 #include <json.h>
          ^~~~~~~~
compilation terminated.
make: *** [Makefile:270: xdebug_trace_fracker.lo] Error 1

EDIT: Running it on a fresh install of Ubuntu19+php7.2+Apache.

[BUG] macOS have some errors

macOS

Env

❯ php -version
PHP 7.3.11 (cli) (built: Jun  5 2020 23:50:40) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.11, Copyright (c) 1998-2018 Zend Technologies
    with Xdebug v2.7.0, Copyright (c) 2002-2019, by Derick Rethans
~/Downloads/fracker/ext master                                                             Py anaconda2 19:36:08
❯ ~/php-private/phpize
Configuring for:
PHP Api Version:         20180731
Zend Module Api No:      20180731
Zend Extension Api No:   320180731

Error after i exec make

/Users/zeroyu/Downloads/fracker/ext/src/tracing/trace_fracker.c:91:20: error: use of undeclared identifier
      'SOL_TCP'
    setsockopt(fd, SOL_TCP, TCP_CORK, &cork, sizeof(cork));
                   ^
/Users/zeroyu/Downloads/fracker/ext/src/tracing/trace_fracker.c:91:29: error: use of undeclared identifier
      'TCP_CORK'
    setsockopt(fd, SOL_TCP, TCP_CORK, &cork, sizeof(cork));
                            ^
/Users/zeroyu/Downloads/fracker/ext/src/tracing/trace_fracker.c:101:20: error: use of undeclared identifier
      'SOL_TCP'
    setsockopt(fd, SOL_TCP, TCP_CORK, &cork, sizeof(cork));
                   ^
/Users/zeroyu/Downloads/fracker/ext/src/tracing/trace_fracker.c:101:29: error: use of undeclared identifier
      'TCP_CORK'
    setsockopt(fd, SOL_TCP, TCP_CORK, &cork, sizeof(cork));
                            ^
4 errors generated.
make: *** [src/tracing/trace_fracker.lo] Error 1

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.