VK TECH LEARN
Home/Courses/Ethical Hacking/लेसन 12
Chapter 6 · Malware Analysis और Reverse Engineering

Python से Malware Analysis और Reverse Engineering

लेसन 12 / 20·~7 मिनट·by Vïnâý Kúshwâhå
Glass sandbox में bug, microscope और code brackets
Glass sandbox में bug, microscope और code brackets — Chapter 6: Malware Analysis और Reverse Engineering

Malware analysis में Python का सबसे बड़ा फ़ायदा है — repetitive जाँच को automate करना। फ़ाइल का type, hash, strings और network indicators एक script से निकाले जा सकते हैं। यह सब isolated lab और safe sample पर ही करें।

पहला कदम — hash और file type

import hashlib

with open("sample.bin", "rb") as f:
    data = f.read()

print("size:", len(data))
print("md5:", hashlib.md5(data).hexdigest())
print("sha256:", hashlib.sha256(data).hexdigest())

Hash से sample की पहचान होती है और VirusTotal जैसी सेवा पर उसकी reputation देखी जा सकती है। File type जानने के लिए magic bytes पढ़ें — MZ से Windows executable, PK से zip/docx, %PDF से PDF।

Strings निकालना

Compiled program के अंदर URLs, file paths, commands और messages अक्सर plain text में रह जाते हैं। Python से readable strings छानें:

import re

text = data.decode("latin-1", errors="ignore")
strings = re.findall(r"[ -~]{6,}", text)

for s in strings[:40]:
    print(s)

इससे अक्सर payload का server, persistence की जगह (जैसे Startup folder) या किसी command का अंदाज़ा मिल जाता है।

Python script की जाँच

अगर sample Python में लिखा है, तो उसे चलाने से पहले पढ़ें। ध्यान दें:

Encoded हिस्सा decode करने के लिए सिर्फ़ decode function अलग चलाएँ, पूरा script नहीं।

Obfuscation और anti-analysis

Malware खुद को छिपाने के लिए code को encode/encrypt करता है, variable नाम बेमतलब रखता है, या यह जाँचता है कि वह VM में चल रहा है या नहीं। कुछ samples sleep करके analysis को थकाते हैं। इन संकेतों को पहचानना ही analysis का हिस्सा है।

Network indicators

Dynamic analysis में sample के network connections देखें। Python से अपनी VM का traffic log भी पढ़ा जा सकता है:

with open("/var/log/syslog") as f:
    for line in f:
        if "DROP" in line or "ACCEPT" in line:
            print(line.strip())

यहाँ मक़सद यह पता लगाना है कि sample किस IP या domain से बात करने की कोशिश करता है — उसी से block rules बनते हैं।

Analysis के नियम

याद रखें
  • Hash + magic bytes से पहचान शुरू करें
  • Strings से URLs, paths और commands मिलते हैं
  • eval/exec/base64 — छिपाने के संकेत
  • Analysis सिर्फ़ isolated VM में, snapshot के साथ
Practical — 10 मिनट

अपने lab की किसी सामान्य फ़ाइल (जैसे कोई installer) का SHA-256 निकालें और magic bytes देखें। फिर strings निकालकर 5 दिलचस्प पंक्तियाँ लिखें — यही तरीका malware sample पर भी काम करता है।