Post

Cookie Arena Writeup: NSLookup_1

Exploit os-command-injection in Web Server.

Cookie Arena Writeup: NSLookup_1

Overview

Ta có mã của nó:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
  include_once('./ignore/design/design.php');
  $design = Design(__FILE__, 'NSLookup Tool');

  if (isset($_GET['domain'])) {
    $domain = $_GET['domain'];
    $result = shell_exec("nslookup $domain");
  }
?>
<html>
  <head>
    <title>NSLookup Tool</title>
    <style>
      .output {
        width: 40%;
        border: 1px solid #ccc;
        padding: 10px;
        margin-top: 10px;
      }
    </style>
  </head>
  <body>
    <?php if(strlen($result) > 0) { ?>
    <div class="output">
      <h2>NSLookup Result</h2>
      <pre><?php echo $result; ?></pre>
      <a href="/index.php">Back</a>
    </div>
    <?php } else { ?>
    <div>
        <h2>NSLookup Tool</h2>
        <form action="/index.php" method="GET">        
            <div class="form-group">
                <label for="domain">Domain</label>
                <input type="text" class="form-control" id="domain" name="domain">
                <button type="submit" class="btn btn-primary">LOOKUP!</button>
            </div>
            
        </form>
    </div>
    <?php } ?>

    <div>
      <?php echo $design; ?>  
    </div>    
  </body>
</html>

Tại dòng thứ 6, ta có hàm shell_exec được dùng để thực hiện nslookup, điều ta có thể khai thác được server nhờ đoạn code này là: nó không validate input cho domain:

1
2
3
4
if (isset($_GET['domain'])) {
    $domain = $_GET['domain'];
    $result = shell_exec("nslookup $domain");
  }

Exploitation

Khi tôi test với IP 103.97.125.56 thì ta sẽ được expected result như sau:

Tôi chợt nhận ra server đã slashback các kí tự đặc biệt và có encode vài kí tự khác

Sau khi test nhiều lần tôi chợt nhận ra nó đóng input của tôi vào trong cặp nháy đơn nên tôi phải escape nó. Minh chứng cho điều này là lỗi khi tôi input: 8.8.8.8' thì lập tức bạn sẽ quay trở lại index.php

Bây giờ bạn sẽ hình dung như sau:

1
2
3
4
nslookup '8.8.8.8''  // <-- wrong
nslookup '8.8.8.8';'ls'  // escaped
Hoặc:
nslookup '8.8.8.8';ls;'' // escaped way 2

Dấu hiệu bị encode cũng có thể cho ta thấy rằng nó được bọc trong 1 cặp string để kiểm tra (mặc dù index.php không tồn tại phần đó)

Exploit

Sử dụng cách 2 có thể giúp bạn tránh khỏi việc bị encode và khai thác sâu hơn:

Conclusion

Việc khai thắc web, ta phải cần tinh ý nhận biết dấu hiệu, hành vi của server có thể giúp bạn dễ dàng khai thác hơn là việc thụ động sử dụng payload


Happy hacking

This post is licensed under CC BY 4.0 by the author.