Interacting with Common Services

Vulnerabilities are commonly discovered by people who use and understand technology, a protocol, or a service. As we evolve in this field, we will find different services to interact with, and we will need to evolve and learn new technology constantly.

To be successful at attacking a service, we need to know its purpose, how to interact with it, what tools we can use, and what we can do with it. This section will focus on common services and how we can interact with them.

File Share Services

A file sharing service is a type of service that provides, mediates, and monitors the transfer of computer files. Years ago, businesses commonly used only internal services for file sharing, such as SMB, NFS, FTP, TFTP, SFTP, but as cloud adoption grows, most companies now also have third-party cloud services such as Dropbox, Google Drive, OneDrive, SharePoint, or other forms of file storage such as AWS S3, Azure Blob Storage, or Google Cloud Storage. We will be exposed to a mixture of internal and external file-sharing services, and we need to be familiar with them.

This section will focus on internal services, but this may apply to cloud storage synced locally to servers and workstations.

Server Message Block (SMB)

SMB is commonly used in Windows networks, and we will often find share folders in a Windows network. We can interact with SMB using the GUI, CLI, or tools. Let us cover some common ways of interacting with SMB using Windows & Linux.

Windows

There are different ways we can interact with a shared folder using Windows, and we will explore a couple of them. On Windows GUI, we can press [WINKEY] + [R] to open the Run dialog box and type the file share location, e.g.: \\192.168.220.129\Finance\

Suppose the shared folder allows anonymous authentication, or we are authenticated with a user who has privilege over that shared folder. In that case, we will not receive any form of authentication request, and it will display the content of the shared folder.

If we do not have access, we will receive an authentication request.

Windows has two command-line shells: the Command shellarrow-up-right and PowerShellarrow-up-right. Each shell is a software program that provides direct communication between us and the operating system or application, providing an environment to automate IT operations.

Let's discuss some commands to interact with file share using Command Shell (CMD) and PowerShell. The command dirarrow-up-right displays a list of a directory's files and subdirectories.

Windows CMD - DIR

The command net usearrow-up-right connects a computer to or disconnects a computer from a shared resource or displays information about computer connections. We can connect to a file share with the following command and map its content to the drive letter n.

Windows CMD - Net Use

We can also provide a username and password to authenticate to the share.

With the shared folder mapped as the n drive, we can execute Windows commands as if this shared folder is on our local computer. Let's find how many files the shared folder and its subdirectories contain.

Windows CMD - DIR

We found 29,302 files. Let's walk through the command:

Syntax
Description

dir

Application

n:

Directory or drive to search

/a-d

/a is the attribute and -d means not directories

/s

Displays files in a specified directory and all subdirectories

/b

Uses bare format (no heading information or summary)

The following command | find /c ":\\" process the output of dir n: /a-d /s /b to count how many files exist in the directory and subdirectories. You can use dir /? to see the full help. Searching through 29,302 files is time consuming, scripting and command line utilities can help us speed up the search. With dir we can search for specific names in files such as:

  • cred

  • password

  • users

  • secrets

  • key

  • Common File Extensions for source code such as: .cs, .c, .go, .java, .php, .asp, .aspx, .html.

If we want to search for a specific word within a text file, we can use findstrarrow-up-right.

Windows CMD - Findstr

We can find more findstr examples herearrow-up-right.

Windows PowerShell

PowerShell was designed to extend the capabilities of the Command shell to run PowerShell commands called cmdlets. Cmdlets are similar to Windows commands but provide a more extensible scripting language. We can run both Windows commands and PowerShell cmdlets in PowerShell, but the Command shell can only run Windows commands and not PowerShell cmdlets. Let's replicate the same commands now using Powershell.

Windows PowerShell

Instead of net use, we can use New-PSDrive in PowerShell.

To provide a username and password with Powershell, we need to create a PSCredential objectarrow-up-right. It offers a centralized way to manage usernames, passwords, and credentials.

Windows PowerShell - PSCredential Object

In PowerShell, we can use the command Get-ChildItem or the short variant gci instead of the command dir.

Windows PowerShell - GCI

We can use the property -Include to find specific items from the directory specified by the Path parameter.

The Select-String cmdlet uses regular expression matching to search for text patterns in input strings and files. We can use Select-String similar to grep in UNIX or findstr.exe in Windows.

Windows PowerShell - Select-String

CLI enables IT operations to automate routine tasks like user account management, nightly backups, or interaction with many files. We can perform operations more efficiently by using scripts than the user interface or GUI.

Linux

Linux (UNIX) machines can also be used to browse and mount SMB shares. Note that this can be done whether the target server is a Windows machine or a Samba server. Even though some Linux distributions support a GUI, we will focus on Linux command-line utilities and tools to interact with SMB. Let's cover how to mount SMB shares to interact with directories and files locally.

Linux - Mount

As an alternative, we can use a credential file.

The file credentialfile has to be structured like this:

circle-exclamation

Once a shared folder is mounted, you can use common Linux tools such as find or grep to interact with the file structure. Let's hunt for a filename that contains the string cred:

Linux - Find

Next, let's find files that contain the string cred:

Other Services

There are other file-sharing services such as FTP, TFTP, and NFS that we can attach (mount) using different tools and commands. However, once we mount a file-sharing service, we must understand that we can use the available tools in Linux or Windows to interact with files and directories. As we discover new file-sharing services, we will need to investigate how they work and what tools we can use to interact with them.

Email

We typically need two protocols to send and receive messages, one for sending and another for receiving. The Simple Mail Transfer Protocol (SMTP) is an email delivery protocol used to send mail over the internet. Likewise, a supporting protocol must be used to retrieve an email from a service. There are two main protocols we can use POP3 and IMAP.

We can use a mail client such as Evolutionarrow-up-right, the official personal information manager, and mail client for the GNOME Desktop Environment. We can interact with an email server to send or receive messages with a mail client. To install Evolution, we can use the following command:

triangle-exclamation

Video - Connecting to IMAP and SMTP using Evolution

We can use the domain name or IP address of the mail server. If the server uses SMTPS or IMAPS, we'll need the appropriate encryption method (TLS on a dedicated port or STARTTLS after connecting). We can use the Check for Supported Types option under authentication to confirm if the server supports our selected method.

Databases

Databases are typically used in enterprises, and most companies use them to store and manage information. There are different types of databases, such as Hierarchical databases, NoSQL (or non-relational) databases, and SQL relational databases. We will focus on SQL relational databases and the two most common relational databases called MySQL & MSSQL. We have three common ways to interact with databases:

1.

Command Line Utilities (mysql or sqsh)

2.

A GUI application to interact with databases such as HeidiSQL, MySQL Workbench, or SQL Server Management Studio.

3.

Programming Languages

MySQL example

Let's explore command-line utilities and a GUI application.

Command Line Utilities

MSSQL

To interact with MSSQL (Microsoft SQL Server)arrow-up-right with Linux we can use sqsharrow-up-right or sqlcmdarrow-up-right if you are using Windows. Sqsh is much more than a friendly prompt. It is intended to provide much of the functionality provided by a command shell, such as variables, aliasing, redirection, pipes, back-grounding, job control, history, command substitution, and dynamic configuration. We can start an interactive SQL session as follows:

Linux - SQSH

The sqlcmd utility lets you enter Transact-SQL statements, system procedures, and script files through a variety of available modes:

  • At the command prompt.

  • In Query Editor in SQLCMD mode.

  • In a Windows script file.

  • In an operating system (Cmd.exe) job step of a SQL Server Agent job.

Windows - SQLCMD

To learn more about sqlcmd usage, you can see Microsoft documentationarrow-up-right.

MySQL

To interact with MySQLarrow-up-right, we can use MySQL binaries for Linux (mysql) or Windows (mysql.exe). MySQL comes pre-installed on some Linux distributions, but we can install MySQL binaries for Linux or Windows using this guidearrow-up-right. Start an interactive SQL Session using Linux:

Linux - MySQL

We can easily start an interactive SQL Session using Windows:

Windows - MySQL

GUI Application

Database engines commonly have their own GUI application. MySQL has MySQL Workbencharrow-up-right and MSSQL has SQL Server Management Studio or SSMSarrow-up-right, we can install those tools in our attack host and connect to the database. SSMS is only supported in Windows. An alternative is to use community tools such as dbeaverarrow-up-right. dbeaverarrow-up-right is a multi-platform database tool for Linux, macOS, and Windows that supports connecting to multiple database engines such as MSSQL, MySQL, PostgreSQL, among others, making it easy for us, as an attacker, to interact with common database servers.

To install dbeaverarrow-up-right using a Debian package we can download the release .deb package from https://github.com/dbeaver/dbeaver/releasesarrow-up-right and execute the following command:

Install dbeaver

To start the application use:

Run dbeaver

To connect to a database, we will need a set of credentials, the target IP and port number of the database, and the database engine we are trying to connect to (MySQL, MSSQL, or another).

Video - Connecting to MSSQL DB using dbeaver

Video - Connecting to MySQL DB using dbeaver

Once we have access to the database using a command-line utility or a GUI application, we can use common Transact-SQL statementsarrow-up-right to enumerate databases and tables containing sensitive information such as usernames and passwords. If we have the correct privileges, we could potentially execute commands as the MSSQL service account. Later in this module, we will discuss common Transact-SQL statements and attacks for MSSQL & MySQL databases.

Tools

It is crucial to get familiar with the default command-line utilities available to interact with different services. However, as we move forward in the field, we will find tools that can help us be more efficient. The community commonly creates those tools. Although, eventually, we will have ideas on how a tool can be improved or for creating our own tools, even if we are not full-time developers, the more we get familiar with hacking. The more we learn, the more we find ourselves looking for a tool that does not exist, which may be an opportunity to learn and create our tools.

Tools to Interact with Common Services

General Troubleshooting

Depending on the Windows or Linux version we are working with or targetting, we may encounter different problems when attempting to connect to a service.

Some reasons why we may not have access to a resource:

  • Authentication

  • Privileges

  • Network Connection

  • Firewall Rules

  • Protocol Support

Keep in mind that we may encounter different errors depending on the service we are targeting. We can use the error codes to our advantage and search for official documentation or forums where people solved an issue similar to ours.

Last updated