{'fraction_non_alphanumeric': 0.03071469730569767, 'fraction_numerical': 0.006705198274808361, 'mean_word_length': 4.817757009345795, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Session-layer Attack Traffic Classification by Program Synthesis

Writing classification rules to identify malicious network traffic is a time-consuming and error-prone task. Learning-based classification systems automatically extract such rules from positive and negative traffic examples. However, due to limitations in the representation of network traffic and the learning strategy, these systems lack both expressiveness to cover a range of attacks and interpretability in fully describing the attack traffic's structure at the session layer. This paper presents Sharingan system, which uses program synthesis techniques to generate network classification programs at the session layer. Sharingan accepts raw network traces as inputs, and reports potential patterns of the attack traffic in NetQRE, a domain specific language designed for specifying session-layer quantitative properties. Using Sharingan, network operators can better analyze the attack pattern due to the following advantages of Sharingan's learning process: (1) it requires minimal feature engineering, (2) it is amenable to efficient implementation of the learnt classifier, and (3) the synthesized program is easy to decipher and edit. We develop a range of novel optimizations that reduce the synthesis time for large and complex tasks to a matter of minutes. Our experiments show that Sharingan is able to correctly identify attacks from a diverse set of network attack traces and generates explainable outputs, while achieving accuracy comparable to state-of-the-art learning-based intrusion detection systems.


I. INTRODUCTION
Network monitoring and intrusion detection systems are essential for network infrastructure management. These systems require classification of network traffic at their core. Today, network operators and equipment vendors write classification programs or patterns upfront in order to differentiate malicious flows from normal ones. The process of writing these classification programs often requires deep operator insights, can be error prone, and is not easy to extend to handle new scenarios.
There have been many attempts at automated learning-based classifiers for malicious traffic using machine learning [16], [39], [3], [10] and data mining [4], [28], [34], [40], [19] techniques. These classifiers have not gained traction in production systems, in part due to unavoidable false positive reports and the gap between the learning output and explainable operational insights [31]. The challenges call for a more expressive, interpretable and maintainable learning-based classification system.
Specifically, existing approaches suffer from the following limitations. First, regular expression and feature vector representations frequently used for input data lack session-layer details and intermediate states in network protocols. Hence, information about vulnerabilities in application-layer protocols or multi-stage attacks will be missed. Second, these representations also require laborious task-specific feature engineering to get effective learning results, which undermines the systems' advantages of automation. Third, it is hard to interpret the learning results to understand the intent and structure of the malicious traffic, due to the blackbox model of many machinelearning approaches and the lack of expressiveness in the inputs and outputs to these learning systems.
To address the above limitations, we introduce Sharingan, which uses program synthesis techniques to auto-generate network classification programs from labeled examples of network traffic traces. Sharingan aims to bridge the gap between learning systems and operator insights, by identifying properties of the attack that can help inform the network operators on the nature of the attack, and provide a basis for automated generation of the classification rules. Sharingan does not aim to outperform state-of-the-art learning systems in accuracy, but rather match their accuracy, while generating output that is more explainable and easier to maintain.
To achieve these goals, we adopt techniques from syntax guided program synthesis [1] to generate a NetQRE [38] program that distinguishes the positive and negative examples. NetQRE, which stands for Network Quantitative Regular Expressions, enables quantitative queries for network traffic, based on flow-level regular pattern matching. The classification is done by comparing the synthesized program's output for each example with a learnt threshold T . Positive examples fall above T . The synthesized NetQRE program serves the role of network classifier, identifying flows which match the program specifications.
Requires minimal feature engineering: NetQRE [38] is an expressive language, and allows succinct description of a wide range of tasks ranging from detecting security attacks to enforcing application-layer network management policies. Sharingan can synthesize any network task on raw traffic expressible as a NetQRE program, without any additional feature engineering. This is an improvement over systems based on manually extracted feature vectors. Efficient implementation: The NetQRE program synthesized by Sharingan can be compiled, as has been shown in prior work [38], to efficient low-level implementations that can be integrated into routers and other network devices. On the other hand, traditional statistical classifiers are not directly usable or executable in network filtering systems. Easy to decipher and edit: Finally, Sharingan generates NetQRE programs that can be read and edited. Since they are generic executable programs with high expressiveness, the patterns in the program reveal the stateful protocol structure that is used for the classification, which blackbox statistical models, packet-level regular expressions and feature vectors have difficulty describing. The programs are also amenable to calibration by a network operator, for example, to mix in local policies or debug.
The key technical challenge in design and implementation of Sharingan is the computationally demanding problem of finding a NetQRE expression that is able to separate positive network traffic examples from the negative ones. This search problem is an instance of the so-called syntax-guided synthesis [1]. While this problem has received a lot of attention in recent years, no existing tools and techniques can solve the instances of interest in our context due to the complexity of the expressions to be synthesized as well as the scale of the data set of network traffic examples used in training. To address this challenge, we devised two novel techniques for optimizing the search -partial execution and merge search, which effectively achieve orders of magnitude reduction in synthesis time. We summarize our key contributions: Synthesis-based classification architecture. We propose the methodology of reducing network attack traffic classification problem to a synthesis from examples. Efficient synthesis algorithm We devise two efficient algorithms: partial execution and merge search , which efficiently explore the program space and enable learning from very large data sets. Independent of our network traffic classification use cases, these algorithms advance the state-of-the-art in program synthesis. Implementation and evaluation. We have implemented Sharingan and evaluated it for a rich set of metrics using the CICIDS2017 [26] intrusion detection benchmark database. Sharingan is able to synthesize a large range of network attack classification programs in a matter of minutes with accuracy comparable to state-of-the-art systems. Moreover, the generated NetQRE program is easy to interpret, tune, and can be compiled into configurations usable by existing network filtering systems.

II. BACKGROUND
In this section, we provide background knowledge to motivate our work and help readers put in context our techniques later in the paper.

A. Learning-based Systems
Learning-based systems face extra difficulties in network security applications compared to traditional use cases such as recommendation systems, spam mail filtering or OCR [31]. It is mainly due to two reasons. First, misclassifications in network attack detection has tangible cost. For example, in intrusion detection systems, operators will need to manually investigate possible intrusion reports in order to take proper mitigation actions. False positive reports will cost extra human labor for the investigation. Reports in a format hard to interpret will make the process even more difficult. Second, networks in production environments typically sends large volumes of data, which amplifies the misclassification costs. Making things worse, network environments have high diversity, and are in constant flux. Ensuring absolute accuracy at training time is difficult. The learnt classifiers need the ability to be analyzed and maintained.
Many machine learning based systems inherently have usability issue [31], since they do not give interpretable reports and are hard to dynamically update. Even for those machine learning systems with interpretable models such as decision trees [36], or signature learning systems based on data mining techniques [4], [28], [34], [40], [19], the lack of expressiveness in the inputs and outputs to these tools undermines the efficiency of the process. We elaborate on these challenges next.

B. Requirements for Trace Analysis
The input representation and output model determines the capability and usability of a learning-based system. A good model choice should address the following concerns to fully capture the information embedded in a network trace. First, a network trace is a sequence of packets of varying lengths listed in increasing timestamp order. As an element of the sequence, each packet is a list of header fields. The input representation should not discard the rich information contained in this potentially large amount of data, and at the same time be adaptive to the varying length. Second, network protocols are state machines by design. The output model should be able to express the change of internal states of the flows.
The categories (1) and (2) above characterize the majority of systems using machine learning or data mining techniques. The input features are typically statistical values at the entire flow level, such as the total duration of a flow, mean forward packet length, min activation time, etc. These systems have difficulty capturing the patterns of attacks that require tracking the change of protocol states. For example, to learn the Slowloris attack, one needs to recognize the establishment of a large number of handshakes, and at the same time an excessively low transmission rate or long duration in each established session. This combination is beyond the expressiveness of simple flow statistics.
Although one can theoretically use more complex features such as 'number of established handshakes', 'average transmission rate' and 'average flow duration' to learn this attack, there are two problems in doing so. In terms of input to the learning system, the discovery of these features are manually done by the user rather than automatically extracted from the traces. In terms of output of the learning system, even if learning-based systems report the most outstanding features in recognizing this attack, it still takes some human effort to figure out how the three individual features can be stitched up to form an attack. Regular expressions in 3) are used to describe patterns in each individual packet's payload, which obviously can not handle session-level attacks. Besides, the widespread use of encryption makes it harder to gain payload information. Type 4) provides a satisfying format. However, since state machine is not a very succinct model, it has been able to work in very limited environments such as verifying protocols.

C. The Case for NetQRE
Sharingan synthesizes NetQRE [38] programs, which we will describe in more details in Section IV. We provide a highlevel intuition here on why learning a NetQRE program from raw network traces can address all of the above challenges. NetQRE has an intuitive syntax for describing session-level patterns with quantities. For example, a Slowloris attack's pattern can be programmed close to this natural language description: A flow starts with a TCP handshake, followed by arbitrary packets whose intervals are large. This pattern repeats many times.
At a high level, NetQRE is comprised of two parts: regular expression on packet sequence and quantitative aggregation. The regular expression part describes the properties of packets of interest and their positional and repeating relations, similar to a plain regular expression describing a string of characters. Regular expressions are equivalent to finite state machines in expressiveness but are typically more succinct, therefore they can easily capture the stateful nature of network protocols. Since NetQRE operates at the session-level, raw network traces can be directly used as inputs.
Based on the session-level regular patterns, the quantitative part of NetQRE further specifies the following types of properties: how many times does a pattern show up in the trace? Which sub-pattern shows up most frequently in the trace and what is its frequency? If the trace is somehow to be split into sub-flows, how many times will the pattern show up in each sub-flow? Such patterns may be concatenated and nested  based on the abstract syntax tree of the regular expression. This gives NetQRE programs the additional ability to express quantitative patterns.
Similar to the probability score given by statistical models, the integer answer from the entire NetQRE program can be seen as how well the pattern is satisfied by the current input trace. Therefore, setting a learned threshold for this output answer can turn the NetQRE program into a classifier, where network traces assigned a higher value by the NetQRE program will be seen as a positive match.
A more detailed comparison between our approach and other models is given in Table I D. Search-based program synthesis Search-based syntax-guided synthesis is the process of finding a satisfying program based on the grammar of the language and the specified logical constraints, which in our case is the labelled input network traces.
The main process of search-based synthesis is exploring the program space guided by the grammar. We can use this simple regular expression grammar as an example: 1 <re > : We begin with the starting symbol or top-level non-terminal, which is the < re > symbol. In each step, we can expand any non-terminal in the program following a rule in the grammar and get a new program. This process is called production or mutation. For example, give the program (< re >) * , we have four production choices over the < re > symbol, which results in four new programs:(0) * , (1) * , (< re >< re >) * and ((< re >) * ) * . The first two results are complete and contain no non-terminals. Therefore they can be checked against the input examples. If either one works, we have found a solution and the search can stop. Otherwise we need to keep exploring the other two, which still contain non-terminals. We call these programs with non-terminals partial programs.
One outstanding feature of search-based synthesis is that the only a priori knowledge it needs is information about the language itself. No task-specific heuristics are required.
Although there has been a proliferation of research on program synthesis in recent years, all the proposed techniques are specific to languages or type of languages. Popular synthesis targets include string manipulation languages [9], [21], [24], typed functional languages [20], [23], simple imperative languages [29], languages that can be reduced to SMT solving problems [27], etc. NetQRE is a stream processing language with valued state. Efficiently synthesizing NetQRE program is the main technical challenge of our work. No known technique can be directly applied to synthesis of NetQRE expressions.
In addition to that, fully describing properties of a malicious behavior, and especially normal behaviors in contrast, requires learning from at least megabytes of network traffic, which is orders of magnitude larger than a typical program synthesis task addressed in existing literature. Since one major overhead of program synthesis is checking explored programs against the training data, larger training data means proportional time consumption. For systems whose exploration states grows exponentially to the input size [9], the situation could be even worse. No known system has a design to especially address this concern. To overcome the difficulties, we design two synthesis techniques: partial execution and merge search, which will be explained in detail in Section §V III. OVERVIEW Fig 1 shows the overall design of Sharingan. Sharingan's workflow is largely similar to a statistical supervised learning system, although the underlying mechanism is different. Sharingan takes labeled positive and negative network traces as input and outputs a classifier that can classify any new incoming trace. To preserve the most of information from input data and minimize the need of feature engineering, Sharingan considers three kinds of properties in a network trace: (1) all available packet-level header fields, (2) position information of each packet within the sequence, (3) time information associated with each packet.
Specifically, Sharingan represents a network trace as a stream of feature vectors: S = v 0 , v 1 , v 2 , . . .. Each vector represents a packet. Vectors are listed in timestamp order. Contents of the vector are parsed field values of that packet. For example, we can define Depending on the information available, different sets of fields can be used to represent a packet. By default, we extract all header fields at the TCP/IP level. To make use of the timestamp information, we also append time interval since the previous packet in the same flow to a packet's feature vector. Feature selection is not necessary for Sharingan.
The output classifier is a NetQRE program p that takes in a stream of feature vectors. Instead of giving a probability score that the data point is positive, it outputs an integer that quantifies the matching of the stream and the pattern. The program includes a learnt threshold T . Sharingan aims to ensure that p's outputs for positive and negative traces fall into different sides of the threshold T . Comparing p's output for a data point with T generates a label. It is possible to translate p and T into executable rules, using a compilation step.
Given the above usage model, a network operator can use Sharingan to generate a NetQRE program trained to distinguish normal and suspected abnormal traffic generated from unsupervised learning systems. The synthesized program itself, as we will later show, forms the basis for deciphering each unknown trace. Consequently, traces whose patterns look suspicious can be subjected to a detailed manual analysis by the network operator. Moreover, the generated NetQRE programs can be further refined and compiled into filtering system's rules. The superior expressiveness and explainability also allows easier maintenance and debugging in later tests and deployment.

IV. BACKGROUND ON NETQRE
In this section, we give a more formal definition of NetQRE [38]. It is a high-level declarative language for querying network traffic. Streams of tokenized packets are matched against regular expressions and aggregated by multiple types of quantitative aggregators. The NetQRE language is defined by the following BNF grammar: 1 <classifier > ::= <program > > <value > As an example, if we want to find out if any single source is sending more than 100 TCP packets, the following expression describes the desired classifier: At the top level, there are two parts of it. A processing program that maps a network trace to an output number: and a threshold against which this value is compared: They together form the classifier. Inputs fall into different classes based on the results of the comparison.
The processing program is designed to count the largest number of TCP packets any single source is sending. The first It splits the trace into sub-flows based on the value of the specified field (source IP address in this example). That is, packets sharing the same value in the field will be assigned to the same sub-flow. Then the matching and aggregation is done on each sub-flow individually. Finally, the expression aggregates results from all sub-flows according to the aggregation operator (< op >) (maximum in this example). In each subflow, we want to count the number of TCP packets. This can be broken down into three operations: (1) specifying a pattern that a single packet is a TCP packet, (2) specifying that this pattern repeats arbitrary number of times, and (3) adding 1 to a counter each time this pattern is matched.
(1) is achieved by a plain regular expression involving predicates. A predicate describes properties of a packet that can match or mismatch one packet in the trace. Four types of properties frequently used in networks can be described: Predicates combined by concatenation and Kleene-star form a plain regular expression, which matches a network trace considered as a string of packets. In this example, we use a plain regular expression containing only one predicate that describes a single packet whose protocol type equals TCP: It is marked as a unit expression, which indicates that this plain regular expression should be viewed as atomic for quantitative aggregation. It either matches a substring of the trace and outputs the value 1, or does not match.
To achieve (2) and (3), we need a construct to both connect the plain regular expressions to match the entire flow and also aggregate outputs bottom up from units at the same time. We call it quantitative regular expression (< qre >). In this example, we use iteration operator: For matching, it works exactly like the Kleene-star operator describing that its sub-pattern repeats arbitrarily many times. At the same time, for each repetition, the sub-expression's output is aggregated by the aggregation operator. In this case, the sum is taken, which acts as a counter for the number of TCP packets. The aggregation result for this expression will in turn be returned as an output for higher-level aggregations.
The language also supports the concatenation operator: which works exactly like concatenation for regular matching. It aggregates the quantity by applying the < op > on the outputs of two sub-expressions that match the prefix and suffix.
In addition to this core language, there is a specialization for synthesis purpose. Enumerating all possible values for a field in the choice of predicates is computationally expensive for the synthesis algorithm. We observe that comparing a field with a value that does not appear in any of the given examples will not produce any meaningful information. Therefore we use the relative position in the examples' value space instead of a specific value. For example, if the packets in the given examples have 4 different sequence numbers: {1, 3, 12, 15}, then [tcp.seq >= 50%] matches a packet with sequence number no less than 50% of values in the set. In other words, it is equivalent to [tcp.seq >= 3]. Prefix is also replaced by the common prefix of values in a range. For example, given the same value set above, 15 = 1111b). When the synthesis procedure finishes, the real values will be substituted into the output program and it can run independently without the examples.

V. SYNTHESIS ALGORITHM
Given a set of positive and negative examples E p and E n , respectively, the goal of our synthesis algorithm is to derive a NetQRE program p f and a threshold T that differentiates E p apart from E n . We start with notations to be used in this section: Notation. p and q denote individual programs, and P and Q denote sets of programs. p 1 → p 2 denotes it is possible to mutate p 1 following NetQRE's grammar to get p 2 (see §II-D for definition of mutation). The relation → is transitive. There is a starting symbol s 0 . All programs must be derived from s 0 , that is, s 0 → p. We primarily care about the program part in the NetQRE classifier. Therefore s 0 is always a single non-terminal < program >.
p(x) denotes program p's output on input x, where x is a sequence of packets and p(x) is a numerical value. If p is an incomplete program, i.e., if p contains some non-terminal, then p(x) = {q(x) | p → q} is a set of numerical values, containing x's output through all possible programs p can mutate into. We define p(x).max to be the maximum value in this set. Similarly, p(x).min is the minimum value.
The synthesis goal can be formally defined as: ∀e ∈ E p , p f (e) > T and ∀e ∈ E n , p f (e) < T .

A. Overview
Our design needs to address two key synthesis challenges. First, NetQRE's rich grammar allows a large possible program space and many possible thresholds for search. Second, the need to check each possible program against a large data set collected from network monitoring tasks adds great overhead to the synthesis process.
We propose two techniques for addressing these challenges: partial execution (Section V-B) and merge search (Section V-C). Figure 2 shows an overview of the synthesizer.
The top-level component is the search planner, that assigns search tasks over subsets of the entire training data to the enumerator in a divide-and-conquer manner. Each such task is a search-based synthesis instance, where the enumerator enumerates all possible programs starting from s 0 , expanded using the productions in NetQRE grammar, until one that can distinguish the assigned subset of E p and E n is found.
The enumerator optimizes for the first challenge by querying the distributed oracle about each partial program's feasibility and doing pruning early. The oracle evaluates partial programs using partial execution. The search planner optimizes for the second challenge by merging search results from subsets of the large training data, so as to save unnecessary checking, which we call the merge search strategy.
We next explain each technique in detail in the rest of this section.

B. Partial Execution
A partial program is an incomplete program with nonterminals. In a standard syntax-guided synthesis process, the oracle only makes assertions about complete programs. However, we observe that even if a program is partial, the existing skeleton already reveals some property about the set of possible completions. By making use of this information, accept/reject decisions can be made earlier in the search process, thereby narrowing down the search to very few valid branches in the search tree.
Specifically, we define p(x) = { q(x) | p → q } for a partial program p. It is obvious that the range of p(x) contains the range of every q(x). That is for all q such that p → q , p(x).min ≤ q(x).min ≤ q(x).max ≤ p(x).max. Making use of this knowledge, we may be able to decide if some completion p f such that p → p f can be a potential solution. If no such completion exists, we do not need to explore p any further. On the other hand, we may be able to conclude that for all q such that p → q , q ∈ P f holds, and in that case, we can select an arbitrary completion of p to get a satisfying solution. We will discuss how to make this decision later. For now, we describe an efficient way to evaluate p(x) for a partial program p and any input x. Equivalent Completion A partial program p with nonterminals cannot be directly evaluated on an input. We use an idea similar to overestimation in prior work [12], [29], [30]. A completed version of p, denotedp, is used to approximate p's behavior. Recall that the necessary condition for early pruning is to have for all q such that p → q , p(x) contains q(x). If we can make sure thatp(x) further contains p(x), then p(x) can be approximated byp(x).
For many missing components in the syntax tree (that is, non-terminals that have not been expanded), it is straightforward to find an equivalent completion. We replace (1) any uncertain numerical value with the largest or smallest possible value depending on the context, (2) any unknown predicate with unknown, (3) any unknown regular expression with * and (4) any unknown quantitative regular expression with (/ * /) * sum. The details of these four cases are briefly described below.
The syntax tree enumeration phase identifies proper numerical values by binary search. For example, to find 62.5%, the range [50%, 100%] is first explored. If it works, this is refined to [50%, 75%], and eventually 62.5%. If there is an incomplete predicate [feat1 >= [50%, 100%]], it can be completed by taking the smallest possible value 50% in the unknown part and turned intop: [feat1 >= 50%]. If the operator is <=, we take the largest possible value instead.
We define unknown as a Boolean state that is possibly true and possibly false, which indicates the uncertain status of a partial predicate with incomplete parts other than numerical values. Wherever a true is required, unknown also works, since it already implies the possibility of matching. The calculation rule for unknown is: There are some non-terminals that cannot be completed in this way, such as flow split and aggregation operators. We put a complexity penalty over these non-terminals if they are not expanded, therefore encouraging expanding them earlier We can write a NetQRE program based on them: 1 ( ( /AA/ )*sum ( /B/ )*sum )max which describes a trace with an even number of TCP packets followed by an arbitrary number of UDP packets. It counts the number of TCP packet pairs and the number of UDP packets, and outputs the larger number. Since A and B are mutually exclusive, the expression is not ambiguous.
Suppose there is a trace of packets CCCCD, where C's are some TCP packets and D is a UDP packet. Let us first consider how it is processed by the unambiguous NetQRE program. The execution can be illustrated by the flowchart in Figure 3. A trace first goes to the left cycle that consumes pairs of TCP packets, and then to the right for UDP packets. The order of packets matched is also shown in the figure as subscripts.
In order to compute the numerical output, the program needs to maintain aggregation states during the matching. For example, when it processes the third packet C 3 , the execution is at the middle of the second loop of the left cycle. The accumulated sum of this iteration cycle is currently 1, and maximum value of the outermost concatenation is currently the initial value 0. These are core states for computing ambiguous To evaluate this partial program, we complete it by replacing the missing predicate with unknown, denoted as below, which matches any packet: 1 ( ( /AA/ )*sum ( /_/ )*sum )max As a result, the program has become ambiguous. To evaluate it on the input trace CCCCD, there are three different correct matching strategies: matching the first iteration of two TCP packets with 0, 2, or 4 number of C packets respectively, and matching the rest of the trace with the iteration of wildcard. They produce three outputs: 5, 3, 2. The set {2, 3, 5} is an optimal result. But in practice, since we will compare with a specific threshold, only the upper-bound and lower-bound of all possible outputs is needed. Therefore we want the output to be the interval [2,5].
A strawman method is simply to enumerate all possible matching strategies and take the union of all their outputs. The problem is that there can be exponentially many distinct matching strategies with respect to the length of the network trace, leading to unacceptable synthesis time.
We solve this problem by an approximation: we merge "close" matching strategies. Two strategies are defined to be "close" if at some step of their matching process (1) they have matched the same number of packets in the trace and (2) the last predicate they have matched is exactly the same. We explore all matching strategies simultaneously and do a merging whenever two strategies can be identified to be close.
We now describe how this merging works. Again, we use the program and trace above as an example. We inspect the two matching strategies that match the first iteration of two TCP packets against 0 and 2 packets of the trace respectively. After matching the third packet C against the wildcard , they can be identified to be close. Their matching and aggregation states before this point are shown in Figures 4 and 5. We observe that the corresponding executions have maintained different aggregation states. For strategy one, the current maximum for outermost concatenation is 0 and the current sum for the second iteration is 3. For strategy two, the current maximum for the concatenation is 1 while the current sum for the second iteration is 1. Similar to the way we handle final outputs, we merge these aggregation states by recording the range of all possible values from merged strategies. In this specific example, the two values will result in intervals [0, 1] and [1,3] respectively. For the remaining part of the matching, the aggregations will be done on the intervals, which is illustrated in Figure 6. Eventually, the final aggregation result [3,5] is the estimated output for this merged strategy. The regular matching result by this approximation is correct, since at any step, a matching strategy's remaining part is determined by matched packets' length and the current predicate. It can also be proven by the properties of interval arithmetic that the aggregation result strictly contains the true output range. Or more formally,p(x).min ≤ p(x).min ≤ p(x).max ≤ p(x).max. Therefore p(x) can be approximated byp(x).
Intuitively, the proposed evaluation scheme works well because we only care about the boundary of outputs, which are represented by intervals as the abstract data type. We implement the execution and approximation process by the Data Transducer model proposed by [2], which consumes a small constant memory and liner time to the input trace's length given a specific program.
Make Decision: Given that we are able to evaluatep(x).min andp(x).max, the next step requires us to make the accept/drop decision and find the proper threshold T .
Let q be a complete program and assume there is only one pair of examples e p and e n . Now that we can evaluate ambiguous programs, we allow q to be ambiguous too. For q to accept e p and e n , there must be a threshold T such that q(e n ).max < T < q(e p ).min. Therefore, given a pair of examples e p and e n , a program q is correct if and only if q(e n ).max < q(e p ).min. When this holds, any value between q(e n ).max and q(e p ).min can be used as the threshold.
Lemma 1: There exists a correct program q such that p → q only ifp(e n ).min <p(e p ).max Lemma 2: Ifp(e n ).max <p(e p ).min then any program q such that p → q is correct.

C. Merge Search
In the rest of this subsection, we describe three heuristics for scaling up synthesis to large data sets, namely divide and conquer, simulated annealing, and parallel processing. We call the combination of these the merge search technique. Divide and Conquer. We process large data sets in a divideand-conquer manner. Enumerating and verifying programs on large data sets is expensive. If we can learn patterns on small subsets and merge them into a global pattern with low overhead, then the performance will be significantly improved. This strategy works well for many pattern learning tasks due to two observations. First, the pattern of the entire data set is usually shaped by a few extreme data points. Most data points are highly similar and provide almost no additional information. For example, if we are learning the SYN flood attack, one complete TCP handshake is enough to tell that a valid TCP connection does not belong to SYN flood. All other complete TCP handshakes provide no more information. Therefore, looking at these extreme data points locally is enough to figure out critical properties of the global pattern. A larger data set has a higher probability to cover more extreme data points, and thus raising the accuracy. But learning by the entire data set is unnecessary.
Second, resolving conflicting patterns is straightforward. Only in rare cases will they fundamentally conflict with each other. Mostly, the local patterns are simply describing different aspects of the same object, e.g. "a broken handshake" and "a large number of flows" in the SYN flood example. Simple merge operations such as disjunction, truncation or concatenation are enough to unify them.
This divide and conquer strategy is captured in the following algorithm: The "split" step corresponds to evenly splitting positive and negative examples. The conquer, or "merge" step is more complex. First, each candidate expression is turned into its abstract syntax tree (AST). Then, low-level subtrees of these ASTs are collected. That is, suppose there is a low-level nonterminal a in the AST, and the sub-program corresponding to a is b 1 b 2 b 3 ... b m where each b i is a terminal. We then remove the entire subtree of a from the AST and add the rule < a >::= b 1 b 2 b 3 ...b m to the syntax. This creates a shortcut for search in the future.
Using the SYN flood attack as an example. Suppose we have found a local pattern: We are then left with a partial program: This process can be recursively done on all non-terminals in a's subtree. The partial programs left over are used as seeds for synthesizing future programs, and a new search on the merged data set will be done starting with these seed programs. With the expanded syntax, all previously learnt local patterns can now be used as building blocks for the global pattern. To encourage reuse of local patterns, the seeds and added syntax nodes are given complexity rewards to raise their priority.
The pseudocode for merge is also given below: In practice, many search results can be directly reused from cached results generated from previous tasks on similar subsets. This optimization can further reduce the synthesis time. Simulated Annealing When searching for local patterns at lower levels, we require the Enumerator to find not 1 but t candidate patterns for each subset. Such searches are fast for smaller data sets and can cover a wider range of possible patterns. As the search goes to higher levels for larger data sets, we discard the least accurate local patterns and also reduce t. The search will focus on refining the currently optimal global pattern. This idea is based on traditional simulated annealing algorithms and helps to improve the synthesizer's performance in many cases. Parallelization. Most steps in the synthesis process are inherently parallelizable. They include (1) doing synthesis on different subsets of data, (2) exploring different programs in the enumeration, (3) verifying different programs found so far, (4) executing a program on different data points during the verification.
We focus less on optimizing (1) and (2) since they are not the performance bottlenecks. We instead focus on parallelizing (3) and (4) over multiple cores. In our implementation, using 5 machines with 32 cores each, we devote one thread each to run task (1) and (2) on one machine, 64 threads on the same machine to run task (3), and 512 threads distributed over the remaining four machines to run task (4). The distributed version is approximately two orders of magnitude faster than the single-threaded version for complex tasks. Given more computing power, a proportional speedup can be expected.

VI. EVALUATION
We implemented Sharingan in 10K lines of C++ code. Our experiments are carried out in a cluster of five machines directly connected by Ethernet cable, each with 32 Intel(R) Xeon(R) E5-2450 CPUs. The frequency for each core is 2.10GHz. The core Sharingan synthesizer runs on one machine, with 64 threads exploring new programs and doing early syntactical checks. 512 execution engine threads are distributed over the remaining four machines. Execution tasks are assigned using RPC. Our evaluation summary is: 1) Sharingan requires minimal feature engineering and data preparation work (Section VI-A); 2) Sharingan generates NetQRE programs that achieve comparable accuracy as state-of-the-art network traffic classification systems (Section VI-B); 3) Sharingan generates NetQRE programs that are natural to decipher, edit and tune by a network operator(Section VI-C); 4) Generated NetQRE programs are amenable to efficient execution and deployment on existing monitoring systems, for example, compilation into Bro [22] programs (Section VI-D); 5) Synthesis algorithms in Sharingan are efficient and can be practically deployed. In several instances, the optimizations ensure that synthesis time is a few minutes, and complete classification tasks that are otherwise impossible to finish within reasonable time limits (Section VI-E).

A. Data Preparation
We utilize the CICIDS2017 database [26], a public repository of attack traffic used for evaluating intrusion detection systems. The database contains five days worth of network traffic consisting of benign and a wide range of attack traffic. The data set consists of labeled data in the conventional format of extracted feature vectors of each flow. Each flow is given a label of "benign" or a specific attack type.
Since NetQRE operates on raw traffic, for our experiments, we pre-process the data to correlate the feature vectors' information and labels with the raw traffic data, and use the labeled raw packet flows for our experiments.
In our experiments, we utilize training and testing data for eight types of attacks. These attacks are based on botnets, Denial of service (DoS), port scanning, and password cracking.
We learn each type of attack against benign traffic separately. To use as many data as possible, for each attack type, we use 1500 positive (attack) flows and 10000 negative (benign) flows for training, and another distinct data set of similar size for testing. Note that "training" in our case refers to generating the NetQRE expression from examples.
The main benefit of Sharingan in this experiment is the minimal need for feature engineering. We simply use all header fields of TCP and IP, and as additional features, we add the inter-packet arrival time between adjacent packets in the same flow. In total, there are 19 features per packet. Given a trace of N packets, the feature matrix presented to Sharingan is of size N ×19. In most of our experiments, this consists of thousands of features.
Sharingan data preparation work requires minimal feature engineering. In contrast, other state-of-the-art systems rely on a carefully designed feature extraction step to work well. For example, the feature vectors included in CICIDS2017 database contain 84 features extracted by the CICFlowMeter [6], [11] tool for each flow, characterizing performance metrics of the entire flow such as duration, mean forward packet length, min activation time, etc. Kitsune [16] extracts bandwidth information over the past short periods as packet-level features. DECANTeR [4] uses HTTP-level properties such as constant header fields, language, amount of outgoing information, etc. as flow-level features. Sharingan avoids complex feature engineering, and this reduces errors and data preparation time. As we will later demonstrate, despite minimal feature engineering, Sharingan is able to achieve comparable accuracy as competing approaches.

B. Learning Accuracy
We next validate Sharingan's learning accuracy using the following evaluation methodology: • For each attack from the CICIDS2017 data set, we use the training data (attack and normal traffic) as input to Sharingan to learn a NetQRE program. The full set of synthesized NetQRE programs is shown in Appendix A. The NetQRE program is then validated on the testing set for accuracy. Note that we focus on single attacks in our experiments. Generating NetQRE programs from traces consisting of multiple attacks is a direction of future research. • The output of Sharingan includes a NetQRE program that maps a network trace to an integer output and a recommended range for the threshold. By modifying the threshold, true positive rate (TP) and false positive rate (FP) can be adjusted, as we will later explain in Section VI-C. We use AUC (Area under Curve) -ROC (Receiver Operating Characteristics) metric, which is a standard statistical measure of classification performance. • In our evaluation, Sharingan computes the top five candidate programs instead of one, and the program with the highest test accuracy is picked as the final answer.
In practice, all the top candidates are presented to the network operator to choose, based on domain knowledge.   Fig. 9: Sharingan's true positive rate under low false positive rate, AUC-ROC and learning rate for 8 attacks in CICIDS2017 (higher is better) Figure 9 contains results for eight types of attacks. Apart from AUC-ROC values, we also show the true positive rates when false positive rate is adjusted to 3 different levels: 0.001, 0.01, and 0.03. Given that noise is common in most network traffic, the last metric shown in Figure 9 is the highest achievable learning rate, which is defined as the ratio of training examples the learnt classifier can correctly classify.
Overall, we observe that Sharingan performs well across a range of attacks with accuracy numbers on par with prior state-of-the-art systems such as Kitsune, which has an average AUC-ROC value of 0.924 on nine types of IoT-based attacks, and DECANTeR, which has an average detection rate of 97.7% and a false positive rate of 0.9% on HTTP-based malware. In six out of eight attacks, Sharingan achieves above 0.994 of AUC-ROC and 100% of true positive rate at 1% false positive rate. The major exception is Botnet ARES, which consists of a mix of malicious attack vectors. Handling such multi-vector attacks is an avenue for our future work.

C. Post-processing and Interpretation
One of the benefits of Sharingan is that it generates an actual classification program that can be further adapted and tuned by a network operator. The program itself is also close to the stateful nature of session-layer protocols and attacks, and thus is readable and provides a basis for the operator to understand the attack cause. We briefly illustrate these capabilities in this section. FP-TP Tradeoff Network operators need to occasionally tune a classifier's sensitivity to false positives and true positives. Sharingan generates a NetQRE program with a threshold T . This threshold can be adjusted to vary the false positive and true positive rate. Figures 10 and 11 show the output distribution from positive and negative examples in the DoS Hulk attack. A denotes the largest negative output and B denotes the smallest positive output. When A > B, there is some unavoidable error. If the training data has such an error, the data points between A and B are treated as noise. We can slide the threshold T from B to A and obtain an ROC curve for the test data, as illustrated in Figure 12.
Interpretation We describe a few learnt NetQRE programs to demonstrate how a network operator can interpret the classifiers. A full list of our classification programs is shown in Appendix A. The NetQRE program synthesized by Sharingan for DDoS is: The DDoS attacker launches a SYN or ACK flood attack from a botnet of machines to exhaust memory resources on the victim server. The above program is generated from the actual DDoS attack traffic. The detected pattern consists of packets that start with source IP in the lower half of the value space, followed by a packet with the reset bit set to 1, with arbitrary packets in-between. This pattern is then followed by a packet with a short time interval from its predecessor. Finally, the program considers the flow a match if the patterns show up with a total count of over 4.
The synthesized pattern reveals the nature of the attack upon further investigation by the network operator. The range of source IP addresses specified in the pattern possibly contains botnet IP addresses. Attack flows are often reset when the load can not be handled or the flows' states can not be recognized, which indicates the attack is successfully launched. Packets with short intervals further provide direct support to this hypothesis. Unique properties of DDoS attack are indeed captured by this program! Our next use case is based on Hulk, an attack similar to Slowloris. Hulk issues multiple HTTPS requests, trying to keep them alive, adding more and more connections as time moves forward, and eventually overwhelming the webserver. Hulk requests have a high level of variety, adding difficulty to learning even with knowledge of the requests' contents. The synthesized NetQRE program to identify Hulk is as follows: The program first identifies a large sequence number, which is an indication that someone is trying to keep the connection long. This is followed by a large number of normally finished TCP connections. Connecting the two, it is not hard to guess someone is launching a long and slow attack. This is exactly how Hulk works to cause a DoS.

D. Deployment Scenarios
We now describe three ways for network operators to deploy the output of Sharingan: (1) taking action hinted by the interpretation; (2) directly executing the NetQRE program as a monitoring system; and (3) translating the NetQRE program to rules in other monitoring systems.
Revisiting the DDoS example in Section VI-C, in the first case, the operator may notice that the attack comes from a certain range of IP addresses. By further investigation (for example by manually doing a binary search on the IP range in the NetQRE program), they find out that the attack is based on a cluster of machines with IP range 205.174.165.69 − 71. Then this IP range can be blocked by a firewall to stop future attacks.
If the NetQRE program itself is to be used as a monitoring system, its runtime system can be directly deployed on any general purpose machine. Prior work [38] has shown that NetQRE generates performance that is comparable to optimized low-level implementations. Moreover, these programs can be easily compiled into other formats. In Appendix A, we demonstrate an example Bro [22] program translated from our synthesized NetQRE program for DDoS.

E. Program Synthesis Performance
Synthesis time: In our final experiment, we measure the performance of Sharingan, in terms of its program synthesis time. Our results show that Sharingan is able to identify  Figure 13 shows the program complexity (Y-axis) and synthesis (learning) time (in minutes). Program complexity is measured by the number of expansion decisions (i.e. expansion of non-terminals in the syntax) during the search process. Not surprisingly, complex programs require more time to synthesize. We further observe that Sharingan is able to synthesize complex programs with at least 20-30 terms. Despite the program complexity, synthesis time ranges from minutes to an hour, which is practical for many use cases and can be further reduced through parallelism. Effectiveness of Optimizations. We explore the effectiveness of the individual optimization strategies described in Section V. In Figure 14, we compare the synthesis time and the number of programs searched for a fully optimized Sharingan against results from disabling each optimization. SSH Patator is used as the demonstrating example since it is moderately complex and can finish within reasonable time even after optimizations are disabled. We observe that disabling partial execution optimization makes both programs searched and synthesis time significantly worse. Being able to prune early can indeed greatly reduce time wasted on unnecessary exploration and checking. By disabling merge search, although the number of programs searched decreases, the total synthesis time increases given the overhead of having to check each program against the entire data set. The synthesis can not finish within reasonable time if both are disabled.
Finally, to evaluate the parallelizability of the learning process, we profiled the task running entirely on a single thread and show the time consumed by each phase in Figure 15. Execution of candidate programs on examples takes up almost all execution time. The profiling result also shows that each individual execution takes time in milliseconds (varying due to the size of the example). It supports that program synthesis is highly parallelizable in our architecture.
In summary, all optimization strategies are effective to speed up the synthesis process. A synthesis task that is otherwise impossible to finish within practical time can now be done in less than 15 minutes. Our architecture allows the parallelization of the most time-consuming part of the learning process.

VII. RELATED WORK
Automatic Generation of Network Configurations. Broadly speaking, network traffic classification rule is a type of network configuration. Apart from the forementioned competing systems of Sharingan, there are also other lines of research that aim at the automatic generation of different categories of network configurations. EasyACL [13] aims at synthesis of access control lists(ACL) from natural language descriptions. Soumya et al [15] instead derives ACL implementations from network topology and input security policy specifications. NetGen [25], NetComplete [7] and Genesis [32] synthesize data plan routing configurations based on SMT solvers given policy specifications in regular expressions or customized policy languages. NetEgg [37] instead takes examples provided by user to generate routing configurations in an interactive way. Sharingan focuses on network traffic classification and has a different target from them. Unsupervised Learning Systems. Unsupervised learning is useful for recognizing outliers and other types of "abnormal" flows [17], [39], [35], most notably in intrusion detection systems. Its ability to differentiate unknown types of traffic from the known cannot be replaced by Sharingan.
The most notable shortcoming of unsupervised learning is relatively low accuracy and the difficulty to create an inclusive set of "normal" traffic. Sharingan augments unsupervised learning systems by reducing the effort required for analyzing the reports. Traffic deemed abnormal can be fed into Sharingan to generate interpretable programs to speed up analysis. Syntax-Guided Synthesis. Sharingan builds on a large body of work on syntax-guided synthesis. However, synthesis techniques proposed in this paper go beyond the state of the art, and have the potential to be applied to other applications of program synthesis. The partial execution technique is different from the classic compiler technique of partial evaluation [8], which aims to optimize a program for a faster but equivalent version by specialization.
Partial execution is similar to the idea in reference [12] (see also follow-ups [29], [30]), where the system learns plain regular expressions and overestimates the feasibility of a non-terminal with a Kleene-star. Sharingan generalizes it to the case when the program does classification based on numerical outputs. Partial execution can possibly be applied to other synthesis tasks where the data is highly structured, and its processing is tightly coupled with the language's syntax elements, e.g. learning SQL expressions from examples [33].
To the best of our knowledge, there is no prior work in program synthesis similar to our proposed merge search technique. Merge search is not specific to Sharingan, and can be used in other synthesis tasks to allow the handling of large data sets. Finally, there is no prior work that solely uses program synthesis to perform accurate real-world large-scale classification. The closest work concerns simple low-accuracy programs synthesized as weak learners [5], and requires a separate SVM to assemble them into a classifier.

VIII. CONCLUSION
This paper presents Sharingan, which develops syntaxguided synthesis techniques to automatically generate NetQRE programs for classifying session-layer attack traffic. Sharingan can be used for generating network monitoring queries or signatures for intrusion detection systems from labeled traces. Our results demonstrate three key value propositions for Sharingan, namely it requires minimal feature engineering for all use cases, is amenable to efficient implementation and compilation into code directly executable in legacy systems, and is easy to decipher and edit to understand the nature of the attacks and adapt to customization needs. While achieving these three benefits, Sharingan has accuracy comparable to state-of-the-art statistical and signature-based learning systems and requires synthesis time in minutes for most use cases.
We show below a NetQRE program translated into Bro for detecting DDoS based on the generated DDoS program in Appendix A. The events can be added by plugins.