<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Reverse Engineering Archives - Tech Antidote</title>
	<atom:link href="https://techantidote.com/category/reverse-engineering/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>‎ &#124; LINUX &#124; Tech &#124; Security &#124;</description>
	<lastBuildDate>Mon, 02 Nov 2020 19:26:03 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.4</generator>

<image>
	<url>https://techantidote.com/wp-content/uploads/2016/02/cropped-google-plus-profile-picture-32x32.png</url>
	<title>Reverse Engineering Archives - Tech Antidote</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">106369530</site>	<item>
		<title>GDB basics with C</title>
		<link>https://techantidote.com/gdb-basics-with-c/</link>
					<comments>https://techantidote.com/gdb-basics-with-c/#respond</comments>
		
		<dc:creator><![CDATA[]]></dc:creator>
		<pubDate>Sun, 01 Nov 2020 23:35:11 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Reverse Engineering]]></category>
		<category><![CDATA[basic]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[gdb]]></category>
		<category><![CDATA[reverse engineering]]></category>
		<category><![CDATA[simple]]></category>
		<category><![CDATA[tutorial]]></category>
		<guid isPermaLink="false">https://techantidote.com/?p=3391</guid>

					<description><![CDATA[<p>This is a beginner level tutorial on learning basics of debugging using GDB by debugging an executable. This post will cover writing a very simple C code, compiling it and then opening the generated executable in GDB for inspecting the working/debugging. The primary objective here is to get familiar with the basics of using GDB. &#8230; <a href="https://techantidote.com/gdb-basics-with-c/" class="more-link">Continue reading <span class="screen-reader-text">GDB basics with C</span> <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a href="https://techantidote.com/gdb-basics-with-c/">GDB basics with C</a> appeared first on <a href="https://techantidote.com">Tech Antidote</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>This is a beginner level tutorial on learning basics of debugging using GDB by debugging an executable. This post will cover writing a very simple C code, compiling it and then opening the generated executable in GDB for inspecting the working/debugging. The primary objective here is to get familiar with the basics of using GDB.</p>
<p><span style="color: #3366ff;"><strong>Requirements:</strong></span></p>
<ol>
<li>Any Linux x64 OS. (Linux Mint 20 used in this tutorial)</li>
<li>C compiler &#8211; gcc</li>
<li>gdb &#8211; for debugging / reverse engineering.</li>
<li>GDB Dashboard (Optional &#8211; Makes GDB easier to read)</li>
</ol>
<p>[Disclaimer: This is blog post adapted from recurse&#8217;s original gdb tutorial.(All Credits/References added in the Credits section)]</p>
<p><span style="color: #3366ff;"><strong>Setting up the Environment:</strong></span></p>
<ul>
<li>Install the following tools to setup the basic environment:</li>
</ul>
<p><span style="color: #993366;"><strong>sudo apt install vim gcc gdb git python3 python3-pip -y</strong></span></p>
<p><span style="color: #3366ff;"><strong>GDB Dashboard:</strong></span></p>
<ul>
<li>To make GDB easier to understand and make it non-alien, use <strong>GDB Dashboard</strong> which is a really good python plugin  for GDB.  Its epic!</li>
<li>Run the following to get the gdbinit dotfile.</li>
</ul>
<pre><span style="color: #993366;"><strong>wget -P ~ https://git.io/.gdbinit</strong></span></pre>
<p>If you would like to check out the source, you can find it  @ https://github.com/cyrus-and/gdb-dashboard</p>
<p>For syntax highlighting, you would need the pygments module. If you are using Python2.x:</p>
<p><span style="color: #ff0000;"><strong>pip install pygments</strong></span></p>
<p>If you are using Python3.x, then use pip3 to install it:</p>
<p><span style="color: #ff0000;"><strong>pip3 install pygments</strong></span></p>
<p>To run gdb without providing any executable, run &#8220;gdb&#8221; and you should see the something like below:</p>
<pre>extr3me@w4rl0ck:gdb$ <span style="color: #ff0000;"><strong>gdb</strong></span>
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
&lt;http://www.gnu.org/software/gdb/bugs/&gt;.
Find the GDB manual and other documentation resources online at:
    &lt;http://www.gnu.org/software/gdb/documentation/&gt;.

For help, type "help".
Type "apropos word" to search for commands related to "word".
&gt;&gt;&gt; 
</pre>
<p>To exit out of gdb, type  &#8220;<strong>quit</strong>&#8221; .</p>
<p>-Below is a simple C program that initializes an integer value and then returns 0.</p>
<pre><strong><span style="color: #ff0000;">cat minimal.c</span>
int main()
{
int i = 1337;
return 0;
}
</strong></pre>
<p>-Compile the C code and make an executable, using the following flags:</p>
<pre><span style="color: #ff0000;"><strong>gcc -g minimal.c -o minimal</strong></span></pre>
<p>-The directory should now have a executable with filename <strong>minimal</strong>.</p>
<pre>extr3me@w4rl0ck:gdb$ <span style="color: #ff0000;"><strong>file minimal</strong></span>
minimal: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=c918c072d119be6a9d18991e812a5414ecae67e1, for GNU/Linux 3.2.0, with debug_info, not stripped
</pre>
<p><span style="color: #3366ff;"><strong>Debugging the binary using GDB:</strong></span></p>
<p>-To examine an executable in GDB, use the following format:</p>
<pre><span style="color: #ff0000;"><strong>gdb &lt;executable_file&gt;</strong></span></pre>
<p>-Here, I would run the following to open the executable for debugging:</p>
<p><span style="color: #ff0000;"><strong>gdb minimal</strong></span></p>
<p><img fetchpriority="high" decoding="async" data-attachment-id="3437" data-permalink="https://techantidote.com/gdb-basics-with-c/gdb-1/" data-orig-file="https://techantidote.com/wp-content/uploads/2020/11/gdb-1.png" data-orig-size="700,328" data-comments-opened="1" data-image-title="gdb-1" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2020/11/gdb-1.png" class="alignnone wp-image-3437 size-full" src="https://techantidote.com/wp-content/uploads/2020/11/gdb-1.png" alt="gdb minimal output" width="700" height="328" srcset="https://techantidote.com/wp-content/uploads/2020/11/gdb-1.png 700w, https://techantidote.com/wp-content/uploads/2020/11/gdb-1-300x141.png 300w" sizes="(max-width: 700px) 100vw, 700px" /></p>
<p>Lets check the functions in this program, to do run &#8220;info functions&#8221;. You should be able to see that this executable has function main().</p>
<pre>&gt;&gt;&gt; <span style="color: #ff0000;"><strong>info functions</strong></span>
All defined functions:

File minimal.c:
<span style="color: #ff0000;"><strong>1: int main();</strong></span>

Non-debugging symbols:
0x0000000000001000 _init
0x0000000000001030 __cxa_finalize@plt
0x0000000000001040 _start
0x0000000000001070 deregister_tm_clones
0x00000000000010a0 register_tm_clones
0x00000000000010e0 __do_global_dtors_aux
0x0000000000001120 frame_dummy
0x0000000000001140 __libc_csu_init
0x00000000000011b0 __libc_csu_fini
0x00000000000011b8 _fini
</pre>
<pre>Here is a screenshot:
</pre>
<p><img decoding="async" data-attachment-id="3438" data-permalink="https://techantidote.com/gdb-basics-with-c/gdb-info-functions/" data-orig-file="https://techantidote.com/wp-content/uploads/2020/11/gdb-info-functions.png" data-orig-size="818,636" data-comments-opened="1" data-image-title="gdb-info-functions" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2020/11/gdb-info-functions.png" class="alignnone wp-image-3438 size-full" src="https://techantidote.com/wp-content/uploads/2020/11/gdb-info-functions.png" alt="gdb info functions output screenshot" width="818" height="636" srcset="https://techantidote.com/wp-content/uploads/2020/11/gdb-info-functions.png 818w, https://techantidote.com/wp-content/uploads/2020/11/gdb-info-functions-300x233.png 300w, https://techantidote.com/wp-content/uploads/2020/11/gdb-info-functions-768x597.png 768w" sizes="(max-width: 818px) 100vw, 818px" /></p>
<p>The output also shows that the <em>line number</em> at which <em>main() function</em> is present in the source minimal.c.</p>
<p>From the <em>info functions</em> output, we can also see that function main() is present at <em>line number 1 </em>in source minimal.c<em>.</em></p>
<p><span style="color: #3366ff;"><strong>Breakpoints:</strong></span></p>
<p>-Lets say we need to examine what a executable is going at a given point of time or to inspect the value of a variable, then we can set one or more breakpoints and run the code upto that breakpoint. You can then examine the the value of a variable upto that breakpoint or directly examine the memory etc.</p>
<p><span style="color: #3366ff;"><strong>Breakpoints example: Setting breakpoints using function name</strong></span></p>
<p>From the previous section,we know that the executable has function main(). Lets start examining the binary using gdb to see the value of &#8220;i&#8221; at different stages of the execution. To start with,  set a breakpoint at function main using the syntax <em>break &lt;function_name&gt;</em>:</p>
<p>&gt;&gt;&gt; <span style="color: #ff0000;"><strong>break main</strong></span><br />
Breakpoint 1 at <span style="color: #ff9999;"><strong>0x1129</strong></span>: file minimal.c, line 2.<br />
&gt;&gt;&gt;</p>
<p>To get the list of breakpoints that were set, you can use &#8220;info breakpoints&#8221;.</p>
<p>&gt;&gt;&gt; <span style="color: #ff9999;"><strong>info breakpoints</strong></span><br />
Num Type Disp Enb Address What<br />
1 breakpoint keep y <span style="color: #ff9999;">0x0000000000001129</span> in main at <span style="color: #ff9999;">minimal.c:2</span></p>
<p><img decoding="async" data-attachment-id="3439" data-permalink="https://techantidote.com/gdb-basics-with-c/gdb-info-breakpoints/" data-orig-file="https://techantidote.com/wp-content/uploads/2020/11/gdb-info-breakpoints.png" data-orig-size="602,417" data-comments-opened="1" data-image-title="gdb-info-breakpoints" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2020/11/gdb-info-breakpoints.png" class="alignnone wp-image-3439 size-full" src="https://techantidote.com/wp-content/uploads/2020/11/gdb-info-breakpoints.png" alt="gdb info breakpoints" width="602" height="417" srcset="https://techantidote.com/wp-content/uploads/2020/11/gdb-info-breakpoints.png 602w, https://techantidote.com/wp-content/uploads/2020/11/gdb-info-breakpoints-300x208.png 300w" sizes="(max-width: 602px) 100vw, 602px" /></p>
<p>The above shows that there is one breakpoint which is set at function main() whose starting address in memory is &#8220;<strong>0x1129</strong>&#8221; or &#8220;<strong>0x0000000000001129</strong>&#8220;.</p>
<p><em><strong>Note</strong>:</em> You can have create multiple breakpoints and they are numbered.</p>
<p>So we have a breakpoint in place, we can run the binary upto the breakpoint that was set. To run the program, use &#8220;<span style="color: #e3abc7;">run&#8221;</span> or &#8220;<span style="color: #993366;">r</span>&#8220;.</p>
<p><img loading="lazy" decoding="async" data-attachment-id="3440" data-permalink="https://techantidote.com/gdb-basics-with-c/gdb-run/" data-orig-file="https://techantidote.com/wp-content/uploads/2020/11/gdb-run.png" data-orig-size="1268,474" data-comments-opened="1" data-image-title="gdb-run" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2020/11/gdb-run-1024x383.png" class="alignnone wp-image-3440 size-full" src="https://techantidote.com/wp-content/uploads/2020/11/gdb-run.png" alt="gdb screenshot" width="1268" height="474" srcset="https://techantidote.com/wp-content/uploads/2020/11/gdb-run.png 1268w, https://techantidote.com/wp-content/uploads/2020/11/gdb-run-300x112.png 300w, https://techantidote.com/wp-content/uploads/2020/11/gdb-run-1024x383.png 1024w, https://techantidote.com/wp-content/uploads/2020/11/gdb-run-768x287.png 768w" sizes="auto, (max-width: 1268px) 100vw, 1268px" /></p>
<p>This is what it looks like now. (You could now see the GDB dashboard with an insane amount of matrix looking stuff. :P)</p>
<p>[<strong>Note:</strong>  Right click the image and click on &#8220;View Image&#8221; / &#8220;Open image in new tab&#8221; if the image for better visibility. ]</p>
<p><img loading="lazy" decoding="async" data-attachment-id="3441" data-permalink="https://techantidote.com/gdb-basics-with-c/gdb-run-dashboard/" data-orig-file="https://techantidote.com/wp-content/uploads/2020/11/gdb-run-dashboard.png" data-orig-size="1908,992" data-comments-opened="1" data-image-title="gdb-run-dashboard" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2020/11/gdb-run-dashboard-1024x532.png" class="alignnone wp-image-3441 size-full" src="https://techantidote.com/wp-content/uploads/2020/11/gdb-run-dashboard.png" alt="GDB Dashboard screenshot" width="1908" height="992" srcset="https://techantidote.com/wp-content/uploads/2020/11/gdb-run-dashboard.png 1908w, https://techantidote.com/wp-content/uploads/2020/11/gdb-run-dashboard-300x156.png 300w, https://techantidote.com/wp-content/uploads/2020/11/gdb-run-dashboard-1024x532.png 1024w, https://techantidote.com/wp-content/uploads/2020/11/gdb-run-dashboard-768x399.png 768w, https://techantidote.com/wp-content/uploads/2020/11/gdb-run-dashboard-1536x799.png 1536w" sizes="auto, (max-width: 1908px) 100vw, 1908px" /></p>
<p>For this tutorial, we are more interested in few sections of Dashboard as of now &#8211;   &#8220;Source&#8221; , &#8220;Threads&#8221;, &#8220;Variables&#8221; and &#8220;Assembly&#8221; sections.</p>
<p>-The &#8220;Source&#8221; section has a blip on the line number which shows where exactly in the code where gdb is looking into. So, here, it shows that its in line 2 of the source. After you ran the program, if you re-run &#8220;info breakpoints&#8221; you can see that the message &#8220;<em>breakpoint already hit 1 time</em>&#8220;.</p>
<blockquote><p>&gt;&gt; <span style="color: #ff9999;"><strong>info breakpoints</strong></span><br />
Num Type Disp Enb Address What<br />
1 breakpoint keep y <span style="color: #ff9999;"><strong>0x0000555555555129</strong></span> in main at minimal.c:2<br />
<span style="color: #ff0000;"><strong>breakpoint already hit 1 time</strong></span></p></blockquote>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" data-attachment-id="3442" data-permalink="https://techantidote.com/gdb-basics-with-c/gdb-after-run-info-breakpoints/" data-orig-file="https://techantidote.com/wp-content/uploads/2020/11/gdb-after-run-info-breakpoints.png" data-orig-size="1909,899" data-comments-opened="1" data-image-title="gdb-after-run-info-breakpoints" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2020/11/gdb-after-run-info-breakpoints-1024x482.png" class="alignnone wp-image-3442 size-full" src="https://techantidote.com/wp-content/uploads/2020/11/gdb-after-run-info-breakpoints.png" alt="" width="1909" height="899" srcset="https://techantidote.com/wp-content/uploads/2020/11/gdb-after-run-info-breakpoints.png 1909w, https://techantidote.com/wp-content/uploads/2020/11/gdb-after-run-info-breakpoints-300x141.png 300w, https://techantidote.com/wp-content/uploads/2020/11/gdb-after-run-info-breakpoints-1024x482.png 1024w, https://techantidote.com/wp-content/uploads/2020/11/gdb-after-run-info-breakpoints-768x362.png 768w, https://techantidote.com/wp-content/uploads/2020/11/gdb-after-run-info-breakpoints-1536x723.png 1536w" sizes="auto, (max-width: 1909px) 100vw, 1909px" /></p>
<p>[<strong>Note:</strong>  Right click the image and click on &#8220;View Image&#8221; / &#8220;Open image in new tab&#8221; if the image for better visibility. ]</p>
<p>As of now, the debugger is in line 2 where the variable &#8220;i&#8221; is not yet yet initiated in the program. So, if you try to print the value of &#8220;i&#8221;, then it should show a value = 0.</p>
<p>To print value of a variable, use the format &#8220;<span style="color: #e3abc7;">print var</span>&#8221; or &#8220;<span style="color: #993366;">p var</span>&#8220;.</p>
<blockquote><p>&gt;&gt; <span style="color: #ff9999;"><strong>p i</strong></span><br />
$1 = <span style="color: #ff0000;">0</span></p></blockquote>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" data-attachment-id="3443" data-permalink="https://techantidote.com/gdb-basics-with-c/gdb-print-variable/" data-orig-file="https://techantidote.com/wp-content/uploads/2020/11/gdb-print-variable.png" data-orig-size="1906,990" data-comments-opened="1" data-image-title="gdb-print-variable" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2020/11/gdb-print-variable-1024x532.png" class="alignnone wp-image-3443 size-full" src="https://techantidote.com/wp-content/uploads/2020/11/gdb-print-variable.png" alt="print i output screenshot" width="1906" height="990" srcset="https://techantidote.com/wp-content/uploads/2020/11/gdb-print-variable.png 1906w, https://techantidote.com/wp-content/uploads/2020/11/gdb-print-variable-300x156.png 300w, https://techantidote.com/wp-content/uploads/2020/11/gdb-print-variable-1024x532.png 1024w, https://techantidote.com/wp-content/uploads/2020/11/gdb-print-variable-768x399.png 768w, https://techantidote.com/wp-content/uploads/2020/11/gdb-print-variable-1536x798.png 1536w" sizes="auto, (max-width: 1906px) 100vw, 1906px" /></p>
<p>To go to the next line of code and execute it, use command &#8220;<span style="color: #e3abc7;"><strong>next</strong></span>&#8221; or &#8220;<span style="color: #e3abc7;"><strong>n</strong></span>&#8220;.  [Note: If the next line of code was a function, it would execute the full function). In this example, once we ran &#8220;<span style="color: #993366;">r</span>&#8220;, the &#8220;Source&#8221; section now highlights line number 3.</p>
<p><img loading="lazy" decoding="async" data-attachment-id="3444" data-permalink="https://techantidote.com/gdb-basics-with-c/gdb-next-1/" data-orig-file="https://techantidote.com/wp-content/uploads/2020/11/gdb-next-1.png" data-orig-size="1908,995" data-comments-opened="1" data-image-title="gdb-next-1" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2020/11/gdb-next-1-1024x534.png" class="alignnone wp-image-3444 size-full" src="https://techantidote.com/wp-content/uploads/2020/11/gdb-next-1.png" alt="gdb print i screenshot 2" width="1908" height="995" srcset="https://techantidote.com/wp-content/uploads/2020/11/gdb-next-1.png 1908w, https://techantidote.com/wp-content/uploads/2020/11/gdb-next-1-300x156.png 300w, https://techantidote.com/wp-content/uploads/2020/11/gdb-next-1-1024x534.png 1024w, https://techantidote.com/wp-content/uploads/2020/11/gdb-next-1-768x401.png 768w, https://techantidote.com/wp-content/uploads/2020/11/gdb-next-1-1536x801.png 1536w" sizes="auto, (max-width: 1908px) 100vw, 1908px" /></p>
<p>At this point, if you print variable &#8220;i&#8221;, it still shows value as &#8220;<span style="color: #e3abc7;">0</span>&#8221; as the initialization is not complete.</p>
<p>You can also check the &#8220;<em>Assembly</em>&#8221; section in the GDB Dashboard which shows the disassembled code. So, the highlighted section in green in the  &#8220;<em>Assembly</em>&#8221; shows the following:</p>
<p>0x0000555555555131 main+8 <span style="color: #ff0000;">movl $<strong>0x539</strong>,-0x4(%rbp)</span></p>
<p>This is the disassembled code (in AT&amp;T syntax) for &#8220;int i = 1337&#8221;. We will come back to reviewing the assembly code later.</p>
<p><img loading="lazy" decoding="async" data-attachment-id="3445" data-permalink="https://techantidote.com/gdb-basics-with-c/gdb-run-3/" data-orig-file="https://techantidote.com/wp-content/uploads/2020/11/gdb-run-3-e1604273604939.png" data-orig-size="658,271" data-comments-opened="1" data-image-title="gdb-run-3" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2020/11/gdb-run-3-e1604273494969-1024x258.png" class="alignnone wp-image-3445 size-full" src="https://techantidote.com/wp-content/uploads/2020/11/gdb-run-3-e1604273604939.png" alt="" width="658" height="271" srcset="https://techantidote.com/wp-content/uploads/2020/11/gdb-run-3-e1604273604939.png 658w, https://techantidote.com/wp-content/uploads/2020/11/gdb-run-3-e1604273604939-300x124.png 300w" sizes="auto, (max-width: 658px) 100vw, 658px" /></p>
<p>Run command &#8220;<span style="color: #993366;"><strong>n</strong></span>&#8221; again in gdb. Now, checking the &#8220;Source&#8221; section in GDB dashboard, should that we are now on <em>line 4</em>.</p>
<p>Now. check the value of  variable &#8220;i&#8221; using print.</p>
<p>&gt;&gt;&gt;<span style="color: #993366;"><strong> p i</strong></span><br />
$3 = <span style="color: #993366;"><strong>1337</strong></span></p>
<p>This shows that the value variable &#8220;i&#8221; is 1337.</p>
<pre>(gdb) <span style="color: #e3abc7;"><strong>print i</strong></span>
$2 = <span style="color: #993366;">1337</span></pre>
<p>Here is the screenshot for reference.</p>
<p><img loading="lazy" decoding="async" data-attachment-id="3446" data-permalink="https://techantidote.com/gdb-basics-with-c/gdb-print-i-value-after-assign/" data-orig-file="https://techantidote.com/wp-content/uploads/2020/11/gdb-print-i-value-after-assign.png" data-orig-size="1908,992" data-comments-opened="1" data-image-title="gdb-print-i-value-after-assign" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2020/11/gdb-print-i-value-after-assign-1024x532.png" class="alignnone wp-image-3446 size-full" src="https://techantidote.com/wp-content/uploads/2020/11/gdb-print-i-value-after-assign.png" alt="GDB print i after initialized" width="1908" height="992" srcset="https://techantidote.com/wp-content/uploads/2020/11/gdb-print-i-value-after-assign.png 1908w, https://techantidote.com/wp-content/uploads/2020/11/gdb-print-i-value-after-assign-300x156.png 300w, https://techantidote.com/wp-content/uploads/2020/11/gdb-print-i-value-after-assign-1024x532.png 1024w, https://techantidote.com/wp-content/uploads/2020/11/gdb-print-i-value-after-assign-768x399.png 768w, https://techantidote.com/wp-content/uploads/2020/11/gdb-print-i-value-after-assign-1536x799.png 1536w" sizes="auto, (max-width: 1908px) 100vw, 1908px" /></p>
<pre>To find the type of the variable and the starting memory address of that variable in memory:</pre>
<pre><span style="color: #ff9752;">&gt;&gt;&gt; <span style="color: #ff0000;">print &amp;i
</span>$4 = (<span style="color: #00ffff;">int</span> *) <span style="color: #e3abc7;"><strong>0x7fffffffdb8c</strong>
</span></span>
<img loading="lazy" decoding="async" data-attachment-id="3449" data-permalink="https://techantidote.com/gdb-basics-with-c/gdb-start-address-of-i/" data-orig-file="https://techantidote.com/wp-content/uploads/2020/11/gdb-start-address-of-i.png" data-orig-size="1906,865" data-comments-opened="1" data-image-title="gdb-start-address-of-i" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2020/11/gdb-start-address-of-i-1024x465.png" class="alignnone wp-image-3449 size-full" src="https://techantidote.com/wp-content/uploads/2020/11/gdb-start-address-of-i.png" alt="GDB print directly using variable address output" width="1906" height="865" srcset="https://techantidote.com/wp-content/uploads/2020/11/gdb-start-address-of-i.png 1906w, https://techantidote.com/wp-content/uploads/2020/11/gdb-start-address-of-i-300x136.png 300w, https://techantidote.com/wp-content/uploads/2020/11/gdb-start-address-of-i-1024x465.png 1024w, https://techantidote.com/wp-content/uploads/2020/11/gdb-start-address-of-i-768x349.png 768w, https://techantidote.com/wp-content/uploads/2020/11/gdb-start-address-of-i-1536x697.png 1536w" sizes="auto, (max-width: 1906px) 100vw, 1906px" /></pre>
<p><span style="color: #ffffff;">-The above shows that &#8220;i&#8221; is of &#8220;<span style="color: #00ffff;">int</span>&#8221; type. Additionally, &#8220;i&#8221; is stored at memory location starting at address <span style="color: #993366;"><strong>0x7fffffffdb8c</strong></span></span></p>
<p>-To check maximum size of int type in memory using the sizeof() function.</p>
<pre>&gt;&gt;&gt; <span style="color: #ff0000;">p sizeof(int)</span>
$5 = <span style="color: #993366;">4</span>
</pre>
<p>The above output shows that int would occupy <span style="color: #ff0000;">4 bytes</span> of space in memory.</p>
<p>To examine memory using gdb use &#8220;<strong>x</strong>&#8220;.</p>
<p>-From above outputs, we know that variable &#8220;i&#8221; is stored in memory with starting address as <span style="color: #993366;"><strong>0x7fffffffdb8c</strong></span>. We also do know that i is of type <em>integer</em> and integer type would occupy a maximum of 4 bytes in memory.</p>
<p>To examine a specific memory address, you could use the following format</p>
<pre><strong><span style="color: #ff0000;">(gdb) x/FMT &lt;starting_memory_address&gt;</span> 
</strong></pre>
<p>Here is information from &#8220;help x&#8221; section:<command></command></p>
<pre>(gdb) <span style="color: #ff9999;"><strong>help x</strong></span>
<strong>Examine memory: <span style="color: #3366ff;">x/FMT ADDRESS</span>.</strong>
<span style="color: #8fabff;">ADDRESS</span> is an expression for the <span style="color: #8fabff;">memory address to examine</span>.
<span style="color: #8fabff;">FMT</span> is a <span style="color: #3366ff;"><strong>repeat count</strong></span> followed by a <span style="color: #3366ff;">format letter </span>and a<span style="color: #3366ff;"> size letter</span>.
<span style="color: #8fabff;">Format letters</span> are o(octal), <span style="color: #8fabff;"><strong>x(hex)</strong></span>, d(decimal), u(unsigned decimal),
t(binary), f(float), a(address), i(instruction), c(char), s(string)
and z(hex, zero padded on the left).
<span style="color: #8fabff;">Size letters</span> are <span style="color: #8fabff;">b(byte)</span>, h(halfword), w(word), g(giant, 8 bytes).
The specified number of objects of the specified size are printed
according to the format. If a negative number is specified, memory is
examined backward from the address.

Defaults for format and size letters are those previously used.
Default count is 1. Default address is following last thing printed
with this command or "print".
(gdb)</pre>
<p>Here, is an example, to examine data from memory location 0x7fffffffdb8c upto the next bytes (or 4 bytes above 0x7fffffffdb8c) ,  use the following:</p>
<pre>&gt;&gt;&gt; <span style="color: #e3abc7;"><span style="color: #ff0000;">x/4xb</span> <span style="color: #e3abc7;">0x7fffffffdb8c</span></span>
0x7fffffffdb8c: <span style="color: #993366;">0x39 0x05 0x00 0x00</span>
</pre>
<p>Alternatively, you can access provide the memory location directly by passing &#8220;<strong>&amp;i</strong>&#8221; i.e.  &#8220;<span style="color: #993366;"><strong>x/4xb &amp;i&#8221;</strong></span></p>
<p><img loading="lazy" decoding="async" data-attachment-id="3447" data-permalink="https://techantidote.com/gdb-basics-with-c/gdb-examine-memory/" data-orig-file="https://techantidote.com/wp-content/uploads/2020/11/gdb-examine-memory.png" data-orig-size="1910,994" data-comments-opened="1" data-image-title="gdb-examine-memory" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2020/11/gdb-examine-memory-1024x533.png" class="alignnone wp-image-3447 size-full" src="https://techantidote.com/wp-content/uploads/2020/11/gdb-examine-memory.png" alt="GDB examine memory location screenshot" width="1910" height="994" srcset="https://techantidote.com/wp-content/uploads/2020/11/gdb-examine-memory.png 1910w, https://techantidote.com/wp-content/uploads/2020/11/gdb-examine-memory-300x156.png 300w, https://techantidote.com/wp-content/uploads/2020/11/gdb-examine-memory-1024x533.png 1024w, https://techantidote.com/wp-content/uploads/2020/11/gdb-examine-memory-768x400.png 768w, https://techantidote.com/wp-content/uploads/2020/11/gdb-examine-memory-1536x799.png 1536w" sizes="auto, (max-width: 1910px) 100vw, 1910px" /></p>
<p>The above output shows raw byte by byte representation in memory. Here, <span style="color: #ff9999;">0x39</span> is one byte in memory and <span style="color: #ff9999;">0x05</span> is another byte etc.<br />
We also know that int would occupy 4 bytes in memory. Here, this integer takes only 2 bytes out of 4 bytes in memory.</p>
<p><strong>Important:</strong> This raw memory representation &#8220;<span style="color: #ff9999;">0x39 0x05 0x00 0x00</span>&#8221; is in &#8220;<strong>little-endian</strong>&#8221; format(the least significant bytes of a number come first in memory).<br />
So, you would need to read the hex bytes  &#8220;<span style="color: #ff9999;">0x39 0x05 0x00 0x00</span>&#8221; in reverse order. Hence, the value reversed <span style="color: #e3abc7;">0x00 0x00 0x05 0x39</span> is <span style="color: #ff00ff;"><strong>00000539</strong></span>.<br />
To get the actual value, we need to convert the hex value to decimal.</p>
<p>You could use trusty bash  to covert hex to decimal by using format &#8220;<span style="color: #993366;"><em><strong>echo $(( 16#$<span style="color: #ff00ff;">hexNum</span> ))</strong></em></span>&#8220;. Here is what it does look like:</p>
<pre>extr3me@w4rl0ck:~$ <span style="color: #993366;"><strong>echo $(( 16#<span style="color: #ff00ff;">00000539</span> ))</strong></span>
<span style="color: #ff0000;"><strong>1337
</strong></span></pre>
<p>So, &#8220;1337&#8221; is the decimal value stored in memory location 4 bytes starting from <span style="color: #993366;"><strong>0x7fffffffdb8c</strong></span>.</p>
<p>So, we now know the value of integer variable starting at memory location <span style="color: #993366;"><strong>0x7fffffffdb8c</strong></span> is <strong>1337</strong>. (i.e. the decimal value of i stored in memory is 1337).</p>
<p>An alternative is to use a online hex to decimal converter such as https://www.binaryhexconverter.com/hex-to-decimal-converter to covert 00000539 to decimal.</p>
<p>-You can also print the raw data using the variable itself by using the following format:</p>
<pre>(gdb) <span style="color: #ff0000;"><strong>x/4xb &amp;i</strong></span>

0x7fffffffdb5c: <span style="color: #ff0000;">0x39 0x05 0x00 0x00</span>
</pre>
<p><span style="color: #3366ff;"><strong>Other formats in GDB:</strong></span></p>
<p>-To print in decimal format, use <strong><em>x/1dw</em></strong>:</p>
<pre>(gdb)<span style="color: #ff9999;"><strong> x/1dw &amp;i</strong></span>
0x7fffffffdb7c: <span style="color: #ff0000;">1337
</span></pre>
<p>-Alternatively, give the memory location as well.</p>
<pre>(gdb) <span style="color: #ff9999;"><strong>x/1dw 0x7fffffffdb7c</strong></span>
0x7fffffffdb7c: <span style="color: #ff9999;">1337
</span></pre>
<p><span style="color: #8fabff;"><strong>Misc Information:</strong></span></p>
<p>To check the number of threads, run &#8220;<strong>info threads</strong>&#8220;.</p>
<pre>&gt;&gt;&gt; <span style="color: #ff0000;"><strong>info threads</strong></span>
Id Target Id Frame <span style="color: #ff9999;">
* 1 process 6570 "minimal" main () at minimal.c:4
&gt;&gt;&gt; 
</span></pre>
<p>This shows there is 1 thread with PID 6570.</p>
<p><strong>Other Interesting Stuff:</strong></p>
<p>Here is a screenshot of the dissembled code of &#8220;minimal&#8221; binary.</p>
<p>#gdb-assembly.png</p>
<p>The one that we are interested in for now is the instruction highlighted below:</p>
<pre>0x0000555555555129 main+0 endbr64
0x000055555555512d main+4 push %rbp
0x000055555555512e main+5 mov %rsp,%rbp
0x0000555555555131 main+8 <span style="color: #ff00ff;">movl $0x539,-0x4(%rbp)</span>
0x0000555555555138 main+15 mov $0x0,%eax
0x000055555555513d main+20 pop %rbp
0x000055555555513e main+21 retq
</pre>
<p>The instruction &#8220;<span style="color: #993366;"><strong>movl $0x539,-0x4(%rbp)</strong></span>&#8221; means =&gt; move the value &#8220;0x539&#8221; to the memory location of register rbp -4.</p>
<p><strong>Differences between AT&amp;T and Intel Syntax:</strong></p>
<p>By default, GDB defaults to displaying in AT&amp;T syntax. Here is what main function dissembled looks like in AT&amp;T syntax:</p>
<pre>extr3me@w4rl0ck:gdb$ <span style="color: #ff9999;"><strong>gdb minimal</strong></span>
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
&lt;http://www.gnu.org/software/gdb/bugs/&gt;.
Find the GDB manual and other documentation resources online at:
&lt;http://www.gnu.org/software/gdb/documentation/&gt;.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from minimal...
&gt;&gt;&gt; <span style="color: #ff9999;"><strong>disass /m main</strong></span>
Dump of assembler code for function main:
2 {
0x0000000000001129 &lt;+0&gt;: endbr64
0x000000000000112d &lt;+4&gt;: push %rbp
0x000000000000112e &lt;+5&gt;: mov %rsp,%rbp

3 int i = 1337;
0x0000000000001131 &lt;+8&gt;: <span style="color: #ff0000;">movl $0x539,-0x4(%rbp)</span>

4 return 0;
0x0000000000001138 &lt;+15&gt;: mov $0x0,%eax

5 }
0x000000000000113d &lt;+20&gt;: pop %rbp
0x000000000000113e &lt;+21&gt;: retq

End of assembler dump.
&gt;&gt;&gt;
</pre>
<p>-If you don&#8217;t like this syntax, you can make GDB use Intel syntax using &#8220;<span style="color: #ff0000;">set disassembly-flavor intel</span>&#8220;.</p>
<p>Here, is an example of main function dissasembled in Intel syntax:</p>
<pre>extr3me@w4rl0ck:gdb$ <span style="color: #ff9999;"><strong>gdb minimal</strong></span>
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
&lt;http://www.gnu.org/software/gdb/bugs/&gt;.
Find the GDB manual and other documentation resources online at:
&lt;http://www.gnu.org/software/gdb/documentation/&gt;.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from minimal...
&gt;&gt;&gt; <span style="color: #ff9999;"><strong>set disassembly-flavor intel</strong></span>
&gt;&gt;&gt; <span style="color: #ff9999;"><strong>disass /m main</strong></span>
Dump of assembler code for function main:
2 {
0x0000000000001129 &lt;+0&gt;: endbr64
0x000000000000112d &lt;+4&gt;: push rbp
0x000000000000112e &lt;+5&gt;: mov rbp,rsp

3 int i = 1337;
0x0000000000001131 &lt;+8&gt;: <span style="color: #ff0000;">mov DWORD PTR [rbp-0x4],0x539</span>

4 return 0;
0x0000000000001138 &lt;+15&gt;: mov eax,0x0

5 }
0x000000000000113d &lt;+20&gt;: pop rbp
0x000000000000113e &lt;+21&gt;: ret

End of assembler dump.
&gt;&gt;&gt;
</pre>
<p>Here, you should see subtle differences  such as the <span style="color: #e3abc7;">AT&amp;T syntax</span> displays &#8220;<span style="color: #e3abc7;">movl $0x539,-0x4(%rbp)</span>&#8221; while the same in <span style="color: #8fabff;">Intel syntax</span> is &#8220;<span style="color: #3366ff;">mov DWORD PTR [rbp-0x4],0x539</span>&#8220;.</p>
<p>Thats it for now. I will add more tutorials on GDB in the near future.</p>
<p><span style="color: #00ffff;"><strong>Happy Debugging!</strong></span></p>
<p><span style="color: #0000ff;"><strong>Sources/References/Credits:</strong></span></p>
<p>Below are all the credits/references/sources that made writing this blog post possible.</p>
<p>https://www.recurse.com/blog/5-learning-c-with-gdb &lt;- Credits to recurse. I used this as my primary resource to learn GDB and then post my understanding of GDB and C here. Do check them out.<br />
https://github.com/cyrus-and/gdb-dashboard &lt;- Creator of the Epic GDB dashboard<br />
https://www.tutorialspoint.com/gnu_debugger/ &lt;- Great place to start.<br />
https://sourceware.org/gdb/current/onlinedocs/gdb/Set-Breaks.html#Set-Breaks<br />
https://stackoverflow.com/questions/209534/how-to-highlight-and-color-gdb-output-during-interactive-debugging<br />
https://bob.cs.sonoma.edu/IntroCompOrg-RPi/sec-gdb1.html<br />
https://www.binaryhexconverter.com &lt;- Simple online convertor<br />
https://stackoverflow.com/questions/13280131/hexadecimal-to-decimal-in-shell-script<br />
https://www.ibm.com/developerworks/library/l-ia/index.html<br />
Credits to my m8 @bytesareana for decoding the stuff with the memory location.</p>
<p>The post <a href="https://techantidote.com/gdb-basics-with-c/">GDB basics with C</a> appeared first on <a href="https://techantidote.com">Tech Antidote</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techantidote.com/gdb-basics-with-c/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3391</post-id>	</item>
		<item>
		<title>Decoding IR Signals of a Blue Star Air Conditioner using an Arduino</title>
		<link>https://techantidote.com/decoding-ir-signals-of-a-blue-star-air-conditioner-using-an-arduino/</link>
					<comments>https://techantidote.com/decoding-ir-signals-of-a-blue-star-air-conditioner-using-an-arduino/#comments</comments>
		
		<dc:creator><![CDATA[]]></dc:creator>
		<pubDate>Sat, 18 Jul 2020 15:46:46 +0000</pubDate>
				<category><![CDATA[Electronics]]></category>
		<category><![CDATA[Reverse Engineering]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[ac]]></category>
		<category><![CDATA[air conditioner]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[arduino uno r3]]></category>
		<category><![CDATA[blue star]]></category>
		<category><![CDATA[diy]]></category>
		<category><![CDATA[electronics]]></category>
		<category><![CDATA[ir]]></category>
		<category><![CDATA[reverse engineering]]></category>
		<category><![CDATA[universal remote]]></category>
		<guid isPermaLink="false">https://techantidote.com/?p=3145</guid>

					<description><![CDATA[<p>This is part of my project to understand the IR protocol and try to decode IR signals that being send from my AC&#8217;s remote to the AC itself. So that the idea is: If I can figure out the different IR signals that being that transmitted, then I should be able to replay the traffic &#8230; <a href="https://techantidote.com/decoding-ir-signals-of-a-blue-star-air-conditioner-using-an-arduino/" class="more-link">Continue reading <span class="screen-reader-text">Decoding IR Signals of a Blue Star Air Conditioner using an Arduino</span> <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a href="https://techantidote.com/decoding-ir-signals-of-a-blue-star-air-conditioner-using-an-arduino/">Decoding IR Signals of a Blue Star Air Conditioner using an Arduino</a> appeared first on <a href="https://techantidote.com">Tech Antidote</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>This is part of my project to understand the IR protocol and try to decode IR signals that being send from my AC&#8217;s remote to the AC itself. So that the idea is: If I can figure out the different IR signals that being that transmitted, then I should be able to replay the traffic and control the Aircon from my PC or use my own custom IR hub instead of sending data to some third party &#8211; Alexa or Google Assistant. This may be applicable on other devices as well that use an IR transmitter/receiver like a TV/remote controlled fan/hubs.</p>
<p><span style="color: #ff0000;"><strong>Disclaimer:</strong></span> I am no means an electronics expert. This is just me experimenting and playing around trying to make sense of how IR works with no prior knowledge of how the protocol works. If you are electronics expert, then this post is probably not for you are better off pressing CTRL+W on your keyboard.</p>
<p><span style="color: #3366ff;"><strong>Requirements:</strong></span></p>
<ol>
<li>KY-022 Infrared Receiver (38kHz)</li>
<li>Arduino UNO U3</li>
<li>Breadboard</li>
<li>3x Male to Male Jumper Cables.</li>
<li>Arduino ID with IRremote library installed (<a href="http://z3t0.github.io/Arduino-IRremote/">Link</a>)</li>
<li>Computer to write your code. Duh!</li>
</ol>
<p>Here is a picture of the KY-22 IR receiver.</p>
<p><img loading="lazy" decoding="async" data-attachment-id="3314" data-permalink="https://techantidote.com/decoding-ir-signals-of-a-blue-star-air-conditioner-using-an-arduino/ky-022-front-2/" data-orig-file="https://techantidote.com/wp-content/uploads/2020/07/ky-022-front-1-scaled.jpg" data-orig-size="2560,2397" data-comments-opened="1" data-image-title="ky-022-front" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2020/07/ky-022-front-1-1024x959.jpg" class="alignnone size-large wp-image-3314" src="https://techantidote.com/wp-content/uploads/2020/07/ky-022-front-1-1024x959.jpg" alt="ky-022 IR receiever picture" width="980" height="918" srcset="https://techantidote.com/wp-content/uploads/2020/07/ky-022-front-1-1024x959.jpg 1024w, https://techantidote.com/wp-content/uploads/2020/07/ky-022-front-1-300x281.jpg 300w, https://techantidote.com/wp-content/uploads/2020/07/ky-022-front-1-768x719.jpg 768w, https://techantidote.com/wp-content/uploads/2020/07/ky-022-front-1-1536x1438.jpg 1536w, https://techantidote.com/wp-content/uploads/2020/07/ky-022-front-1-2048x1918.jpg 2048w, https://techantidote.com/wp-content/uploads/2020/07/ky-022-front-1-210x197.jpg 210w, https://techantidote.com/wp-content/uploads/2020/07/ky-022-front-1-1960x1835.jpg 1960w" sizes="auto, (max-width: 980px) 100vw, 980px" /></p>
<p><span style="color: #3366ff;"><strong>Circuit Connection:</strong></span></p>
<p>Connect pin labelled &#8220;<span style="color: #ff0000;">Y</span>&#8221; on the KY-022 to Pin <span style="color: #00ff00;">11</span> on the Arduino<br />
Connect pin labelled G to GND pin on the Arduino<br />
Connect pin labelled R pin to the 5V pin on Arduino</p>
<p>-Below is the source for the IR decoder:</p>
<pre>#include &lt;IRremote.h&gt;

<span style="color: #ff0000;">int RECV_PIN = <span style="color: #00ff00;">11</span></span>; // define input pin on Arduino.
<span style="color: #ff0000;">IRrecv irrecv(RECV_PIN);</span> 
decode_results results; // decode_results class is defined in IRremote.h

<span style="color: #ff0000;">IRsend irsend; </span>

void setup() { 
  Serial.begin(9600); // Set Serial monitor baud rate
 <span style="color: #ff0000;"> irrecv.enableIRIn();</span> // Start the receiver 
} 

void loop() {

  if (irrecv.decode(&amp;results)) {
  <span style="color: #ff0000;">  Serial.println(results.value, HEX);</span> 
    irrecv.resume(); // Receive the next value 
  }
 
  delay(1000);
  
}
</pre>
<p>Upload the code to the Arduino and then navigate to <strong>Tools&gt;Serial Monitor</strong>.</p>
<p>Now <strong>point</strong> your <strong>IR remote</strong> to the IR <strong>receiver sensor</strong> and press any key in the remote. <strong>Now, you should see the HEX value that was send from the remote in the serial console.</strong></p>
<p>For example, a test case I pointed my AC&#8217;s remote to the IR receiver(KY-022) and pressed the power button. This result printed on the Serial Monitor was HEX value 90900E0A. Note: While pressing the power On button, the remote had 25 degrees set.</p>
<p>Here is a sample screenshot of the setup.</p>
<p><img loading="lazy" decoding="async" data-attachment-id="3312" data-permalink="https://techantidote.com/decoding-ir-signals-of-a-blue-star-air-conditioner-using-an-arduino/send-and-decoded/" data-orig-file="https://techantidote.com/wp-content/uploads/2020/07/send-and-decoded.png" data-orig-size="1492,1024" data-comments-opened="1" data-image-title="send-and-decoded" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2020/07/send-and-decoded-1024x703.png" class="alignnone size-large wp-image-3312" src="https://techantidote.com/wp-content/uploads/2020/07/send-and-decoded-1024x703.png" alt="sending and decoding IR picture using KY-022" width="980" height="673" srcset="https://techantidote.com/wp-content/uploads/2020/07/send-and-decoded-1024x703.png 1024w, https://techantidote.com/wp-content/uploads/2020/07/send-and-decoded-300x206.png 300w, https://techantidote.com/wp-content/uploads/2020/07/send-and-decoded-768x527.png 768w, https://techantidote.com/wp-content/uploads/2020/07/send-and-decoded-210x144.png 210w, https://techantidote.com/wp-content/uploads/2020/07/send-and-decoded.png 1492w" sizes="auto, (max-width: 980px) 100vw, 980px" /></p>
<p>Here is a screenshot of the source and the data from the serial session.</p>
<p><img loading="lazy" decoding="async" data-attachment-id="3309" data-permalink="https://techantidote.com/decoding-ir-signals-of-a-blue-star-air-conditioner-using-an-arduino/arduino-console/" data-orig-file="https://techantidote.com/wp-content/uploads/2020/07/arduino-console.png" data-orig-size="1197,755" data-comments-opened="1" data-image-title="arduino-console" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2020/07/arduino-console-1024x646.png" class="alignnone wp-image-3309 size-medium" src="https://techantidote.com/wp-content/uploads/2020/07/arduino-console-300x189.png" alt="arduino code and serial monitor displaying decoded IR signal" width="300" height="189" srcset="https://techantidote.com/wp-content/uploads/2020/07/arduino-console-300x189.png 300w, https://techantidote.com/wp-content/uploads/2020/07/arduino-console-1024x646.png 1024w, https://techantidote.com/wp-content/uploads/2020/07/arduino-console-768x484.png 768w, https://techantidote.com/wp-content/uploads/2020/07/arduino-console-210x132.png 210w, https://techantidote.com/wp-content/uploads/2020/07/arduino-console.png 1197w" sizes="auto, (max-width: 300px) 100vw, 300px" /></p>
<p>Similarly when pressing the Power Off button in the return, a different IR code of <strong>80900C0A</strong> was send.</p>
<p>Hell Yeah!!!!! Ok, maybe I got excited bit too much.</p>
<p>I know this probably sounds dumb, but I did notice something interesting on how this is all put together. So, lets say the temperature of the AC in the remote is set to 16C. Now, if I press the &#8220;+&#8221; button to increase the temperature to 17C, then I see a different code and if I press it again again, I see a different code being send.</p>
<p>Here what the different hex codes for different temperatures look like(used by pressing the increment/+ sign on the remote):</p>
<pre>Temp 16: 90000E0A
Temp 17: 90800E0A
Temp 18: 90400E0A
Temp 19: 90C00E0A
Temp 20: 90200E0A
Temp 21: 90A00E0A
Temp 22: 90600E0A
Temp 23: 90E00E0A
Temp 24: 90100E0A
Temp 25: 90900E0A
Temp 26: 90500E0A
Temp 27: 90D00E0A
Temp 28: 90300E0A
Temp 29: 90B00E0A
Temp 30: 90700E0A
</pre>
<p><span style="color: #3366ff;"><strong>Trying to make sense of IR (Sort Of):</strong></span></p>
<p>So, in a programming point of view, I was expecting something like &#8220;new temperature&#8221; = &#8220;current temperature&#8221; + 1 and then set the value rite? Well this sort of the same with a slight difference which I guess is the way IR protocol works. So, the handheld IR remote has some logic in itself. <strong><span style="color: #3366ff;">Here is my assumption of what is happening</span>.</strong></p>
<ol>
<li>Say, the handheld IR remote for the Air Conditioner has current temperate as 16 degree Celsius (16C).</li>
<li>Now lets say the user, clicks the + sign on the AC remote (IR transmitter) to increase the temperature from 16C to 17C. The IR device knows the old value as 16 from its on-board memory and now knows the new temperate has to be set to 17C. So here, the arithmetic operation i.e. increment is done on the client side i.e on the IR remote itself.</li>
<li>The IR remote has a hard coded value of 17C somewhere in its code. The remote simply sends the IR signal with IR code for 17C. I.e. It will send 90800E0A to the IR Receiver.</li>
<li>The IR receiver (in this case the AC), receives the IR code for 17C and then sets the temperature to 17C. Here, the AC has hardcoded values for each IR code for each temperature in its code. Something like: If &#8220;<em>IR code received</em>&#8221; == &#8220;<em>90800E0A</em>&#8220;, then set temperature to 17C.</li>
<li>It looks more like each temperate value is hard coded and the IR remote has these values hard-coded.</li>
</ol>
<p>Next, I will need to learn how to get a IR transmitter and replay these codes so that I can control my Aircon and other electronics from my PC. Maybe create a DIY IR hub and hook it to my PC?</p>
<p>So, I did buy a IR transmitter to replay the signals but the transmitter that I received was <span style="color: #ff0000;">DOA</span>. <span style="color: #00ccff;">Well that went well. Haha!</span> <strong><br />
</strong></p>
<p><img loading="lazy" decoding="async" data-attachment-id="3317" data-permalink="https://techantidote.com/decoding-ir-signals-of-a-blue-star-air-conditioner-using-an-arduino/ir-transmitter/" data-orig-file="https://techantidote.com/wp-content/uploads/2020/07/ir-transmitter.jpg" data-orig-size="941,1969" data-comments-opened="1" data-image-title="ir-transmitter" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2020/07/ir-transmitter-489x1024.jpg" class="alignnone wp-image-3317 size-medium" src="https://techantidote.com/wp-content/uploads/2020/07/ir-transmitter-143x300.jpg" alt="ir transmitter picture" width="143" height="300" srcset="https://techantidote.com/wp-content/uploads/2020/07/ir-transmitter-143x300.jpg 143w, https://techantidote.com/wp-content/uploads/2020/07/ir-transmitter-489x1024.jpg 489w, https://techantidote.com/wp-content/uploads/2020/07/ir-transmitter-768x1607.jpg 768w, https://techantidote.com/wp-content/uploads/2020/07/ir-transmitter-734x1536.jpg 734w, https://techantidote.com/wp-content/uploads/2020/07/ir-transmitter-100x210.jpg 100w, https://techantidote.com/wp-content/uploads/2020/07/ir-transmitter.jpg 941w" sizes="auto, (max-width: 143px) 100vw, 143px" /></p>
<p>Anyways, will probably order a new transmitter to test this out post the whole COVID-19 shiZstorm.</p>
<p>Regards.</p>
<p><strong>ΞXΤЯ3МΞ</strong></p>
<p><span style="text-decoration: underline;"><strong>Sources/Credits/References:</strong></span></p>
<p>PS: Credits to Arduino modules for making it super simple to understand this. Below are credits/resources that helped out.</p>
<p><a href="https://arduinomodules.info/ky-022-infrared-receiver-module/">Arduino Modules.info</a><br />
<a href="https://github.com/Grensom/IR-Project">IR-Project</a><br />
<a href="https://duino4projects.com/arduino-ir-remote-control/">Duino4projects</a><br />
<a href="https://learn.sparkfun.com/tutorials/ir-communication/all">Sparkfun</a><br />
<a href="https://www.youtube.com/watch?v=muAkBQb24NI">Robojax</a></p>
<p>The post <a href="https://techantidote.com/decoding-ir-signals-of-a-blue-star-air-conditioner-using-an-arduino/">Decoding IR Signals of a Blue Star Air Conditioner using an Arduino</a> appeared first on <a href="https://techantidote.com">Tech Antidote</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techantidote.com/decoding-ir-signals-of-a-blue-star-air-conditioner-using-an-arduino/feed/</wfw:commentRss>
			<slash:comments>7</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3145</post-id>	</item>
		<item>
		<title>Reverse Engineering Router Firmware TP-Link TD-W8970</title>
		<link>https://techantidote.com/reverse-engineering-router-firmware-tp-link-td-w8970/</link>
					<comments>https://techantidote.com/reverse-engineering-router-firmware-tp-link-td-w8970/#respond</comments>
		
		<dc:creator><![CDATA[]]></dc:creator>
		<pubDate>Mon, 10 Jun 2019 01:04:03 +0000</pubDate>
				<category><![CDATA[Hacking]]></category>
		<category><![CDATA[Reverse Engineering]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[binwalk]]></category>
		<category><![CDATA[cracking]]></category>
		<category><![CDATA[dd]]></category>
		<category><![CDATA[reverse engineering]]></category>
		<category><![CDATA[router firmware]]></category>
		<category><![CDATA[squashfs]]></category>
		<category><![CDATA[TD-W8970]]></category>
		<category><![CDATA[tp-link]]></category>
		<category><![CDATA[tplink]]></category>
		<category><![CDATA[unsquashfs]]></category>
		<guid isPermaLink="false">https://techantidote.com/?p=2976</guid>

					<description><![CDATA[<p>This is a blog post on reverse engineering TP Link  TD-W8970v3 router firmware. Requirements: Router Firmware  [ TP-Link TD-W8970 v3 ] Linux Tools &#8211; binwalk, unsquashfs, dd, strings Optional: &#8220;John&#8221; a.k.a john the ripper (for Brute forcing passwords) Disclaimer: This is strictly for educational purposes ONLY and not be used for conducting any illegal activities. &#8230; <a href="https://techantidote.com/reverse-engineering-router-firmware-tp-link-td-w8970/" class="more-link">Continue reading <span class="screen-reader-text">Reverse Engineering Router Firmware TP-Link TD-W8970</span> <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a href="https://techantidote.com/reverse-engineering-router-firmware-tp-link-td-w8970/">Reverse Engineering Router Firmware TP-Link TD-W8970</a> appeared first on <a href="https://techantidote.com">Tech Antidote</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>This is a blog post on reverse engineering TP Link  TD-W8970v3 router firmware.</p>
<p><span style="color: #3366ff;" data-darkreader-inline-color=""><strong>Requirements:</strong></span></p>
<ul>
<li style="list-style-type: none;">
<ul>
<li style="list-style-type: none;">
<ul>
<li>Router Firmware  [ TP-Link TD-W8970 v3 ]</li>
<li>Linux Tools &#8211; binwalk, unsquashfs, dd, strings</li>
<li>Optional: &#8220;John&#8221; a.k.a john the ripper (for Brute forcing passwords)</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><span style="color: #3366ff;" data-darkreader-inline-color=""><strong>Disclaimer</strong></span>:</p>
<p>This is strictly for educational purposes ONLY and not be used for conducting any illegal activities. I hold no responsibility for misuse of this information.</p>
<p><span style="color: #3366ff;" data-darkreader-inline-color=""><strong>Download the firmware:</strong></span></p>
<p>First, we need to download the firmware that we need to reverse engineer. I am using the TP Link TD-W8970 v3 firmware.</p>
<p>To download the firmware, go to the below link. -Select &#8220;V3&#8221; as the version and click on &#8220;Firmware&#8221;.</p>
<p><a href="https://www.tp-link.com/us/support/download/td-w8970/">https://www.tp-link.com/us/support/download/td-w8970/</a></p>
<p><img loading="lazy" decoding="async" data-attachment-id="3024" data-permalink="https://techantidote.com/reverse-engineering-router-firmware-tp-link-td-w8970/1-blk/" data-orig-file="https://techantidote.com/wp-content/uploads/2019/06/1-blk.png" data-orig-size="1239,524" data-comments-opened="1" data-image-title="" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2019/06/1-blk-1024x433.png" class="alignnone size-full wp-image-3024" src="https://techantidote.com/wp-content/uploads/2019/06/1-blk.png" alt="TP Link TD-W8970 firmware download page" width="1239" height="524" srcset="https://techantidote.com/wp-content/uploads/2019/06/1-blk.png 1239w, https://techantidote.com/wp-content/uploads/2019/06/1-blk-300x127.png 300w, https://techantidote.com/wp-content/uploads/2019/06/1-blk-768x325.png 768w, https://techantidote.com/wp-content/uploads/2019/06/1-blk-1024x433.png 1024w, https://techantidote.com/wp-content/uploads/2019/06/1-blk-210x89.png 210w" sizes="auto, (max-width: 1239px) 100vw, 1239px" /></p>
<p>&nbsp;</p>
<p>Download the firmware.</p>
<p><img loading="lazy" decoding="async" data-attachment-id="3025" data-permalink="https://techantidote.com/reverse-engineering-router-firmware-tp-link-td-w8970/2-blk/" data-orig-file="https://techantidote.com/wp-content/uploads/2019/06/2-blk.png" data-orig-size="757,644" data-comments-opened="1" data-image-title="2-blk" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2019/06/2-blk.png" class="alignnone wp-image-3025 size-full" src="https://techantidote.com/wp-content/uploads/2019/06/2-blk.png" alt="TP Link TD-W8970 select latest firmware" width="757" height="644" srcset="https://techantidote.com/wp-content/uploads/2019/06/2-blk.png 757w, https://techantidote.com/wp-content/uploads/2019/06/2-blk-300x255.png 300w, https://techantidote.com/wp-content/uploads/2019/06/2-blk-210x179.png 210w" sizes="auto, (max-width: 757px) 100vw, 757px" /></p>
<p><span style="color: #3366ff;" data-darkreader-inline-color=""><strong>Time to dig around!!</strong></span></p>
<p>Copy the firmware to a new location and extract it.</p>
<pre>mkdir ~/firmware
cp ~/Downloads/TD-W8970_V3_150427.zip ~/firmware/
cd firmware/
unzip TD-W8970_V3_150427.zip
cd TD-W8970\(UN\)_V3_150427/</pre>
<p>So, here we can see the firmware upgrade image itself with the &#8220;.bin&#8221; extension along with the firmware upgrade guide.</p>
<pre>$ <span style="color: #ff0000;" data-darkreader-inline-color="">ls -l</span>
total 8296
-rw-rw-r-- 1 extr3me extr3me  317017 Dec 25  2013 'How to upgrade TP-LINK ADSL Modem Router - Copy.pdf'
-rw-rw-r-- 1 extr3me extr3me 8174304 Apr 27  2015 '<span style="color: #ff0000;" data-darkreader-inline-color="">TD-W8970v3_0.9.1_1.2_up_boot(150427)_2015-04-27_17.48.51.bin</span>'
extr3me@op3n TD-W8970(UN)_V3_150427 $</pre>
<p>For sanity purposes, I removed the firmware upgrade guide which I don&#8217;t need here.</p>
<pre>$ <span style="color: #ff0000;" data-darkreader-inline-color="">rm How\ to\ upgrade\ TP-LINK\ ADSL\ Modem\ Router\ -\ Copy.pdf</span></pre>
<p>Inspecting the binary with &#8220;file&#8221; command shows that it of type &#8220;data&#8221;.</p>
<pre>$ <span style="color: #ff0000;" data-darkreader-inline-color="">file TD-W8970v3_0.9.1_1.2_up_boot\(150427\)_2015-04-27_17.48.51.bin</span>
TD-W8970v3_0.9.1_1.2_up_boot(150427)_2015-04-27_17.48.51.bin: <span style="color: #ff0000;" data-darkreader-inline-color="">data</span></pre>
<p>Below is a screenshot:</p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" data-attachment-id="3026" data-permalink="https://techantidote.com/reverse-engineering-router-firmware-tp-link-td-w8970/file/" data-orig-file="https://techantidote.com/wp-content/uploads/2019/06/file.png" data-orig-size="864,49" data-comments-opened="1" data-image-title="file" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2019/06/file.png" class="alignnone size-full wp-image-3026" src="https://techantidote.com/wp-content/uploads/2019/06/file.png" alt="checking the binary using &quot;file&quot; command" width="864" height="49" srcset="https://techantidote.com/wp-content/uploads/2019/06/file.png 864w, https://techantidote.com/wp-content/uploads/2019/06/file-300x17.png 300w, https://techantidote.com/wp-content/uploads/2019/06/file-768x44.png 768w, https://techantidote.com/wp-content/uploads/2019/06/file-210x12.png 210w" sizes="auto, (max-width: 864px) 100vw, 864px" /></p>
<p>I tried to run hexdump and filter out some data but did not get any useful info here yet.</p>
<pre><span style="color: #ff0000;" data-darkreader-inline-color=""><span style="color: #ffffff;" data-darkreader-inline-color="">$</span> hexdump -C TD-W8970v3_0.9.1_1.2_up_boot\(150427\)_2015-04-27_17.48.51.bin | head -10</span>
00000000  03 00 00 00 76 65 72 2e  20 32 2e 30 00 ff ff ff  |....ver. 2.0....|
00000010  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
*
00000030  ff ff ff ff 08 97 00 03  00 00 00 35 00 00 00 00  |...........5....|
00000040  9c e8 56 2f 7d cd f2 5a  80 92 27 b5 dd 23 66 ea  |..V/}..Z..'..#f.|
00000050  00 00 00 00 ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00000060  ff ff ff ff ff ff ff ff  80 01 00 00 80 2e 00 20  |............... |
00000070  00 7e 02 00 00 00 02 00  00 13 d6 d3 00 13 d6 e0  |.~..............|
00000080  00 67 e0 00 00 00 00 00  00 00 e3 90 55 aa 01 02  |.g..........U...|
00000090  a5 00 09 01 55 d1 51 bb  ff ff ff ff ff ff ff ff  |....U.Q.........|
</pre>
<p>Now, use the tools &#8220;strings&#8221; to print out all the human readable strings in the binary. Here, I filtered the 1st 10 lines.</p>
<p>-The output does show two strings that seemed interesting.</p>
<pre>$ <span style="color: #ff0000;" data-darkreader-inline-color="">strings TD-W8970v3_0.9.1_1.2_up_boot\(150427\)_2015-04-27_17.48.51.bin | head -10</span>
ver. 2.0
79!8
@ !&lt;
6H!$
<span style="color: #ffffff;" data-darkreader-inline-color=""><strong><span style="color: #ff0000;" data-darkreader-inline-color="">cfe-v</span></strong>
e=192.168.1.1:ffffff00 h=192.168.1.100 g= r=f f=vmlinux <strong>i=<span style="color: #ff0000;" data-darkreader-inline-color="">bcm963xx_fs_kernel</span></strong> d=1 p=0</span>
96361I2
  c
!P%@
!H$5)</pre>
<p>A bit off topic. But out of curiosity, I searched online for &#8220;<span style="color: #ff0000;" data-darkreader-inline-color="">bcm963xx_fs_kernel</span>&#8221; and came across a PDF document &#8220;Broadcom BCM963xx CFE Boot Loader and Flash Memory Structure Application Notes&#8221;&#8216; from Jan 2006.</p>
<p><img loading="lazy" decoding="async" data-attachment-id="3031" data-permalink="https://techantidote.com/reverse-engineering-router-firmware-tp-link-td-w8970/broadcom-search-result/" data-orig-file="https://techantidote.com/wp-content/uploads/2019/06/broadcom-search-result.png" data-orig-size="1795,595" data-comments-opened="1" data-image-title="broadcom-search-result" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2019/06/broadcom-search-result-1024x339.png" class="alignnone size-full wp-image-3031" src="https://techantidote.com/wp-content/uploads/2019/06/broadcom-search-result.png" alt="search result" width="1795" height="595" srcset="https://techantidote.com/wp-content/uploads/2019/06/broadcom-search-result.png 1795w, https://techantidote.com/wp-content/uploads/2019/06/broadcom-search-result-300x99.png 300w, https://techantidote.com/wp-content/uploads/2019/06/broadcom-search-result-768x255.png 768w, https://techantidote.com/wp-content/uploads/2019/06/broadcom-search-result-1024x339.png 1024w, https://techantidote.com/wp-content/uploads/2019/06/broadcom-search-result-210x70.png 210w, https://techantidote.com/wp-content/uploads/2019/06/broadcom-search-result-1536x509.png 1536w" sizes="auto, (max-width: 1795px) 100vw, 1795px" /></p>
<p>Here is a screenshot of the document itself:</p>
<p><img loading="lazy" decoding="async" data-attachment-id="3027" data-permalink="https://techantidote.com/reverse-engineering-router-firmware-tp-link-td-w8970/broadcom-blk/" data-orig-file="https://techantidote.com/wp-content/uploads/2019/06/broadcom-blk.png" data-orig-size="1156,815" data-comments-opened="1" data-image-title="broadcom-blk" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2019/06/broadcom-blk-1024x722.png" class="alignnone size-full wp-image-3027" src="https://techantidote.com/wp-content/uploads/2019/06/broadcom-blk.png" alt="broadcom CFE PDF document" width="1156" height="815" srcset="https://techantidote.com/wp-content/uploads/2019/06/broadcom-blk.png 1156w, https://techantidote.com/wp-content/uploads/2019/06/broadcom-blk-300x212.png 300w, https://techantidote.com/wp-content/uploads/2019/06/broadcom-blk-768x541.png 768w, https://techantidote.com/wp-content/uploads/2019/06/broadcom-blk-1024x722.png 1024w, https://techantidote.com/wp-content/uploads/2019/06/broadcom-blk-210x148.png 210w" sizes="auto, (max-width: 1156px) 100vw, 1156px" /></p>
<p>So, it looks like the router firmware has &#8220;Broadcom&#8221; and we now know the bootloader and we can get more information from the PDF<strong>.</strong></p>
<p><strong>Here is a link to the document</strong>: <a href="https://raw.githubusercontent.com/Noltari/cfe_bcm63xx/master/bcm963xx_bootloader_appnote.pdf" target="_blank" rel="noopener noreferrer">Broadcom CFE Link</a></p>
<pre><span style="color: #ff0000;" data-darkreader-inline-color="">cfe<span style="color: #ffffff;" data-darkreader-inline-color="">-v</span></span>
e=192.168.1.1:ffffff00 h=192.168.1.100 g= r=f f=vmlinux <strong>i=bcm963xx_fs_kernel</strong> d=1 p=0
</pre>
<p>The strings output also shows &#8220;<span style="color: #ff0000;" data-darkreader-inline-color="">cfe-v</span>&#8221; which looks to &#8220;Common Firmware Environment&#8221;.</p>
<p>It also has information on the CFE bootloader flash memory:</p>
<p><img loading="lazy" decoding="async" data-attachment-id="3028" data-permalink="https://techantidote.com/reverse-engineering-router-firmware-tp-link-td-w8970/flash-mem-blk/" data-orig-file="https://techantidote.com/wp-content/uploads/2019/06/flash-mem-blk.png" data-orig-size="713,783" data-comments-opened="1" data-image-title="flash-mem-blk" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2019/06/flash-mem-blk.png" class="alignnone size-full wp-image-3028" src="https://techantidote.com/wp-content/uploads/2019/06/flash-mem-blk.png" alt="Broadcom bootloader flash memory architecture" width="713" height="783" srcset="https://techantidote.com/wp-content/uploads/2019/06/flash-mem-blk.png 713w, https://techantidote.com/wp-content/uploads/2019/06/flash-mem-blk-273x300.png 273w, https://techantidote.com/wp-content/uploads/2019/06/flash-mem-blk-191x210.png 191w" sizes="auto, (max-width: 713px) 100vw, 713px" /></p>
<p>In Page 5 of the Broadcom documentation, it does refer to the TFTP flashing method and could see some similarties from the output of the &#8220;strings&#8221; command.</p>
<p><img loading="lazy" decoding="async" data-attachment-id="3029" data-permalink="https://techantidote.com/reverse-engineering-router-firmware-tp-link-td-w8970/minicom-black/" data-orig-file="https://techantidote.com/wp-content/uploads/2019/06/minicom-black.png" data-orig-size="761,174" data-comments-opened="1" data-image-title="minicom-black" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2019/06/minicom-black.png" class="alignnone size-full wp-image-3029" src="https://techantidote.com/wp-content/uploads/2019/06/minicom-black.png" alt="minicom update via TFTP" width="761" height="174" srcset="https://techantidote.com/wp-content/uploads/2019/06/minicom-black.png 761w, https://techantidote.com/wp-content/uploads/2019/06/minicom-black-300x69.png 300w, https://techantidote.com/wp-content/uploads/2019/06/minicom-black-210x48.png 210w" sizes="auto, (max-width: 761px) 100vw, 761px" /></p>
<p>T his is what I could come up so far, comparing the screenshot and the below line:</p>
<p>&#8220;<span style="color: #ff0000;" data-darkreader-inline-color="">e=192.168.1.1:ffffff00 h=192.168.1.100 g= r=f f=vmlinux <strong>i=bcm963xx_fs_kernel</strong> d=1 p=0</span>&#8220;,</p>
<pre><span style="color: #ff0000;" data-darkreader-inline-color="">e</span>=192.168.1.1:ffffff00     <span style="color: #ff0000;" data-darkreader-inline-color="">=&gt;</span> "192.168.1.1" =&gt; Board IP. I assume "ffffff00" =&gt; subnet mask 255.255.255.0 or CIDR.
<span style="color: #ff0000;" data-darkreader-inline-color="">h</span>=192.168.1.100            <span style="color: #ff0000;" data-darkreader-inline-color="">=&gt;</span> "192.168.1.100" =&gt; Host IP
<span style="color: #ff0000;" data-darkreader-inline-color="">g</span>=                         <span style="color: #ff0000;" data-darkreader-inline-color="">=&gt;</span> "empty" =&gt; Gateway IP
<span style="color: #ff0000;" data-darkreader-inline-color="">r</span>=f                        <span style="color: #ff0000;" data-darkreader-inline-color="">=&gt;</span> "f" =&gt; Run from Flash
<span style="color: #ff0000;" data-darkreader-inline-color="">f</span>=vmlinux                  <span style="color: #ff0000;" data-darkreader-inline-color="">=&gt;</span> "vmlinux" | Default run hostfilename is "vmlinux"
<span style="color: #ff0000;" data-darkreader-inline-color="">i</span>=bcm963xx_fs_kernel       <span style="color: #ff0000;" data-darkreader-inline-color="">=&gt;</span> "bcm963xx_fs_kernel" | Default flash filename is "bcm963xx_fs_kernel"
<span style="color: #ff0000;" data-darkreader-inline-color="">d</span>=1                        <span style="color: #ff0000;" data-darkreader-inline-color="">=&gt; "</span>1" | Delay = 1
<span style="color: #ff0000;" data-darkreader-inline-color="">p</span>=0                        <span style="color: #ff0000;" data-darkreader-inline-color="">=&gt;</span> "0" | [I have no idea what this is.]
</pre>
<p>So, the e, h, g, r, f, i, d and p seems to be variables that would be used during the flashing procedure via minicom to emulate a serial device.</p>
<p>If you would like to dig deeper, you could read about CPE by clicking <a href="https://en.wikipedia.org/wiki/Common_Firmware_Environment" target="_blank" rel="noopener noreferrer">here</a>.</p>
<p><span style="color: #3366ff;" data-darkreader-inline-color=""><strong>Extracting Router Filesystem:</strong></span></p>
<p>To check what is in the router firmware binary, I have used &#8220;<span style="color: #ff0000;" data-darkreader-inline-color="">binwalk</span>&#8220;. Below is output of binwalk:</p>
<pre>$ <span style="color: #ff0000;" data-darkreader-inline-color="">binwalk TD-W8970v3_0.9.1_1.2_up_boot\(150427\)_2015-04-27_17.48.51.bin
</span>
DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
13300         0x33F4          LZMA compressed data, properties: 0x6D, dictionary size: 4194304 bytes, uncompressed size: 220576 bytes
66572         0x1040C         LZMA compressed data, properties: 0x6D, dictionary size: 4194304 bytes, uncompressed size: 3876096 bytes
151315        0x24F13         MySQL MISAM index file Version 4
<span style="color: #ff0000;" data-darkreader-inline-color="">1366752</span>       0x14DAE0        <span style="color: #ff0000;" data-darkreader-inline-color="">Squashfs filesystem</span>, little endian, non-standard signature, version 4.0, compression:gzip, size: 6806057 bytes, 594 inodes, blocksize: 65536 bytes, created: 2015-04-27 09:45:54</pre>
<p><strong><span style="color: #3366ff;" data-darkreader-inline-color="">The Ahaaa moment!</span></strong></p>
<p>So, here we see this binary contains a squashfs filesystem starting at decimal &#8220;1366752&#8221;.</p>
<p><img loading="lazy" decoding="async" data-attachment-id="3030" data-permalink="https://techantidote.com/reverse-engineering-router-firmware-tp-link-td-w8970/binwalk-squashfs/" data-orig-file="https://techantidote.com/wp-content/uploads/2019/06/binwalk-squashfs.png" data-orig-size="1689,138" data-comments-opened="1" data-image-title="binwalk-squashfs" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2019/06/binwalk-squashfs-1024x84.png" class="alignnone size-full wp-image-3030" src="https://techantidote.com/wp-content/uploads/2019/06/binwalk-squashfs.png" alt="binwalk binary squashfs" width="1689" height="138" srcset="https://techantidote.com/wp-content/uploads/2019/06/binwalk-squashfs.png 1689w, https://techantidote.com/wp-content/uploads/2019/06/binwalk-squashfs-300x25.png 300w, https://techantidote.com/wp-content/uploads/2019/06/binwalk-squashfs-768x63.png 768w, https://techantidote.com/wp-content/uploads/2019/06/binwalk-squashfs-1024x84.png 1024w, https://techantidote.com/wp-content/uploads/2019/06/binwalk-squashfs-210x17.png 210w, https://techantidote.com/wp-content/uploads/2019/06/binwalk-squashfs-1536x125.png 1536w" sizes="auto, (max-width: 1689px) 100vw, 1689px" /></p>
<p>We can extract the squashfs filesystem alone from the firmware binary using the &#8220;<span style="color: #ff0000;" data-darkreader-inline-color="">dd</span>&#8221; command by skiping up to &#8220;1366752&#8221; using the &#8220;skip&#8221; flag.</p>
<pre>$ <span style="color: #ff0000;" data-darkreader-inline-color="">dd if=TD-W8970v3_0.9.1_1.2_up_boot\(150427\)_2015-04-27_17.48.51.bin skip=1366752 bs=1 of=router-fs.squashfs</span>
6807552+0 records in
6807552+0 records out
6807552 bytes (6.8 MB, 6.5 MiB) copied, 8.81061 s, 773 kB/s
</pre>
<p>Here, we are providing the input file as the router firmware, setting the block size to &#8220;1&#8221; and getting data from &#8220;1366752&#8221; to the end of the binary and storing it to a file &#8220;router-fs.squashfs&#8221;. Here, if we dont specify the &#8220;bs&#8221; the copy would most likely fail.</p>
<p>So, the squashfs file system is about <strong>~6.5MB</strong> in size compressed.</p>
<pre>$ <span style="color: #ff0000;" data-darkreader-inline-color="">ls -lh</span>
total 15M
-rw-rw-r-- 1 extr3me extr3me <span style="color: #ff0000;" data-darkreader-inline-color="">6.5M</span> Jun 10 00:05  <span style="color: #ff0000;" data-darkreader-inline-color="">router-fs.squashfs</span>
-rw-rw-r-- 1 extr3me extr3me 7.8M Apr 27  2015 'TD-W8970v3_0.9.1_1.2_up_boot(150427)_2015-04-27_17.48.51.bin'</pre>
<p><img loading="lazy" decoding="async" data-attachment-id="3032" data-permalink="https://techantidote.com/reverse-engineering-router-firmware-tp-link-td-w8970/dd-firmware-extract/" data-orig-file="https://techantidote.com/wp-content/uploads/2019/06/dd-firmware-extract.png" data-orig-size="1668,343" data-comments-opened="1" data-image-title="dd firmware extract" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2019/06/dd-firmware-extract-1024x211.png" class="alignnone size-full wp-image-3032" src="https://techantidote.com/wp-content/uploads/2019/06/dd-firmware-extract.png" alt="Screenshot: dd to extract router filesystem" width="1668" height="343" srcset="https://techantidote.com/wp-content/uploads/2019/06/dd-firmware-extract.png 1668w, https://techantidote.com/wp-content/uploads/2019/06/dd-firmware-extract-300x62.png 300w, https://techantidote.com/wp-content/uploads/2019/06/dd-firmware-extract-768x158.png 768w, https://techantidote.com/wp-content/uploads/2019/06/dd-firmware-extract-1024x211.png 1024w, https://techantidote.com/wp-content/uploads/2019/06/dd-firmware-extract-210x43.png 210w, https://techantidote.com/wp-content/uploads/2019/06/dd-firmware-extract-1536x316.png 1536w" sizes="auto, (max-width: 1668px) 100vw, 1668px" /></p>
<p>&#8211; Now, Checking the file shows as &#8220;data&#8221;.</p>
<pre>$ <span style="color: #ff0000;" data-darkreader-inline-color="">file router-fs.squashfs
</span>router-fs.squashfs: <span style="color: #ff0000;" data-darkreader-inline-color="">data
</span></pre>
<ul>
<li>For verification, check the unpacked binary &#8220;router-fs.squashfs&#8221; and it does show as &#8220;squashfs filesystem&#8221;.</li>
</ul>
<pre>$ <span style="color: #ff0000;" data-darkreader-inline-color="">binwalk router-fs.squashfs</span>

DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
<span style="color: #ff0000;" data-darkreader-inline-color="">0</span>             0x0             <span style="color: #ff0000;" data-darkreader-inline-color="">Squashfs filesystem</span>, little endian, non-standard signature, version 4.0, compression:gzip, size: 6806057 bytes, 594 inodes, blocksize: 65536 bytes, created: 2015-04-27 09:45:54</pre>
<p>Now, we can extract the filesystem using &#8220;unsquashfs&#8221;.</p>
<pre>$ <span style="color: #ff0000;" data-darkreader-inline-color="">unsquashfs router-fs.squashfs</span></pre>
<p>-You may see some error such as below:</p>
<p><em>&#8220;create_inode: could not create character device squashfs-root/dev/bcmadsl0, because you&#8217;re not superuser!&#8221;</em></p>
<p><strong>These logs can be safely ignored</strong>. Here is the output for reference:</p>
<pre>$ <span style="color: #ff0000;" data-darkreader-inline-color="">unsquashfs router-fs.squashfs</span> 
[130/91284]
Parallel unsquashfs: Using 8 processors
546 inodes (811 blocks) to write
create_inode: could not create character device squashfs-root/dev/bcmadsl0, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/bcmarl, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/bcmfap, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/bcmvlan, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/bcmxtmcfg0, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/bpm, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/brcmboard, because you're not superuser!
create_inode: could not create block device squashfs-root/dev/caldata, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/console, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/dk0, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/fcache, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/flash0, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/gmac, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/gpio, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/gpio1, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ingqos, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/led, because you're not superuser!
create_inode: could not create block device squashfs-root/dev/mtd, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/mtd0, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/mtd1, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/mtd2, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/mtd3, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/mtd4, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/mtd5, because you're not superuser!
create_inode: could not create block device squashfs-root/dev/mtdblock0, because you're not superuser!
create_inode: could not create block device squashfs-root/dev/mtdblock1, because you're not superuser!
create_inode: could not create block device squashfs-root/dev/mtdblock2, because you're not superuser!
create_inode: could not create block device squashfs-root/dev/mtdblock3, because you're not superuser!
create_inode: could not create block device squashfs-root/dev/mtdblock4, because you're not superuser!
create_inode: could not create block device squashfs-root/dev/mtdblock5, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/net/tun, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/null, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/pmap, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ppp, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/pppox_iptables, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ptmx, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ptyp0, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ptyp1, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ptyp2, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/pwrmngt, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/qostype, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/random, because you're not superuser!
create_inode: could not create block device squashfs-root/dev/sda, because you're not superuser!
create_inode: could not create block device squashfs-root/dev/sda1, because you're not superuser!
create_inode: could not create block device squashfs-root/dev/sda2, because you're not superuser!
create_inode: could not create block device squashfs-root/dev/sdb, because you're not superuser!
create_inode: could not create block device squashfs-root/dev/sdb1, because you're not superuser!
create_inode: could not create block device squashfs-root/dev/sdb2, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/tty, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/tty0, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyACM0, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyACM1, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyACM10, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyACM11, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyACM12, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyACM13, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyACM14, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyACM15, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyACM2, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyACM3, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyACM4, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyACM5, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyACM6, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyACM7, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyACM8, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyACM9, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyS0, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyUSB0, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyUSB1, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyUSB10, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyUSB11, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyUSB12, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyUSB13, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyUSB14, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyUSB15, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyUSB2, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyUSB3, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyUSB4, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyUSB5, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyUSB6, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyUSB7, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyUSB8, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyUSB9, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyp0, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyp1, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/ttyp2, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/urandom, because you're not superuser!
create_inode: could not create character device squashfs-root/dev/watchdog, because you're not superuser!

create_inode: could not create character device squashfs-root/dev/zero, because you're not superuser!
[====================================================================================================================================================================================================| ] 722/811 89%

<span style="color: #ff0000;" data-darkreader-inline-color="">created 370 files
created 48 directories
created 87 symlinks
created 0 devices
created 0 fifos</span></pre>
<p>-Now, we should see a <span style="color: #ff0000;" data-darkreader-inline-color="">folder</span> named &#8220;<span style="color: #ff0000;" data-darkreader-inline-color="">squashfs-root</span>&#8221;</p>
<pre>$ <span style="color: #ff0000;" data-darkreader-inline-color="">ls -l</span>
total 14636
-rw-rw-r--  1 extr3me extr3me 6807552 Jun 10 00:05  router-fs.squashfs
<span style="color: #ff0000;" data-darkreader-inline-color="">d</span>rwxrwxr-x 13 extr3me extr3me    4096 Apr 27  2015  <span style="color: #ff0000;" data-darkreader-inline-color="">squashfs-root</span>
-rw-rw-r--  1 extr3me extr3me 8174304 Apr 27  2015 'TD-W8970v3_0.9.1_1.2_up_boot(150427)_2015-04-27_17.48.51.bin'</pre>
<p>Now, change into the folder &#8221;</p>
<pre>$ <span style="color: #ff0000;" data-darkreader-inline-color="">cd squashfs-root/</span></pre>
<p>-Listing the files shows that we have the router&#8217;s filesystem.</p>
<pre>$ <span style="color: #ff0000;" data-darkreader-inline-color="">ls -a</span>
.  ..  bin  dev  etc  lib  linuxrc  mnt  proc  sbin  sys  tmp  usr  var  web</pre>
<p>-To value a view of the filesystem in a tree like format, I have used the command &#8220;tree&#8221; and piping it to less:</p>
<pre>$ <span style="color: #ff0000;" data-darkreader-inline-color="">tree -C | less -R</span></pre>
<p><span style="color: #3366ff;" data-darkreader-inline-color=""><strong>Guessing the Linux Kernel version:</strong></span></p>
<p>-Checking the files with &#8220;.ko&#8221; extension for keyword &#8220;vermagic&#8221; would be the best guess to the Linux kernel version.</p>
<pre>$ <span style="color: #ff0000;" data-darkreader-inline-color="">strings ./lib/modules/NetUSB.ko | grep vermagic</span>
vermagic=<span style="color: #ff0000;" data-darkreader-inline-color="">2.6.30</span> SMP preempt mod_unload MIPS32_R1 32BIT</pre>
<p>Now, that I know the kernel version I could then check for exploits that affect this kernel version.</p>
<p>For example: Check for CVEs that affect this version. [<a href="https://www.cvedetails.com/version/81666/Linux-Linux-Kernel-2.6.30.html" target="_blank" rel="noopener noreferrer">Link]</a></p>
<p><strong><span style="color: #3366ff;" data-darkreader-inline-color="">Some random things worth checking:</span><br />
</strong></p>
<p>-The list of libraries installed can be found under:</p>
<pre>$ <span style="color: #ff0000;" data-darkreader-inline-color="">tree -C lib/ | less -R</span></pre>
<p>&#8211; So, the chrooted environment were the user inputs commands seems to be via a binary &#8220;usr/bin/cli&#8221; or could be translating the user commands to the actual deamon.</p>
<pre>$ <span style="color: #ff0000;" data-darkreader-inline-color="">strings ./usr/bin/cli | less</span></pre>
<p>-Btw, I found a blooper while checking this binary. Searching this binary, I did find a few misspelled words. <strong>Example</strong>: &#8220;<strong>histroy</strong>&#8221; instead of &#8220;history&#8221; <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f61b.png" alt="😛" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<pre>serial
wan2lan
start
exit
clear
clear screen
enter config mode
enable
enter privilege mode
leave to the privious mode
help
help info
history
show <span style="color: #ff0000;" data-darkreader-inline-color="">histroy</span> commands</pre>
<p><span style="color: #3366ff;" data-darkreader-inline-color=""><strong>Guessing GCC Version:</strong></span></p>
<pre>$ <span style="color: #ff0000;" data-darkreader-inline-color="">strings ./usr/bin/cli | grep GCC</span>
GCC: (GNU) 3.3.2
GCC: (Buildroot 2010.02-git) 4.4.2
</pre>
<p>&nbsp;</p>
<p><span style="color: #3366ff;" data-darkreader-inline-color=""><strong>Cracking password from the router&#8217;s filesystem:</strong></span></p>
<p>-Looking at the filesystem, I could see a file &#8220;passswd.bak&#8221; under etc/ directory.</p>
<p><img loading="lazy" decoding="async" data-attachment-id="3033" data-permalink="https://techantidote.com/reverse-engineering-router-firmware-tp-link-td-w8970/tree-passwd/" data-orig-file="https://techantidote.com/wp-content/uploads/2019/06/tree-passwd.png" data-orig-size="638,707" data-comments-opened="1" data-image-title="tree-passwd" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2019/06/tree-passwd.png" class="alignnone size-full wp-image-3033" src="https://techantidote.com/wp-content/uploads/2019/06/tree-passwd.png" alt="" width="638" height="707" srcset="https://techantidote.com/wp-content/uploads/2019/06/tree-passwd.png 638w, https://techantidote.com/wp-content/uploads/2019/06/tree-passwd-271x300.png 271w, https://techantidote.com/wp-content/uploads/2019/06/tree-passwd-190x210.png 190w" sizes="auto, (max-width: 638px) 100vw, 638px" /></p>
<p>-Reading the file shows there are two users with has shell access.</p>
<pre>$ <span style="color: #ff0000;" data-darkreader-inline-color="">cat etc/passwd.bak
<strong><span style="color: #339966;" data-darkreader-inline-color="">admin</span></strong></span>:<strong><span style="color: #ff9900;" data-darkreader-inline-color="">$1$$iC.dUsGpxNNJGeOm1dFio/</span></strong>:0:0:root:/:<span style="color: #ff00ff;" data-darkreader-inline-color=""><strong>/bin/sh</strong></span>
<strong><span style="color: #339966;" data-darkreader-inline-color="">nobody</span></strong>:*:0:0:nobody:/:<span style="color: #ff00ff;" data-darkreader-inline-color=""><strong>/bin/sh</strong></span></pre>
<p>Here:</p>
<p><span style="color: #339966;" data-darkreader-inline-color="">      Username</span> indicated in <span style="color: #339966;" data-darkreader-inline-color="">green.</span><br />
<span style="color: #ff9900;" data-darkreader-inline-color="">      Hashed password</span> indicated in <span style="color: #ff9900;" data-darkreader-inline-color="">orange.</span><br />
Assigned <span style="color: #ff00ff;" data-darkreader-inline-color="">shell</span> indicated in <span style="color: #ff00ff;" data-darkreader-inline-color="">pink</span>.</p>
<p>This looks to be the &#8220;shadow&#8221; file usually located under etc/shadow which my assumption is to be copied to etc/shadow during upgrade.</p>
<p><strong>Here is something wierd.</strong> Why does user &#8220;<strong>nobody</strong>&#8221; has <strong>/bin/bash</strong> shell ? <span style="color: #ff6600;" data-darkreader-inline-color=""><strong>Manufacturer backdoor??</strong><strong>Mmmmm&#8230;.! </strong></span><strong>We will come to that later.</strong><span style="color: #ff6600;" data-darkreader-inline-color=""><strong><br />
</strong></span></p>
<p><span style="color: #3366ff;" data-darkreader-inline-color=""><strong>Messing around  &#8211; Cracking passwords:</strong></span></p>
<p>Before cracking the password, lets understand something about the hash+salt. Here, the &#8220;$&#8221; signs are special</p>
<p><img loading="lazy" decoding="async" data-attachment-id="3034" data-permalink="https://techantidote.com/reverse-engineering-router-firmware-tp-link-td-w8970/pass/" data-orig-file="https://techantidote.com/wp-content/uploads/2019/06/pass.png" data-orig-size="452,142" data-comments-opened="1" data-image-title="pass" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2019/06/pass.png" class="alignnone size-full wp-image-3034" src="https://techantidote.com/wp-content/uploads/2019/06/pass.png" alt="passwd.bak file" width="452" height="142" srcset="https://techantidote.com/wp-content/uploads/2019/06/pass.png 452w, https://techantidote.com/wp-content/uploads/2019/06/pass-300x94.png 300w, https://techantidote.com/wp-content/uploads/2019/06/pass-210x66.png 210w" sizes="auto, (max-width: 452px) 100vw, 452px" /></p>
<p><strong>admin</strong>:<span style="color: #ff6600;" data-darkreader-inline-color=""><strong>$1$$iC.dUsGpxNNJGeOm1dFio/</strong></span>:0:0:root:/:<strong>/bin/sh</strong></p>
<p>The string that we require is the following:</p>
<p><span style="color: #ff6600;" data-darkreader-inline-color="">$1</span><span style="color: #00ffff;" data-darkreader-inline-color="">$</span><span style="color: #ff00ff;" data-darkreader-inline-color="">$iC.dUsGpxNNJGeOm1dFio/</span></p>
<p>The string is specificied in the following format:</p>
<pre><span style="color: #ff6600;" data-darkreader-inline-color="">$id</span><span style="color: #00ffff;" data-darkreader-inline-color="">$salt</span><span style="color: #ff00ff;" data-darkreader-inline-color="">$encrypted</span></pre>
<p><strong><span style="color: #ff6600;" data-darkreader-inline-color="">$1</span></strong> =&gt; Indicates that  <span style="color: #ff6600;" data-darkreader-inline-color=""><strong>MD5 </strong><span style="color: #808080;" data-darkreader-inline-color="">is used to create the hash the password. Below is a table of the list of possible values for the 1st section.<br />
</span></span></p>
<pre>--------------------
| 1  | MD5         |
--------------------
| 2  | Blowfish    |
--------------------
| 2a | eksBlowfish |
--------------------
| 5  | SHA-256     |
--------------------
| 6  | SHA-512     |
--------------------
</pre>
<p>For testing purposes, I was able to crack the password with <strong>John the ripper</strong>.</p>
<p>If the passwords had complex salts + hashing methods, for ex SHA-512 it may take longer than expected. [Again, this is for educational purposes ONLY!]</p>
<pre>$ <span style="color: #ff0000;" data-darkreader-inline-color="">john etc/passwd.bak
Loaded 1 password hash</span> (md5crypt [<span style="color: #ff0000;" data-darkreader-inline-color="">MD5</span> 32/64 X2])
No password hashes left to crack (see FAQ)</pre>
<p>&#8211; To view the cracked password, you could use the &#8211;show flag along with the input file etc/passwd.bak</p>
<pre>$ <span style="color: #ff0000;" data-darkreader-inline-color="">john --show etc/passwd.bak</span>
admin:<span style="color: #ff0000;" data-darkreader-inline-color="">1234</span>:0:0:root:/:/bin/sh
1 password hash cracked, 0 left</pre>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" data-attachment-id="3035" data-permalink="https://techantidote.com/reverse-engineering-router-firmware-tp-link-td-w8970/cracked-password/" data-orig-file="https://techantidote.com/wp-content/uploads/2019/06/cracked-password.png" data-orig-size="514,235" data-comments-opened="1" data-image-title="cracked password" data-image-description="" data-image-caption="" data-large-file="https://techantidote.com/wp-content/uploads/2019/06/cracked-password.png" class="alignnone size-full wp-image-3035" src="https://techantidote.com/wp-content/uploads/2019/06/cracked-password.png" alt="cracked password using john the ripper" width="514" height="235" srcset="https://techantidote.com/wp-content/uploads/2019/06/cracked-password.png 514w, https://techantidote.com/wp-content/uploads/2019/06/cracked-password-300x137.png 300w, https://techantidote.com/wp-content/uploads/2019/06/cracked-password-210x96.png 210w" sizes="auto, (max-width: 514px) 100vw, 514px" /></p>
<p>-So, the password &#8220;admin&#8221; user is &#8220;1234&#8221;.</p>
<p><span style="color: #3366ff;" data-darkreader-inline-color=""><strong>The user &#8220;nobody&#8221;:</strong></span></p>
<p>Coming back to the user &#8220;nobody&#8221;.</p>
<p>Ideally, for security purposes &#8220;<span style="color: #339966;" data-darkreader-inline-color="">nobody</span>&#8221; user is used with a combination of a <span style="color: #ff0000;" data-darkreader-inline-color="">non existing directory</span> along with a <span style="color: #ff00ff;" data-darkreader-inline-color="">nologin</span> shell. Below is a sample of an acceptable configuration to me:</p>
<pre><span style="color: #339966;" data-darkreader-inline-color="">nobody</span>:x:65534:65534:nobody:<span style="color: #ff0000;" data-darkreader-inline-color="">/nonexistent</span>:<span style="color: #ff00ff;" data-darkreader-inline-color="">/usr/sbin/nologin</span></pre>
<p>However, in the router&#8217;s FS, it seems wierd that the user &#8220;nobody&#8221; seems to have &#8220;/&#8221; or the actual root as mount along with &#8220;/bin/bash&#8221;.</p>
<pre><strong><span style="color: #339966;" data-darkreader-inline-color="">nobody</span></strong>:*:0:0:nobody:/:<span style="color: #ff00ff;" data-darkreader-inline-color=""><strong>/bin/sh</strong></span></pre>
<p>Anyways, may be this is just my paranoia/<span style="color: #3366ff;" data-darkreader-inline-color="">spidey sense</span> kicking in. <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f61b.png" alt="😛" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>&nbsp;</p>
<p>If you read this far. Thanks a ton! Hope you learned something from this article. Do bookmark this page for future references. Cheers</p>
<p>&nbsp;</p>
<p>Regards,<br />
<strong>ΞXΤЯ3МΞ</strong></p>
<p><span style="color: #3366ff;" data-darkreader-inline-color=""><strong>References</strong></span>:</p>
<p>https://openwrt.org/docs/techref/bootloader/cfe<br />
https://en.wikipedia.org/wiki/Common_Firmware_Environment<br />
https://www.cvedetails.com/version/81666/Linux-Linux-Kernel-2.6.30.html<br />
https://charlesreid1.com/wiki/John_the_Ripper/Shadow_File<br />
https://www.openwall.com/john/doc/EXAMPLES.shtml</p>
<p><a href="https://raw.githubusercontent.com/Noltari/cfe_bcm63xx/master/bcm963xx_bootloader_appnote.pdf" target="_blank" rel="noopener noreferrer nofollow">Click to access bcm963xx_bootloader_appnote.pdf</a></p>
<p>http://plastilinux.blogspot.com/2009/11/how-to-know-version-of-kernel-without.html<br />
ase1590</p>
<p>The post <a href="https://techantidote.com/reverse-engineering-router-firmware-tp-link-td-w8970/">Reverse Engineering Router Firmware TP-Link TD-W8970</a> appeared first on <a href="https://techantidote.com">Tech Antidote</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techantidote.com/reverse-engineering-router-firmware-tp-link-td-w8970/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2976</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Minified using Disk

Served from: techantidote.com @ 2026-10-31 20:12:04 by W3 Total Cache
-->