SQL Injection? Reduce your attack surface!

There are a few good ways to reduce the attack surface of SQL injection attacks, both in MySQL and more broadly in database security. 

First, input validation. Ensuring that an input matches an expected type or range of values, which can be done in code or with built-in filters, can help prevent the execution of malicious code through things like compromised URLs. It’s important not to trust client-side validation, since tools like cURL can bypass it. Server side validation should be used whenever possible.

Using precompiled SQL queries, like prepared statements, can also help. There are libraries that can be used which contain functions DBAs can use to keep functionality secure and consistent. Prepared statements generally require less overhead than the alternatives, and because they use parameterized values, they can use unescaped values and delimiters. Forcing the developer to prepare the SQL command separately from processing user data makes it challenging to stage a SQL injection attack.

for example, when user data is embedded directly in a SQL query:

$qry = “Select * From useraccounts Where user=’$usrname’ and password=’$pwd’;”

$proc = mysql_query($qry)

If a malicious user were to enter the username of a privileged account, they would be able to gain access without the need for the password. 

A prepared statement could be used to mitigate this risk:

$stmt = $mysqli->prepare(“Select * From useraccounts Where user= ? and password= ?;”

$stmt->bind_param(“ss”,$usrname,$pwd);

$stmt->execute();

Using placeholders in the prepared statement will prevent a string from being passed in and immediately executed. 

On the backend, prepared statements work by first parsing the query, checking semantics to establish validity of the query, then binding the query into machine-readable code. The DBMS then chooses a method for executing the query and caches it, to optimize future runs of the same query. Once that’s complete, the query is executed.

Broadly speaking, OWASP recommends the following techniques for avoiding SQL injection. 

Primary Defenses:

  • Option 1: Use of Prepared Statements (with Parameterized Queries)
  • Option 2: Use of Stored Procedures
  • Option 3: Allow-list Input Validation
  • Option 4: Escaping All User Supplied Input

Additional Defenses:

  • Also: Enforcing Least Privilege
  • Also: Performing Allow-list Input Validation as a Secondary Defense

They also provide language specific recommendations and further examples of safe and unsafe usage. 

Language specific recommendations:

  • Java EE – use PreparedStatement() with bind variables
  • .NET – use parameterized queries like SqlCommand() or OleDbCommand() with bind variables
  • PHP – use PDO with strongly typed parameterized queries (using bindParam())
  • Hibernate – use createQuery() with bind variables (called named parameters in Hibernate)
  • SQLite – use sqlite3_prepare() to create a statement object (Links to an external site.)

Link for more information: https://cheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html

Database Permissions – A quick overview

Privileges in databases, like other permissions, should be managed according to least privilege principles, including database administrators and security administrators. 

On that first point, it’s important to keep security and database administration roles separate whenever possible. The security admin should be the only user or group of users able to perform security-related tasks. To avoid issues of privilege creep, neither users nor administrators should have access to things to which they do not need access. 

When managing systems with multiple users, it’s important to ensure each user is identified uniquely, and prevent shared user accounts. Effectively monitoring user activities and the ability to manage audit trails is key to identifying sources of security breaches and hold user activity accountable.  

User groups and roles should relate to the function of the user. For example, users who work in sales should be grated access to sales databases, preferably with a dedicated admin or admins. As with system and file permissions, it’s also best to avoid assigning privileges directly to users. Users should be assigned to groups or roles, with privileges assigned to the group/role, instead of to the individual user.  

What the heck is an RTOS?

Generally speaking, a general-purpose OS like Windows is focused on the management of the hardware resources of a computer, and managing the application which run on the computer, while providing a smooth and accessible user interface for computer users to interact with.

While an RTOS has to manage CPU hardware and such as well, the primary development focus is on running applications with a high degree of reliability, and extremely precise timing. 

RTOS must be able to guarantee a maximum amount of time for its most critical processes, including OS calls and interrupt handling. There are two main categories here: “hard real-time” where the OS is able to guarantee maximum process time all of the time; and “soft real-time” where the OS is able to guarantee maximum process times most of the time, but not necessarily always. There are differing use cases for these categories depending on the sensitivity of the system being designed. If developed correctly, RTOS allows for extremely precise process control by giving developers a great deal of control over the prioritization of tasks.

In addition to hard vs soft real-time, there are a couple of other important concepts in RTOS development, such as:

Determinism: An application (or critical section of an application) is considered deterministic if its timing can be guaranteed within a certain margin of error.

Jitter: The amount of error in the timing of a task over subsequent iterations of a program or loop is referred to as jitter. When RTOS are developed well and fully optimized, they provide very little jitter, and what is present can be predicted and appropriately considered in scheduling and prioritization.

There are a couple of general classes of RTOS application, referred to as “event-response” and “closed loop.” Event response systems are used in cases which require a response to some external stimuli within a predictable amount of time, such as automated visual inspection of manufactured parts in high-precision manufacturing facilities. Closed loop systems continuously receive and respond to an ongoing feed of data, such as an automotive cruise control system.

RTOS are best-suited for use cases which require extended runtime without interruptions in service and are often intended for uninterrupted 24/7 operations. In cases where downtime is unacceptable, like airline flight control systems, self-driving vehicles, and precision manufacturing, RTOS provide reliability, predictability, and performance optimized for these sorts of workloads.