# lightly

There's also a video version, if you want to check it out: [lightly](https://youtu.be/18OGrv16Bsg)

**Port Scanning**

First we perform a port scan using nmap to identify open ports and the services and their versions running on these ports.

```
❯ nmap --open -sCV -n -Pn -T5 -v 10.0.160.170 -oN scan
<SNIP>

PORT   STATE SERVICE REASON  VERSION
80/tcp open  http    syn-ack nginx 1.18.0
|_http-title: Site doesn't have a title (text/html; charset=UTF-8).
|_http-favicon: Unknown favicon MD5: 844B11BD64CCAD62F48FD96A886544CA
| http-methods: 
|_  Supported Methods: GET HEAD POST
```

* `-p <ports>`
* `-sCV combines the options -sC and -sV.`
* nmap help: `-sC: equivalent to --script=default`&#x20;
* `-sV: Probe open ports to determine service/version info`&#x20;
* `-n: Never do DNS resolution`&#x20;
* `-vvv: Increase verbosity level`&#x20;
* `-oN <file>: Output scan in normal format.`

The result confirms our expectation: a web server is running on port 80. Let's use a web browser to view the site’s content:

<figure><img src="https://3863537643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDdYSqYDpT0Aiur7jvsM1%2Fuploads%2FIchciwwyGQK6znYgScml%2FPasted%20image%2020240731215647.png?alt=media&#x26;token=47ef4425-aec7-4def-af02-0ee6ff98cc8c" alt=""><figcaption></figcaption></figure>

**Code review**

\
The content is the code shown in the image above. Let's break it down step by step:

```
 <?php
if(isset($_GET['QUERY']))
{
    header('Content-Type: text/plain');
    $db = new SQLite3(':memory:', SQLITE3_OPEN_CREATE | SQLITE3_OPEN_READWRITE);
    $db->loadExtension('libfileio.so');
    $db->enableExceptions(true);
    $results = $db->query($_GET['QUERY']);
    var_dump($results->fetchArray(SQLITE3_NUM));
} else {
    highlight_file(__file__);
}
```

* The code starts by checking if a GET parameter named 'QUERY' is set using `isset($_GET['QUERY'])`
* If 'QUERY' is set:
  1. It sets the content type of the response to plain text.
  2. It creates a new in-memory SQLite database.
  3. It loads an extension called 'libfileio.so'.
  4. It enables exceptions for the database operations.
  5. It executes the query provided in the GET parameter directly on the database.
  6. It fetches the results of the query as a numeric array and dumps them to the output.

<figure><img src="https://3863537643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDdYSqYDpT0Aiur7jvsM1%2Fuploads%2Fw4vjWWPXOP53pWbSfxPS%2FPasted%20image%2020240731220623.png?alt=media&#x26;token=cc0d8895-d16b-44a0-8c53-809efbfbd8a8" alt=""><figcaption></figcaption></figure>

* If 'QUERY' is not set it displays the source code of the current file using `highlight_file()`

<figure><img src="https://3863537643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDdYSqYDpT0Aiur7jvsM1%2Fuploads%2FVzfIQgX4VqXi4JmicqOD%2FPasted%20image%2020240731220710.png?alt=media&#x26;token=c203c64a-95e3-45c1-9f95-4f19d95a2223" alt=""><figcaption></figcaption></figure>

At this point you may be wondering, "What is libfileio.so???"

If you search for it on Google and look at the following result:

<figure><img src="https://3863537643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDdYSqYDpT0Aiur7jvsM1%2Fuploads%2Fdm2vqy29BuAyXfgfKTxM%2FPasted%20image%2020240731233312.png?alt=media&#x26;token=7678dca7-584b-4b5f-8e37-267ad58a7499" alt=""><figcaption></figcaption></figure>

Here you will find a post on the SQLite Forum

<figure><img src="https://3863537643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDdYSqYDpT0Aiur7jvsM1%2Fuploads%2F4WRKt5Hrh4WqRPtsMySQ%2FPasted%20image%2020240731233734.png?alt=media&#x26;token=8d37d9ec-89da-4a8b-a973-1c2066b71d20" alt=""><figcaption></figcaption></figure>

In this post a user reported an issue with the loadable extension `ext/misc/fileio.c`, which throws the error "undefined symbol sqlite3\_sqlitefileio\_init." Another user suggests renaming the shared library to `libfileio.so` as a solution.

If you search for this file on Google and look at the following result:

<figure><img src="https://3863537643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDdYSqYDpT0Aiur7jvsM1%2Fuploads%2FZLEAJEvtbiJ17kUUERsj%2FPasted%20image%2020240731234143.png?alt=media&#x26;token=d61def95-2b35-40a6-9c08-344d1a7a05da" alt=""><figcaption></figcaption></figure>

You find a file written in C with a part that looks interesting:&#x20;

<figure><img src="https://3863537643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDdYSqYDpT0Aiur7jvsM1%2Fuploads%2Fyr9tA5arLbl79A0oxwW3%2FPasted%20image%2020240731234320.png?alt=media&#x26;token=51096ef2-2010-49aa-a8b9-71019729477a" alt=""><figcaption></figcaption></figure>

&#x20;Basically this code allows us to read files using readfile() and write files using writefile().

**Solution**

The main vulnerability here is the lack of validation. The script directly executes any SQL query passed to it through the 'QUERY' GET parameter without any kind of validation or escaping. For solving this target we have to read the /etc/passwd file, with the query `SELECT readfile('/etc/passwd')` this way:

<figure><img src="https://3863537643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDdYSqYDpT0Aiur7jvsM1%2Fuploads%2Fjo8MBFrc9XawpUJ0aa80%2FPasted%20image%2020240731222755.png?alt=media&#x26;token=7778c2c9-f15b-4563-b4b0-1cd5cc911d07" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://b13ss3d.gitbook.io/hackmex-prep/readme/lightly.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
