<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>Searching for similar code sequences in executable files using siamese neural network</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Alexander Yumaganov</string-name>
          <email>yumagan@gmail.com</email>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Geoinformatics and Information Security Department Samara National Research University Samara</institution>
          ,
          <country country="RU">Russia</country>
        </aff>
      </contrib-group>
      <pub-date>
        <year>2020</year>
      </pub-date>
      <fpage>203</fpage>
      <lpage>206</lpage>
      <abstract>
        <p>-This work is dedicated to solving the problem of finding similar code sequences (functions) in executable files. The description of functions obtained using the proposed method for solving this problem is based on the mutual spatial position of processor instructions and the corresponding operands in the function body. The word embedding model word2vec is used to form an intermediate description of the executable file functions. The final description of the functions is formed using the siamese long short-term memory network (siamese-LSTM). Then it description directly used to search for similar functions. The results of experimental studies of the developed method are presented in comparison with some previously known methods.</p>
      </abstract>
      <kwd-group>
        <kwd>searching</kwd>
        <kwd>code sequence</kwd>
        <kwd>siamese neural network</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>-</title>
      <p>I. INTRODUCTION</p>
      <p>
        Nowadays, developers of new software often use code
that was developed earlier to solve other problems. Reusing
the code can improve the development time of new software,
but it can also cause errors and vulnerabilities in the created
products. According to studies presented in [1], 69
vulnerable fragments of C ++ code found from
StackOverflow.com were used in 2859 projects on the
GitHub service. In addition, using someone else's code may
be illegal. According to the Synopsys report “Open source
security and risk analysis” [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ] for 2019, it was found that out
of more than 1200 audited software products, 67% contained
components with license conflicts.
      </p>
      <p>Most software developers do not share their source code,
so it is difficult to perform the analysis of their software. In
this case, the analysis of executable files is the only way to
analyze the code of that kind of software.</p>
      <p>Thus, solving the problem of finding similar code
sequences in executable files allow us to solve such problems
such as finding known vulnerabilities and plagiarism
detection without using the source code of the analyzed
software.</p>
      <p>
        There are a lot of methods of similar code sequences
search in executable files. In [3], a method finding similar
code sequences of code was presented. This method is based
on comparing limited sequences of processor instructions
(kgram). To identify library functions in the IDA disassembler,
the Fast Track Identification and Recognition Technology
(FLIRT) algorithm [
        <xref ref-type="bibr" rid="ref3">4</xref>
        ] is used. This algorithm is based on a
comparison of function templates. The methods mentioned
above are sensitive to syntactic changes of the program code
(for example, replacing a command with an equivalent
command or group of commands). There are also methods
based on the analysis of function’s control flow graph
(CFG). The authors of [5] proposed a search method in
which the vector representation of function is formed by
taking into account the structure of its CFG, and the
similarity rate is estimated using a neural network. The
methods of similar code sequences search presented in [6, 7,
8] are based on comparing subgraphs of function’s CFG. The
authors of [9] presented an intrusion detection system for
distributed data processing systems based on a comparison of
process signatures obtained from their control flow graphs.
This group of methods also has disadvantages: high
sensitivity to structural changes in functions, inapplicability
to functions with a small number of basic blocks CFG. Thus,
despite the large number of works in the field of similar code
sequences search in executable files, there is no universal
method or approach to solve this problem, which is devoid of
all the shortcomings.
      </p>
      <p>This paper presents a method of similar code sequences
search in which the initial description of functions is formed
on the basis of the spatial position of processor instructions
and their corresponding operands. The siamese neural
network is used to obtain the final description of the
function.</p>
      <p>The paper is structured as follows. The first section
presents the basic definitions and a brief description of the
proposed method. In the second section, the process of
obtaining the initial description of functions is considered.
The third section presents the descriptions of intermediate
function representation. The fourth section describes the
siamese neural network, which is used to obtain the final
representation of functions. The fifth section is devoted to
the description of the similar functions search algorithm. The
sixth section provides a method for evaluating the
effectiveness of the method and the results of the
experiments. In the final part of the paper, the conclusions
and a list of references are presented.</p>
      <p>II. BASIC CONCEPTS AND PRINCIPLE OF OPERATION
The following definitions are used in this paper:
 current library - the set of the investigated executable
functions;
 archived data - the set of the known functions.</p>
      <p>Taking into account the above definitions, the problem
solved by the proposed method is formulated as follows: for
a given (or each) function of the current library, find the
most similar function from the archive data.</p>
      <p>The method of similar code sequences search proposed in
this work includes several stages. At the first stage, the
process of training the siamese neural network is carried out.
At the second stage a description of the archive data
functions is formed using a trained neural network. At the
third stage, a description of the current library functions is
formed in a similar way. At the final stage, the search for
similar functions is performed.</p>
      <p>Each command and its corresponding operands are
converted into lexemes (words) as follows:</p>
      <p>Structural chart of siamese neural network is shown in
Fig. 1.

</p>
      <p>The input of each siamese network's LSTM network is a
function in the form of previously obtained intermediate
description. Then the outputs of the last layers of each
network are sent to the input of a function that evaluates the
similarity between these outputs. In this paper, the following
metric is used as such a function:
of the intermediate description vector of the first function,
corresponding to its lexemes, I 0B , I1B , I 2B , I 3B are the
components of the intermediate description vector of the
second function, corresponding to its lexemes, y is the output
value of the siamese neural network, taking the value 0 if the
functions are different, otherwise 1.</p>
      <p>
        Contrastive loss [
        <xref ref-type="bibr" rid="ref12">13</xref>
        ] is used as a loss function, which is
given by:
      </p>
      <p>III. CONSTRUCTION OF THE INITIAL AND INTERMEDIATE</p>
      <p>DESCRIPTIONS OF FUNCTIONS
A. Construction of the initial descriptions of functions</p>
      <p>After disassembly analysis of the executable code
performed by disassembler, we can get an assembler of every
function in the executable file. Each function consists of a
sequence of processor instructions and corresponding
operands.</p>
      <p>
        All processor instructions are divided into K functional
groups according to the type of operations they perform (for
example, a group of logical operations, a group of arithmetic
operations). All operands are also divided into N groups
according to the types of operands presented in the IDA
disassembler [
        <xref ref-type="bibr" rid="ref9">10</xref>
        ] (for example, base register, FPP register,
the value itself).
      </p>
      <p>L exem e(k , n0 , n1 )  C oncatenate(k , n0 , n1 ), k  [0, K  1], n0  [0, N  1], n1  [0, N  1], 
where k is a index number of the functional group of the
considered processor instruction, n0 and n1 are index
numbers of the operands groups to which the first and second
operands belong respectively.
k  0</p>
      <p>For example, let us consider the command "mov rax, 1":
(Data Transfer Instructions), n0  1 (General
Register), n1  5 (Immediate):</p>
      <p>L exem e (0 ,1, 5 )  ' 0 0 0 1 0 5 ' </p>
      <p>Thus, each function can be represented as an ordered set
of lexemes.</p>
      <p>B. Construction of the intermediate description of the
function</p>
      <p>
        The word2vec model [
        <xref ref-type="bibr" rid="ref10">11</xref>
        ] is used to obtain a vector
representation of the function's initial description. The vector
representations obtained by this model are based on the
contextual proximity of lexemes inside functions. About a
dozen different binary files were used to train this model. As
a result of the training process, we have a dictionary in which
a vector of a given dimension is assigned to each lexeme
(obtained from training binary files).
      </p>
      <p>Thus, a function consisting of m commands describes as
a two-dimensional vector I of dimension m  s . Let us call
this vector as an intermediate description.</p>
      <p>IV. CONSTRUCTION OF THE FINAL DESCRIPTION OF THE</p>
      <p>FUNCTION</p>
      <p>
        It is proposed to use the siamese neural network [
        <xref ref-type="bibr" rid="ref11">12</xref>
        ] to
construct the final description of the functions. This network
consists of two identical long short-term memory (LSTM)
neural networks with the same weights.
      </p>
      <p>The LSTM network is a special type of recurrent neural
network (RNN). In contrast to RNN, it is able to work with
long-term dependencies. This is achieved due to its ability to
transfer the state of the cell from the previous time step to the
next step, as well as to control the information flow in the
LSTM cell.</p>
      <p>J 1
D ( a , b )  e x p (   a i  bi ), 
i  0

where a i is the value of the i-th element of the last layer of
the first LSTM network, bi is the value of the i-th element of
the last layer of the second LSTM network. Two functions
are considered to be same if D  1 . In the contrary case, if
D  0 two functions are considered to be totally different.</p>
      <p>1
L ( y , D )  (1  y ) D 2 
2
1
2
y{ m a x ( 0 ,1  D )} 2 . 
</p>
      <p>This loss function maximizes and minimizes the value of
(1) between similar ( y  1 ) and different ( y  0 ) functions,
respectively.</p>
      <p>
        Two “archives” of functions were used to train siamese
neural network : the first one contained the library functions
libtiff 4.0.3 [
        <xref ref-type="bibr" rid="ref13">14</xref>
        ] and proj 4.9.1 [15], and the second one
contained the library functions libtiff 4.0.8 and proj 5.0.1.
      </p>
      <p>In the learning process, for each function of the first
archive, two pairs were formed: current function and the
function having the same name from the second archive;
current function, and a random function from the second
archive, which has a name different from the first function.
The resulting pairs of vectors are fed to the input of the
neural network, the output value for the first pair is 1
(similar), and for the second pair is 0 (different).


k
  l
Pk  l 1</p>
      <p>k 
Recall for the k-th position of the list:
k
  l
R k  l 1</p>
      <p>K

</p>
    </sec>
    <sec id="sec-2">
      <title>The average precision of the list:</title>
      <p>L
AveP   Pk ( R k  R k 1 ) , R 0  0
k 1
</p>
      <p>The average precision for all functions included in the
current library is calculated by the formula:</p>
      <p>P 
1 S 1</p>
      <p> AveP s</p>
      <p>S s  0
  
where S is a number of functions in the current library.</p>
      <p>The archive data was represented by the curl 7.6.3 [16],
and the current library functions was represented by other
versions of the curl library.</p>
      <p>
        The average precision of search of the proposed method
was compared with some previously known methods: a
method based on the analysis of the spatial position of
processor functional groups [
        <xref ref-type="bibr" rid="ref16">19</xref>
        ] and a method based on
kgram comparison [3].As a comparison object for the first
method, the comparison object recommended by the authors
was used (the spatial distribution of instructions in the
function body in the integral form). For the second method,
the value of the parameter k = 5 was also chosen based on
the recommendations of the authors. The results are
presented in table 1.
      </p>
      <p>The trained model of the siamese neural network is used
to form the final description of the functions by passing an
intermediate description of the analyzed function to the input
of one of the LSTM networks. The output vector of this
LSTM network, in this case, is the final description of the
function. The dimension of the output layer is J  3 2 .</p>
      <p>Thus, any function of the analyzed binary file can be
represented as a vector of dimension J.</p>
    </sec>
    <sec id="sec-3">
      <title>V. SEARCH FOR SIMILAR FUNCTIONS</title>
      <p>The final stage of the proposed method is the search for
similar functions using the final description of the functions
of archive data and the current library.</p>
      <p>Let a be the final description vector of the current
library function, b be the final description vector of the
archive data library. For feature vector comparison we use
the Euclidean metric (distance):</p>
      <p>J 1
d ( a , b )   ( a i  bi ) 2 , 
i  0
where a i is the value of the i-th component of the current
library function feature vector, bi is the value of the
icomponent of the archive data library function feature vector.
If d  0 the functions are considered equal.</p>
      <p>The final description of the given function of the current
library is compared with each function of archive data using
Euclidian metric. Then, the obtained results are sorted by
increasing the distance (2). As a result, we get the list of
archival functions, sorted by similarity in descending order,
for the analyzed current library function.</p>
    </sec>
    <sec id="sec-4">
      <title>VI. EXPERIMENTS</title>
      <p>To evaluate the efficiency of the proposed method of
similar code sequences search in executable files, the
functions of one dynamic library are used as archive data,
and the functions of the same library, but of a different
version, are used as the current library. It was considered that
in the process of switching from one version of the dynamic
library to another, the names of the functions did not change
and there are no functions with the same name among the
functions of the archive data.</p>
      <p>
        Using the search algorithm described in the fifth section,
for a given function of the current library, we obtain an
ordered list of archive data functions. Let us assign a binary
sequence   (  1 ,  2 , ...,  L ) to this list, the i-th element
of which is equal to one, if the name of the function at the
ith position of the list is identical to the name of the function
being checked, and the i-th element of which is equal to zero
otherwise. The following criteria to evaluate the quality of
information retrieval [
        <xref ref-type="bibr" rid="ref14 ref15">17,18</xref>
        ] are used:
      </p>
    </sec>
    <sec id="sec-5">
      <title>Precision for the k-th position of the list:</title>
      <p>Current
library
curl 7.5.4
curl 7.5.6
curl 7.5.9
curl 7.6.0</p>
      <p>The analysis of the obtained results shows that the
method of similar code sequences search presented in this
work is highly competitive with known methods, and in
some cases surpasses them. Since the training of the siamese
neural network was carried out on a small data set, the results
obtained for this method can be further improved by
increasing the amount of data for training the neural network.</p>
    </sec>
    <sec id="sec-6">
      <title>VII. CONCLUSION</title>
      <p>The paper presents a method of similar code sequences
search in executable files based on the siamese neural
network. The results of experimental studies demonstrating
the efficiency of the developed method (average precision
0.78 - 0.89). Further research will be aimed at tuning the
parameters of the siamese neural network in order to increase
the average precision of the search for this method.</p>
    </sec>
    <sec id="sec-7">
      <title>ACKNOWLEDGMENT</title>
      <p>This work was supported by RFBR research project №
18-01-00748 A.</p>
      <p>Utilities,
2019
[Online].</p>
      <p>URL:
[15] PROJ coordinate transformation software library, 2019 [Online].</p>
      <p>URL: https://proj.org/about.html.
[16] Libcurl - the multiprotocol file transfer library, 2019 [Online]. URL:
https://curl.haxx.se/libcurl/.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          <string-name>
            <surname>Motlagh</surname>
          </string-name>
          , “
          <article-title>An Empirical Study of C++ Vulnerabilities in CrowdSourced Code Examples</article-title>
          ,” arXiv:
          <year>1910</year>
          .01321,
          <year>2019</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          [2]
          <string-name>
            <given-names>Synopsys</given-names>
            <surname>Cybersecurity</surname>
          </string-name>
          Research Center:
          <article-title>Open source security and risk analysis, 2020 [Online]</article-title>
          . URL: https://www.synopsys.com/ content/dam/synopsys/sig-assets/reports/2020-ossra-report.pdf G. Myles and
          <string-name>
            <given-names>C.</given-names>
            <surname>Collberg</surname>
          </string-name>
          , “
          <article-title>K-gram based software birthmarks</article-title>
          ,
          <source>” Proceedings of the ACM symposium on Applied computing</source>
          ,
          <year>2005</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          [4]
          <string-name>
            <given-names>IDA</given-names>
            <surname>Pro. F.L.I.R.T</surname>
          </string-name>
          ,
          <year>2019</year>
          [Online]. URL: https://www.hexrays.com/products/ida/tech/flirt/in_depth.shtml.
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          <string-name>
            <given-names>N.</given-names>
            <surname>Shalev</surname>
          </string-name>
          and
          <string-name>
            <given-names>N.</given-names>
            <surname>Partush</surname>
          </string-name>
          , “
          <article-title>Binary Similarity Detection Using Machine Learning</article-title>
          ,
          <source>” Proceedings of the 13th Workshop on Programming Languages and Analysis for Security - PLAS</source>
          ,
          <year>2018</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          <string-name>
            <given-names>C.</given-names>
            <surname>Kruegel</surname>
          </string-name>
          ,
          <string-name>
            <given-names>E.</given-names>
            <surname>Kirda</surname>
          </string-name>
          ,
          <string-name>
            <given-names>D.</given-names>
            <surname>Mutz</surname>
          </string-name>
          ,
          <string-name>
            <given-names>W.</given-names>
            <surname>Robertson</surname>
          </string-name>
          , and G. Vigna, “
          <source>Polymorphic Worm Detection Using Structural Information of Executables,” Lecture Notes in Computer Science</source>
          , Springer Berlin, Heidelberg, pp.
          <fpage>207</fpage>
          -
          <lpage>226</lpage>
          ,
          <year>2006</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          <string-name>
            <given-names>Y.</given-names>
            <surname>David</surname>
          </string-name>
          and E. Yahav, “
          <article-title>Tracelet-based code search in executables,” ACM SIGPLAN Notices</article-title>
          , vol.
          <volume>49</volume>
          , no.
          <issue>6</issue>
          , pp.
          <fpage>349</fpage>
          -
          <lpage>360</lpage>
          ,
          <year>2014</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          <string-name>
            <given-names>S.</given-names>
            <surname>Eschweiler</surname>
          </string-name>
          ,
          <string-name>
            <given-names>K.</given-names>
            <surname>Yakdan</surname>
          </string-name>
          and
          <string-name>
            <given-names>E.</given-names>
            <surname>Gerhards-Padilla</surname>
          </string-name>
          , “
          <article-title>DiscovRE: Efficient Cross-Architecture Identification of Bugs in Binary Code,”</article-title>
          <source>Proceedings Network and Distributed System Security Symposium</source>
          ,
          <year>2016</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          <string-name>
            <given-names>S.</given-names>
            <surname>Aditham</surname>
          </string-name>
          and
          <string-name>
            <given-names>N.</given-names>
            <surname>Ranganathan</surname>
          </string-name>
          , “
          <article-title>A Novel Control-flow based Intrusion Detection Technique for Big Data Systems</article-title>
          ,” arXiv:
          <fpage>1611</fpage>
          .07649,
          <year>2016</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>
          [10]
          <string-name>
            <surname>Hex-Rays</surname>
            <given-names>IDA</given-names>
          </string-name>
          : About,
          <year>2019</year>
          [Online]. URL: http://hex-rays.com/ products/ida/.
        </mixed-citation>
      </ref>
      <ref id="ref10">
        <mixed-citation>
          [11]
          <string-name>
            <given-names>T.</given-names>
            <surname>Mikolov</surname>
          </string-name>
          ,
          <string-name>
            <given-names>K.</given-names>
            <surname>Chen</surname>
          </string-name>
          ,
          <string-name>
            <given-names>G.</given-names>
            <surname>Corrado</surname>
          </string-name>
          and
          <string-name>
            <given-names>J.</given-names>
            <surname>Dean</surname>
          </string-name>
          ,
          <article-title>"Efficient estimation of word representations in vector space"</article-title>
          ,
          <source>Proc. of ICLR Workshop</source>
          ,
          <year>2013</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref11">
        <mixed-citation>
          [12]
          <string-name>
            <given-names>J.</given-names>
            <surname>Bromley</surname>
          </string-name>
          , I. Guyon,
          <string-name>
            <given-names>Y.</given-names>
            <surname>LeCun</surname>
          </string-name>
          , E. Siickinger and
          <string-name>
            <given-names>R.</given-names>
            <surname>Shah</surname>
          </string-name>
          , “
          <article-title>Signature verification using a “siamese” time delay neural network</article-title>
          ,
          <source>” Proceedings of the 6th International Conference on Neural Information Processing Systems</source>
          , pp.
          <fpage>737</fpage>
          -
          <lpage>744</lpage>
          ,
          <year>1994</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref12">
        <mixed-citation>
          [13]
          <string-name>
            <given-names>R.</given-names>
            <surname>Hadsell</surname>
          </string-name>
          ,
          <string-name>
            <given-names>S.</given-names>
            <surname>Chopra</surname>
          </string-name>
          and Y. LeCun, “
          <article-title>Dimensionality Reduction by Learning an Invariant Mapping,”</article-title>
          <source>IEEE Computer Society Conference on Computer Vision and Pattern Recognition</source>
          , vol.
          <volume>2</volume>
          ,
          <year>2006</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref13">
        <mixed-citation>
          [14]
          <string-name>
            <given-names>TIFF</given-names>
            <surname>Library</surname>
          </string-name>
          and http://www.libtiff.org/.
        </mixed-citation>
      </ref>
      <ref id="ref14">
        <mixed-citation>
          [17]
          <string-name>
            <given-names>M.</given-names>
            <surname>Buckland</surname>
          </string-name>
          and
          <string-name>
            <given-names>F.</given-names>
            <surname>Gey</surname>
          </string-name>
          , “
          <article-title>The relationship between Recall and Precision,”</article-title>
          <source>Journal of the American Society for Information Science</source>
          , vol.
          <volume>45</volume>
          , no.
          <issue>1</issue>
          , pp.
          <fpage>12</fpage>
          -
          <lpage>19</lpage>
          ,
          <year>1994</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref15">
        <mixed-citation>
          [18]
          <string-name>
            <surname>D.M.W. Powers</surname>
          </string-name>
          , “
          <article-title>Evaluation: From Precision, Recall and</article-title>
          <string-name>
            <surname>F-Measure to</surname>
            <given-names>ROC</given-names>
          </string-name>
          , Informedness, Markedness &amp; Correlation,”
          <source>Journal of Machine Learning Technologies</source>
          , vol.
          <volume>2</volume>
          , no.
          <issue>1</issue>
          , pp.
          <fpage>37</fpage>
          -
          <lpage>63</lpage>
          ,
          <year>2011</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref16">
        <mixed-citation>
          [19]
          <string-name>
            <given-names>A.S.</given-names>
            <surname>Yumaganov</surname>
          </string-name>
          and
          <string-name>
            <given-names>V.V.</given-names>
            <surname>Myasnikov</surname>
          </string-name>
          , “
          <article-title>A method of searching for similar code sequences in executable binary files using a featureless approach</article-title>
          ,” Computer Optics, vol.
          <volume>41</volume>
          , no.
          <issue>5</issue>
          , pp.
          <fpage>756</fpage>
          -
          <lpage>764</lpage>
          ,
          <year>2017</year>
          . DOI:
          <volume>10</volume>
          .18287/
          <fpage>2412</fpage>
          -6179-2017-41-5-
          <fpage>756</fpage>
          -764.
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>