c 语言 cgi,几种语言的 CGI 编程

为了了解 PHP、JSP、ASP 出现之前人们写 站的方法,洒家研究了一波 CGI,使用 C、Python、batch、shell script 语言写了几个简单的 页。

CGI 即通用 关接口,指 Web 服务器调用编程语言编写的程序的一个接口。洒家用的是 Apache 的 CGI,QUERY_STRING、REMOTE_ADDR、REQUEST_URI 等参数是通过环境变量传递给 CGI 程序的,请求主体(POST 数据)作为 CGI 程序的标准输入(stdin),而 CGI 程序的标准输出(stdout)作为 HTTP 响应的部分(注:标准错误输出 stderr 会出现在错误日志中)。

系统和软件环境配置?

WAMP Apache 2.4.18 64 位,Ubuntu Server 16.04 64 位。

需要开启 Apache 的 cgi_module。

sudo a2enmod cgi

sudo service apache2 restart

对于 Linux,cgi-bin 的目录在 /etc/apache2/conf-enabled/serve-cgi-bin.conf 中规定,使用浏览器访问这个目录的 alias:/cgi-bin/。对于 Windows 下的 WAMP 套件,对应目录默认在安装目录内(例如 C:/wamp64/cgi-bin/)。

一个 say hello 的动态 页?

Linux/Windows,Python,头部需要加 Python 可执行文件的位置。对于 Windows,则可能是 #!C:/Python27/python.exe。

#!/usr/local/bin/python

import os

import urllib

import sys

import cgi

print ‘Content-Type: text/htmln’

postData = sys.stdin.read()

if postData != ”:

items = postData.split(‘&’)

for item in items:

key,value = urllib.splitvalue(item)

if key == ‘name’:

print ‘

Hello,%s

‘ % (cgi.escape(urllib.unquote_plus(value)),)

print ”’

”’

print os.environ[‘QUERY_STRING’]

效果如下图。此程序读取 POST 数据(stdin),Tom 未在 URL 中出现。

执行任意命令的 Webshell,及其过程与经验?

Linux,shell script

#!/bin/bash

echo -e ‘Content-Type: text/htmln’

echo ‘

I am using cgi (shell script)

echo ‘

echo ”

echo ”

echo ‘

echo -e ‘n

#echo ${QUERY_STRING}

cmd=${QUERY_STRING#’cmd=’}

#echo ${cmd}

cmd=${cmd:-‘ping -c 2 baidu.com’}

cmd=$(echo ${cmd}| python -c ‘import sys;import urllib;sys.stdout.write(urllib.unquote_plus(sys.stdin.read()))’)

echo ${cmd}

echo ‘


eval ${cmd} 2>&1

#ping -c 2 baidu.com

echo -e ‘n

n’

效果:

这个错误输出和下面的情况的输出相同:

user@localhost:/usr/lib/cgi-bin$cmd=python-c ‘importthis’

user@localhost:/usr/lib/cgi-bin$ echo $cmd

python -c ‘import this’

user@localhost:/usr/lib/cgi-bin$ $cmd

File “”, line 1

‘import

^

SyntaxError: EOL while scanning string literal

user@localhost:/usr/lib/cgi-bin$ eval $cmd

The Zen of Python, by Tim Peters

Beautiful is better than ugly.

Explicit is better than implicit.

经过一番研究之后洒家恍然大悟,这个 错表示,Python 收到的参数(sys.argv[2])为 ‘import,单引 的作用不是命令行中参数的边界,而是直接作为参数的一部分传进了 Python。也就是说,直接执行 ${cmd} 相当于 Python 中的

subprocess.call([“python”,”-c”,”‘import”,”this'”])

一个字符串变量直接执行会有问题,正确的做法要用 eval 命令“扫描两次”,才能正确解析。使用 eval ${cmd} 使 shell 重新扫描命令,把 import 和 this 看做同一个参数,相当于 subprocess.call([“python”,”-c”,”import this”])。

总而言之,这次的错误类似于这种情况:

user@localhost:/usr/lib/cgi-bin$pipe=”|”

user@localhost:/usr/lib/cgi-bin$ ls $pipe grep sh

ls: cannot access ‘|’: No such file or directory

ls: cannot access ‘grep’: No such file or directory

ls: cannot access ‘sh’: No such file or directory

user@localhost:/usr/lib/cgi-bin$eval ls $pipe grep sh

env_var.sh

test.sh

使用 Windows batch script(批处理)写的一个功能有限的 webshell?

批处理,这个的坑也有点多,而且洒家也不太熟悉,只能写这么多了。

@echo off

echo Content-Type: text/html; charset=GBK

echo.

echo ^

batch webshell^

echo ^

echo ^

echo ^

echo ^

echo ^

set ccmmdd=%QUERY_STRING:~4%

set ccmmdd=%ccmmdd:+= %

set ccmmdd=%ccmmdd:^%20= %

echo ^

%ccmmdd%

echo ^

echo ^

相关资源:基于单片机的智能灌溉喷洒系统的设计_单片机灌溉喷头-单片机文档…

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年4月16日
下一篇 2021年4月16日

相关推荐