Improper Assets Management

Hello everyone! Let’s talk about something that’s been weighing heavily on the minds of developers and security experts alike: improper assets management in API security. You know what they say, a chain is only as strong as its weakest link. And when it comes to APIs, the weakest link is often the way we manage our assets.

But don’t worry, we’re here to help! With a little bit of humor and some easy-to-understand code examples. We’ll show you how to secure your asset management & keep your APIs secure from threats like hackers and prying eyes. So sit back; relax & let’s dive into the world of proper assets management in API security.

Real-World Scenario

Let’s say; you’re building APIs for a social-media platform that allows users to upload photos. To store the photos, you decide to save them in a folder, on the server.

  • Storing files without proper security measures: You write the following code to store the photos:
<?php
$photo = $_FILES["photo"]["tmp_name"];
$filename = $_FILES["photo"]["name"];

move_uploaded_file($photo, "/var/www/images/$filename");
?>
  • Hacker’s attack: A malicious file is uploaded to the server after a hacker finds a vulnerability in your API. An executable script found in the malicious file enables the hacker to access private data by executing code on the server.
  • The consequences: This breach may have serious consequences. The hacker has access to server resources, sensitive data like user data and even full control of the platform. Users will quite using the platform and the company will experience a huge loss in revenue and reputation.
  • Fixing the issue: To fix the issue; you need to implement proper security measures to ensure that only trusted files/images are allowed to be uploaded to the server. Here’s an updated version of the code;
<?php
$photo = $_FILES["photo"]["tmp_name"];
$filename = $_FILES["photo"]["name"];

$allowed_extensions = array("jpg", "jpeg", "png", "gif");
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));

if (in_array($extension, $allowed_extensions)) {
    move_uploaded_file($photo, "/var/www/images/$filename");
} else {
    echo "File type not allowed.";
}
?>

This way, you can ensure that only trusted file types are allowed to be uploaded to the server reducing the risk of a security breach.
This scenario shows how important it is to secure your assets, especially files that are uploaded to the server. Improper assets management can lead to severe consequences, so it’s crucial to always follow best practices and secure your APIs.

Some Vulnerabilities and their Preventions of Improper Assets Management

Improper assets management can lead to several vulnerabilities in APIs. Here are few most common ones along with examples and code snippets to illustrate the issues.

Insecure File Upload

Insecure file upload occurs when an attacker is able to upload a malicious file/image to the server. For example; consider the following code:

$filename = $_FILES["file"]["name"];
$temp_file = $_FILES["file"]["tmp_name"];
$destination = "/var/www/uploads/$filename";

move_uploaded_file($temp_file, $destination);

In this code; an attacker could upload a malicious file that could compromise the server.
To prevent insecure file upload, you can check the file type and size before uploading, as in the following code:

$filename =
$_FILES["file"]["name"];
$temp_file = $_FILES["file"]["tmp_name"];
$destination = "/var/www/uploads/$filename";
$allowed_file_types = array("image/jpeg", "image/png", "image/gif");
$max_filesize = 1000000;

if (in_array($_FILES["file"]["type"], $allowed_file_types) && $_FILES["file"]["size"] <= $max_filesize) {
move_uploaded_file($temp_file, $destination);
} else {
echo "File type not allowed or file size too large.";
}

Directory Traversal

An attack, that allows an attacker to access files outside of the authorized directory is known as directory-traversal. The file path can be modified by the attacker by using ../. For example; consider the following code:

$filename = $_GET["file"];
$file = "/var/www/images/$filename";

if (file_exists($file)) {
    readfile($file);
} else {
    echo "File not found.";
}

In this code, an attacker could send a request to ‘http://your-api.com/getimage.php?file=../../secret/secret.txt‘, which would allow them to access the ‘secret.txt‘ file.
To prevent directory traversal attacks, you can use a whitelist of allowed filenames as in the following code;

$filename = $_GET["file"];
$allowed_filenames = array("image1.jpg", "image2.jpg", "image3.jpg");

if (in_array($filename, $allowed_filenames)) {
    $file = "/var/www/images/$filename";

    if (file_exists($file)) {
        readfile($file);
    } else {
        echo "File not found.";
    }
} else {
    echo "File type not allowed.";
}

Session Hijacking

Session hijacking occurs when an attacker is able to steal a user’s session and gain unauthorized access to sensitive information. For example; consider the following code:

session_start();

if (isset($_SESSION["logged_in"]) && $_SESSION["logged_in"] == true) {
    echo "Welcome, " . $_SESSION["username"];
} else {
    echo "Access denied.";
}
```
```
```

In this code; an attacker could steal the user’s session ID & use it to gain access to sensitive information.
To prevent session hijacking, you can use secure session IDs and IP addresses, as in the following code:

session_start();

if (isset($_SESSION["logged_in"]) && $_SESSION["logged_in"] == true && $_SESSION["ip_address"] == $_SERVER["REMOTE_ADDR"]) {
    echo "Welcome, " . $_SESSION["username"];
} else {
    echo "Access denied.";
}

$_SESSION["logged_in"] = true;
$_SESSION["username"] = "infoalth";
$_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
```
```
```

Unrestricted File Upload

Unrestricted file upload occurs when an attacker is able to upload any type of file including executable files. For illustration; consider the following code:

$filename = $_FILES["file"]["name"];
$temp_file = $_FILES["file"]["tmp_name"];
$destination = "/var/www/uploads/$filename";

move_uploaded_file($temp_file, $destination);
```
```

In this code, an attacker could upload a malicious file, such as a PHP script, that could compromise the server.
To prevent unrestricted file upload, you can restrict the file types that can be uploaded as in the following code;

$filename = $_FILES["file"]["name"];
$temp_file = $_FILES["file"]["tmp_name"];
$destination = "/var/www/uploads/$filename";
$allowed_file_types = array("image/jpeg", "image/png", "image/gif");

if (in_array($_FILES["file"]["type"], $allowed_file_types)) {
    move_uploaded_file($temp_file, $destination);
    } else {
echo "File type not allowed.";
}

Before permitting a file to be uploaded, you can also validate the file size and look inside it for any malicious content. This can be done by comparing the file’s contents to a list of known malicious file signatures or by using tools like virus scanners.

$filename = $_FILES["file"]["name"];
$temp_file = $_FILES["file"]["tmp_name"];
$destination = "/var/www/uploads/$filename";
$allowed_file_types = array("image/jpeg", "image/png", "image/gif");
$max_filesize = 1000000;

if (in_array($_FILES["file"]["type"], $allowed_file_types) && $_FILES["file"]["size"] <= $max_filesize) {
    // Check for malicious content here
    move_uploaded_file($temp_file, $destination);
} else {
    echo "File type not allowed or file size too large.";
}
```
```

Sensitive Data Exposure

Sensitive data exposure occurs when confidential information is stored in an insecure manner.
For example: consider the following code;

$password = $_GET["password"];

if ($password == "secret") {
    echo "Access granted.";
} else {
    echo "Access denied.";
}

In this code, the password is passed in plain text as part of the URL. An attacker could easily intercept this information and use it to gain unauthorized access.
To prevent sensitive data exposure, you can use encryption and hash functions as in the following code:

$password = $_GET["password"];
$hash = hash("sha256", $password);

if ($hash == "0e7338269869798666747356783189345678943256789") {
    echo "Access granted.";
} else {
    echo "Access denied.";
}

By placing these security measures in place you can aid in defending your application against the risk associated with improper assets management. It is crucial to always be observant and stay up-to-date with the latest security threats and vulnerabilities to ensure the safety and security of your application and its customers.

Summary

Improper assets management is a common vulnerability in web applications that can lead to serious security risks such as data theft, unauthorized access and malware infections. Some illustrations of improper assets management include uploading untrusted files, improperly storing sensitive information and using weak file permissions. To prevent these vulnerabilities, it’s important to implement proper validation and security measures when uploading and storing assets in your application. This can be achieved by checking file types, validating file size, scanning for malicious content and properly securing files with strong file permissions. To stay secure, it’s essential to stay up-to-date with the latest security threats and implement best practices in your application’s asset management process.

References

OWASP API Security TOP-10
OWASP API Security Project