- Machine Learning for Cybersecurity Cookbook
- Emmanuel Tsukerman
- 254字
- 2021-06-24 12:29:08
How to do it...
In the following steps, we will enumerate all the 4-grams of a sample file and select the 50 most frequent ones:
- We begin by importing the collections library to facilitate counting and the ngrams library from nltk to ease extraction of N-grams:
import collections
from nltk import ngrams
- We specify which file we would like to analyze:
file_to_analyze = "python-3.7.2-amd64.exe"
- We define a convenience function to read in a file's bytes:
def read_file(file_path):
"""Reads in the binary sequence of a binary file."""
with open(file_path, "rb") as binary_file:
data = binary_file.read()
return data
- We write a convenience function to take a byte sequence and obtain N-grams:
def byte_sequence_to_Ngrams(byte_sequence, N):
"""Creates a list of N-grams from a byte sequence."""
Ngrams = ngrams(byte_sequence, N)
return list(Ngrams)
- We write a function to take a file and obtain its count of N-grams:
def binary_file_to_Ngram_counts(file, N):
"""Takes a binary file and outputs the N-grams counts of its binary sequence."""
filebyte_sequence = read_file(file)
file_Ngrams = byte_sequence_to_Ngrams(filebyte_sequence, N)
return collections.Counter(file_Ngrams)
- We specify that our desired value is N=4 and obtain the counts of all 4-grams in the file:
extracted_Ngrams = binary_file_to_Ngram_counts(file_to_analyze, 4)
- We list the 10 most common 4-grams of our file:
print(extracted_Ngrams.most_common(10))
The result is as follows:
[((0, 0, 0, 0), 24201), ((139, 240, 133, 246), 1920), ((32, 116, 111, 32), 1791), ((255, 255, 255, 255), 1663), ((108, 101, 100, 32), 1522), ((100, 32, 116, 111), 1519), ((97, 105, 108, 101), 1513), ((105, 108, 101, 100), 1513), ((70, 97, 105, 108), 1505), ((101, 100, 32, 116), 1503)]
推薦閱讀
- Microsoft Dynamics CRM Customization Essentials
- Instant Raspberry Pi Gaming
- ABB工業(yè)機(jī)器人編程全集
- Verilog HDL數(shù)字系統(tǒng)設(shè)計(jì)入門(mén)與應(yīng)用實(shí)例
- AWS:Security Best Practices on AWS
- 計(jì)算機(jī)應(yīng)用復(fù)習(xí)與練習(xí)
- 啊哈C!思考快你一步
- 工業(yè)機(jī)器人集成應(yīng)用
- Oracle 11g Anti-hacker's Cookbook
- Raspberry Pi Projects for Kids
- 算法設(shè)計(jì)與分析
- Wireshark Revealed:Essential Skills for IT Professionals
- 實(shí)戰(zhàn)突擊
- 深度學(xué)習(xí)500問(wèn):AI工程師面試寶典
- Kibana 7 Quick Start Guide