Home/Blog/Automatic malicious file upload detection with Uploadcare

Automatic malicious file upload detection with Uploadcare

Imagine this: youโ€™ve just come across anย adorable kitten picture, eager toย upload itย toย your gallery. But hidden within the seemingly innocent image lies aย threatโ€”malicious code disguised inย the metadata. With aย single upload, your system could beย compromised, like inย this simple snippet:

function runMaliciousCode() {
  const payload = `alert('You have been hacked!')`;
  eval(payload);
}

Unfortunately, this isย not just anย imaginary situation. Hackers often exploit vulnerabilities inย file uploads and embed malicious code inย ways that are invisible toย the average user. These attacks can come from various angles, affecting everything from images and videos toย documents.

Inย this article, weโ€™ll explore the most common ways hackers inject malicious content into files and discuss how Uploadcare detects and mitigates these threats during aย file upload process.

How does aย file get infected

Aย file can beย corrupted orย contain malicious content inย various ways. Letโ€™s consider some ways inย which toย inject malicious content into files during the upload process:

  1. Embedded malicious code inย files: Malicious code can beย hidden inside images (e.g., inย EXIF metadata) orย other media files like videos orย audio. When this file isย opened, the malicious code isย executed. Anย example isย Stegosploit, where hackers hide malicious code inย images, which isย executed when the image isย opened orย processed byย aย server.

  2. File format exploitation: Files like images (JPEG, PNG) orย documents (PDF, DOCX) can contain specially crafted content that triggers vulnerabilities inย how aย server parses orย opens the file. When the server attempts toย open orย parse the file, itย triggers aย buffer overflow orย other vulnerability, allowing the hacker toย execute arbitrary code.

  3. Changing file extensions: Byย disguising executables asย safe files, hackers might upload anย executable (.exe, .bat) but disguise itย byย changing the extension toย something more familiar and seemingly safe, like .jpg or .txt. Harmful programs could beย executed ifย the server doesnโ€™t validate the file type correctly. Files can beย named like file.jpg.exe, where the true executable nature ofย the file isย hidden behind aย false extension. Some systems orย users may only see file.jpg, assuming itโ€™s safe.

    If youโ€™re interested in making a file uploader with security measures set in place we have a video tutorial just for you! It walks you through everything you need to know, file validation, MIME type checking, file renaming, application-level mapping and lots more.

How Uploadcare protects against malicious files

Atย Uploadcare, weย use ClamAV, anย open source tool that swiftly scans and blocks any malicious files uploaded toย your project toย ensure that your projects are secure and free from attacks.

How does Uploadcare use ClamAV? Letโ€™s briefly discuss how ClamAV works behind the scenes when itย encounters aย virus orย malicious file.

ClamAV has three main components:

  1. Clamscan: The scanning tool that scans files and directories

  2. Freshclam: Aย service that updates the virus definition database toย stay current with new malware threats.

  3. Clamd: Aย daemon (background service) that provides faster scanning byย keeping the virus database inย memory.

Combining these components, ClamAV can scan aย server and effectively provide security against malware threats.

ClamAV uses aย signature-based approach for detecting malware such that when ClamAV scans aย file, itย compares the file toย its virus definition database toย know ifย the file isย infected; ifย itย matches any genre ofย virus onย the virus definition database, itย marks that file asย infected.

Utilizing ClamAVโ€™s capability, weย can run scanning onย our servers with minimal latency toย protect files uploaded.

How Uploadcare scans and filter malicious content

Toย scan and filter malicious content, Uploadcare uses ClamAV inย two ways:

Pre-upload scanning

Say you create aย new project and integrate our File Uploader into your application for your users toย upload files into your project; you have little toย noย control over what files your users will beย uploading orย where they got the files theyโ€™re uploading from. Suppose anย unknowing userโ€™s computer isย infected, orย the file theyโ€™re trying toย upload has been tampered with orย corrupted. Inย that case, they might unknowingly upload the malicious file into your project inย Uploadcare servers.

Toย prevent issues with malicious file uploads using our File Uploader, our server scans each file for potential security threats before acceptingย it. This process ensures that any harmful files are identified and the upload isย halted. Ifย aย file isย found toย beย corrupted, the user receives anย error notification indicating the type ofย virus the file isย infected with.

This can beย seen inย action inย the screenshot below:

Malicious file detection inย Uploadcare File UploaderMalicious file detection inย Uploadcare File Uploader

Byย default, pre-upload scanning isย enabled for all new projects onย Uploadcare toย protect your project from malicious attacks.

Toย change the default behavior ofย the File Uploader for malicious files, you can update the setting ofย your project inย Settings -> Uploading -> Malware Protection.

Enabled malicious file detection from uploadcare dashboardEnabled malicious file detection from uploadcare dashboard

You can also view the logs for malicious files that the File Uploader rejected.

Post-upload scanning

How about aย situation where malicious files have already been uploaded toย your project? Worry not!

Using the Uploadcare REST API ClamAV add-on, you can scan files inย your project and remove any malicious files detected.

The Uploadcare REST API isย available inย various programming languages and can beย easily integrated into your project inย aย few lines ofย code. Letโ€™s implement the post-upload scanning inย aย JavaScript project.

First, install the Uploadcare REST JavaScript SDK:

npm install @uploadcare/upload-client

Then you can import the add-on from the REST client library toย scan the files you want toย scan

import {
  executeAddon,
  AddonName,
  UploadcareSimpleAuthSchema,
} from '@uploadcare/rest-client';

const uploadcareSimpleAuthSchema = new UploadcareSimpleAuthSchema({
  publicKey: 'YOUR_PUBLIC_KEY',
  secretKey: 'YOUR_SECRET_KEY',
});

const scanFile = async () => {
  try {
    const result = await executeAddon(
      {
        addonName: AddonName.UC_CLAMAV_VIRUS_SCAN,
        target: 'bd5547b4-578a-4c53-8bf1-5cb43f5d4d30' // UUID of the file you want to scan
      },
      { authSchema: uploadcareSimpleAuthSchema },
    );
    
    console.log(result);
  } catch (error) {
    console.log(error);
  }
}

scanFile()

The code above:

  • Creates an UploadcareSimpleAuthSchema toย authenticate the request. Replace YOUR_PUBLIC_KEY and YOUR_SECRET_KEY with your Public and Secret keys from your Uploadcare project.

  • Uses the add-on name UC_CLAMAV_VIRUS_SCAN toย scan the file bd5547b4-578a-4c53-8bf1-5cb43f5d4d30 ifย itโ€™s anย infected file and removes itย from the project orย quarantine it

Using these two methods, you can ensure your project isย free from malicious files, and your users are safe from attacks.

How ClamAV compares with other solutions

There are several solutions out there for scanning files for malicious content, but ClamAV stands out for several reasons when compared toย other solutions:

  1. Open-Source and Customizable: ClamAV isย anย open-source antivirus engine, allowing Uploadcare toย customize its implementation according toย our specific needs. This flexibility isย essential for adapting toย unique workflows, tailoring scans toย suit different file types, and integrating smoothly into Uploadcareโ€™s existing systems.

  2. Comprehensive Malware Detection: ClamAVโ€™s database includes malware signatures covering various malicious filesโ€”viruses, trojans, worms, and more. Its broad detection capabilities make itย well-suited for scanning the different file types that Uploadcare processes (e.g., images, documents, videos).

  3. High Scalability: ClamAV can handle large-scale file uploads and scanning operations, making itย perfectย us since weย manage high volumes ofย files while maintaining security. Itย can beย deployed across multiple servers toย distribute the workload and ensure scanning doesnโ€™t slow down file processing.

  4. Compliance with Security Standards: ClamAV meets the security requirements ofย many regulatory and compliance frameworks, such asย GDPR, HIPAA, and PCI-DSS, which makes itย aย idea choice forย us atย Uploadcare since weย prioritize the security and privacy ofย our usersโ€™ data.

Conclusion

File uploads are essential parts ofย applications when building for users, but they pose significant security risks ifย not handled properly. From hidden malicious code inย images toย exploits inย file formats, hackers have developed numerous ways toย compromise systems through uploaded files. Understanding these threats and implementing robust security measures toย protect your projects isย crucial.

Atย Uploadcare, protecting your files against malicious attacks isย aย priority. Weย provide various ways toย safeguard your projects, offering pre-upload and post-upload scanning solutions toย ensure your systems remain secure. Weย also have security measures inย placeย to securely upload and store your files while protecting them from malicious attacks.

Stay vigilantโ€”even aย cute kitten picture could beย more dangerous than itย seems.

Ready to get started?

Join developers who use Uploadcare to build fileย handling quickly andย reliably.

Sign up for free