使用c语言调用python小结

最近在做一个漏洞展示平台,攻击实现部分使用Python实现,C语言实现部分使用libcli库做一个类似telnet的东东,回调函数run的时候调用python模块。针对c调用python,做个了小demo

python模块:demo.py

[python]  view plain  copy
  1. def print_arg(str):  
  2.     print str  
  3.   
  4. def add(a,b):  
  5.     print 'a=', a  
  6.     print 'b=', b  
  7.     return a + b  
  8.   
  9. class Class_A:  
  10.     def __init__(self):  
  11.         print "init"  
  12.     def fun(self, str):  
  13.         print 'hello', str  
  14.         return str  
  15.   
  16.   
  17. class dedecms_get_webshell:  
  18.     def __init__(self):  
  19.         ''''' 
  20.         '''  
  21.         self._run = True  
  22.   
  23.     #must rewrite function  
  24.     def check(self,site,port):  
  25.         ''''' 
  26.         '''  
  27.         print "Exploiting Host:%s,Port:(%d)......" % (site,port)  
  28.         flag = 1  
  29.         if flag:  
  30.             content={"flag":1,"content":"POST http://www.baidu.com/shell.php (cmd)"}  
  31.         else:  
  32.             content={"flag":0,"content":"POST http://www.baidu.com/shell.php (cmd)"}  
  33.         return content  
  34. if __name__=="__main__":  
  35.     site="www.baidu.com"  
  36.     port=80  
  37.     obj = dedecms_get_webshell()  
  38.     ret=obj.check(site,port)  
  39. print ret  

分析:

1. print_arg定义了一个传参的函数

2. add 定义了一个传如多个参数,且有返回值的函数

3. Class_A定义了一个类及类的一个方法fun(传参数,有返回值)

4. dedecms_get_webshell定一个了类及类的一个方法check(传多个参数,返回值是个元组)

 

下面使用c语言调用demo.py文件中的函数。

 测试函数:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <Python.h>  
  2.   
  3. int main(int argc, char* argv[])  
  4. {  
  5.     test();  
  6.     test1();  
  7.     test2();  
  8.     test3();  
  9.     test4();  
  10.   
  11.     return 0;  
  12. }  

逐个分析:

//导出当前环境变量

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void getcurrent()  
  2. {  
  3.     PyRun_SimpleString("import sys");  
  4.     PyRun_SimpleString("sys.path.append('./')");  
  5.     return;  
  6. }  

1. 一个最基本的调用方式

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void test()  
  2. {  
  3.     Py_Initialize();//初始化python  
  4.     PyRun_SimpleString("print 'hello python'");//直接运行python代码  
  5.     Py_Finalize(); //释放python  
  6.     return;  
  7. }  

分析:直接运行python 代码,在调用的时候必须先做初始化操作(Py_Initialize),调用完后做清理工作(Py_Finalize)

2. 调用模块中的一个普通函数

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void test1()  
  2. {  
  3.     Py_Initialize();//初始化python  
  4.     getcurrent();  
  5.   
  6.     PyObject *pModule = NULL, *pFunc = NULL, *pArg = NULL;  
  7.     pModule = PyImport_ImportModule("demo");//引入模块  
  8.     pFunc = PyObject_GetAttrString(pModule, "print_arg");//直接获取模块中的函数  
  9.     pArg = Py_BuildValue("(s)""hello_python"); //参数类型转换,传递一个字符串。将c/c++类型的字符串转换为python类型,元组中的python类型查看python文档  
  10.     PyEval_CallObject(pFunc, pArg); //调用直接获得的函数,并传递参数  
  11.   
  12.     Py_Finalize(); //释放python  
  13.     return;  
  14. }  

分析:先引用模块(PyImport_ImportModule),然后获取模块中的函数(PyObject_GetAttrString),对c传入python 的参数做类型转换(Py_BuildValue("(s)","hello_python")),最后直接调用函数并传递参数(PyEval_CallObject)。

3. 调用模块中的一个函数(多参数,带返回值)

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void test2()  
  2. {  
  3.     Py_Initialize();  
  4.     getcurrent();  
  5.   
  6.     PyObject *pModule = NULL, *pDict = NULL, *pFunc = NULL, *pArg = NULL, *result = NULL;  
  7.     pModule = PyImport_ImportModule("demo"); //引入模块  
  8.     pDict = PyModule_GetDict(pModule); //获取模块字典属性 //相当于Python模块对象的__dict__ 属性,得到模块名称空间下的字典对象  
  9.     pFunc = PyDict_GetItemString(pDict, "add"); //从字典属性中获取函数  
  10.     pArg = Py_BuildValue("(i, i)", 1, 2); //参数类型转换,传递两个整型参数  
  11.     result = PyEval_CallObject(pFunc, pArg); //调用函数,并得到python类型的返回值  
  12.     int sum;  
  13.     PyArg_Parse(result, "i", &sum); //将python类型的返回值转换为c/c++类型  
  14.     printf("sum=%d\n", sum);  
  15.   
  16.     Py_Finalize();  
  17. }  

4. 调用模块中简单的一个类(单个返回值)

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void test3()  
  2. {  
  3.     Py_Initialize();  
  4.     getcurrent();  
  5.   
  6.     PyObject *pModule = NULL, *pDict = NULL, *pClass = NULL, *pInstance = NULL, *result = NULL;  
  7.     pModule = PyImport_ImportModule("demo"); //引入模块  
  8.     pDict = PyModule_GetDict(pModule); //获取模块字典属性  
  9.     pClass = PyDict_GetItemString(pDict, "Class_A"); //通过字典属性获取模块中的类  
  10.     pInstance = PyInstance_New(pClass, NULL, NULL);//实例化获取的类  
  11.     result = PyObject_CallMethod(pInstance, "fun""(s)""python_000"); //调用类的方法  
  12.     char* name=NULL;  
  13.     PyArg_Parse(result, "s", &name); //将python类型的返回值转换为c/c++类型  
  14.     printf("%s\n", name);  
  15.   
  16.     Py_Finalize();  
  17. }  

5. 调用模块中一个简单的类(返回值是个元组)

void test4()

{

    Py_Initialize();

    getcurrent();

 

    PyObject *pModule = NULL, *pDict = NULL,*pClass = NULL, *pInstance = NULL, *result = NULL;

    pModule =PyImport_ImportModule("demo"); //引入模块

    pDict = PyModule_GetDict(pModule); //获取模块字典属性

    pClass = PyDict_GetItemString(pDict,"dedecms_get_webshell"); //通过字典属性获取模块中的类

    pInstance = PyInstance_New(pClass, NULL,NULL);

    result = PyObject_CallMethod(pInstance,"check", "(s,i)", "www.baidu.com", 80);

    int flag;

    char*content = NULL;

    PyObject *obj_content =PyDict_GetItemString(result, "content");

    content = PyString_AsString(obj_content);

    PyObject *obj_flag =PyDict_GetItemString(result, "flag");

    flag = PyInt_AsLong(obj_flag);

    printf("content: %s, flag: %d\n",content, flag);

 

    Py_Finalize();

}

Makefile书写:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. all: test  
  2.   
  3. test: pytest.o  
  4.     gcc -L/usr/lib/python2.7/ -lpython2.7 -ldl pytest.o -o test  
  5.   
  6. pytest.o: pytest.c  
  7.     gcc -g -std=gnu99 -Wall -c  pytest.c -I/usr/include/python2.7/   
  8.   
  9. clean:  
  10.     @rm -rf *.o *.pyc test  

针对python不同的版本,使用2.5,2.6等,库的路径参照安装的路径。

 

本打算展开多写些东西,结果草草完事。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值