Malicious Domain Names Detection Algorithm Based on N -Gram

. Malicious domain name attacks have become a serious issue for Internet security. In this study, a malicious domain names detection algorithm based on N -Gram is proposed. The top 100,000 domain names in Alexa 2013 are used in the N -Gram method. Each domain name excluding the top-level domain is segmented into substrings according to its domain level with the lengths of 3, 4, 5, 6, and 7. The substring set of the 100,000 domain names is established, and the weight value of a substring is calculated according to its occurrence number in the substring set. To detect a malicious attack, the domain name is also segmented by the N - Gram method and its reputation value is calculated based on the weight values of its substrings. Finally, the judgment of whether the domain name is malicious is made by thresholding. In the experiments on Alexa 2017 and Malware domain list, the proposed detection algorithm yielded an accuracy rate of 94.04%, a false negative rate of 7.42%, and a false positive rate of 6.14%. The time complexity is lower than other popular malicious domain names detection algorithms.


Introduction
While rapid development of Internet has changed our lives positively, different types of malicious cyberattacks have been increasing simultaneously. According to the 36th issue of the 2018 "Network information security and dynamic weekly report" released by CNCERT, Chinese servers were attacked 178,156 times in 2018 by 3,724 malicious domain names such as Conficker, Trojan, and Srizbis, an year-overyear increase of 55.8% [1]. DNS (domain name system), one of the basic services for realizing the conversion between network domain name and IP addresses in the Internet [2], has been widely used in e-commerce, instant messaging, and network media. Almost all Internet applications are needed to use DNS to resolve domain names and achieve resource location [3,4].
In order to achieve malicious purpose, attackers implant malicious programs through the vulnerabilities of system or service to infect the host, and the infected host is controlled by attackers remotely [5]. e infected host will issue resolution requests, using a large number of nonexistent domain names randomly generated by the DGA (domain generation algorithm) or domain flux [6] in a short time.
ese resolution requests of malicious domain name are forwarded multiple times among DNS servers and are discarded eventually due to the failure of domain name resolution. However, the record of the failure of domain name resolution is also forwarded multiple times, then returned to the infected host that initiates the domain name resolution request. A large number of resolution requests and resolution failure records of the malicious domain name are forwarded multiple times among the DNS servers, which increases the usage of network bandwidth and brings a heavy payload on the DNS servers. Moreover, it will affect the execution of normal domain name resolution tasks seriously as well. If the malicious domain name is not detected in an accurate and timely manner, the DNS servers may be down due to malicious domain name attacks, all Internet services relying on DNS servers will stop, and the results will be catastrophic.
erefore, accurate and timely detection of malicious domain name attacks has the significant impact on Internet security. e remaining of this study is organized as follows. A number of related works are reviewed in Section 2. e proposed approach, system architecture, and progress to detect malicious domain names are presented in Section 3. e experimental results and performance evaluation of the study are presented in Section 4, and the conclusion is presented in Section 5.

Related Works
From the perspective of domain name structure features and lexical composition, there are two main types of malicious domain names detection methods in the literature: domain name model [7][8][9][10][11] and domain name semantic [12][13][14][15] detection.

Domain Name Model Detection.
ere are many differences between the normal domain names and the malicious domain names in terms of behaviors and structures. erefore, the legitimacy of domain names can be determined by analyzing the behavior and structure of domain names. For example, Truong et al. [16] used the DNS traffic characteristics to detect the DNS query flow of abnormal DNS servers. Zang et al. [17] proposed a malicious domain names detection algorithm based on AGD (algorithmically generated domain) by using cluster correlation that identifies the domain names generated by a domain generation algorithm or its variants. Features such as TTL, the distribution of IP addresses, Whois features, and historical information from the domain names in each cluster were extracted, and the support vector machine (SVM) algorithm was used to identify the malicious domain names. Sharifnya and Abadi [18] proposed a DGAbased botnet detection algorithm by clustering the hosts of the DNS request query and generating the potential candidate set to be tested. eir algorithm achieves malicious domain names detection by calculating the probability of the threatened candidate hosts in the candidate set to be tested. Kwon et al. [19] proposed a botnet detection algorithm based on DNS traffic features using PSD (power spectral density) testing technology, which detects malicious domain names by analyzing malicious behavior within large volumes of DNS traffic. Zhang et al. [20] proposed a botnet detection algorithm that combined the domain names' request behaviors with construction characteristics and conducted malicious domain names detection through SVM classification. Vinayakumar et al. [21] proposed a method that used big data computation platforms on a distributed cluster and deep learning algorithm to detect fraud and malicious activities where attackers used combinational method to avoid blacklist detection.

Domain Name Semantic Detection.
e method of domain name semantic detection includes detection based on character matching and on content analysis. For example, Yadav et al. [22] proposed a malicious domain names detection algorithm by using linguistic features that measures information entropy of bigrams in all domains and statistical measurements such as Kullback-Leibler divergence, Jaccard index, and Levenshtein edit distance for identifying malicious domain names. Huang et al. [23] analyzed the differences between normal domain names and malicious domain names in character constitution. e statistical characteristics, resolution features, and similarity characteristics of domain names were extracted, and malicious domain names detection was achieved using a machine learning algorithm in the character and resolution features space. Zhang et al. [24] proposed a lightweight domain names detection algorithm based on morpheme features and natural language processing (NLP), which analyzed the domain name features such as the root, affix, Chinese spelling, and special noun abbreviation and used the C4.5 algorithm to construct a decision tree with recursive thinking. Zhang et al. [25] proposed a malicious domain names detection algorithm that analyzed the domain name features of character composition and the lexical hierarchical structure which included domain name length, double letters, and character frequency to distinguish malicious domain names. Zhao [26] proposed a high efficiency pattern matching detection algorithm for intrusion detection. e algorithm reduces computational time by constructing a hash table for attack pattern strings.
Each of the above malicious domain names detection methods has its own advantages. e domain name model detection methods have high detection accuracy rate and wide application range. However, this kind of detection method has a long data collection period, and it is difficult to obtain a large amount of resolution data from both the local domain name server and the root domain name server, thus resulting in high detection time overhead. Although the detection methods of domain name semantic have the advantage of low detection time overhead, this kind of detection method is based on domain name blacklist to design detection features and cannot effectively detect newly generated domain names.
To address the problems such as low detection accuracy rate and high detection overhead, a new method of malicious domain names detection based on N-Gram is proposed by combining domain name model and semantic features. In addition, unlike current methods that analyze the lexical composition and structure of the whole domain names, the new method divides the whole domain names into multiple domain name substrings and deeply analyzes the features of domain name substrings in terms of lexical composition and structure. In this algorithm, the domain names which are accessed with high frequency are chosen as the normal domain names sample, and the N-Gram method is applied to segment each domain name in the domain name whitelist sample to obtain the substrings on which domain name model and semantic features depend. en, a test domain name is also segmented by the N-Gram method, and its substrings are compared with the domain name substrings in domain name whitelist substring set to determine whether it is a malicious domain name.

Proposed Approach
In the following sections, we provide more details for the components of the proposed algorithm. Figure 1 gives a flowchart of our proposed detection algorithm. Malicious domain names detection algorithm based on N-Gram consists of two main sections. (1) Domain name whitelist substring set construction. (2) Malicious domain names detection. ere are two steps to construct the domain name whitelist substring set, namely, obtaining the substring statistics from the normal domain names segmented by the N-Gram method and calculating the substrings weight values. e domain names with high frequency of access excluding the top-level domain are segmented into multiple substrings according to its domain name level with the lengths of 3, 4, 5, 6, and 7 by using the N-Gram method.

System Overview.
en, substring statistics is calculated according to each domain name substring repetition occurrence number. e substring weight value is calculated according to the domain name substring occurrence frequency in the substring set.
In the process of malicious domain names detection, a test domain name is segmented also by the N-Gram method, and its reputation value is calculated according to the weight values of its substrings. en, the malicious domain name is determined according to the reputation value.

Domain Name Whitelist Substring Set Construction.
Alexa ranking is a service that Amazon provides to the public to evaluate the popularity of domain names [27].
rough the statistics and analysis of each domain name in the number of access, links, and other aspects, the domain name evaluation and ranking are produced according to the analysis results. erefore, if a domain name ranks relatively high in the Alexa, it is more likely to be secure and normal.
rough the observation of a large number of normal domain names in Alexa 2013 [28], it is found that the domain name has the features of hierarchical structure on its composition form. Top-level domain is the domain name substring of the end of a domain name, including the country top-level domains (such as cn, jp, and gb) and international organization top-level domains (such as com, net, and org). Moreover, the total number of top-level domains is small, and there are unified naming rules in terms of its lexical structure and lexical composition [29]; namely, top-level domains are generally named on the basis of the name of country or region. For example, for a given domain name lut.edu.cn, cn is China's top-level domain on the Internet. Second-level domain (SLD) is the domain name substring that is adjacent to the top-level domain. For example, edu is a SLD, which represents education organization. ird-level domain (TLD) refers to the domain name substring that is adjacent to the right of SLD. For example, lut is a TLD, which is the abbreviation of the Lanzhou University of Technology. erefore, domain name substrings at each level have a specific meaning in its construction.
In the process of domain name resolution, when the domain name resolution request is not recorded in local network DNS servers, the resolution request is forwarded to the superior network DNS servers, until it reaches the root domain DNS servers. After reaching the root domain, the resolution requests are forwarded to the DNS servers again where the top-level, second-level, third-level, and other level domain names are located, until the domain name resolution result is found. Given that the domain name resolution request is forwarded from the superior domain to the inferior domain, if the given inferior domain name does not exist, the domain name resolution fails. en, the domain name resolution result or the cause of the domain name resolution failure will be returned to the host that initiates the domain name resolution according to the original path of the request.
From the domain name resolution process, it is noted that the deeper level a malicious domain name is at, the greater its forwarding number is, thus the heavier burden it creates on the system. Conversely, the closer a malicious domain name is to the top-level domain, the smaller its forwarding number is, and thus the easier it can be found. In addition, because of the small quantity, short length, and high popularity of top-level domains, they are readily identified.
erefore, malicious domain names are rarely found in the top-level domain, but often exist in the second, third, or fourth domain. Hence, this study focuses on each domain name substring excluding the top-level domain.

Substring Statistics.
e character string in the text is segmented by a sliding window with a size of N, and multiple substrings of length N are obtained, each of which is called a gram. For example, the process of 5-Gram segmentation for a string "microsoftword" of length 13 is shown in Figure 2.
When the N-Gram method is applied to segment the text, the value of N will influence the number of gained substrings. If the value of N is too small, the number of domain name substrings obtained by segmentation will be large, which leads to enormous calculation quantity and storage space. If the value of N is too large, the number of domain name substrings obtained by segmentation will be small, which can lead to little effective lexical feature information obtained by segmentation, which is not conducive to extract domain name composition structure and semantic information. After excluding the top-level domains of top 100,000 domain names in Alexa 2013, domain name substring length proportions of each level are counted, as shown in Table 1.
As seen from Table 1, after excluding top-level domains of top 100,000 domain names in Alexa 2013, the proportion of the length of each level domain name in the [3,7] interval is up to 97.63%. erefore, the size of the sliding window N is set to 3, 4, 5, 6, and 7, and each domain name excluding toplevel domains is segmented by the N-Gram method to form the domain name substring set.
where count(j) (j 1, 2,. . ., n) represents the number of domain name substrings that are obtained from segmenting the jth-level domain of a domain name, L represents the length of jth-level domain, n represents the maximum level number of a domain name, and N represents the size of the sliding window whose value ranges from N ∈ N * |3 ≤ N ≤ 7 { }. When the size of the sliding window N is set to 3, 4, 5, 6, and 7, the number of completely di erent domain name substrings obtained is shown in Table 2. Where the number of domain name substrings with the length of 3 is 21,584, with the length of 4 is 84,431, with the length of 5 is 120,626, with the length of 6 is 116,908, and with the length of 7 is 55,274, with a total of 398,823 domain name substrings. And the domain name whitelist substring set is constructed according to these completely di erent domain name substrings.

Substring Weight Values Calculation.
e extraction of the lexical features of the domain name turns into numerical calculation by calculating the weight values of 398,823 domain name substrings in the domain name whitelist substring set. e weight value of domain name substring is calculated by the following formula: where W N-Gram (N 3, 4, 5, 6, and 7) is the weight value of the ith domain name substring and C N-Gram (i) represents the     Table 3.

Malicious Domain Names Detection.
In the process of malicious domain names detection, a test domain name is segmented by the N-Gram method, and its reputation value is calculated according to the weight values of its substrings in the domain name whitelist substring set. Finally, the judgment of whether a domain name is malicious is made according to its reputation value. e process of the malicious domain names detection is shown in Figure 4.

Reputation Values Calculation.
Each domain name excluding the top-level domain is segmented into multiple substrings with the lengths of 3, 4, 5, 6, and 7 by the N-Gram method. e total weight value of the domain names to be tested is calculated according to the weight values of its substrings in the domain name whitelist substring set. Additionally, the total weight value is used as the reputation value (RV) to evaluate whether the domain name to be tested is a malicious domain name. e RV is calculated by the following formula: where W N-Gram (N 3, 4, 5, 6, and 7) is the weight value of ith domain name substring which is referenced from 398,823 domain name substring weight values (as shown in Table 3), l l ∈ N * |l > 0 { }represents a domain name to be tested that the serial number is l, and m represents the total number of obtained domain name substrings whose domain name l is segmented, when the sliding window N is set to 3, 4, 5, 6, and 7. Since the substrings of the malicious domain names appear less frequently in the domain name whitelist substring set, the RV of malicious domain names is smaller. On the contrary, the RV of normal domain names is larger. erefore, a simple technique of thresholding can be used to achieve the detection of malicious domain names.
For example, after removing the top-level domain "com" and replacing the letter "o" in the normal domain name "taobao.com" with the number "0," the RV of the normal domain name "taobao.com" and the malicious domain name "ta0ba0.com," which are similar to the normal domain name "taobao.com," can be calculated. e RV is calculated as follows: By calculating the RV of the normal domain name "taobao.com" and the malicious domain name "ta0ba0.com," it can be seen that the RV of the normal domain name "taobao.com" is 17.227. When the size of N is 3, 4, 5, 6, and 7,   Journal of Computer Networks and Communications the normal domain name "taobao.com" is segmented into multiple domain name substrings which appear frequently in the domain name whitelist substring set. However, the RV of the malicious domain name "ta0ba0.com" is 0.415. When the size of N is 3, 4, 5, 6, and 7, the malicious domain name "ta0ba0" is segmented into multiple domain name substrings which appear with very small probability in the domain name whitelist substring set.

3.3.2.
reshold Setting for Malicious Domain Names Detection. In the process of malicious domain names recognition, the size of threshold decides the accuracy rate of the detection algorithm in this study. In order to attain the superior detection accuracy rate, the variable parameter threshold D is debugged on the same dataset and the optimal threshold D is selected. e corresponding relationship between the threshold D size and the detection accuracy rate is shown in Figure 5.
From Figure 5, it can be seen that the curve of detection accuracy rate shows a trend of left rising and right falling. When threshold D is 0.65, the detection accuracy rate reaches the optimal level of 94.04%, which refers to it as a better detection e ect when the threshold D is 0.65. erefore, threshold D in this study is set to 0.65. In this study, the threshold for malicious domain names detection is set on the basis of the domain name whitelist substring set that is constructed by the top 100,000 domain names in Alexa 2013. If the domain name whitelist sample on constructing domain name whitelist substring set is replaced, the threshold for malicious domain names detection needs to be reset according to the above steps.
When the threshold is set, the RV of the domain name to be tested is calculated to judge whether the domain name to be tested is malicious based on the size of the RV and threshold for malicious domain names detection. If the RV of the domain name to be tested is greater or equal than the threshold for malicious domain names detection, the domain name is judged to be a normal domain name. If not, it is a malicious domain name.

Experimental and Result Analysis
To verify the performance of the proposed algorithm based on N-Gram, experiments on malicious domain names detection were conducted using large volumes of data collected and published by Alexa and other sources.

Experiments.
e experimental environment is shown in Table 4.

Evaluation Standard.
In order to evaluate the performance of the malicious domain names detection algorithm based on N-Gram in malicious domain names detection, the accuracy rate (AR), false negative rate (FNR), and false positive rate (FPR) are used. e evaluation standard is calculated based on the confusion matrix [36] of the experimental results, as shown in Table 5. Evaluation standard is calculated by the following formula:   Figure 6 shows the e ect of threshold D on false negative rate and false positive rate, where Figure 6(a) shows the relation between the threshold D on the X-axis and the false negative rate on the Y-axis and Figure 6(b) shows the relation between the threshold D on the X-axis and the false positive rate on the Y-axis.
From the results of the two experiments presented in Figure 6, when the threshold D is less than 0.65 and gradually increases within this range, the curve of detection accuracy rate takes on ascend trend ( Figure 5). e main reason is that the number of malicious domain names accurately detected by the algorithm increases and the number of malicious domain names incorrectly reported as normal domain names gradually decreases, which makes the detection accuracy rate increase gradually and the false negative rate decrease gradually (Figure 6(a)). When the threshold D is greater than 0.65 and it gradually increases within this range, the curve of detection accuracy rate takes on downward trend ( Figure 5). e reason is that the number of normal domain names accurately detected by the algorithm decreases and the number of normal domain names incorrectly reported as malicious domain names gradually increases, which makes the detection accuracy rate decrease gradually and the false positive rate increase gradually ( Figure 6(b)).

Comparison with Other Approaches.
To verify the effectiveness of the proposed algorithm, experiments were also performed using the methods in the latest literatures [8,10,13,14] with the same experimental conditions. Accuracy and computational cost are the measures for the e ectiveness of the algorithms. Performance comparisons in terms of AR, FNR, FPR, and time overhead (TO) are shown in Table 6.
Our proposed method yielded a superior combinational result of accuracy rate and computational e ciency. Other methods either has lower accuracy rate or is computational more expensive. In addition, compared to the other methods that are based on machine learning techniques, our method is much easier to add new data when they become available. While the machine learning algorithms require a new training process of all the data, our method only needs modi cations to the weight of relevant substrings.

Conclusion and Future Work
is study proposes a new method for malicious domain names detection. e main contributions are as follows: (1) e top 100,000 domain names in Alexa 2013 are taken as the domain name whitelist sample set to construct the domain name whitelist substring set (2) e N-Gram method in the natural language processing technology is used to segment the domain names, and the malicious domain names are quickly recognized according to its occurrence number in the substring set   Compared to the malicious domain names detection methods proposed by [8,10,13,14], our approach demonstrated a superior comprehensive performance of lower time overhead and higher detection accuracy rate. It has a good practical value in defending against the botnet, Spam, and remote access Trojan attack and can help security experts and organizations in their fight against cybercrime. However, our proposed approach is not comprehensive in homographic domain names (such as linkedin.com and 1inkedin.com, apple.com and app1e.com) detection aspects, but if the malicious domain names are generated randomly, our approach can detect them efficiently. ese homographic domain names detection problems should be considered in the future.

Data Availability
e Alexa and Malware domain list datasets used to support the findings of this study are cited at relevant places within the text as references [28,30,31,[33][34][35]].

Conflicts of Interest
e authors declare that there are no conflicts of interest regarding the publication of this paper.