{"id":716,"date":"2021-09-01T22:03:05","date_gmt":"2021-09-01T14:03:05","guid":{"rendered":"http:\/\/www.wayln.com\/?p=716"},"modified":"2021-09-01T22:03:05","modified_gmt":"2021-09-01T14:03:05","slug":"python%e8%b0%83%e7%94%a8c-c-%ef%bc%88%e5%85%a5%e5%8f%82%e5%87%ba%e5%8f%82%e4%b8%ba%e6%8c%87%e9%92%88%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wayln.com\/?p=716","title":{"rendered":"python\u8c03\u7528c\/c++ \uff08\u5165\u53c2\u51fa\u53c2\u4e3a\u6307\u9488\uff09"},"content":{"rendered":"<p>python\u53ef\u4ee5\u4f7f\u7528ctypes\u5e93\u8c03\u7528c++\u7f16\u8bd1\u7684so\u5e93\u51fd\u6570<\/p>\n<p>0x01  c\/c++\u7f16\u8bd1\u4e3aso\u5e93\u6587\u4ef6<br \/>\n\u7f16\u8bd1C\u6587\u4ef6<br \/>\ngcc -o libpycallfoo.so -shared -fPIC rsa.c<\/p>\n<p>\u7f16\u8bd1C++\u6587\u4ef6<br \/>\ng++ -o libcallfoo.so -shared -fPIC rsa.cpp<br \/>\n\u5bf9\u4e8ecpp\u6587\u4ef6\u9700\u8981\u7528extern &#8220;C&#8221;{} \u628amain\u62ec\u8d77\u6765 ,\u5426\u5219\u6709\u53ef\u80fd\u62a5\u9519<\/p>\n<pre><code class=\"language-CPP line-numbers\">extern \"C\"{\nint foo(int len,char *p,char* ret)\n{\n    __int64 a1 = 0x36;\n    __int64 a2 = 0x100;\n    __int64 a3 = 0xb5547;\n\n    int j;\n    char xz[100]={};\n    memcpy(xz,p,len);\n    printf(\"your input is %s,len xz=%d\\n\",xz,strlen(xz));\n\n    int* pResult = new int[100];\/\/\u5bc6\u6587\n    int i;\n    for(i = 0; i &lt; strlen(xz); i++)\n    {\n        int result1 = rsa_mod(xz[i],0,0x101,0,0xb5547,0);\n        pResult[i] = swapEndian(result1);\n         \/\/printf(\"0x%04X \",pResult[i]);\n    }\n    memcpy((char *)ret,(char *)pResult,4*strlen(xz));\n    return 0;\n}\n}\n<\/code><\/pre>\n<p>0x02 ctypes\u8c03\u7528so<br \/>\n\u5165\u53c2\u4f7f\u7528ctypes.c_char_p\u521b\u5efa\uff0c\u51fa\u53c2\u9700\u8981\u4f7f\u7528ctypes.create_string_buffer\u521b\u5efa\u5185\u5b58\uff0c\u5426\u5219so\u4e2d\u7684\u5185\u5b58\u5728\u7528\u5b8c\u5c31\u91ca\u653e\u4e86\uff0c\u65e0\u6cd5\u4f20\u51fa\u6765<\/p>\n<pre><code class=\"language-python line-numbers\">import ctypes\n\ndef callfoo(str_in):\n    #print 'input:\\n%s' %str_in\n    #\u8c03\u7528\u5e93\n    input = ctypes.c_char_p()  #\u5bf9\u5e94c\u6307\u9488\u7c7b\u578b char *p\n    input.value=str_in  #\u5b57\u7b26\u4e32\u8d4b\u503c\n    ll = ctypes.cdll.LoadLibrary\n    lib = ll(\".\/libcallfoo.so\")  #\u8c03\u7528so\n    p=ctypes.create_string_buffer(4*len(str_in)) #\u7533\u8bf7\u51fa\u53c2\u7684\u5185\u5b58\u5927\u5c0f\n    lib.foo(len(str_in), input, p)\n    print p.raw    #\u51fa\u53c2\u7684\u8bbf\u95ee\u65b9\u5f0f\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>python\u53ef\u4ee5\u4f7f\u7528ctypes\u5e93\u8c03\u7528c++\u7f16\u8bd1\u7684so\u5e93\u51fd\u6570 0x01 c\/c++\u7f16\u8bd1\u4e3aso\u5e93\u6587\u4ef6 \u7f16\u8bd1C\u6587\u4ef6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6,2,33],"tags":[],"class_list":["post-716","post","type-post","status-publish","format-standard","hentry","category-c-c","category-python","category-2","category-33"],"_links":{"self":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/716","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=716"}],"version-history":[{"count":1,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/716\/revisions"}],"predecessor-version":[{"id":717,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/716\/revisions\/717"}],"wp:attachment":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}