tag:blogger.com,1999:blog-58365441981734102602024-03-19T14:08:31.568-07:00Fungus CodesFUNGUSCODES - Confucius - 'The man who moves a mountain begins by carrying away small stones.'CoolerVoidhttp://www.blogger.com/profile/04320162188823710256noreply@blogger.comBlogger10125tag:blogger.com,1999:blog-5836544198173410260.post-65399272028641589022022-06-10T23:06:00.009-07:002022-06-18T07:25:06.430-07:00The magic of bits<p> Before the long tale to the course of magic bits, let's gonna for a little walk in the world of C language.</p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTgb--NVkFhpj0Ikaa4eGVDaakYggCFp8-gZCTuY91piqXmW4LrbWJWMBbrMtrs4O-Q64mqrhG298fQbhd1nM9BE2q18O-1KhdnuAGmZc3wjtA4dpNVv4WgDtdPubTc_eD3UP-uyqJYoEVlgjvIHaJQWr8PH-GPfqh39yeYdu3YTJBBTZ6KOCx8TVj/s258/captura-de-tela-2014-07-14-acc80s-21-14-21.png" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" data-original-height="213" data-original-width="258" height="213" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTgb--NVkFhpj0Ikaa4eGVDaakYggCFp8-gZCTuY91piqXmW4LrbWJWMBbrMtrs4O-Q64mqrhG298fQbhd1nM9BE2q18O-1KhdnuAGmZc3wjtA4dpNVv4WgDtdPubTc_eD3UP-uyqJYoEVlgjvIHaJQWr8PH-GPfqh39yeYdu3YTJBBTZ6KOCx8TVj/s1600/captura-de-tela-2014-07-14-acc80s-21-14-21.png" width="258" /></a></div>Variable of type int has 4 bytes, so 32bits. <p></p><p>If you do it:</p><p><span style="background-color: white; color: blue; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; white-space: pre;">"int var = 3;"</span></p><p>You can see this schema:<br /></p><p><b>4bytes = 32bits</b></p><p>Looking to this context, each octet is a byte, half an octet can be called a nibble.</p><p><b>“…00000000 00000000 0000<span style="color: #e06666;">0011</span>”</b></p><p>Each place of binary number counts as one bit.</p><p>The bit is nothing but a mnemonic for “<b>B</b>inary digi<b>T</b>”</p><p>To know the number of bytes, we use the “sizeof(var)” operator, which returns the value in bytes of the variable. If we want to know the binary value of a number, we divide by 2</p><p>and as long as the remainder of the division is different from “1”, we divide the end. We take the leftovers in reverse to be the final result</p><p>For example number 12</p><p><br /></p>
<div class="container" style="margin-top: 20px;">
<pre><code class="hljs" id="code">
12 | 2
12 +------ 1100 -- reverse result
0 6 | 2 -------------
+-----
0 3 | 2
+-----
1 1
</code></pre>
</div>
<div><br />going on, there's another better way to do it, but no</div><div><div>it's time to address this now.</div></div><div><br /></div><div><br /></div><div>
<div class="container" style="margin-top: 20px;">
<pre><code class="hljs" id="code">
#include <stdio.h>
int main()
{
int bin[8],num=0,counter=0;
puts("Dec2Bin\n digite um numero");
scanf("%d",&num);
while(num!=1)
{
bin[counter]=(num%2)?1:0;
num/=2;
counter++;
}
bin[counter]=num;
printf("\nResultado: ");
while(counter!=-1)
{
printf("%d",bin[counter]);
counter--;
}
printf("\n");
return 0;
}
</stdio></code> </pre>
</div>
<span style="font-size: large;">Introduction to "Bitwise"</span></div><div><br /></div><div><div>When we say bitwise, it is a mere reference to work</div><div>to manipulate bits to get specific results. Staff who work</div><div>with microcontrollers, AVR and PIC often have to do such</div><div>manipulation. When we want bitwise performance, we can</div><div>also help, although the compiler already optimizes many tasks. Another</div><div>use is in image processing and manipulation. OpenCV</div><div>even forces you to use it whenever there is no ready-made solution.</div></div><div><br /></div><div><br /></div><h3 style="text-align: left;">Bit shifting</h3><div><span style="font-size: medium;"><br /></span></div><div><div>Shifting a bit is nothing more than changing the bit from its original position.</div><div>to get a specific result, we call this technique bit shift</div><div>is a mnemonic of the assembly “shl,shr”(shifting left, shifting right),</div><div>Let's look at an example of shifting to the left:</div></div><div><br /></div><div><div class="line number1 index0 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp color1 bold" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: grey; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; font-weight: bold; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">int</code> <code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">var = 3;</code></div><div class="line number2 index1 alt1" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">var <<= 1;</code></div><div class="line number3 index2 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"> </div><div class="line number4 index3 alt1" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">So the 3</code></div><div class="line number5 index4 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"> </div><div class="line number6 index5 alt1" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp spaces" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;"> </code><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">0011</code></div><div class="line number7 index6 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"> </div><div class="line number8 index7 alt1" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">recv left shift</code></div><div class="line number9 index8 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"> </div><div class="line number10 index9 alt1" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp spaces" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;"> </code><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">0110</code></div></div><div><br /></div><div><br /></div><div>The result is “6”, which can give an illusion of product arithmetic or</div><div><div>addition by itself, but it was the result of displacement, form</div><div>Correct according to the K&R math textbook to explain our expression</div><div>an example would be “2*3¹”.</div><div><br /></div><div>Now look that following a shift to the right:</div><div><br /></div><div><div class="line number1 index0 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 1em; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp color1 bold" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: grey; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; font-weight: bold; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">int</code> <code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">var = 100;</code></div><div class="line number2 index1 alt1" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 1em; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"> </div><div class="line number2 index1 alt1" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 1em; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><span style="background-color: initial; color: black; font-size: 10px;">So you can see 1100100</span></div><div class="line number4 index3 alt1" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 1em; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"> </div><div class="line number5 index4 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 1em; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">var >>= 2; </code></div><div class="line number6 index5 alt1" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 1em; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"> </div><div class="line number7 index6 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 1em; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">remove last two digits </code></div><div class="line number8 index7 alt1" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 1em; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"> </div><div class="line number9 index8 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 1em; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp spaces" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;"> </code><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">11001</code></div></div><div class="line number9 index8 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 1em; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;"><br /></code></div><div><div>The result gives us “25” the mathematical form for this is the following “(100/2)²”.</div><div>You tell me 25 thousand. Where are the zeros? As I said, remove the last two digits.</div></div><div><br /></div><h3 style="text-align: left;">Bit mask</h3><div><br /></div><div>OR</div><div><br /></div><div>Let's go to the “|” operator with the “OR” mnemonic in assembly. Let's go to understand its impact.</div><div><br /></div><div><div class="line number1 index0 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">x=26|19;</code></div><div class="line number2 index1 alt1" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"> </div><div class="line number3 index2 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp spaces" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;"> </code><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">11010</code></div><div class="line number4 index3 alt1" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">| 10011</code></div><div class="line number5 index4 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">---------</code></div><div class="line number6 index5 alt1" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp spaces" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;"> </code><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">11011 == 27</code></div></div><div><br /></div><div>AND</div><div><br /></div><div>Now the “&” mnemonic with “AND” in assembly.</div><div><br /></div><div><div class="line number1 index0 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">x=26&19;</code></div><div class="line number2 index1 alt1" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"> </div><div class="line number3 index2 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp spaces" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;"> </code><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">11010</code></div><div class="line number4 index3 alt1" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">& 10011</code></div><div class="line number5 index4 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">---------</code></div><div class="line number6 index5 alt1" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp spaces" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;"> </code><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">10010 == 18</code></div></div><div><br />NOT</div><div><br /></div><div><div>The "~" is mnemonic with "NOT"; that is, it is a negation, making an inverse effect</div><div>of its loaded value, where one is, it becomes zero and vice versa.</div></div><div><br /></div><div><div class="line number1 index0 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">x=~26; </code></div><div class="line number2 index1 alt1" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"> </div><div class="line number3 index2 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp spaces" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;"> </code><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">11010</code></div><div class="line number4 index3 alt1" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp spaces" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;"> </code><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">flip</code></div><div class="line number5 index4 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp spaces" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;"> </code><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">00101</code></div></div><div><br /></div><div><br />the result would be -27, Like but why not 5 ? remember that I said an "int" is 4 bytes equals 32bits, so look at the following:</div><div><br /></div><div><div>0000 0000 0001 1010</div><div><br /></div><div>1111 1111 1110 0101</div></div><div><br /></div><div>I did it in a nibble, so I don't have to write too much.</div><div><br /></div><div><br /></div><div>XOR</div><div><br /></div><div>The “^” is a mnemonic for XOR.</div><div><br /></div><div><div class="line number1 index0 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">x=26^19;</code></div><div class="line number2 index1 alt1" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"> </div><div class="line number3 index2 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp spaces" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;"> </code><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">11010</code></div><div class="line number4 index3 alt1" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">^ 10011</code></div><div class="line number5 index4 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">---------</code></div><div class="line number6 index5 alt1" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp spaces" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;"> </code><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">01001 == 9</code></div></div><div><br /></div><div><br />So look at that table in the following:</div><div><br /></div><div><div class="line number1 index0 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">,---,---,--------,</code></div><div class="line number2 index1 alt1" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">| a | b | a ^ b | pode-se fazer SWAP sem usar uma terceira variável</code></div><div class="line number3 index2 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">|---|---|--------| exemplo:</code></div><div class="line number4 index3 alt1" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">| 0 | 0 | 0 |</code></div><div class="line number5 index4 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">| 0 | 1 | 1 | </code><code class="cpp color1 bold" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: grey; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; font-weight: bold; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">int</code> <code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">A=4,B=7;</code></div><div class="line number6 index5 alt1" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">| 1 | 0 | 1 | A^=B; B^=A; A^=B;</code></div><div class="line number7 index6 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">| 1 | 1 | 0 | </code><code class="cpp comments" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #008200; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">// "A" agora vale 7</code></div><div class="line number8 index7 alt1" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp string" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: blue; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">'---'</code><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">---</code><code class="cpp string" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: blue; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">'--------'</code></div><div class="line number9 index8 alt2" style="background: none white; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: #444444; direction: ltr; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 13px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; vertical-align: baseline; white-space: pre; width: auto;"><code class="cpp spaces" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;"> </code><code class="cpp plain" style="background: none; border-radius: 0px; border: 0px; box-shadow: none; box-sizing: content-box; color: black; direction: ltr; display: inline; float: none; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 10px; height: auto; inset: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto;">alguns usam XOR em criptografia também...</code></div></div><div style="text-align: left;"><br /><h3><u>The magic of the bits</u></h3><div><u><div>Let's use bitwise to get "performance" let's go to get </div><div>some bitwise code snips.</div><div><br /></div><div>
<div class="container" style="margin-top: 20px;">
<pre><code class="hljs" id="code">
// check if a value is even or odd
main(int argc,char *argv[]){printf("%s\x0a",(!((atoi(argv[1]))&1))?"odd":"even");}
// This "x&1" same effect like "x%2"
// num is multiple
resto = num & (divisor - 1);
x = 122 % 6;
// maybe faster
x = 122 & (6 - 1);
/*
casting float to Int
"x = int(3.142)" try to "x=3.142>>0;" can be improve the performance
*/
// ternary operation
i = x < 0 ? -x : x;
// try
i = (x ^ (x >> 31)) - (x >> 31);
// compare ints
x = a * b > 0;
// or try
x = a ^ b >= 0;
//Compare two vars
gamma = y ^ ((x ^ y) & -(x < y)); // equivalent to gamma=menor(x, y)
gamma = x ^ ((x ^ y) & -(x < y)); // equivalent to gamma=maior(x, y)
//check 2 potence
x = v && !(v & (v - 1));
//average number to integer
int a=6,b=8; printf("%d\n",((a&b)+(a^b)>>1));
// check if exist position "n" in bit "1"
if( n & 1 << i )
</code></pre>
</div>
</div><div><br /></div></u></div></div></div><div>So remember our simple code to convert decimal to binary? Let's make one using bitwise 🙂</div><div>
<div class="container" style="margin-top: 20px;">
<pre><code class="hljs" id="code">
// https://github.com/CoolerVoid/C/
char * dec2bin(int n, char * string)
{
int i;
static int size = 8 * sizeof(int);
for(i = size - 1; i >= 0; i--, n >>= 1)
string[i] = (01 & n) + '0';
string[size] = '\0';
return string;
}
</code></pre>
</div>
<br /></div><div><br />The square root calc, following bitwise path:</div><div>
<div class="container" style="margin-top: 20px;">
<pre><code class="hljs" id="code">
// header beer.h https://github.com/CoolerVoid/C/edit/master/beer.h
int bit_sqrt(int num)
{
//so 32 is sizeof(int)<<3 -1="" bit_sqrt="" error="" if="" int="" num0="num,result=0,tbit=1<<((sizeof(int)<<3)-2);" num="" printf="" return="" tbit="" while="">num0)
tbit>>=2;
while(tbit^0)
{
if(num0>=result+tbit)
{
num0-=result+tbit;
result=(result>>1)+tbit;
}else
result>>=1;
tbit>>=2;
}
return result;
}
</3></code></pre>
</div>
<br /></div><div><br /><div>This function cannot be compared to APIs such as GMP and OpenSSL</div><div>even because it is a simple function, much less “math.h”</div><div>it was more to illustrate.</div><div>Can I use bitwise on strings?</div><div>If it is a pointer, why not?</div>
<div class="container" style="margin-top: 20px;">
<pre><code class="hljs" id="code">
// return reverse string
char *strrev(char *str)
{
char *p1, *p2;
if(! str || ! *str)
return str;
for(p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
return str;
}
</code></pre>
</div>
<div class="container" style="margin-top: 20px;">
<p>So this second example is a simple benchmark following CPU cyles to compare arithmetic division using bitwise and common resource.</p>
<pre><code class="hljs" id="code">
/*
Author: Cooler_
Contact: c00f3r[at]gmail[dot]com
Compares arithmetic division with bitwise and without...
test CPU cycles...
$ gcc -o code code.c; ./code
*/
#include <stdio.h>
#include <stdlib.h>
#include <x86intrin.h>
#include <cpuid.h>
#include <inttypes.h>
#define LAST "\033[0m"
#define RED "\033[22;31m"
void mul()
{
int x=50,y=0;
while(x)
{
y=x*2;
x--;
}
}
void leftshift()
{
register int x=50,y=0;
while(x)
{
y=x<<1 bit_div7="" div7="" n="" num="" register="" return="" unsigned="" x--="" x="(num" y="">>1)+(num>>4);
x+=x>>6;
x+=(x>>12)+(x>>24);
x>>=2;
y=num-((x<<3 return="" x="" y="">>3);
}
unsigned div3(unsigned n)
{
return n/3;
}
//
unsigned bit_div3(unsigned num)
{
register unsigned x,y;
x=(num>>2)+(num>>4);
x+=x>>4;
x+=x>>8;
x+=x>>16;
y=num-((x<<2 ficou="" return="" ruim="" x="" y="">>5);
// ruim return x+( (y+5+(y<<2>>4);
return x+( (((y+1)<<2 y="">>4);
}
int main(void)
{
int x=0;
uint32_t a=0, b=0, c=0, d=0;
register uint64_t y=0;
x = 0;
do {
__cpuid(0, a, b, c, d);
y = _rdtsc();
leftshift();
y = _rdtsc() - y;
} while (++x < 2);
printf("::: left shift: %s %lld %s cicles\n",RED, y,LAST);
x = 0;
do {
__cpuid(0, a, b, c, d);
y = _rdtsc();
mul();
y = _rdtsc() - y;
} while (++x < 2);
printf("::: mul: %s %lld %s cicles\n", RED,y,LAST);
unsigned int z=0;
x = 0;
do {
__cpuid(0, a, b, c, d);
y = _rdtsc();
z=div7(560000*x);
printf("result: %d\n",z);
y = _rdtsc() - y;
} while (++x < 2);
printf("::: div7: %s %lld %s cicles\n", RED,y,LAST);
z=0;
x = 0;
do {
__cpuid(0, a, b, c, d);
y = _rdtsc();
z=bit_div7(560000*x);
printf("result: %d\n",z);
y = _rdtsc() - y;
} while (++x < 2);
printf("::: bit_div7: %s %lld %s cicles\n",RED, y,LAST);
x = 0;
do {
__cpuid(0, a, b, c, d);
y = _rdtsc();
z=div3(560000*x);
printf("result: %d\n",z);
y = _rdtsc() - y;
} while (++x < 2);
printf("::: div3: %s %lld %s cicles\n",RED, y,LAST);
z=0;
x = 0;
do {
__cpuid(0, a, b, c, d);
y = _rdtsc();
z=bit_div3(560000*x);
printf("result: %d\n",z);
y = _rdtsc() - y;
} while (++x < 2);
printf("::: bit_div3: %s %lld %s cicles\n", RED,y,LAST);
exit(0);
}
</2></2></2></3></1></inttypes></cpuid></x86intrin></stdlib></stdio></code></pre>
</div>
</div><div></div><div></div><div><br /><div>This subject is huge. I'll stop here for the end</div><div>I suggest you read it to delve deeper into the subject.</div><div>by bitwise, the following book “hacker’s delight.”</div></div><div><br /><iframe allowfullscreen="" frameborder="0" height="550" sandbox="allow-scripts allow-same-origin allow-popups" src="https://ler.amazon.com.br/kp/card?asin=B009GMUMTM&preview=inline&linkCode=kpe&ref_=cm_sw_r_kb_dp_5WGN4M3X91X0NCHWXM1H" style="max-width: 100%;" type="text/html" width="336"></iframe></div><div><br /></div>
CoolerVoidhttp://www.blogger.com/profile/04320162188823710256noreply@blogger.com0tag:blogger.com,1999:blog-5836544198173410260.post-6811895318335412702018-11-15T13:16:00.001-08:002021-03-09T18:59:53.369-08:00mtg to relieve my cortisolFollow Wikipedia:<br />https://en.wikipedia.org/wiki/Magic:_The_Gathering<br /><br /> <br />There's a lot of strategic depth to Magic or MTG. There is usually a pretty good variety of top-tier decks you can play. From my perspective, you can generally find a tier that fits your playstyle or color preference and is still competitive.<br /><br />Do you believe that games can relieve stress levels?<br />So, I choice play mtg to do that. <br /><br />The cool thing is, if you stop playing for a year or four, it's not like your deck becomes obsolete. If you were playing standard, you could switch to modern legacy, and that's that.<br /><br />Here "brazil" have devir call events and challenges https://magic.wizards.com/pt-br/events/event-types <br /><br />If you have some curiosity to view my strategy's look that following:<br />https://tappedout.net/users/Cooler_/mtg-decks/ <br /><br />Open Source simulators:<br />https://www.slightlymagic.net/wiki/Forge<br />http://www.blueisme.com/portfolio/cardforge/ <br /><br /><br />CoolerVoidhttp://www.blogger.com/profile/04320162188823710256noreply@blogger.com0tag:blogger.com,1999:blog-5836544198173410260.post-54841565845423119112017-12-23T17:10:00.007-08:002022-06-18T16:41:32.107-07:00Firefox tunnelA crucial element of the Red Team's task is stealth in the attack. Success is its ability to espouse an aggressive mindset. If the red team wins, an absolute cracker's point of view can help construct a better defence for the Blue Team in the future. Don't bear this content. All content here has a practical purpose.<br /><br />This blog post is about a different attack approach to remote control the machine and bypasses the firewall. We have many weapons to work in that perspective, something like veil framework, msfvenom. But sometimes, following a different path will generally bring good results.<br /><br /> The attack aims to use firefox to communicate between client and server. Using hooks to do that is not impossible, but DLL injection sometimes is boring to implement and can be harder to turn in portable. Do you know that? x32 and x64, each architecture needs a different approach to develop(in the future, I discovered that the easy hook API could solve that). Another day I was studying the firefox internals, reading something about the use of SQLite to work with cookies that gave me a different focus.<br />
Look at the following:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCmdV5pEO0f8KBICZtvLzc0c8JlLoM8-sUq82Cecyj2GILE-TL6E9oBQH45TZf9W60UXtpYEcliM6ns-OVz7WCh13VG48UB0Mvl779hKwIZ-JNwvdjjQAfEVsacHcX08TQPoCYXuK4xAs/s1600/tunnel4.png" style="clear: left; display: inline; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="520" data-original-width="740" height="345" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCmdV5pEO0f8KBICZtvLzc0c8JlLoM8-sUq82Cecyj2GILE-TL6E9oBQH45TZf9W60UXtpYEcliM6ns-OVz7WCh13VG48UB0Mvl779hKwIZ-JNwvdjjQAfEVsacHcX08TQPoCYXuK4xAs/w572-h345/tunnel4.png" width="572" /></a></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div>That is a complete plan to create a program to use firefox with the tunnel. I going to explain in each step:<br /><br />1- Programm of tunnel call firefox browser in hidden mode, sending a URL, URL has a malicious server, that malicious server sends a cookie with command.<br /><br />2- The tunnel gets a cookie of evil server in the cookie.SQLite uses that in the command shell.<br /><br />3- Result of command shell is used to write an HTML with javascript to make auto-submit with the content result.<br /><br />4- Programm open wrote HTML in hidden mode to send CMD to the malicious server.<br /><br /><br /> Now you can look at the following:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCcU6jE90CuSFywXzwdP_Zb_wkKG2P00zcHMy42-6Fz0isbcshNLg6DV9IVh7y1WoAy2LXJ98cGY8SW7UK5WSCgkS306sM8E-NFbnMBV7VOI5hpgeJWMPIXo4-DeV6oeEKthOLZPS2mi8/s1600/tunnel8.png" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="533" data-original-width="746" height="351" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCcU6jE90CuSFywXzwdP_Zb_wkKG2P00zcHMy42-6Fz0isbcshNLg6DV9IVh7y1WoAy2LXJ98cGY8SW7UK5WSCgkS306sM8E-NFbnMBV7VOI5hpgeJWMPIXo4-DeV6oeEKthOLZPS2mi8/w569-h351/tunnel8.png" width="569" /></a></div>
<br /><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">For a satisfactory conclusion, I wrote the code and recorded the proof of concept. The incredible fact of the empirical point turns all into reality. You can view all staff at the following:</span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><a class="editor-rtfLink" href="https://www.blogger.com/u/0/#" style="background: none 0% 0% repeat scroll transparent; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;">https://github.com/convisoappsec/firefox_tunnel</span></a></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><b style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">Future insights:</span></b></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">* </span><a class="editor-rtfLink" href="https://www.blogger.com/u/0/#" style="background: none 0% 0% repeat scroll transparent; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;">Insert persistence,</span></a><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"> using function RegOpenKeyEx() to open path: </span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">"Software\Microsoft\Windows\CurrentVersion\Run" </span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">write with function RegSetValueEx() to launches a program automatically at system startup.</span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"> *Using images in I/O using steganography.</span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"> *Running process in hidden mode.</span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"> *</span><a class="editor-rtfLink" href="https://www.blogger.com/u/0/#" style="background: none 0% 0% repeat scroll transparent; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;">Turn to tunnel unkillable process.</span></a></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><b style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">Possible mitigations:</span></b></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">* Global hooking, to get OpenFile(), CreateFIle() functions and filter argv "cookie.sqlite" and block when programm route is different of firefox.exe.</span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">* File watch API to monitor the database of cookies.</span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">* Programm to open database of cookies by periodicity and search wrong domain or hosts, that can use a blocklist to find and uses DELETE query to remove the evil cookie.</span></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><b style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">Thank you for reading this. Cheers! <br /></span></b></p>CoolerVoidhttp://www.blogger.com/profile/04320162188823710256noreply@blogger.com1tag:blogger.com,1999:blog-5836544198173410260.post-73704921975358177812016-12-31T18:54:00.007-08:002022-06-10T23:19:05.049-07:00Killing dragons spawned by arithmetic-related security pitfalls<p style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;">In the last week, which followed my attempt to earn money with financial trading, I glanced through </span><a class="editor-rtfLink" href="https://www.blogger.com/#" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;">the Black & Scholes</span></a><span data-preserver-spaces="true" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;"> model.</span></p><p style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;"><span><br /></span></p><p style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;"><span><span data-preserver-spaces="true" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;">This study resulted in the creation of </span><a class="editor-rtfLink" href="https://www.blogger.com/#" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;">OptionsCat</span></a><span data-preserver-spaces="true" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;">, an open-source tool to work with European options. I faced many Arithmetic-related security pitfalls when writing this tool, which motivated me to study it and write a blog post. </span></span></p><p style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;"> </span></p><p style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;"> I always develop my implementations for the algorithms presented throughout the finance books. That's because the writers are often careless about security pitfalls. From this article's perspective, this is a problem or dragon that can be solved by adding a chapter about validation.</span></p><p style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;"><span><br /></span></p><p style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;">Programming languages that enable direct memory access and do not provide buffer boundary checks and arithmetic numeric checks are particularly vulnerable to integer overflow attacks. An integer overflow may occur when computing the memory size to allocate a buffer, often leading to a buffer overflow.</span></p><p style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;"> </span></p><p style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;"> Look at the following quote: </span></p><p style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;"><span><br /></span></p><p style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;"> "Integer overflows cannot be detected after they have happened, so there is no way for an application to tell if a result it has calculated previously is correct. This action can get dangerous if the calculation has to do with a buffer's size or how far into an array to index. Of course, most integer overflows are not exploitable because memory is not being directly overwritten, but sometimes they can lead to other bug classes, frequently buffer overflows. As well as this, integer overflows can be difficult to spot, so even well-audited code can spring surprises." </span></p><p style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;"><span><span data-preserver-spaces="true" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;"> by blexim </span><a class="editor-rtfLink" href="https://www.blogger.com/#" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;">- Phrack Volume 0x0b</span></a><span data-preserver-spaces="true" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;">, Issue 0x3c, Phile #0x0a of 0x10</span></span></p><p style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;"><span><br /></span></p><p style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;"><span><span data-preserver-spaces="true" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;">Some people talk to me about the use of the Big integer library. Like </span><a class="editor-rtfLink" href="https://www.blogger.com/#" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;">LibGMP</span></a><span data-preserver-spaces="true" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;"> to solve it, but when you work with big int need limit that numbers, arithmetic operations with bigint when a user has input with considerable length can cause Denial of service. The use of Integers is not hard to find in the stock markets. But double is then expected and can bring you a problem if you don't control the length, for example:</span></span></p><pre class="brush:c" name="code"><span>#include < math.h>
#include < stdio.h>
double mul_code(double x,double y)
{
double result=0;
return result = x*y;
}
int main()
{
double a=90000000000, b=20000000000000;
printf("Result: %f\n", mul_code(a,b));
return 0;
}
</span></pre>
<span><br /></span><p style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;">If you compile it and run it, it returns something like "1799999999999999916112.*(dirts...)". You ask me, "why to return it ?" you don't validate the operation and pass the carrying limit. This action can cause undefined behaviour and overflow.</span></p><p style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"><span><br /></span></p><p style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"><b style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;">Killing dragons in integers </span></b></p><p style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"><span><br /></span></p><p style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"><span><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;">There are lots of ways for you to solve. One is validating user input. This way, you can use automatons, regular expressions, </span><a class="editor-rtfLink" href="https://www.blogger.com/u/0/#" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;">and strnlen()</span></a><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"> to limit the number of lengths. Remember phrack; the correct way to test for integer overflow during multiplication is to try before the multiplication, test if the number is negative, and replace functions like atoi() to </span><a class="editor-rtfLink" href="https://www.blogger.com/u/0/#" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;">strtol()</span></a><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;">.</span></span></p><p style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"> </span></p><p style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"> </span></p><p style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"> Some operating systems have solutions at libraries to mitigate the problem. For example, OSX has </span><b style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;">os/overflow.h. With</span></b><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"> this header, you can do something like it:</span></p>
<pre class="brush:c" name="code"><span>#include < "os/overflow.h">
if (os_mul_overflow(m, n, &bytes)) {
/* Overflow occured. Handle appropriately. */
} else {
/* Allocate "bytes" space. */
</span></pre>
<span><br />
Another way to mitigate this way is from <a href="https://www.openbsd.org/" target="_blank">OpenBSD</a>:<br />
<br />
</span><pre class="brush:c" name="code"><span>#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t)*4))
// based in OpenBSD reallocarray() function http://man.openbsd.org/reallocarray.3
void *reallocarray (void *ptr, size_t nmemb, size_t size)
{
if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && nmemb > 0 && SIZE_MAX / nmemb < size)
{
DEBUG("integer overflow block");
return NULL;
}
void *p = realloc (ptr, nmemb*size);
if (p == NULL)
return NULL;
return p;
}
</span></pre>
<span><br /></span><p style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"><span><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;">Other approaches that you can see is the using libraries and different ways to write safe code with integers, sometimes calling each function </span><b style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;">safe_add()</span></b><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;">, </span><b style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;">safe_sub(), safe_mul(), and safe_div()</span></b><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"> is very dull when having significant expressions, and thinking about it I wrote a solution, look my project </span><a class="editor-rtfLink" href="https://www.blogger.com/u/0/#" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;">Here!</span></a></span></p><p style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"><span><br /></span></p><p style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"><b style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;">Killing dragons in double</span></b></p><p style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"><span><br /></span></p><p style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"><span><br /></span></p><p style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"><span><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"> The </span><a class="editor-rtfLink" href="https://www.blogger.com/u/0/#" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;">Cert C book by Robert Seacord</span></a><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"> has an example of solving the problem at the double, the derivatives and futures have a lot of operations with double, one way to detect possible bug is using the function </span><a class="editor-rtfLink" href="https://www.blogger.com/u/0/#" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;">fetestexcept()</span></a><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"> :</span></span></p><p style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"><span><br /></span></p><span><br />
</span><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUkWUlq1PckmdA_utZ9TsUzx0m4bZ47x0d0llVrm8lIeD3Hvp3cyoxl9GFFLUEjFTdWgbemR-2OTujrAosV-O53JUU_ZX4SQmdIlancmyW8VKYRlv64ERgax2pK1Wqdi7PENZWsQ7ZQyY/s1600/Screenshot+from+2017-08-10+15%253A14%253A06.png" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><span style="color: black;"><img border="0" data-original-height="333" data-original-width="768" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUkWUlq1PckmdA_utZ9TsUzx0m4bZ47x0d0llVrm8lIeD3Hvp3cyoxl9GFFLUEjFTdWgbemR-2OTujrAosV-O53JUU_ZX4SQmdIlancmyW8VKYRlv64ERgax2pK1Wqdi7PENZWsQ7ZQyY/s1600/Screenshot+from+2017-08-10+15%253A14%253A06.png" /></span></a></div>
<span><br />
<br />
</span><div style="text-align: left;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"><br /></span></div><div style="text-align: left;"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"><br /></span></div><div style="text-align: left;"><span><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;">At inputs, don't use the atof() function. Replace to </span><a class="editor-rtfLink" href="https://www.blogger.com/u/0/#" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;">strtod(), and</span></a><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;"> double look the following code </span><a class="editor-rtfLink" href="https://www.blogger.com/u/0/#" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: none 0% 0% repeat scroll transparent; margin-bottom: 0pt; margin-top: 0pt;">here.</span></a><br />
<br /></span></div>
<div style="text-align: center;">
<span><br /></span></div>
<div style="text-align: center;"><span>
Thank you for reading.<b><br /></b>
<b>Cheers! </b></span></div>
CoolerVoidhttp://www.blogger.com/profile/04320162188823710256noreply@blogger.com1tag:blogger.com,1999:blog-5836544198173410260.post-76433985524848971362016-08-08T18:13:00.004-07:002021-12-04T20:34:19.406-08:00Steps to create your WAF(web application firewall) in C<div style="text-align: justify;"><span data-preserver-spaces="true" style="background: transparent none repeat scroll 0% 0%; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">Following definition (like </span><a class="editor-rtfLink" href="https://www.blogger.com/u/0/#" style="background: transparent none repeat scroll 0% 0%; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: transparent none repeat scroll 0% 0%; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;">OWASP</span></a><span data-preserver-spaces="true" style="background: transparent none repeat scroll 0% 0%; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">), a WAF is a piece of software intended to protect a web app on the application level. Nowadays, a WAF action is not only defined by the web app. It’s not a customized solution specific to that application but similarly to a general software firewall, where one contains parameters to protect against intrusion in a wide variety of frameworks and codes.</span></div>
<div style="text-align: justify;">
<br /></div>
<table cellpadding="0" cellspacing="0" class="tr-caption-container" style="float: left; margin-right: 1em; text-align: left;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNiP2u_cIjjpVQvZj5sEasvulNoKaSVKHOuycimF0MrbI9xzF_sujHEIdEm7X9zl5qIR6st1pk72b05Fs0rISpM_oxTpny4ITyvCCdEqU6eW9IfwlDjoZdbV-c82hKC1nZKQ3QdktWuEM/s1600/Wall-of-Fire1.jpg" style="clear: left; margin-bottom: 1em; margin-left: auto; margin-right: auto;"><img border="0" height="295" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNiP2u_cIjjpVQvZj5sEasvulNoKaSVKHOuycimF0MrbI9xzF_sujHEIdEm7X9zl5qIR6st1pk72b05Fs0rISpM_oxTpny4ITyvCCdEqU6eW9IfwlDjoZdbV-c82hKC1nZKQ3QdktWuEM/s400/Wall-of-Fire1.jpg" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Firewall burning invasors hehehe !</td></tr>
</tbody></table><br /><p style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">Let's go to clear your mind. There is overlap between the different types of firewalls. Software and hardware firewalls are used in their own right to protect networks. However, with their specialized function for web applications, WAFs can take the form of input of either of those two main types. Per default, a firewall uses a blocklist, protecting against an individual, previously logged attacks.</span></p><p style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">Additionally, it can also use an allowlist, providing allowable users and instances of interaction for the application. Another function is to block </span><a class="editor-rtfLink" href="https://www.blogger.com/#" style="background: transparent; color: #0e101a; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;">SQL Injection </span></a><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">attacks and </span><a class="editor-rtfLink" href="https://www.blogger.com/#" style="background: transparent; color: #0e101a; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;">XSS attacks</span></a><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">... In another context, WAFs can create random tokens and put them in forms to stop web robots and automated attacks. This practice can try to mitigate </span><a class="editor-rtfLink" href="https://www.blogger.com/#" style="background: transparent; color: #0e101a; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;">CSRF</span></a><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"> pitfalls.</span></p><p style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">Before you ask, "</span><strong style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">how-to, I create my WAF ?</span></strong><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">" I have got to bring you some principles, anyway, the theory around facts.</span></p><p style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><strong style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">Have two common WAFs:</span></strong></p><p style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"> </span><strong style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">1-</span></strong><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">Uses plugin in HTTPd to get information of INPUT or OUTPUT, before the finish he receives the request and blocks some contents, this function focuses at HTTP METHODs POST, GET... </span></p><p style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><strong style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">2-</span></strong><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">this way is my favourite. It is an independent reverse proxy server. He brings all requests of the client to the proxy. The proxy makes some analysis in the content. If not, block, he sends all the information to the external server.</span></p><p style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">Number One is cold, and this path is not fully portable. Another bad thing is you need to create a different plugin for each HTTPd, something to apache another to NGINX, IIs, Lighttpd... it's not cool!</span><strong style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"> If you are not an excellent low-level programmer. </span></strong><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">You can try using </span><a class="editor-rtfLink" href="https://www.blogger.com/#" style="background: transparent; color: #0e101a; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;">twisted </span></a><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">python. It is easy to make a reverse proxy with it, but it is not the right way because not have good performance in production. If you piss off at it, </span><a class="editor-rtfLink" href="https://www.blogger.com/#" style="background: transparent; color: #0e101a; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;">study the Stevens book of sockets.</span></a></p><p style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">It is OK, the title of this post is "create waf in C", Task is entirely done here and commented and with some documentations in </span><a class="editor-rtfLink" href="https://www.blogger.com/#" style="background: transparent; color: #0e101a; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;">LaTex...</span></a><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"> relax, you can get it in this repository: </span><a class="editor-rtfLink" href="https://www.blogger.com/#" style="background: transparent; color: #0e101a; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;">https://github.com/CoolerVoid/raptor_waf</span></a></p><div style="text-align: justify;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEireDnjoSY0-pYhDJ7feVXmwZOgr8mcMts9wFodKDAp_dGhkipQZs8NN-01v2ArrdQlPAtpEX4G11IwsFnmUwb6cGEnYjEXRFg8PRGFn3_9y3qkJhkURZnq3g3FiCHfOHlaRqPIzcfHXeM/s1600/raptor2.png" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEireDnjoSY0-pYhDJ7feVXmwZOgr8mcMts9wFodKDAp_dGhkipQZs8NN-01v2ArrdQlPAtpEX4G11IwsFnmUwb6cGEnYjEXRFg8PRGFn3_9y3qkJhkURZnq3g3FiCHfOHlaRqPIzcfHXeM/s1600/raptor2.png" /></a> </div><p style="background: transparent none repeat scroll 0% 0%; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><b style="background: transparent none repeat scroll 0% 0%; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: transparent none repeat scroll 0% 0%; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">Raptor WAF</span></b><span data-preserver-spaces="true" style="background: transparent none repeat scroll 0% 0%; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"> is a simple web application firewall made in C, </span><a class="editor-rtfLink" href="https://www.blogger.com/u/0/#" style="background: transparent none repeat scroll 0% 0%; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: transparent none repeat scroll 0% 0%; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;">using KISS principle</span></a><span data-preserver-spaces="true" style="background: transparent none repeat scroll 0% 0%; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">, to make poll use the select() function, is not better than epoll() or kqueue() from *BSD but is portable, the core of match engine using DFA to detect XSS, SQLi and path traversal, you can see here </span><a class="editor-rtfLink" href="https://www.blogger.com/u/0/#" style="background: transparent none repeat scroll 0% 0%; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: transparent none repeat scroll 0% 0%; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;">https://github.com/CoolerVoid/raptor_waf/tree/master/doc/test_dfa</span></a></p><p style="background: transparent none repeat scroll 0% 0%; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: transparent none repeat scroll 0% 0%; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: transparent none repeat scroll 0% 0%; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"> No more words, look at the following :</span></p><div style="text-align: justify;">
<br /></div>
<iframe allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/VtL0apDv1N4" width="560"></iframe>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<b><br /></b></div>
<div style="text-align: center;">
<b>Thank you for reading this! </b><br />
<b>Cheeers! </b></div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
</div>
CoolerVoidhttp://www.blogger.com/profile/04320162188823710256noreply@blogger.com1tag:blogger.com,1999:blog-5836544198173410260.post-70046331416186571572016-08-01T13:27:00.008-07:002022-06-08T20:25:12.046-07:00Talking about text classifiers<p style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">In the last year following search, I searched something about machine learning, like trying to detect SPAMs at my private projects. I saw something about KNN, random decision forests and naive Bayes.</span></p><p style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">Consequently, I wrote</span><a class="editor-rtfLink" href="https://github.com/CoolerVoid/libtext_bayes" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: transparent; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;"> a </span><span data-preserver-spaces="true" style="background: transparent; color: red; margin-bottom: 0pt; margin-top: 0pt;">C++ library to classify texts</span></a><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"> and some slides for a presentation, which you can view at the end of this blog post. <br /></span></p><div style="text-align: justify;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfwdpSO9fd4AnTvq4QQFMiqM0BuvGwvw_zuVosbMZjbq7PqhVTVp4CDiV2xmiw_airRqYeDE_VTGPLqvMN1_0Ba28z9u4kIfkj-txUBUn6fGuLha0tQslD5fKCOyJg_Tpny3MKmIFKNk8/s1600/dilberspam6.gif" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfwdpSO9fd4AnTvq4QQFMiqM0BuvGwvw_zuVosbMZjbq7PqhVTVp4CDiV2xmiw_airRqYeDE_VTGPLqvMN1_0Ba28z9u4kIfkj-txUBUn6fGuLha0tQslD5fKCOyJg_Tpny3MKmIFKNk8/s1600/dilberspam6.gif" /></a><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">So I chose </span><a class="editor-rtfLink" href="https://en.wikipedia.org/wiki/Naive_Bayes_classifier" style="background: transparent; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: transparent; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;"><b>Naive Bayes </b></span></a><span data-preserver-spaces="true" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;"><span style="color: #0e101a;">because Naive Bayes is one of the simplest classifiers, based on Bayes theorem with naïve and complete independence assumptions. It is one of the most basic text classification techniques with various email spam detection, document categorization, sexually explicit content detection, personal email sorting, language detection and sentiment detection</span><b><span style="color: red;">(</span></b></span><a class="editor-rtfLink" href="https://github.com/CoolerVoid/libtext_bayes" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: transparent; color: red; margin-bottom: 0pt; margin-top: 0pt;"><b>i think something like NLP</b></span></a><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">). Despite the naïve design and oversimplified assumptions that this technique uses, Naive Bayes performs well in many complex real-world problems. Another good thing, Naive Bayes is suitable for limited CPU and memory resources. <br /></span></div>
<center>
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_H9SYtwz6PnsvqOJEZRQIl_3Ye-5ssRdmrXaqDBLSn83oTWq3TKkEBDrD6orD7LV-ecBQG_IoSRYwayCrGM05sHdgLdzJNxD3Hb_tYVD8XC94rAuJ1yzGGY_foqC-j5BXe3IJBDlKMSs/s1600/Melhorando+a+detec%25C3%25A7%25C3%25A3o+de+SPAM+-+Conviso+2016-08-02+19-02-09.png" />
</center>
<div style="text-align: justify;">
<span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">To optimize detection accuracy, I use </span><a class="editor-rtfLink" href="https://github.com/CoolerVoid/email_audit/blob/master/doc/PoC/spam2.c" style="background: transparent; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: transparent; color: #cc0000; margin-bottom: 0pt; margin-top: 0pt;">DFA(deterministic finite automaton)</span><span data-preserver-spaces="true" style="background: transparent; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;"> </span></a><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">to match patterns and put each mark in ranking. That ranking has one classification. You can view the following code</span><a class="editor-rtfLink" href="https://github.com/CoolerVoid/email_audit/blob/master/doc/PoC/spam2.c" style="background: transparent; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;" target="_blank"><span data-preserver-spaces="true" style="background: transparent; color: #4a6ee0; margin-bottom: 0pt; margin-top: 0pt;"> here</span></a><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">. To make your automaton, you can use Flex, bison in another way.</span></div><p style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><br /></p><p style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;">If you view a presentation on slide number 12, you can see my point of view about ranking to optimize the accuracy of the classifier at results.</span></p><p style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"><span data-preserver-spaces="true" style="background: transparent; color: #0e101a; margin-bottom: 0pt; margin-top: 0pt;"> </span></p>
<center><iframe allowfullscreen="" frameborder="0" height="485" marginheight="0" marginwidth="0" scrolling="no" src="//www.slideshare.net/slideshow/embed_code/key/gT4C7VoLNL9aF1" style="border-width: 1px; border: 1px solid #CCC; margin-bottom: 5px; max-width: 100%;" width="595"> </iframe> <div style="margin-bottom: 5px;"> <strong> <a href="//www.slideshare.net/antoniocooler/improving-spam-detection-with-automaton" target="_blank" title="Improving spam detection with automaton">Improving spam detection with automaton</a> </strong> from <strong><a href="//www.slideshare.net/antoniocooler" target="_blank">Antonio Costa aka Cooler_</a></strong> </div><br /></center>
<div style="text-align: center;">
SO, This is a very cool trick to gain accuracy. No more words, friends. Thank you for reading this! </div>
<div style="text-align: center;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJkTPnbtmdmO2nVVjlEMNLl0SKg31DmzWEWZmM3zhwLMVjro5LmeLej11Fyv8-ajPOC5k4Ys78PbN3fcOffBF8D2yaIXLEGzK1cYLWxr-mPD7yOp4vy7ugx8JY715LVgoHn0fHDf0nkY/s1600/giphy.gif" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="298" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeJkTPnbtmdmO2nVVjlEMNLl0SKg31DmzWEWZmM3zhwLMVjro5LmeLej11Fyv8-ajPOC5k4Ys78PbN3fcOffBF8D2yaIXLEGzK1cYLWxr-mPD7yOp4vy7ugx8JY715LVgoHn0fHDf0nkY/s400/giphy.gif" width="400" /></a></div>
<br /></div>
<div style="text-align: center;">
<span style="font-size: x-large;"><b>Cheers!</b></span></div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
References:</div>
<ul>
<li><a href="https://class.coursera.org/nlp/lecture" target="_blank">Natural Language Processing by Dan Jurafsky, Christopher Manning</a></li>
<li>John, G. H. e Langley, P. (1995). Estimating continuous distributions in bayesian classifiers. Montreal, Quebec; Canada.</li>
<li>Svore, K. M., Wu, Q., e Burges, C. J. (2007). Improving web spam
classification using rank-time features. Banff, Alberta, Canada.</li>
</ul>
<div style="text-align: justify;">
<br /></div>
<br />CoolerVoidhttp://www.blogger.com/profile/04320162188823710256noreply@blogger.com0tag:blogger.com,1999:blog-5836544198173410260.post-47980212092803727702016-05-18T15:57:00.001-07:002016-05-22T10:56:14.031-07:00Uncommon trick to bypass windows firewall<div style="text-align: justify;">
<span style="font-family: inherit;"> When I was about to write post I remembered a scene from the movie Monty Python. It's about a black knight that blocks a bridge (his main purpose) saying the following to Arthur, a guy who insisted on crossing the bridge in safety: "<b>None shall pass!</b>". But Arthur kept on insisting, so the black night said: "Then you shall die.".</span><br />
<span style="font-family: inherit;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjm5UZd4SRoRCAw6doCmKrcdcKbDqbgFgbd9gG1Rwr8qsFHEL41O0YI1KubP4ZpbUT1zCd-IOEa1mOcDAt9ZiHDh8CJA0aXItTG-lcjOBmbtAkFoxhu8S4u0yGJ8RNV0HpZZK8BtTJIAy4/s1600/7670110.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjm5UZd4SRoRCAw6doCmKrcdcKbDqbgFgbd9gG1Rwr8qsFHEL41O0YI1KubP4ZpbUT1zCd-IOEa1mOcDAt9ZiHDh8CJA0aXItTG-lcjOBmbtAkFoxhu8S4u0yGJ8RNV0HpZZK8BtTJIAy4/s200/7670110.jpg" width="200" /></span></a></div>
<div style="text-align: justify;">
<span style="font-family: inherit;"><span style="font-family: inherit;">A firewall is supposed to stop all applications that don't have a good reason to get privileged access to computers or networks. If your app could "bypass" it, so could malicious applications. Firewall plays the same role as the black night, i.e. prevent anything suspicious from going through the bridge.</span></span><br />
<span style="font-family: inherit;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-family: inherit;">You can configure firewalls to let particular connections go through the checks, but sometimes you need physical access to the equipment running the firewall to do that. Don't you have access to the equipment running the firewall? If so, you need to bypass it.</span></div>
<div style="text-align: justify;">
<div>
<br /></div>
</div>
<div style="text-align: justify;">
<span style="font-family: inherit;">Are you <a href="https://github.com/CoolerVoid/X_files/blob/master/src/RegistryKey_manager.c#L4" target="_blank">willing to change rules</a> via <a href="https://en.wikipedia.org/wiki/Windows_Registry" target="_blank">RegEdit</a> to allow a specific service to run? I have some bad news to you. Your antivirus can detect this trick, because it's a common approach used by malwares.</span></div>
<ul>
<li><span style="color: #ffd966; font-family: inherit;">HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\StandardProfile</span></li>
<li><span style="color: #ffd966; font-family: inherit;">SYSTEM\ControlSet%03d\Services\SharedAccess\Parameters\FirewallPolicy\StandardProfile\AuthorizedApplications\List</span></li>
</ul>
<span style="font-family: inherit;"><span style="color: #ffd966;">
</span>
</span><br />
<div style="text-align: justify;">
<span style="font-family: inherit;">Before you say "<b>I can try to hook it</b>" or "I can execute a function like execl(), system() with the argument: </span></div>
<ul>
<li><span style="color: #ffd966; font-family: inherit;">netsh advfirewall set currentprofile state off</span></li>
</ul>
<div style="text-align: justify;">
<span style="font-family: inherit;">What if I tell you that you can use Windows API to <b>simulate a keystroke</b> so as to bypass firewall checks?</span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiB8TqQ8qSdNZ0UOMPW251xZU74HgzDD0SAslzAM9bqIMkcfZ-SM8yC-Fg11NDmOHdJSp1a1h4CB5RVCw8ZwBRaRa9TXT1lvc-BkNDX5W7RxIEdTKTFeEUkzxjMypzWixZTXxSJKRSLyoc/s1600/xwindows-firewall-prompt.png.pagespeed.gp%252Bjp%252Bjw%252Bpj%252Bjs%252Brj%252Brp%252Brw%252Bri%252Bcp%252Bmd.ic.Gfbu1mhpxC.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiB8TqQ8qSdNZ0UOMPW251xZU74HgzDD0SAslzAM9bqIMkcfZ-SM8yC-Fg11NDmOHdJSp1a1h4CB5RVCw8ZwBRaRa9TXT1lvc-BkNDX5W7RxIEdTKTFeEUkzxjMypzWixZTXxSJKRSLyoc/s1600/xwindows-firewall-prompt.png.pagespeed.gp%252Bjp%252Bjw%252Bpj%252Bjs%252Brj%252Brp%252Brw%252Bri%252Bcp%252Bmd.ic.Gfbu1mhpxC.png" /></span></a></div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<span style="font-family: inherit;">Windows has the function<i> </i><a href="https://msdn.microsoft.com/pt-br/library/windows/desktop/ms646310%28v=vs.85%29.aspx"><i>SendInput()</i> </a>to simulate a keystroke. This function accepts as argument an array of INPUT structures. The INPUT structures can be either a mouse or a keyboard event. The keyboard event structure has a member called wVk which can be any key on the keyboard.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><b><i>SendInput()</i></b> played an important role when writing the code for bypassing Windows firewall. How does it work? </span><br />
<span style="font-family: inherit;"></span><br />
<span style="font-family: inherit;">Firstly, it finds a window with title 'Windows Security Alert' using the function <i><a href="https://msdn.microsoft.com/pt-br/library/windows/desktop/ms633520%28v=vs.85%29.aspx" target="_blank">GetWindowText()</a>.</i> Secondly, it calls <a href="https://msdn.microsoft.com/pt-br/library/windows/desktop/ms646310%28v=vs.85%29.aspx"><i>SendInput()</i></a> with TAB and ENTER keys to choose button 'allow access'. As simple as that</span></div>
<div style="text-align: justify;">
<span style="font-family: inherit;"><br /></span></div>
<div style="text-align: justify;">
Take a look at the following video:</div>
<div>
<div style="text-align: center;">
<span style="font-family: inherit;"><br /></span></div>
</div>
<div style="text-align: center;">
<span style="font-family: inherit;"><iframe allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/iLPBvbwUJss" width="560"></iframe></span></div>
<div style="text-align: center;">
<span style="font-family: inherit;"><br /></span></div>
<div style="text-align: justify;">
<br />
Take a look at my code that bypasses Windows firewall:<br />
<a href="https://github.com/CoolerVoid/X_files/blob/master/docs/PoCs/bypass_firewall_windows.cpp">https://github.com/CoolerVoid/X_files/blob/master/docs/PoCs/bypass_firewall_windows.cpp</a> </div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
This is a very cool trick. No more words friends. Thank you for reading this!</div>
<div style="text-align: justify;">
<span style="font-family: inherit;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiO6yjs2Wcl-N7KWq-EKFzfrVD_QKzjhsnzX-YWQqeLqeZ2GNhT50YaCHpxNm6gesE_IJy_oNvtOaH8pPx8WVE9SuuLcRDojgk9jULocvLti3XQ9S1ko4vtFvDbEfyuiHvT9kLNaC5Lex0/s1600/X78HXm4.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="172" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiO6yjs2Wcl-N7KWq-EKFzfrVD_QKzjhsnzX-YWQqeLqeZ2GNhT50YaCHpxNm6gesE_IJy_oNvtOaH8pPx8WVE9SuuLcRDojgk9jULocvLti3XQ9S1ko4vtFvDbEfyuiHvT9kLNaC5Lex0/s320/X78HXm4.gif" width="320" /></span></a></div>
<div style="text-align: justify;">
<span style="font-family: inherit;"><br /></span></div>
<div style="text-align: justify;">
<div style="text-align: center;">
<span style="font-family: inherit;">cheers !</span></div>
</div>
<div style="text-align: justify;">
<br /></div>
CoolerVoidhttp://www.blogger.com/profile/04320162188823710256noreply@blogger.com4tag:blogger.com,1999:blog-5836544198173410260.post-86043291761326553972016-04-21T14:39:00.000-07:002017-08-14T05:54:33.560-07:00Hack any TV remote control <div style="text-align: justify;">
Do you have an old TV remote? What if I told you can create your code to make communication with this remote controller.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<b>You can do it!</b></div>
<div style="text-align: justify;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgop6JL_xsJuUU1hii9Wk7OYkjcFDkU5PFL4qj5MSgb7EYQDEs7FaqX-HrtfLmIjUhMPOI57tw4OmlLQ1loi4K_rFTOfxSCTPgK_qf7Ebh_MoTPrFQhawORdQvkmTcHRiSlqcTQ1LGzV0w/s1600/macgyver.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgop6JL_xsJuUU1hii9Wk7OYkjcFDkU5PFL4qj5MSgb7EYQDEs7FaqX-HrtfLmIjUhMPOI57tw4OmlLQ1loi4K_rFTOfxSCTPgK_qf7Ebh_MoTPrFQhawORdQvkmTcHRiSlqcTQ1LGzV0w/s1600/macgyver.png" /></a></div>
<div style="text-align: justify;">
<a href="https://en.wikipedia.org/wiki/Remote_control" target="_blank"><b>Infrared remote control</b></a> is cool, the idea of this blog post is turn it in presentation tool. It is easy with this remote to keep your finger on the advance button and simply advance slides — or turn the screen back, at your favorite PDF viewer. </div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Think like <a href="https://en.wikipedia.org/wiki/MacGyver" target="_blank">MacGyver</a>, to handle a difficult situation through improvisation using only available materials to do an intelligent activity...</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Its Ok, lets go to the hack...</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<b>First step you need get some things:</b></div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<ul>
<li>Computer with Unix Like OS(at my tests i using Fedora Linux) </li>
<li>Any TV remote control (i use a <a href="http://www.amazon.com/Samsung-Remote-Control-AA59-00469A-Batteries/dp/B009JVN73I" target="_blank">samsung model “AA59-00469A”</a>) </li>
<li><a href="https://www.arduino.cc/en/Main/ArduinoBoardNano" target="_blank">Arduino nano</a> 12,00 USD </li>
<li>IR recv (model “1838B”) - 2,00 USD </li>
<li>Jumpers 1,00 USD </li>
<li>Breadboard 2,50 USD</li>
</ul>
<div>
<br /></div>
<div>
Total of costs is 17.50 USD, looks good is not expensive, try following this image to assembly your hardware:</div>
<br />
<div class="separator" style="clear: both; text-align: left;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3EZd3ocp0PpBrlvl1zVtxepqMDbNg7y15IVQPvejr0VL_01zSuuydhT0myP8uR5QnNSI0f_NGH-hyYCR2S_JRpRxgJ9MgWttDx8y6_I9HAMi53-dmnsJKe2eA4N3pmf2tx3hyphenhyphensWIbObo/s1600/projetoremoto.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><br /></a><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3EZd3ocp0PpBrlvl1zVtxepqMDbNg7y15IVQPvejr0VL_01zSuuydhT0myP8uR5QnNSI0f_NGH-hyYCR2S_JRpRxgJ9MgWttDx8y6_I9HAMi53-dmnsJKe2eA4N3pmf2tx3hyphenhyphensWIbObo/s1600/projetoremoto.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><br /></a><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3EZd3ocp0PpBrlvl1zVtxepqMDbNg7y15IVQPvejr0VL_01zSuuydhT0myP8uR5QnNSI0f_NGH-hyYCR2S_JRpRxgJ9MgWttDx8y6_I9HAMi53-dmnsJKe2eA4N3pmf2tx3hyphenhyphensWIbObo/s1600/projetoremoto.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="257" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3EZd3ocp0PpBrlvl1zVtxepqMDbNg7y15IVQPvejr0VL_01zSuuydhT0myP8uR5QnNSI0f_NGH-hyYCR2S_JRpRxgJ9MgWttDx8y6_I9HAMi53-dmnsJKe2eA4N3pmf2tx3hyphenhyphensWIbObo/s400/projetoremoto.png" width="400" /></a></div>
<div>
<div style="text-align: left;">
<br /></div>
<ul>
<li style="text-align: left;">Green wire is GND </li>
<li style="text-align: left;">Orange wire is 5v </li>
<li style="text-align: left;">Yellow wire is pin 6(this is input to make communication with arduino) </li>
<li style="text-align: left;">USB connected at arduino(usually at mini series uses <a href="http://www.ftdichip.com/" target="_blank">FTDI </a>input)</li>
</ul>
</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Second step mapping your TV remote control:</div>
<div style="text-align: left;">
<br /></div>
At this step we are get some libraries, look this following:<br />
<br />
<ul>
<li><span style="background-color: black; color: #93c47d; font-family: inherit; line-height: 20px;">$ git clone https://github.com/shirriff/Arduino-IRremote </span></li>
</ul>
<ul>
<li><span style="background-color: black; color: #93c47d; font-family: inherit; line-height: 20px;">$ mv Arduino-IRremote ArduinoRemote; sudo cp -rf ArduinoRemote/ /usr/share/arduino/libraries </span></li>
</ul>
<br />
At your arduino IDE tool, you can view examples of use it at tab "File", load example that show the input of serial, look this following:<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<span style="font-family: inherit;"><br /></span>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6bUH5SJ5IKoyvBGYtXY_WyBtkEJRvkagPUxMrnS_VhTey4pAVJScpSgcs4aHn2oBeRQMzINGQGbczzLW8JLlfrv_DWbtydeSCXHdE5TzHgksiZue7p751rJFyoGmT9zUOe5tZRwqCEmc/s1600/mapping.png" style="margin-left: auto; margin-right: auto;" /></td></tr>
<tr><td class="tr-caption" style="text-align: center;"><br /></td><td class="tr-caption" style="text-align: center;"><span style="font-family: inherit;"><br /></span></td></tr>
</tbody></table>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6bUH5SJ5IKoyvBGYtXY_WyBtkEJRvkagPUxMrnS_VhTey4pAVJScpSgcs4aHn2oBeRQMzINGQGbczzLW8JLlfrv_DWbtydeSCXHdE5TzHgksiZue7p751rJFyoGmT9zUOe5tZRwqCEmc/s1600/mapping.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><span style="font-family: inherit;"></span></a><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6bUH5SJ5IKoyvBGYtXY_WyBtkEJRvkagPUxMrnS_VhTey4pAVJScpSgcs4aHn2oBeRQMzINGQGbczzLW8JLlfrv_DWbtydeSCXHdE5TzHgksiZue7p751rJFyoGmT9zUOe5tZRwqCEmc/s1600/mapping.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><span style="font-family: inherit;"></span></a></div>
<div style="text-align: justify;">
<span style="line-height: 20px;">The function Serial.println() shows the output of serial input. for example: when you hold the button one of TV remote control, <a href="https://github.com/CoolerVoid/arduino_ppt_walk/blob/master/IR_remote.c#L152" target="_blank">this action shows the hexadecimal value "E13DDA28".</a></span></div>
<div style="text-align: justify;">
<span style="line-height: 20px;"><br /></span>
<span style="line-height: 20px;">Done the mapping process of buttons, the next step is use <a href="http://man7.org/linux/man-pages/man2/syscall.2.html" target="_blank">syscall</a> open() to open the file "/dev/ttyUSB0" and use the syscall read() to get INPUTs of arduino device, remember to put diferent condition at each button input of device.</span><br />
<div>
<br /></div>
<span style="font-family: inherit; line-height: 20px;">To automate keyboard hold keys, at Unix like system, you need use some libraries like libXtst and libX11, to emulate keys to turn screens of presentation, to install this libraries look the following:</span></div>
<span style="color: #6aa84f; font-family: inherit; line-height: 20px;"><b style="background-color: black;">$ yum install libXtst-devel libX11-devel</b></span><span style="font-family: inherit;"><span style="font-family: "open sans" , "helvetca" , "arial" , sans-serif;"><span style="line-height: 20px;"><span style="background-color: white;"><b></b></span></span></span></span><br />
<span style="color: #cccccc;"><span style="background-color: black;"><span style="font-family: inherit;"><span style="font-family: "open sans" , "helvetca" , "arial" , sans-serif;"><span style="line-height: 20px;"><b>at deb based distros uses apt-get install pkg_name-dev</b></span></span></span></span></span><br />
<span style="font-family: inherit;"><span style="font-family: "open sans" , "helvetca" , "arial" , sans-serif;"><span style="line-height: 20px;"><span style="background-color: white;"><b><br /></b></span></span></span><span style="font-family: "open sans" , "helvetca" , "arial" , sans-serif;"><span style="line-height: 20px;">To get final code, <a href="https://github.com/CoolerVoid/arduino_ppt_walk/blob/master/IR_remote.c#L152" target="_blank"><span style="font-family: "open sans" , "helvetca" , "arial" , sans-serif;">rewrite this lines 152 and 159 with address of your button mapping,</span></a> compile it and run:</span></span></span><br />
<span style="color: #93c47d; font-family: inherit; line-height: 20px;"><b style="background-color: black;">$ git clone https://github.com/CoolerVoid/arduino_ppt_walk </b></span><br />
<span style="color: #93c47d; font-family: inherit; line-height: 20px;"><b style="background-color: black;">$ gcc IR_remote.c -o IR_remote -lX11 -lXtst -Wall</b></span><br />
<span style="color: #93c47d; font-family: inherit; line-height: 20px;"><b style="background-color: black;">$ ./IR_remote /dev/ttyUSB0</b></span><span style="font-family: inherit;"><span style="background-color: white; line-height: 20px;"><b></b></span></span><br />
<span style="font-family: inherit;"><span style="line-height: 20px;">Look this following:</span></span><br />
<span style="font-family: inherit;"><span style="line-height: 20px;"><br /></span></span>
<span style="font-family: inherit;"><span style="line-height: 20px;"><a href="https://www.youtube.com/watch?v=Wx64BfLgxQU">https://www.youtube.com/watch?v=Wx64BfLgxQU</a></span></span></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<span style="font-family: inherit;"><iframe allowfullscreen="" class="YOUTUBE-iframe-video" data-thumbnail-src="https://i.ytimg.com/vi/Wx64BfLgxQU/0.jpg" frameborder="0" height="266" src="https://www.youtube.com/embed/Wx64BfLgxQU?feature=player_embedded" width="320"></iframe></span></div>
CoolerVoidhttp://www.blogger.com/profile/04320162188823710256noreply@blogger.com0tag:blogger.com,1999:blog-5836544198173410260.post-25412598575532223752016-04-16T17:40:00.002-07:002016-04-19T22:19:14.380-07:00Solving the fizzbuzz problem in Assembly code<div style="text-align: justify;">
Hello ladies and gentlemen, also the loyal readers of my blog. In this post, we will not be working with optimization techniques. Instead, I will share a small challenge that I worked on. Working on a small challenge can be a good way of relieving stress at the end of the day.</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgt3mCIkIlM9ggCeH5vtRrtux5mzb2SiGlNUFy8R-hDnKwkERDi8X45gkNOq2xlmXt3UN6TjUB2ZZMRXKWGeHukNEq5XtilS5s8QkYtqWEoYxwNO4D9K170hyphenhyphenJxfIrEJ_aXlk8BTei916Q/s1600/alice-and-wonderland-cat-crazy-creepy-Favim.com-1807734.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="273" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgt3mCIkIlM9ggCeH5vtRrtux5mzb2SiGlNUFy8R-hDnKwkERDi8X45gkNOq2xlmXt3UN6TjUB2ZZMRXKWGeHukNEq5XtilS5s8QkYtqWEoYxwNO4D9K170hyphenhyphenJxfIrEJ_aXlk8BTei916Q/s320/alice-and-wonderland-cat-crazy-creepy-Favim.com-1807734.jpg" width="320" /></a></div>
<div style="text-align: justify;">
<div style="text-align: justify;">
For those who don't know, the problem is about replacing a number that is multiple of 3 or 5 by Fizz or Buzz,respectively. Numbers that are multiple of both 3 and 5 should be replaced by "FizzBuzz".</div>
<br />
<div style="text-align: justify;">
More information about the problem can be found <a href="https://en.wikipedia.org/wiki/Fizz_buzz" target="_blank"> here.</a></div>
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<div style="text-align: justify;">
<div style="text-align: justify;">
This problem can be easily solved using a high-level programming language, but if you use Assembly language instead, it's harder.</div>
</div>
<br />
That's basically what I am going to share with you. I solved fizzbuzz using x86-64 Assembly with Intel syntax.<br />
<br />
Let's take a look at the code:</div>
<br />
<pre class="brush:c" name="code">;; Author: CoolerVoid
;;
;; https://en.wikipedia.org/wiki/Fizz_buzz
;; for multiples of three print "Fizz" instead of the number, and for the multiples of five print "Buzz".
;; For numbers which are multiples of both three and five print "FizzBuzz".
;;
;; $ nasm -g -f elf64 fizz_buzz_game.asm -o buzz.o
;; $ ld -m elf_x86_64 buzz.o -o gamebuzz; ./gamebuzz
section .data
tick db 'tick'
fizz db 'fizz'
buzz db 'buzz'
newline db 0xA
section .bss
three resb 1
five resb 1
zero resb 1
section .text
global _start
_start:
xor si, si
mov si, 0xFF
mov [three], byte 0x3
mov [five], byte 0x5
Loop:
push si
mov [zero], byte 1
sub [three], byte 1
mov bl, [three]
cmp [three], byte 0
jne Zero_Fizz
mov [three], byte 3
Zero_Fizz:
xor rax, rax
cmp bl, 0x0
jnz Not_Fizz
mov [zero], byte 0
mov rax, 4 ; syscall write()
mov rbx, 1
mov rcx, fizz
mov rdx, 4
int 0x80
Not_Fizz:
sub [five], byte 1
mov bl, [five]
cmp [five], byte 0
jne Zero_Buzz
mov [five], byte 5
Zero_Buzz:
xor rax, rax
cmp bl, 0x0
jnz Not_Buzz
mov [zero], byte 0
mov rax, 4 ; syscall write()
mov rbx, 1
mov rcx, buzz
mov rdx, 4
int 0x80
Not_Buzz:
xor rax, rax
mov al, [zero]
cmp al, 0x0
jz Not_Tick
mov rax, 4 ; syscall write()
mov rbx, 1
mov rcx, tick
mov rdx, 4
int 0x80
Not_Tick:
push 0x0
xor rax, rax
mov rax, 4 ; syscall write()
mov rbx, 1
mov rcx, newline
mov rdx, 1
int 0x80
pop rax
pop si
dec si
jnz Loop
mov rax, 1 ; syscall exit()
mov rbx, 0
int 0x80
</pre>
<br />
<div style="text-align: justify;">
If you have familiarity with Assembly, you will notice that the solution is simple. For those unfamiliar, Assembly is not hard, but it does require that the programmer pays attention to slight details.</div>
<br />
If you have doubt you can decrease value 0xFF at line 24, and try run again...its all right in do this...<br />
<br />
My fifty cents ! <b>CHEERS !</b><br />
<br />
<br />
<br />CoolerVoidhttp://www.blogger.com/profile/04320162188823710256noreply@blogger.com1tag:blogger.com,1999:blog-5836544198173410260.post-21447932559603890122016-01-08T11:14:00.002-08:002016-04-21T19:15:16.237-07:00Simple fast string comparison with SSE4.2<br />
Hello ladies and gentlemen, Royal readers of my blog !<br />
<br />
<br />
<div style="text-align: justify;">
No more jokes, so i wrote this post in english, consequently i need make some task at other languages(to study)… keep warning and prepare your eyes… (will be hard experience, my english is not very good)</div>
<a href="https://coolerlab.files.wordpress.com/2014/10/chimp_karate_kick.gif?w=645" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://coolerlab.files.wordpress.com/2014/10/chimp_karate_kick.gif?w=645" /></a><br />
<div style="text-align: justify;">
In last week following search algorithms, like a try to gain some performance at my private projects, i view some thing about “<a href="http://en.wikipedia.org/wiki/SSE4">SSE4.2</a>“. so when i view the possibility to use<a href="http://en.wikipedia.org/wiki/Streaming_SIMD_Extensions#Registers"> “xmm0″(a register of 128 bits</a>), thinking “oh my god ! i wanna use it ! this is awesome!”, some days studying it with my friend <a href="https://github.com/pl4nkt0n">João Victorino aka “Pl4kt0n”</a>, After studying the concepts around SSE4.2, I ended up writing a program.</div>
<br />
Relax brows ! don’t have karate trick here !<br />
<br />
<div style="text-align: justify;">
To explain, i make two functions, one with the simple function “strcmp()”, other with my implementation using SSE4.2 with Assembly ( i change AT&T to Intel syntax(“<a href="http://x86asm.net/articles/what-i-dislike-about-gas/">AT&T” is very boring</a> ), for the reason that i guess easy to follow examples of the manual<a href="https://software.intel.com/en-us/intel-isa-extensions">‘intel’s manual’</a>), other fact, i test my “strcmp()” function with “array of words”, to carry some results like “<b>CPU cycles</b>” to make the benchmark, so with it, we have some conditions to compare, just a cartesian choice to view and compare like a simple plot bar with “<a href="http://www.gnuplot.info/">gnuplot</a>“.</div>
<br />
You can view result here ! and <a href="https://github.com/CoolerVoid/cooler_sse42_strcmp/blob/master/plot_compare.gp">gnuplot cmd here</a>!<br />
<br />
<a href="https://lh6.googleusercontent.com/-E0Emg7FFoz0/VG1tdXrP1iI/AAAAAAAAItg/ilFLzMjIFC0/w699-h524-no/profit.png" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img height="300" src="https://lh6.googleusercontent.com/-E0Emg7FFoz0/VG1tdXrP1iI/AAAAAAAAItg/ilFLzMjIFC0/w699-h524-no/profit.png" width="400" /></a><br />
<br />
<b>Ok Cooler_ , what’s the trick ?</b><br />
<br />
<div style="text-align: justify;">
So there is no trick, generic condition results in common result, then following other way to find uncommon result…</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
This code doesn’t have trick, i use instruction “<b>pcmpistri</b>”(Packed Compare Implicit LengthStrings, Return Index) and the “<b>movdqu</b>”(move unaligned double quadword) instruction must be used to transfer data from this into an XMM register, this istructions you can make many things around “strings”, take a look at the following:</div>
<br />
<pre class="brush:c" name="code">global strcmp_sse42_64
; by Cooler_ c00f3r[at]gmail[dot]com
; 64 bit
; nasm -f elf64 code.s -o code.o
; int strcmp_sse42_64(const char *, const char *); // declare in C code
strcmp_sse42_64:
push rbp
mov rbp, rsp
mov rax, rdi
mov rdx, rsi
sub rax, rdx
sub rdx, 32
strloop_64:
add rdx, 32
movdqu xmm0, [rdx]
pcmpistri xmm0, [rdx+rax], 0011000b ;compare... jump again if above...
ja strloop_64
jc blockmov_64 ; jump 2 movzx
xor rax, rax ; clear return result...
jmp quit
blockmov_64:
add rax, rdx
movzx rax, byte[rax+rcx] ; move with zero
movzx rdx, byte[rdx+rcx]
sub rax, rdx
quit:
pop rbp
ret
</pre>
So i use it to hook functions <b>32bit</b> and <b>64bit</b> version:
<br />
<pre class="brush:c" name="code">#if UINTPTR_MAX == 0xffffffff
static int (*strcmp_sse42)(const char *, const char *) = strcmp_sse42_32;
#elif UINTPTR_MAX == 0xffffffffffffffff
static int (*strcmp_sse42)(const char *, const char *) = strcmp_sse42_64;
#else
fprintf(stderr,"error in arch\n");
exit(0);
#endif
</pre>
<div style="text-align: justify;">
Before hooking it, you need to check whether or not your machine has SSE4.2 support. There are many ways of doing it, however, for the sake of simplicity, let’s go with the following one:</div>
<br />
<pre class="brush:c" name="code">void cpu_get(int* cpuinfo, int info)
{
#if UINTPTR_MAX == 0xffffffff
__asm__ __volatile__(
"xchg %%ebx, %%edi;"
"cpuid;"
"xchg %%ebx, %%edi;"
:"=a" (cpuinfo[0]), "=D" (cpuinfo[1]), "=c" (cpuinfo[2]), "=d" (cpuinfo[3])
:"0" (info)
);
#elif UINTPTR_MAX == 0xffffffffffffffff
__asm__ __volatile__(
"xchg %%rbx, %%rdi;"
"cpuid;"
"xchg %%rbx, %%rdi;"
:"=a" (cpuinfo[0]), "=D" (cpuinfo[1]), "=c" (cpuinfo[2]), "=d" (cpuinfo[3])
:"0" (info)
);
#endif
}
void test_sse42_enable()
{
int cpuinfo[4];
int sse42=0;
cpu_get(cpuinfo,1);
sse42=cpuinfo[2] & (1 << 20) || 0;
if(sse42)
puts("SSE4.2 Test...\n OK SSE 4.2 instruction enable !\n");
else {
puts("SSE4.2 Not enabled\n your CPU need SSE 4.2 instruction to run this programm\n");
exit(0);
}
}
</pre>
<br />
look all source code <a href="https://github.com/CoolerVoid/cooler_sse42_strcmp">here</a>!<br />
<br />
<br />
<pre class="brush:c" name="code">$ git clone https://github.com/CoolerVoid/cooler_sse42_strcmp
$ make; ./test
SSE4.2 Test…
OK SSE 4.2 instruction enable !
::: strcmp() with SSE42: 2812 cicles
Array size of words is: 245
Benchmark strcmp() with SSE42 matchs is: 84
::: simple strcmp(): 12663 cicles
Array size of words is: 245
Benchmark strcmp() matchs is: 84
$ cat /proc/cpuinfo | grep “model name”
model name : Intel(R) Core(TM) i5-4690K CPU @ 3.50GHz
$ gcc -v | grep “gcc version”
gcc version 4.8.3 20140911 (Red Hat 4.8.3-7) (GCC)
$ uname -a
Linux localhost.localdomain 3.15.10-201.fc20.i686 #1 SMP Wed Aug 27 21:33:30 UTC 2014 i686 i686 i386 GNU/Linux
</pre>
<br />
<div style="text-align: justify;">
SSE is very common in image processing, game developers use it too, take a look at the following:</div>
<a href="https://software.intel.com/en-us/articles/using-intel-streaming-simd-extensions-and-intel-integrated-performance-primitives-to-accelerate-algorithms">https://software.intel.com/en-us/articles/using-intel-streaming-simd-extensions-and-intel-integrated-performance-primitives-to-accelerate-algorithms</a><br />
<br />
<b>Do you like CPU features ? </b>look <a href="https://gist.github.com/CoolerVoid/0e2b5791963dd6aa714e">this</a><br />
<br />
well well well a cup of Moloko to my little Droogies<br />
<a href="https://coolerlab.files.wordpress.com/2014/10/moloko_gif.gif"><img src="https://coolerlab.files.wordpress.com/2014/10/moloko_gif.gif?w=645" /></a><br />
my fifty cents ! CHEERS !CoolerVoidhttp://www.blogger.com/profile/04320162188823710256noreply@blogger.com7