Pattern Matching (การจับคู่รูปแบบ) คืออะไร?
Pattern Matching คือกระบวนการตรวจสอบว่าลำดับข้อมูล (String, Packet Payload, Log Entry, Binary Sequence) ตรงกับรูปแบบ (Pattern) ที่กำหนดไว้หรือไม่ ใช้เป็นหัวใจของการตรวจจับภัยคุกคามใน Network Security
หลักการทำงานพื้นฐาน
- Input: Data Stream (Network Traffic, File, Log)
- Pattern Set: Signature, Regex, YARA Rule, Sigma Rule, Behavioral Baseline
- Algorithm: Exact Match, Regex Engine, Aho-Corasick, Boyer-Moore, Hyperscan, DFA/NFA
- Output: Match / No Match + Action (Alert, Block, Quarantine, Log)
อัลกอริทึมสำคัญใน Network Security
| อัลกอริทึม | ลักษณะ | ใช้ใน |
|---|---|---|
| Aho-Corasick | Multi-pattern String Matching O(n+m+z) ใช้ Trie + Failure Links | IDS/IPS (Snort, Suricata), Antivirus, DLP |
| Boyer-Moore / BMH | Single Pattern, Skip Heuristics เร็วมากสำหรับ Pattern ยาว | File Scanning, Log Grep |
| DFA (Deterministic Finite Automaton) | Regex → State Machine เร็วแต่ Memory 多い | Regex Engine (PCRE, RE2), WAF |
| NFA (Non-deterministic FA) | Regex Backtracking ช้ากว่า DFA แต่ Memory น้อย | Legacy Regex Engine |
| Hyperscan (Intel) | Hybrid DFA+NFA + SIMD Vectorization เร็วมาก Multi-pattern | Suricata, Zeek, Cisco Snort 3, Cloudflare WAF |
| Bit-Parallel (Shift-Or/Shift-And) | ใช้ Bitwise Operations เร็วสำหรับ Pattern สั้น (<64 chars) | Approximate Matching, DNA, Hardware |
ประเภท Pattern ใน Network Security
1. Signature-based (Static Patterns)
- Malware Hash (MD5, SHA256)
- Byte Sequences (Magic Bytes, Shellcode NOP Sled)
- String Literals (
/etc/passwd,cmd.exe,eval() - Regex (
SELECT.*FROM.*WHERE,\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b)
2. Behavioral / Anomaly Patterns (Dynamic)
- Traffic Baselines (Bytes/sec, Packets/sec, Connection Count)
- Protocol State Machine Violations (TCP Flag Anomalies, DNS Tunneling)
- Machine Learning Features (Entropy, N-gram, Graph Embedding)
3. Structured Patterns (YARA, Sigma, STIX/TAXII)
- YARA: File/Memory Scanning (Condition + Strings)
- Sigma: Log Detection Rules (Generic SIEM Query Language)
- STIX/TAXII: Threat Intelligence Sharing (IOC Patterns)
การประยุกต์ใช้ในเครื่องมือจริง
| เครื่องมือ | Pattern Matching Approach |
|---|---|
| Snort / Suricata (IDS/IPS) | Rule-based: Content Match (Aho-Corasick) + PCRE + HTTP Inspectors + Lua Scripts |
| Zeek (Bro) | Script-based: Pattern Matching ใน Event Engine (Regex, Subnet Matching, Bloom Filters) |
| WAF (ModSecurity, Cloudflare, AWS WAF) | OWASP CRS: Regex + IP Reputation + Rate Limiting + Bot Detection |
| DLP (Symantec, Forcepoint, Microsoft Purview) | Exact Match (Fingerprinting) + Regex (CCN, SSN, PII) + OCR + Machine Learning |
| EDR/XDR (CrowdStrike, SentinelOne, Microsoft Defender) | Behavioral Patterns (MITRE ATT&CK TTPs) + YARA + Memory Scanning |
| SIEM (Splunk, Elastic, QRadar) | Sigma Rules → Transpile to Native Query (SPL, KQL, AQL) + Regex Extraction |
ตัวอย่าง Pattern Matching ใน SQL (LIKE / REGEXP)
-- หา IP Address ใน Log Table
SELECT * FROM firewall_logs
WHERE message REGEXP '\\b(?:[0-9]{1,3}\\.){3}[0-9]{1,3}\\b';
-- หา SQL Injection Attempts
SELECT * FROM web_logs
WHERE uri LIKE '%UNION%SELECT%' OR uri REGEXP '(?i)(SELECT|INSERT|UPDATE|DELETE|DROP|OR\\s+1=1)';
ท้าทายและแนวโน้มอนาคต
- Encrypted Traffic (TLS 1.3, DoH, DoT, QUIC): ไม่สามารถ Inspect Payload ได้ → ต้องใช้ JA3 Fingerprinting, SNI Analysis, Encrypted Traffic Analysis (ETA)
- High Throughput (100Gbps+): ต้องใช้ Hardware Offload (SmartNIC, DPU, FPGA, ASIC) + Hyperscan / Vectorized Engines
- Evasion Techniques: Fragmentation, Encoding, Polymorphism, Encryption → ต้องมี Normalization/Decoding ก่อน Match
- False Positives: Pattern กว้างเกินไป → ใช้ Context-aware Matching, ML-assisted Triage
สรุป
Pattern Matching เป็น Core Primitive ของ Network Security ตั้งแต่ Signature-based IDS/IPS, WAF, DLP ไปจนถึง Modern Threat Hunting (YARA, Sigma, MITRE ATT&CK) การเข้าใจอัลกอริทึม (Aho-Corasick, Hyperscan, DFA/NFA) และข้อจำกัด (Encryption, Performance, Evasion) จะช่วยให้เลือกและปรับแต่งเครื่องมือได้อย่างมีประสิทธิภาพ