dpj997991 2015-11-21 12:46
浏览 31

使用mod_php覆盖在Apache上运行的PHP脚本中的connect()

How to override connect() system call called from within PHP script, in an Apache request, when PHP is enabled via mod_php?

I have my custom connect() version defined in custom-connect.c:

#define _GNU_SOURCE 1
#include <stdio.h>
#include <arpa/inet.h>
#include <dlfcn.h>

int (*real_connect)(int, const struct sockaddr *, socklen_t);
FILE *f;

void _init (void) {
    const char *err;

    f = fopen("/tmp/connect.log", "a");

    real_connect = dlsym (RTLD_NEXT, "connect");
    if ((err = dlerror()) != NULL) {
        fprintf (f, "dlsym (connect): %s
", err);
    }
}

int connect(int fd, const struct sockaddr *sk, socklen_t sl) {
    static struct sockaddr_in *sk_in;
    sk_in = (struct sockaddr_in *)sk;

    fprintf(f, "Custom connect: %d %s:%d
", fd, inet_ntoa(sk_in->sin_addr), ntohs(sk_in->sin_port));

    return real_connect(fd, sk, sl);
}

I compile it with:

gcc -nostartfiles -fpic -shared custom-connect.c -o custom-connect.so -ldl

I have a simple script curl-test.php, that internally makes calls to the connect():

<?php
$ch = curl_init("http://example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
echo 'Received length: ' . strlen($data) . "
";

When I run my script with LD_PRELOAD from command line:

LD_PRELOAD=./custom-connect.so php curl-test.php

Then it looks fine, i can see that my custom connect() logged something to /tmp/connect.log:

Custom connect: 4 192.168.178.1:53
Custom connect: 4 93.184.216.34:80

It looks like it works fine from the command line. But how can I override connect() with my own version, when I run my script from Apache with PHP enabled via mod_php? Should I also use LD_PRELOAD? If yes, then how to configure it?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 我的数据无法存进链表里
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
    • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
    • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
    • ¥15 如何在炒股软件中,爬到我想看的日k线
    • ¥15 seatunnel 怎么配置Elasticsearch
    • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
    • ¥15 (标签-MATLAB|关键词-多址)
    • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
    • ¥500 52810做蓝牙接受端