GithubHelp home page GithubHelp logo

请求支持云函数 about neo-regeorg HOT 17 CLOSED

l-codes avatar l-codes commented on June 8, 2024 1
请求支持云函数

from neo-regeorg.

Comments (17)

lovelyjuice avatar lovelyjuice commented on June 8, 2024 1

客户端其实不需要变动,主要是服务端变化比较大。云函数的所有headers和参数都是通过api网关预定义格式的参数传入,因此需要自行解析cookie中的sessionid,否则无法使用session,同时还需通过传参的方式给客户端setcookie。除此之外,还有一些其他没能解决的坑点,比如php的fsockopen已经建立连接但会报错。改了一天的模板,现在卡在这里解决不了

<?php

function main_handler($event, $context)
{
    $Return_headers=array('X-Test' =>'test');


    $header_add = function ($str) use (&$Return_headers)
    {
        $split_header = explode(':', $str);
        $Return_headers[$split_header[0]] = ltrim($split_header[1]);
    };


    ini_set("allow_url_fopen", true);
    ini_set("allow_url_include", true);
//    error_reporting(E_ERROR | E_PARSE);

    $Return_code = 200;
    if (version_compare(PHP_VERSION, '5.4.0', '>=')) $Return_code = HTTPCODE;
    $Return_body='';

    set_time_limit(0);
    foreach ($event->headers as $key => $val) {
        $headers[ucwords($key,'-')] = $val;     # 腾讯云函数的headers key全是小写,比如user-agent而不是User-Agent
    }


    if(isset($headers['Cookie'])){
        $arr = explode('=', $headers['Cookie'])[0];
        $key = $arr[0];
        $val = trim($arr[1]);
        if($key === 'PHPSESSID') session_id($val);
    }

    $en = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    $de = "BASE64 CHARSLIST";

    $cmd = $headers["X-CMD"];
    $mark = substr($cmd, 0, 22);
    $cmd = substr($cmd, 22);
    $run = "run" . $mark;
    $writebuf = "writebuf" . $mark;
    $readbuf = "readbuf" . $mark;

    switch ($cmd) {
        case "CONNECT":
            {
                $target_ary = explode("|", base64_decode(strtr($headers["X-TARGET"], $de, $en)));
                $target = $target_ary[0];
                $port = (int)$target_ary[1];
                $res = fsockopen($target, $port, $errno, $errstr, 1);
                if ($res === false) {
                    $header_add('X-STATUS: FAIL');
                    $header_add('X-ERROR: Failed connecting to target');
                    goto end;
                }

                stream_set_blocking($res, false);
                ignore_user_abort();

                @session_start();
                $_SESSION[$run] = true;
                $_SESSION[$writebuf] = "";
                $_SESSION[$readbuf] = "";
                session_write_close();

                while ($_SESSION[$run]) {
                    if (empty($SESSION[$writebuf])) {
                        usleep(50000);
                    }

                    $readBuff = "";
                    @session_start();
                    $writeBuff = $_SESSION[$writebuf];
                    $_SESSION[$writebuf] = "";
                    session_write_close();
                    if ($writeBuff != "") {
                        stream_set_blocking($res, false);
                        $i = fwrite($res, $writeBuff);
                        if ($i === false) {
                            @session_start();
                            $_SESSION[$run] = false;
                            session_write_close();
                            goto end;
                        }
                    }
                    stream_set_blocking($res, false);
                    while ($o = fgets($res, 10)) {
                        if ($o === false) {
                            @session_start();
                            $_SESSION[$run] = false;
                            session_write_close();
                            goto end;
                        }
                        $readBuff .= $o;
                    }
                    if ($readBuff != "") {
                        @session_start();
                        $_SESSION[$readbuf] .= $readBuff;
                        session_write_close();
                    }
                }
                fclose($res);
            }
            if(isset($Return_headers['set-cookie']))unset($Return_headers['set-cookie']);       # 移除header
            break;
        case "DISCONNECT":
            {
                @session_start();
                unset($_SESSION[$run]);
                unset($_SESSION[$readbuf]);
                unset($_SESSION[$writebuf]);
                session_write_close();
            }
            break;
        case "READ":
            {
                @session_start();
                $readBuffer = $_SESSION[$readbuf];
                $_SESSION[$readbuf] = "";
                $running = $_SESSION[$run];
                session_write_close();
                if ($running) {
                    $header_add('X-STATUS: OK');
                    $header_add("Connection: Keep-Alive");
                    $Return_body .= strtr(base64_encode($readBuffer), $en, $de);
                } else {
                    $header_add('X-STATUS: FAIL');
                }
            }
            break;
        case "FORWARD":
            {
                @session_start();
                $running = $_SESSION[$run];
                session_write_close();
                if (!$running) {
                    $header_add('X-STATUS: FAIL');
                    $header_add('X-ERROR: No more running, close now');
                    goto end;
                }
                $header_add('Content-Type: application/octet-stream');
                $rawPostData = file_get_contents("php://input");
                if ($rawPostData) {
                    @session_start();
                    $_SESSION[$writebuf] .= base64_decode(strtr($rawPostData, $de, $en));
                    session_write_close();
                    $header_add('X-STATUS: OK');
                    $header_add("Connection: Keep-Alive");
                } else {
                    $header_add('X-STATUS: FAIL');
                    $header_add('X-ERROR: POST request read filed');
                }
            }
            break;
        default:
        {
            @session_start();
            session_write_close();
            $Return_body .= "Georg says, 'All seems fine'";
        }
    }
end:
    $sessionid = session_id();
    $header_add("Set-Cookie: PHPSESSID=$sessionid; path=/");

    return array(
        'isBase64Encoded' => false,
        'statusCode' => $Return_code,
        'headers' => $Return_headers,
        'body' => $Return_body
    );
}

?>

from neo-regeorg.

ViCrack avatar ViCrack commented on June 8, 2024 1

云函数 这个我后续看看能否独立成一个项目。
neoreg 准备发布的 v3.0 版本 java/asp.net 都是不再依赖 session 的,唯独 php 还没有解决,而你现在刚好是 php 版本,php 是否有与 java 类似的 application 全局变量吗?我找了一下没有得以解决

我看了最新的更新日志, 我不太了解最近把session换application的原因, 不过如果你想知道php类似java application的功能, 那你看看这个链接能不能给予你一些帮助
http://php.js.cn/blog/php_asp_application_session/
调用session_id固定一个id
另外通过固定的某个缓存文件来共享读写可能也是个方法, php session其实也是通过文件来存储的, 还有用putenv之类的方法存到环境变量里面

from neo-regeorg.

ViCrack avatar ViCrack commented on June 8, 2024 1

@L-codes

你不妨用这个试试, 正常来说应该类似于application的功能了, 而且Cookie是完全不需要传的, 对于落地文件更应该不是什么问题(因为就算用原来的PHP $_SESSION其实默认也是用文件来保存的)

图片

<?php
@session_id("AAA");
@session_start();
echo "old: ".$_SESSION["test"];
if(!empty($_GET["p"])){
	$_SESSION["test"] = $_GET["p"];
	echo "\nnew: ".$_SESSION["test"];
}
ignore_user_abort();
?>

from neo-regeorg.

L-codes avatar L-codes commented on June 8, 2024

这个思路是好的!但是我觉得不应该让neoreg内置实现,而应该外部另一个程序实现,neoreg 有 -x 参数支持外部代理扩展

from neo-regeorg.

L-codes avatar L-codes commented on June 8, 2024

云函数 这个我后续看看能否独立成一个项目。
neoreg 准备发布的 v3.0 版本 java/asp.net 都是不再依赖 session 的,唯独 php 还没有解决,而你现在刚好是 php 版本,php 是否有与 java 类似的 application 全局变量吗?我找了一下没有得以解决

from neo-regeorg.

L-codes avatar L-codes commented on June 8, 2024

我看一下,你是指用 scf-proxy 开启的 http proxy,直接用 neoreg 的 --proxy 参数无法正常使用,因为无法使用session?
对的,后面 v3.0 版本的 java/asp.net 就可以直接使用 scf-proxy 进行代理

from neo-regeorg.

lovelyjuice avatar lovelyjuice commented on June 8, 2024

我看一下,你是指用 scf-proxy 开启的 http proxy,直接用 neoreg 的 --proxy 参数无法正常使用,因为无法使用session?
对的,后面 v3.0 版本的 java/asp.net 就可以直接使用 scf-proxy 进行代理

不是,scf-proxy 只是我举的一个例子(实际上我没有下载运行过这个项目),我只是希望把这个项目的思路搬到neo-reGeorg上面来

from neo-regeorg.

L-codes avatar L-codes commented on June 8, 2024

neoreg 项目是不会加入这个的思路的,两个原因:

  1. 这个云函数的薅羊毛方法,它也许某天就修了
  2. 这种能变换ip的灵活性,应该交由 neoreg 下游的代理等去做,neoreg 本身已经提供了 --proxy 的支持允许流量被下游程序代理了

小即是美,非常不建议弄成IDE一样。。。

from neo-regeorg.

lovelyjuice avatar lovelyjuice commented on June 8, 2024

如果是通过--proxy参数设置下游代理,通过下游代理连接到tunnel.php,那么服务端代码必然是已经上传到目标机器上了,此时tunnel.php仅起到了内网穿透的作用,而上传webshell的时候就可能会被封IP甚至溯源。
我的想法是在对目标机器进行攻击之前,就能够隐匿自身IP,同时通过IP随机变换防止被封IP,也可用于替代Tor。我本来想直接跑一个shadowsocks在上面,但查阅文档后发现云函数无法绑定socket,只支持通过API网关传递HTTP数据,所以才想到用reGeorg这种方式建立HTTP隧道传输TCP流量。

除此之外,其实还有更简单的方式通过云函数中转,比如云函数运行一段python代码,反弹到msf,msf中添加路由并开启代理。不过缺点是需要一台有公网IP的VPS。

from neo-regeorg.

L-codes avatar L-codes commented on June 8, 2024

对的 neoreg 的使用只能起到内网穿透作用,不能先在服务端部署好 Neoreg的server端,则无法使用其进行流量转发。
你是要把云函数的服务器当做是 reg 的服务端?然后访问用其代理来访问其他站?

然后是后面你说的运行一段python,反弹到msf,都能执行任意代码了,那不是薅羊毛都薅到了一台免费的VPS? 你觉得这个真的可行吗?还是我哪里没有搞懂?

方便的话,可以加我详谈?

from neo-regeorg.

lovelyjuice avatar lovelyjuice commented on June 8, 2024

云函数的服务器确实是可以作为 reg 的服务端,可以理解为互联网就是个大内网,云函数运行的服务器是边界服务器,但是这个边界服务器具有很多块网卡。
弹shell回来真的可以“白嫖”一台服务器,我看了配置,2核2G,硬盘20多G,部署区域在国外甚至有惊喜。不过鉴于云函数本身按计算量收费,所谓的白嫖应该只是在计算量限额之内可以使用,并且云函数的权限是qcloud,只是一个普通用户,内核版本是4.4,编译时间是去年,提权还没试过。

from neo-regeorg.

L-codes avatar L-codes commented on June 8, 2024

原来云函数是要收费的啊 我还以为是免费的 那明白了
如果理解互联网的多个云函数服务器是,reg也需要云函数服务器能够存储数据状态,否则也是无法实现为此tcp连接,那感觉云函数可玩性并不高,只能当做自动变换的地址池使用,开启一个socks或http等proxy

方便的话,可以发qq或wechat等联系方式到我邮箱 bC1jb2Rlc1thXXFxLmNvbQ==

from neo-regeorg.

L-codes avatar L-codes commented on June 8, 2024

@ViCrack 首先感谢你的帮助。把session换application的原因是为了不依赖 http 的 session 来暂存数据。你提到的php模拟 application 功能,首先需要落地文件,其次是还是依赖 Cookie,并不能解决问题

from neo-regeorg.

L-codes avatar L-codes commented on June 8, 2024

@ViCrack 感谢!明白了,用session_id指定到固定的id,我测试稳定后发布

from neo-regeorg.

L-codes avatar L-codes commented on June 8, 2024

后面想了一下,目前 php 环境暂未遇到需要解决 session 依赖的问题,还是暂不修改,因为固定 session_id 哪怕是随机的,也会产生不必要的指纹

from neo-regeorg.

ViCrack avatar ViCrack commented on June 8, 2024

@L-codes 后续能用到再考虑吧, 算是一种思路, 后续如果用上了 代码里面还可以加个header_remove("Set-Cookie");隐藏响应头中的session_id

from neo-regeorg.

L-codes avatar L-codes commented on June 8, 2024

@ViCrack 确实!不过 header_remove("Set-Cookie") 不能用,因为部分负载均衡的环境下,是根据 Cookie 分流到同一台主机的

from neo-regeorg.

Related Issues (20)

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.