pwnlink
This commit is contained in:
parent
5554f984f7
commit
a3d8a10f08
45
_posts/2019-06-27-PWNlink.md
Normal file
45
_posts/2019-06-27-PWNlink.md
Normal file
@ -0,0 +1,45 @@
|
||||
---
|
||||
layout: post
|
||||
title: "I had some fun with a router"
|
||||
description: "cleartext passwords + external management = death wish"
|
||||
date: 2019-06-27 17:16:00
|
||||
categories: random
|
||||
---
|
||||
|
||||
I was playing around with some D-link routers today and remembered an [ExploitDB Entry](https://www.exploit-db.com/exploits/33520) I read a while ago. Many D-link routers have a great feature that allows remote management and configuration queries. Interestingly, this cannot be disabled, and one of the pages contains a cleartext version of the admin password (yay!).
|
||||
|
||||
## How to get yourself an admin password
|
||||
On any supported router, make an HTTP request to `http://your.router.ip.addr/tools_admin.asp/`. This will return a pretty large XML file containing information about your router's hardware and configuration.
|
||||
|
||||
Notice the fact that you did not have to log in. This is due to the fact that this file seems to be used by a remote management service of some sort.
|
||||
|
||||
The important thing to note here is that, when parsed with the regex pattern: `name="user_password_tmp" value="(.*)">`, you get a single string. This string is the admin password of the device.
|
||||
|
||||
## Supported routers
|
||||
This is supported by many D-link routers. The ones I know about are:
|
||||
- DIR-835
|
||||
- DIR-855L
|
||||
- DGL-5500
|
||||
|
||||
Some routers have this XML file, but it is restricted... By a user without a password. These are:
|
||||
- DHP-1565
|
||||
- DIR-652
|
||||
|
||||
## PWNlink
|
||||
Like everything I play with, I made a script to do this all for me (and spent a large amount of time adding colours to the text).
|
||||
|
||||
My script is called PWNlink (PWN + D-link), It automatically finds a router on your network by looking for a specific DNS entry created by many D-link routers, then checking your gateway. Next, PWNlink reads you router's `hnap1` config to find it's model number. If supported, the script will read and parse the appropriate configs to give you the admin credentials for your router.
|
||||
|
||||
PWNlink can be installed on any *nix computer that has both `python3.7` and `python3-pip` installed. To install PWNlink, run:
|
||||
```
|
||||
pip3 install pwnlink
|
||||
```
|
||||
|
||||
Run the script without any arguments for automatic detection, or pass any IP address to use manual detection.
|
||||
|
||||
## Disclamier thingy
|
||||
I don't see much point to these, but I should probably put one anyways.
|
||||
|
||||
**Don't be dumb with this script.**
|
||||
|
||||
I have only used it on my own (or 5024's) routers, and did not create PWNlink with any malicious intent.
|
@ -81,6 +81,177 @@
|
||||
<div class="inner" style="text-align: center">
|
||||
|
||||
|
||||
<!-- <header class="major"> -->
|
||||
<h1><a href="/random/2019/06/27/PWNlink.html" class="link" title="2019-06-27 13:16:00 -0400">I had some fun with a router</a></h1>
|
||||
<!-- </header> -->
|
||||
|
||||
<!-- Cheaty way to display a description -->
|
||||
<!-- <p><i>2019-06-27 13:16:00 -0400</i></p> -->
|
||||
<hr>
|
||||
<!--
|
||||
<p>2019-06-27 13:16:00 -0400</p>
|
||||
<p><p>I was playing around with some D-link routers today and remembered an <a href="https://www.exploit-db.com/exploits/33520">ExploitDB Entry</a> I read a while ago. Many D-link routers have a great feature that allows remote management and configuration queries. Interestingly, this cannot be disabled, and one of the pages contains a cleartext version of the admin password (yay!).</p>
|
||||
|
||||
<h2 id="how-to-get-yourself-an-admin-password">How to get yourself an admin password</h2>
|
||||
<p>On any supported router, make an HTTP request to <code class="highlighter-rouge">http://your.router.ip.addr/tools_admin.asp/</code>. This will return a pretty large XML file containing information about your router’s hardware and configuration.</p>
|
||||
|
||||
<p>Notice the fact that you did not have to log in. This is due to the fact that this file seems to be used by a remote management service of some sort.</p>
|
||||
|
||||
<p>The important thing to note here is that, when parsed with the regex pattern: <code class="highlighter-rouge">name="user_password_tmp" value="(.*)"></code>, you get a single string. This string is the admin password of the device.</p>
|
||||
|
||||
<h2 id="supported-routers">Supported routers</h2>
|
||||
<p>This is supported by many D-link routers. The ones I know about are:</p>
|
||||
<ul>
|
||||
<li>DIR-835</li>
|
||||
<li>DIR-855L</li>
|
||||
<li>DGL-5500</li>
|
||||
</ul>
|
||||
|
||||
<p>Some routers have this XML file, but it is restricted… By a user without a password. These are:</p>
|
||||
<ul>
|
||||
<li>DHP-1565</li>
|
||||
<li>DIR-652</li>
|
||||
</ul>
|
||||
|
||||
<h2 id="pwnlink">PWNlink</h2>
|
||||
<p>Like everything I play with, I made a script to do this all for me (and spent a large amount of time adding colours to the text).</p>
|
||||
|
||||
<p>My script is called PWNlink (PWN + D-link), It automatically finds a router on your network by looking for a specific DNS entry created by many D-link routers, then checking your gateway. Next, PWNlink reads you router’s <code class="highlighter-rouge">hnap1</code> config to find it’s model number. If supported, the script will read and parse the appropriate configs to give you the admin credentials for your router.</p>
|
||||
|
||||
<p>PWNlink can be installed on any *nix computer that has both <code class="highlighter-rouge">python3.7</code> and <code class="highlighter-rouge">python3-pip</code> installed. To install PWNlink, run:</p>
|
||||
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>pip3 install pwnlink
|
||||
</code></pre></div></div>
|
||||
|
||||
<p>Run the script without any arguments for automatic detection, or pass any IP address to use manual detection.</p>
|
||||
|
||||
<h2 id="disclamier-thingy">Disclamier thingy</h2>
|
||||
<p>I don’t see much point to these, but I should probably put one anyways.</p>
|
||||
|
||||
<p><strong>Don’t be dumb with this script.</strong></p>
|
||||
|
||||
<p>I have only used it on my own (or 5024’s) routers, and did not create PWNlink with any malicious intent.</p>
|
||||
</p> -->
|
||||
|
||||
|
||||
|
||||
<!-- <header class="major"> -->
|
||||
<h1><a href="/random/2019/06/27/Python.html" class="link" title="2019-06-27 03:00:00 -0400">Hunting snakes with a shotgun</a></h1>
|
||||
<!-- </header> -->
|
||||
|
||||
<!-- Cheaty way to display a description -->
|
||||
<!-- <p><i>2019-06-27 03:00:00 -0400</i></p> -->
|
||||
<hr>
|
||||
<!--
|
||||
<p>2019-06-27 03:00:00 -0400</p>
|
||||
<p><p>A rather large number of people know me as “the guy who does weird things with python”. I would object to this title, but it is quite accurate. So, here are some of the things I like playing with in python. None of these are actually breaking the language, just little known facts and syntax. At some point I will share about actually breaking the language. For now, enjoy the weird things I have found over the past 6 years.</p>
|
||||
|
||||
<h2 id="type-hints">Type hints</h2>
|
||||
<p>A little known feature of python is called “type hinting” (PEP 484). This is actually quite common to see in standard libraries, and has it’s own special syntax:</p>
|
||||
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Here is a regular function
|
||||
</span><span class="k">def</span> <span class="nf">meep</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
|
||||
<span class="k">return</span> <span class="n">a</span><span class="o">*</span><span class="n">b</span><span class="o">^</span><span class="mi">2</span>
|
||||
|
||||
<span class="c1"># This function has no real reason to exsist, and is lacking any sort of documentation.
|
||||
# Let's add a docstring to explain what it does
|
||||
</span>
|
||||
<span class="k">def</span> <span class="nf">meep</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
|
||||
<span class="s">""" This function returns the result of a times b squared """</span>
|
||||
<span class="k">return</span> <span class="n">a</span><span class="o">*</span><span class="n">b</span><span class="o">^</span><span class="mi">2</span>
|
||||
|
||||
<span class="c1"># Ok. The docstring explains the function, but is not too helpful
|
||||
# what are a and b? what does this return?
|
||||
# For all we know, a could actually be a string (in which case, this function would return a string)
|
||||
# Let's fix that up with a type hint
|
||||
</span>
|
||||
<span class="k">def</span> <span class="nf">meep</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
|
||||
<span class="s">""" This function returns the result of a times b squared """</span>
|
||||
<span class="k">return</span> <span class="n">a</span><span class="o">*</span><span class="n">b</span><span class="o">^</span><span class="mi">2</span>
|
||||
|
||||
<span class="c1"># Thanks to the :int (called a type hint in case you didn't notice that yet), we now know that this function expects two ints.
|
||||
# Now, to finish this up with a secondary type hint to specify the return type
|
||||
</span><span class="k">def</span> <span class="nf">meep</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
||||
<span class="s">""" This function returns the result of a times b squared """</span>
|
||||
<span class="k">return</span> <span class="n">a</span><span class="o">*</span><span class="n">b</span><span class="o">^</span><span class="mi">2</span>
|
||||
|
||||
<span class="c1"># There. Now we can clearly see that this function takes too ints, and returns one int.
|
||||
# If only this was a requirement in the language. So many headaches could be solved.
|
||||
</span></code></pre></div></div>
|
||||
|
||||
<p>Now, keep in mind that this is called a type <em>hint</em>. The python compiler (yes.. Give me a second for that one) does not actually care if you obey the hint or not. Feel free to send incorrect data into a hinted function and see what you can break. Critical functions should both hint and check the data types being provided.</p>
|
||||
|
||||
<h2 id="type-declarations">Type declarations</h2>
|
||||
<p>Just like type hints for functions, python has hints for variables too.</p>
|
||||
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># A regular variable. Must be declared with an initial value
|
||||
</span><span class="n">my_state</span> <span class="o">=</span> <span class="bp">None</span>
|
||||
|
||||
<span class="c1"># my_state is None, as it has not been set, but needs to exist.
|
||||
# Let's assume that my_state is to be a state:
|
||||
</span><span class="k">class</span> <span class="nc">State</span><span class="p">:</span>
|
||||
<span class="n">status</span> <span class="o">=</span> <span class="bp">False</span>
|
||||
<span class="k">def</span> <span class="nf">toggle</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">status</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">status</span>
|
||||
|
||||
<span class="c1"># Finally, its time to set the state to something useful
|
||||
</span><span class="n">my_state</span> <span class="o">=</span> <span class="n">State</span><span class="p">()</span>
|
||||
<span class="n">my_state</span><span class="o">.</span><span class="n">toggle</span><span class="p">()</span>
|
||||
|
||||
<span class="c1"># Ok.. I hate this. Let's start by using type declarations first
|
||||
# Any variable can be un-initialized and just have a type. Like so:
|
||||
</span><span class="n">my_state</span><span class="p">:</span> <span class="n">State</span>
|
||||
|
||||
<span class="c1"># This works for anything
|
||||
</span><span class="n">is_alive</span><span class="p">:</span> <span class="nb">bool</span>
|
||||
<span class="n">age</span><span class="p">:</span> <span class="nb">int</span>
|
||||
<span class="n">name</span><span class="p">:</span> <span class="nb">str</span>
|
||||
|
||||
<span class="c1"># Now, with this new knowledge, let's rewrite State
|
||||
</span><span class="k">class</span> <span class="nc">State</span><span class="p">:</span>
|
||||
<span class="n">status</span><span class="p">:</span> <span class="nb">bool</span>
|
||||
<span class="k">def</span> <span class="nf">toggle</span><span class="p">(</span><span class="bp">self</span><span class="p">:</span> <span class="n">State</span><span class="p">)</span> <span class="o">-></span> <span class="bp">None</span><span class="p">:</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">status</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">status</span>
|
||||
|
||||
<span class="c1"># And initialize my_state with slightly different syntax
|
||||
</span><span class="n">my_state</span> <span class="o">=</span> <span class="n">State</span><span class="p">(</span><span class="n">status</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
|
||||
</code></pre></div></div>
|
||||
|
||||
<p>I have not found much use for this yet. Hopefully there is something cool to use it for.</p>
|
||||
|
||||
<h2 id="one-line-functions">One-line functions</h2>
|
||||
<p>This is more common knowlage. A function can be declared in one line</p>
|
||||
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Here is an adder function
|
||||
</span><span class="k">def</span> <span class="nf">adder1</span><span class="p">(</span><span class="n">a</span><span class="p">:</span><span class="nb">int</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span><span class="nb">int</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
||||
<span class="k">return</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
|
||||
|
||||
<span class="c1"># Here is a one-line adder function
|
||||
</span><span class="n">adder2</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">a</span><span class="p">,</span><span class="n">b</span> <span class="p">:</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
|
||||
|
||||
<span class="c1"># State from above can be compacted further:
|
||||
</span><span class="k">class</span> <span class="nc">State</span><span class="p">:</span>
|
||||
<span class="n">status</span><span class="p">:</span> <span class="nb">bool</span>
|
||||
<span class="n">toggle</span> <span class="o">=</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">status</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">status</span>
|
||||
</code></pre></div></div>
|
||||
|
||||
<h2 id="ternary-operations">Ternary operations</h2>
|
||||
<p>On the trend of one-line code, We have the one-line if/else, also known as a Ternary in more sensible languages.</p>
|
||||
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Here is an if/else
|
||||
</span><span class="k">if</span> <span class="mi">100</span> <span class="ow">is</span> <span class="mi">5</span><span class="p">:</span>
|
||||
<span class="k">print</span><span class="p">(</span><span class="s">"The world has ended"</span><span class="p">)</span>
|
||||
<span class="k">else</span><span class="p">:</span>
|
||||
<span class="k">print</span><span class="p">(</span><span class="s">"All is good"</span><span class="p">)</span>
|
||||
|
||||
<span class="c1"># Here is a smaller if/else
|
||||
</span><span class="k">print</span><span class="p">(</span><span class="s">"The world has ended"</span> <span class="k">if</span> <span class="mi">100</span> <span class="ow">is</span> <span class="mi">5</span> <span class="k">else</span> <span class="s">"All is good"</span><span class="p">)</span>
|
||||
</code></pre></div></div>
|
||||
|
||||
<h2 id="compiled-python">Compiled python</h2>
|
||||
<p>This one is interesting. Python, like Java, is compiled into bytecode. So yes, it technically is a compiled language. To see said bytecode, take a look at any <code class="highlighter-rouge">.pyc</code> file sitting in your <code class="highlighter-rouge">__pycache__</code></p>
|
||||
|
||||
<h2 id="blog-formatting-experiments">Blog formatting experiments</h2>
|
||||
<p>I am still playing with post formats, and various types of content. This is more random than I usually prefer. Let me know your thoughts on the social media platform of your choosing.</p>
|
||||
</p> -->
|
||||
|
||||
|
||||
|
||||
<!-- <header class="major"> -->
|
||||
<h1><a href="/random/2019/06/26/BashSmash.html" class="link" title="2019-06-26 11:48:00 -0400">BashSmash</a></h1>
|
||||
<!-- </header> -->
|
||||
|
187
_site/feed.xml
187
_site/feed.xml
@ -1,4 +1,147 @@
|
||||
<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.8.5">Jekyll</generator><link href="http://localhost:4000/feed.xml" rel="self" type="application/atom+xml" /><link href="http://localhost:4000/" rel="alternate" type="text/html" /><updated>2019-06-26T22:13:34-04:00</updated><id>http://localhost:4000/feed.xml</id><title type="html">Evan Pratten</title><subtitle>Computer wizard, student, <a href="https://github.com/frc5024">@frc5024</a> programming team lead, and radio enthusiast.</subtitle><entry><title type="html">BashSmash</title><link href="http://localhost:4000/random/2019/06/26/BashSmash.html" rel="alternate" type="text/html" title="BashSmash" /><published>2019-06-26T11:48:00-04:00</published><updated>2019-06-26T11:48:00-04:00</updated><id>http://localhost:4000/random/2019/06/26/BashSmash</id><content type="html" xml:base="http://localhost:4000/random/2019/06/26/BashSmash.html"><p>I was watching this great <a href="https://www.google.com/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=1&amp;cad=rja&amp;uact=8&amp;ved=2ahUKEwiOhNze_4fjAhUiB50JHR12D8AQwqsBMAB6BAgJEAQ&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D6D1LnMj0Yt0&amp;usg=AOvVaw2nOgft0SoPZujc9js9Vxhx">Liveoverflow video</a> yesterday, and really liked the idea of building escape sequences with strings. So, I built a new tool, <a href="https://pypi.org/project/bashsmash/">BashSmash</a>.</p>
|
||||
<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.8.5">Jekyll</generator><link href="http://localhost:4000/feed.xml" rel="self" type="application/atom+xml" /><link href="http://localhost:4000/" rel="alternate" type="text/html" /><updated>2019-06-27T17:46:56-04:00</updated><id>http://localhost:4000/feed.xml</id><title type="html">Evan Pratten</title><subtitle>Computer wizard, student, <a href="https://github.com/frc5024">@frc5024</a> programming team lead, and radio enthusiast.</subtitle><entry><title type="html">I had some fun with a router</title><link href="http://localhost:4000/random/2019/06/27/PWNlink.html" rel="alternate" type="text/html" title="I had some fun with a router" /><published>2019-06-27T13:16:00-04:00</published><updated>2019-06-27T13:16:00-04:00</updated><id>http://localhost:4000/random/2019/06/27/PWNlink</id><content type="html" xml:base="http://localhost:4000/random/2019/06/27/PWNlink.html"><p>I was playing around with some D-link routers today and remembered an <a href="https://www.exploit-db.com/exploits/33520">ExploitDB Entry</a> I read a while ago. Many D-link routers have a great feature that allows remote management and configuration queries. Interestingly, this cannot be disabled, and one of the pages contains a cleartext version of the admin password (yay!).</p>
|
||||
|
||||
<h2 id="how-to-get-yourself-an-admin-password">How to get yourself an admin password</h2>
|
||||
<p>On any supported router, make an HTTP request to <code class="highlighter-rouge">http://your.router.ip.addr/tools_admin.asp/</code>. This will return a pretty large XML file containing information about your router’s hardware and configuration.</p>
|
||||
|
||||
<p>Notice the fact that you did not have to log in. This is due to the fact that this file seems to be used by a remote management service of some sort.</p>
|
||||
|
||||
<p>The important thing to note here is that, when parsed with the regex pattern: <code class="highlighter-rouge">name="user_password_tmp" value="(.*)"&gt;</code>, you get a single string. This string is the admin password of the device.</p>
|
||||
|
||||
<h2 id="supported-routers">Supported routers</h2>
|
||||
<p>This is supported by many D-link routers. The ones I know about are:</p>
|
||||
<ul>
|
||||
<li>DIR-835</li>
|
||||
<li>DIR-855L</li>
|
||||
<li>DGL-5500</li>
|
||||
</ul>
|
||||
|
||||
<p>Some routers have this XML file, but it is restricted… By a user without a password. These are:</p>
|
||||
<ul>
|
||||
<li>DHP-1565</li>
|
||||
<li>DIR-652</li>
|
||||
</ul>
|
||||
|
||||
<h2 id="pwnlink">PWNlink</h2>
|
||||
<p>Like everything I play with, I made a script to do this all for me (and spent a large amount of time adding colours to the text).</p>
|
||||
|
||||
<p>My script is called PWNlink (PWN + D-link), It automatically finds a router on your network by looking for a specific DNS entry created by many D-link routers, then checking your gateway. Next, PWNlink reads you router’s <code class="highlighter-rouge">hnap1</code> config to find it’s model number. If supported, the script will read and parse the appropriate configs to give you the admin credentials for your router.</p>
|
||||
|
||||
<p>PWNlink can be installed on any *nix computer that has both <code class="highlighter-rouge">python3.7</code> and <code class="highlighter-rouge">python3-pip</code> installed. To install PWNlink, run:</p>
|
||||
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>pip3 install pwnlink
|
||||
</code></pre></div></div>
|
||||
|
||||
<p>Run the script without any arguments for automatic detection, or pass any IP address to use manual detection.</p>
|
||||
|
||||
<h2 id="disclamier-thingy">Disclamier thingy</h2>
|
||||
<p>I don’t see much point to these, but I should probably put one anyways.</p>
|
||||
|
||||
<p><strong>Don’t be dumb with this script.</strong></p>
|
||||
|
||||
<p>I have only used it on my own (or 5024’s) routers, and did not create PWNlink with any malicious intent.</p></content><author><name></name></author><summary type="html">I was playing around with some D-link routers today and remembered an ExploitDB Entry I read a while ago. Many D-link routers have a great feature that allows remote management and configuration queries. Interestingly, this cannot be disabled, and one of the pages contains a cleartext version of the admin password (yay!).</summary></entry><entry><title type="html">Hunting snakes with a shotgun</title><link href="http://localhost:4000/random/2019/06/27/Python.html" rel="alternate" type="text/html" title="Hunting snakes with a shotgun" /><published>2019-06-27T03:00:00-04:00</published><updated>2019-06-27T03:00:00-04:00</updated><id>http://localhost:4000/random/2019/06/27/Python</id><content type="html" xml:base="http://localhost:4000/random/2019/06/27/Python.html"><p>A rather large number of people know me as “the guy who does weird things with python”. I would object to this title, but it is quite accurate. So, here are some of the things I like playing with in python. None of these are actually breaking the language, just little known facts and syntax. At some point I will share about actually breaking the language. For now, enjoy the weird things I have found over the past 6 years.</p>
|
||||
|
||||
<h2 id="type-hints">Type hints</h2>
|
||||
<p>A little known feature of python is called “type hinting” (PEP 484). This is actually quite common to see in standard libraries, and has it’s own special syntax:</p>
|
||||
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Here is a regular function
|
||||
</span><span class="k">def</span> <span class="nf">meep</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
|
||||
<span class="k">return</span> <span class="n">a</span><span class="o">*</span><span class="n">b</span><span class="o">^</span><span class="mi">2</span>
|
||||
|
||||
<span class="c1"># This function has no real reason to exsist, and is lacking any sort of documentation.
|
||||
# Let's add a docstring to explain what it does
|
||||
</span>
|
||||
<span class="k">def</span> <span class="nf">meep</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
|
||||
<span class="s">""" This function returns the result of a times b squared """</span>
|
||||
<span class="k">return</span> <span class="n">a</span><span class="o">*</span><span class="n">b</span><span class="o">^</span><span class="mi">2</span>
|
||||
|
||||
<span class="c1"># Ok. The docstring explains the function, but is not too helpful
|
||||
# what are a and b? what does this return?
|
||||
# For all we know, a could actually be a string (in which case, this function would return a string)
|
||||
# Let's fix that up with a type hint
|
||||
</span>
|
||||
<span class="k">def</span> <span class="nf">meep</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
|
||||
<span class="s">""" This function returns the result of a times b squared """</span>
|
||||
<span class="k">return</span> <span class="n">a</span><span class="o">*</span><span class="n">b</span><span class="o">^</span><span class="mi">2</span>
|
||||
|
||||
<span class="c1"># Thanks to the :int (called a type hint in case you didn't notice that yet), we now know that this function expects two ints.
|
||||
# Now, to finish this up with a secondary type hint to specify the return type
|
||||
</span><span class="k">def</span> <span class="nf">meep</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
|
||||
<span class="s">""" This function returns the result of a times b squared """</span>
|
||||
<span class="k">return</span> <span class="n">a</span><span class="o">*</span><span class="n">b</span><span class="o">^</span><span class="mi">2</span>
|
||||
|
||||
<span class="c1"># There. Now we can clearly see that this function takes too ints, and returns one int.
|
||||
# If only this was a requirement in the language. So many headaches could be solved.
|
||||
</span></code></pre></div></div>
|
||||
|
||||
<p>Now, keep in mind that this is called a type <em>hint</em>. The python compiler (yes.. Give me a second for that one) does not actually care if you obey the hint or not. Feel free to send incorrect data into a hinted function and see what you can break. Critical functions should both hint and check the data types being provided.</p>
|
||||
|
||||
<h2 id="type-declarations">Type declarations</h2>
|
||||
<p>Just like type hints for functions, python has hints for variables too.</p>
|
||||
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># A regular variable. Must be declared with an initial value
|
||||
</span><span class="n">my_state</span> <span class="o">=</span> <span class="bp">None</span>
|
||||
|
||||
<span class="c1"># my_state is None, as it has not been set, but needs to exist.
|
||||
# Let's assume that my_state is to be a state:
|
||||
</span><span class="k">class</span> <span class="nc">State</span><span class="p">:</span>
|
||||
<span class="n">status</span> <span class="o">=</span> <span class="bp">False</span>
|
||||
<span class="k">def</span> <span class="nf">toggle</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">status</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">status</span>
|
||||
|
||||
<span class="c1"># Finally, its time to set the state to something useful
|
||||
</span><span class="n">my_state</span> <span class="o">=</span> <span class="n">State</span><span class="p">()</span>
|
||||
<span class="n">my_state</span><span class="o">.</span><span class="n">toggle</span><span class="p">()</span>
|
||||
|
||||
<span class="c1"># Ok.. I hate this. Let's start by using type declarations first
|
||||
# Any variable can be un-initialized and just have a type. Like so:
|
||||
</span><span class="n">my_state</span><span class="p">:</span> <span class="n">State</span>
|
||||
|
||||
<span class="c1"># This works for anything
|
||||
</span><span class="n">is_alive</span><span class="p">:</span> <span class="nb">bool</span>
|
||||
<span class="n">age</span><span class="p">:</span> <span class="nb">int</span>
|
||||
<span class="n">name</span><span class="p">:</span> <span class="nb">str</span>
|
||||
|
||||
<span class="c1"># Now, with this new knowledge, let's rewrite State
|
||||
</span><span class="k">class</span> <span class="nc">State</span><span class="p">:</span>
|
||||
<span class="n">status</span><span class="p">:</span> <span class="nb">bool</span>
|
||||
<span class="k">def</span> <span class="nf">toggle</span><span class="p">(</span><span class="bp">self</span><span class="p">:</span> <span class="n">State</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="bp">None</span><span class="p">:</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">status</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">status</span>
|
||||
|
||||
<span class="c1"># And initialize my_state with slightly different syntax
|
||||
</span><span class="n">my_state</span> <span class="o">=</span> <span class="n">State</span><span class="p">(</span><span class="n">status</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
|
||||
</code></pre></div></div>
|
||||
|
||||
<p>I have not found much use for this yet. Hopefully there is something cool to use it for.</p>
|
||||
|
||||
<h2 id="one-line-functions">One-line functions</h2>
|
||||
<p>This is more common knowlage. A function can be declared in one line</p>
|
||||
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Here is an adder function
|
||||
</span><span class="k">def</span> <span class="nf">adder1</span><span class="p">(</span><span class="n">a</span><span class="p">:</span><span class="nb">int</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span><span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
|
||||
<span class="k">return</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
|
||||
|
||||
<span class="c1"># Here is a one-line adder function
|
||||
</span><span class="n">adder2</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">a</span><span class="p">,</span><span class="n">b</span> <span class="p">:</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
|
||||
|
||||
<span class="c1"># State from above can be compacted further:
|
||||
</span><span class="k">class</span> <span class="nc">State</span><span class="p">:</span>
|
||||
<span class="n">status</span><span class="p">:</span> <span class="nb">bool</span>
|
||||
<span class="n">toggle</span> <span class="o">=</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">status</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">status</span>
|
||||
</code></pre></div></div>
|
||||
|
||||
<h2 id="ternary-operations">Ternary operations</h2>
|
||||
<p>On the trend of one-line code, We have the one-line if/else, also known as a Ternary in more sensible languages.</p>
|
||||
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Here is an if/else
|
||||
</span><span class="k">if</span> <span class="mi">100</span> <span class="ow">is</span> <span class="mi">5</span><span class="p">:</span>
|
||||
<span class="k">print</span><span class="p">(</span><span class="s">"The world has ended"</span><span class="p">)</span>
|
||||
<span class="k">else</span><span class="p">:</span>
|
||||
<span class="k">print</span><span class="p">(</span><span class="s">"All is good"</span><span class="p">)</span>
|
||||
|
||||
<span class="c1"># Here is a smaller if/else
|
||||
</span><span class="k">print</span><span class="p">(</span><span class="s">"The world has ended"</span> <span class="k">if</span> <span class="mi">100</span> <span class="ow">is</span> <span class="mi">5</span> <span class="k">else</span> <span class="s">"All is good"</span><span class="p">)</span>
|
||||
</code></pre></div></div>
|
||||
|
||||
<h2 id="compiled-python">Compiled python</h2>
|
||||
<p>This one is interesting. Python, like Java, is compiled into bytecode. So yes, it technically is a compiled language. To see said bytecode, take a look at any <code class="highlighter-rouge">.pyc</code> file sitting in your <code class="highlighter-rouge">__pycache__</code></p>
|
||||
|
||||
<h2 id="blog-formatting-experiments">Blog formatting experiments</h2>
|
||||
<p>I am still playing with post formats, and various types of content. This is more random than I usually prefer. Let me know your thoughts on the social media platform of your choosing.</p></content><author><name></name></author><summary type="html">A rather large number of people know me as “the guy who does weird things with python”. I would object to this title, but it is quite accurate. So, here are some of the things I like playing with in python. None of these are actually breaking the language, just little known facts and syntax. At some point I will share about actually breaking the language. For now, enjoy the weird things I have found over the past 6 years.</summary></entry><entry><title type="html">BashSmash</title><link href="http://localhost:4000/random/2019/06/26/BashSmash.html" rel="alternate" type="text/html" title="BashSmash" /><published>2019-06-26T11:48:00-04:00</published><updated>2019-06-26T11:48:00-04:00</updated><id>http://localhost:4000/random/2019/06/26/BashSmash</id><content type="html" xml:base="http://localhost:4000/random/2019/06/26/BashSmash.html"><p>I was watching this great <a href="https://www.google.com/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=1&amp;cad=rja&amp;uact=8&amp;ved=2ahUKEwiOhNze_4fjAhUiB50JHR12D8AQwqsBMAB6BAgJEAQ&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D6D1LnMj0Yt0&amp;usg=AOvVaw2nOgft0SoPZujc9js9Vxhx">Liveoverflow video</a> yesterday, and really liked the idea of building escape sequences with strings. So, I built a new tool, <a href="https://pypi.org/project/bashsmash/">BashSmash</a>.</p>
|
||||
|
||||
<h2 id="the-goal">The goal</h2>
|
||||
<p>The goal of BashSmash is very similar to that described in Liveoverflow’s video. Do anything in bash without using any letters or numbers except <code class="highlighter-rouge">n</code> and <code class="highlighter-rouge">f</code> (he used <code class="highlighter-rouge">i</code> instead of <code class="highlighter-rouge">f</code>). This can both bypass shell injection filters, and generally mess with people.</p>
|
||||
@ -295,44 +438,4 @@ Your browser does not support audio players
|
||||
</code></pre></div></div>
|
||||
|
||||
<h2 id="use-it-yourself">Use it yourself</h2>
|
||||
<p>I put this theme on userstyles.org. You can download and install it by going to <a href="https://userstyles.org/styles/172679/ewpratten-s-githubtheme">my userstyles page</a>.</p></content><author><name></name></author><summary type="html">I have been using GitHub since 2017, and have been getting tired of GitHub’s theme. I didn’t need a huge change, just a small refresh. So, to solve this, I whipped out Stylus and made a nice little CSS file for it.</summary></entry><entry><title type="html">Building a safe and easy system for sending computer vision data from a raspberry pi to a roborio</title><link href="http://localhost:4000/frc/2019/05/27/Building-Safe-Vision-Comms.html" rel="alternate" type="text/html" title="Building a safe and easy system for sending computer vision data from a raspberry pi to a roborio" /><published>2019-05-27T05:22:00-04:00</published><updated>2019-05-27T05:22:00-04:00</updated><id>http://localhost:4000/frc/2019/05/27/Building-Safe-Vision-Comms</id><content type="html" xml:base="http://localhost:4000/frc/2019/05/27/Building-Safe-Vision-Comms.html"><p>Computer vision on an FRC robot has some problems.</p>
|
||||
<ul>
|
||||
<li>RoboRIO is not powerfull enough</li>
|
||||
<li>NetworkTables is not fast enough</li>
|
||||
<li>A TCP connection is great until you lose connection</li>
|
||||
<li>mDNS discovery is not reliable on the field</li>
|
||||
<li>UDP can skip frames</li>
|
||||
</ul>
|
||||
|
||||
<h2 id="needs">Needs</h2>
|
||||
<p>These are the things I need to have.</p>
|
||||
<ul>
|
||||
<li>Send data from any device</li>
|
||||
<li>Recive data on RoboRIO at any time</li>
|
||||
<li>Data rate faster than period time</li>
|
||||
</ul>
|
||||
|
||||
<h2 id="wants">Wants</h2>
|
||||
<p>These are the things I would like to have.</p>
|
||||
<ul>
|
||||
<li>Easy discovery</li>
|
||||
<li>Threaded</li>
|
||||
<li>Simple interface for new programmers</li>
|
||||
<li>Fallback in case of UDP issues</li>
|
||||
<li>FMS network firewall compliant</li>
|
||||
</ul>
|
||||
|
||||
<p>I am currently working on a protocol for solving this problem, nad will post an update here once it has been tested. Feel free to let me know your thoughts and ideas.</p></content><author><name></name></author><summary type="html">Computer vision on an FRC robot has some problems. RoboRIO is not powerfull enough NetworkTables is not fast enough A TCP connection is great until you lose connection mDNS discovery is not reliable on the field UDP can skip frames</summary></entry><entry><title type="html">The language hunt</title><link href="http://localhost:4000/frc/2019/04/30/FRC-Languages.html" rel="alternate" type="text/html" title="The language hunt" /><published>2019-04-30T14:32:00-04:00</published><updated>2019-04-30T14:32:00-04:00</updated><id>http://localhost:4000/frc/2019/04/30/FRC-Languages</id><content type="html" xml:base="http://localhost:4000/frc/2019/04/30/FRC-Languages.html"><p>Our programming team is looking to switch languages in the 2020 season. Here is the what, why, and how.</p>
|
||||
|
||||
<h2 id="our-history">Our history</h2>
|
||||
<p>We started out as a java team back in 2014 because java was (and still is) the language being taught in our programming classes. Honestly, our code sucked, as many rookie team’s do. There where no fancy features, or sensor-assisted autonomous. Direct input into talons was our way to roll.</p>
|
||||
|
||||
<p>A few years later, we had a change in team organization and switched to C++. Up until the 2019 / 2020 season, this was our language and we where getting pretty good at using it.</p>
|
||||
|
||||
<h2 id="the-problem">The Problem</h2>
|
||||
<p>We, as a team are looking to bring our programming and robots to the next level in 2020. Because of this, we ran into a problem. While C++ is an amazing language for embedded and robotics programming, some of it’s “features” where starting to act as a bottleneck to our design. Less time was being spent on polishing our new vision system or autonomous climb, and more on that crazy linker error that came out of nowhere.</p>
|
||||
|
||||
<p>It’s time for a change, but what do we change to?</p>
|
||||
|
||||
<h2 id="part-2">Part 2</h2>
|
||||
<p>The followup can be found <a href="/frc/2019/06/24/LanguageHunt2.html">HERE</a>.</p></content><author><name></name></author><summary type="html">Our programming team is looking to switch languages in the 2020 season. Here is the what, why, and how.</summary></entry></feed>
|
||||
<p>I put this theme on userstyles.org. You can download and install it by going to <a href="https://userstyles.org/styles/172679/ewpratten-s-githubtheme">my userstyles page</a>.</p></content><author><name></name></author><summary type="html">I have been using GitHub since 2017, and have been getting tired of GitHub’s theme. I didn’t need a huge change, just a small refresh. So, to solve this, I whipped out Stylus and made a nice little CSS file for it.</summary></entry></feed>
|
@ -123,6 +123,30 @@
|
||||
<section id="one" class="tiles">
|
||||
|
||||
|
||||
<article>
|
||||
<span class="image">
|
||||
<img src="" alt="" />
|
||||
</span>
|
||||
<header class="major">
|
||||
<h3><a href="/random/2019/06/27/PWNlink.html" class="link">I had some fun with a router</a></h3>
|
||||
<p>cleartext passwords + external management = death wish</p>
|
||||
</header>
|
||||
</article>
|
||||
|
||||
|
||||
|
||||
<article>
|
||||
<span class="image">
|
||||
<img src="" alt="" />
|
||||
</span>
|
||||
<header class="major">
|
||||
<h3><a href="/random/2019/06/27/Python.html" class="link">Hunting snakes with a shotgun</a></h3>
|
||||
<p>Python is a little too forgiving</p>
|
||||
</header>
|
||||
</article>
|
||||
|
||||
|
||||
|
||||
<article>
|
||||
<span class="image">
|
||||
<img src="" alt="" />
|
||||
@ -219,30 +243,6 @@
|
||||
|
||||
|
||||
|
||||
<article>
|
||||
<span class="image">
|
||||
<img src="" alt="" />
|
||||
</span>
|
||||
<header class="major">
|
||||
<h3><a href="/frc/2019/05/27/Building-Safe-Vision-Comms.html" class="link">Building a safe and easy system for sending computer vision data from a raspberry pi to a roborio</a></h3>
|
||||
<p></p>
|
||||
</header>
|
||||
</article>
|
||||
|
||||
|
||||
|
||||
<article>
|
||||
<span class="image">
|
||||
<img src="" alt="" />
|
||||
</span>
|
||||
<header class="major">
|
||||
<h3><a href="/frc/2019/04/30/FRC-Languages.html" class="link">The language hunt</a></h3>
|
||||
<p></p>
|
||||
</header>
|
||||
</article>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
180
_site/random/2019/06/27/PWNlink.html
Normal file
180
_site/random/2019/06/27/PWNlink.html
Normal file
@ -0,0 +1,180 @@
|
||||
<!DOCTYPE html>
|
||||
<!--
|
||||
Forty by HTML5 UP
|
||||
html5up.net | @ajlkn
|
||||
Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
|
||||
-->
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Evan Pratten</title>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
|
||||
<!--[if lte IE 8]><script src="/assets/js/ie/html5shiv.js"></script><![endif]-->
|
||||
<link rel="stylesheet" href="/assets/css/main.css" />
|
||||
<!--[if lte IE 9]><link rel="stylesheet" href="/assets/css/ie9.css" /><![endif]-->
|
||||
<!--[if lte IE 8]><link rel="stylesheet" href="/assets/css/ie8.css" /><![endif]-->
|
||||
|
||||
<!-- Syntax highlight -->
|
||||
<link rel="stylesheet" href="/assets/css/vs.css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<!-- Wrapper -->
|
||||
<div id="wrapper">
|
||||
|
||||
<!-- Header -->
|
||||
<header id="header" >
|
||||
<a href="http://localhost:4000//" class="logo"><strong>Evan Pratten</strong> <span>retrylife</span></a>
|
||||
<nav>
|
||||
<!-- <a href="#menu">Menu</a> -->
|
||||
</nav>
|
||||
</header>
|
||||
|
||||
<!-- Menu -->
|
||||
<!-- <nav id="menu">
|
||||
<ul class="links">
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<li><a href="http://localhost:4000//">Home</a></li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<li><a href="http://localhost:4000/all_posts.html">All posts</a></li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</ul>
|
||||
<ul class="actions vertical">
|
||||
<li><a href="#" class="button special fit">Get Started</a></li>
|
||||
<li><a href="#" class="button fit">Log In</a></li>
|
||||
</ul>
|
||||
</nav> -->
|
||||
|
||||
|
||||
<!-- Main -->
|
||||
<div id="main" class="alt">
|
||||
|
||||
<!-- One -->
|
||||
<section id="one">
|
||||
<div class="inner">
|
||||
<header class="major">
|
||||
<h1>I had some fun with a router</h1>
|
||||
</header>
|
||||
|
||||
<p><p>I was playing around with some D-link routers today and remembered an <a href="https://www.exploit-db.com/exploits/33520">ExploitDB Entry</a> I read a while ago. Many D-link routers have a great feature that allows remote management and configuration queries. Interestingly, this cannot be disabled, and one of the pages contains a cleartext version of the admin password (yay!).</p>
|
||||
|
||||
<h2 id="how-to-get-yourself-an-admin-password">How to get yourself an admin password</h2>
|
||||
<p>On any supported router, make an HTTP request to <code class="highlighter-rouge">http://your.router.ip.addr/tools_admin.asp/</code>. This will return a pretty large XML file containing information about your router’s hardware and configuration.</p>
|
||||
|
||||
<p>Notice the fact that you did not have to log in. This is due to the fact that this file seems to be used by a remote management service of some sort.</p>
|
||||
|
||||
<p>The important thing to note here is that, when parsed with the regex pattern: <code class="highlighter-rouge">name="user_password_tmp" value="(.*)"></code>, you get a single string. This string is the admin password of the device.</p>
|
||||
|
||||
<h2 id="supported-routers">Supported routers</h2>
|
||||
<p>This is supported by many D-link routers. The ones I know about are:</p>
|
||||
<ul>
|
||||
<li>DIR-835</li>
|
||||
<li>DIR-855L</li>
|
||||
<li>DGL-5500</li>
|
||||
</ul>
|
||||
|
||||
<p>Some routers have this XML file, but it is restricted… By a user without a password. These are:</p>
|
||||
<ul>
|
||||
<li>DHP-1565</li>
|
||||
<li>DIR-652</li>
|
||||
</ul>
|
||||
|
||||
<h2 id="pwnlink">PWNlink</h2>
|
||||
<p>Like everything I play with, I made a script to do this all for me (and spent a large amount of time adding colours to the text).</p>
|
||||
|
||||
<p>My script is called PWNlink (PWN + D-link), It automatically finds a router on your network by looking for a specific DNS entry created by many D-link routers, then checking your gateway. Next, PWNlink reads you router’s <code class="highlighter-rouge">hnap1</code> config to find it’s model number. If supported, the script will read and parse the appropriate configs to give you the admin credentials for your router.</p>
|
||||
|
||||
<p>PWNlink can be installed on any *nix computer that has both <code class="highlighter-rouge">python3.7</code> and <code class="highlighter-rouge">python3-pip</code> installed. To install PWNlink, run:</p>
|
||||
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>pip3 install pwnlink
|
||||
</code></pre></div></div>
|
||||
|
||||
<p>Run the script without any arguments for automatic detection, or pass any IP address to use manual detection.</p>
|
||||
|
||||
<h2 id="disclamier-thingy">Disclamier thingy</h2>
|
||||
<p>I don’t see much point to these, but I should probably put one anyways.</p>
|
||||
|
||||
<p><strong>Don’t be dumb with this script.</strong></p>
|
||||
|
||||
<p>I have only used it on my own (or 5024’s) routers, and did not create PWNlink with any malicious intent.</p>
|
||||
</p>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
</div>
|
||||
|
||||
<!-- Footer -->
|
||||
<footer id="footer">
|
||||
<div class="inner">
|
||||
<ul class="icons">
|
||||
|
||||
<li><a href="https://twitter.com/ewpratten" class="icon alt fa-twitter" target="_blank"><span class="label">Twitter</span></a></li>
|
||||
|
||||
<li><a href="https://gitlab.com/u/ewpratten" class="icon alt fa-gitlab" target="_blank"><span class="label">GitLab</span></a></li>
|
||||
|
||||
<li><a href="https://github.com/ewpratten" class="icon alt fa-github" target="_blank"><span class="label">GitHub</span></a></li>
|
||||
|
||||
|
||||
<li><a href="/feed.xml" class="icon alt fa-rss" target="_blank"><span class="label">RSS</span></a></li>
|
||||
</ul>
|
||||
<ul class="copyright">
|
||||
<li>© Evan Pratten retrylife</li>
|
||||
<li>Design: <a href="https://html5up.net" target="_blank">HTML5 UP</a></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
</footer>
|
||||
|
||||
</div>
|
||||
|
||||
<!-- Scripts -->
|
||||
<script src="http://localhost:4000/assets/js/jquery.min.js"></script>
|
||||
<script src="http://localhost:4000/assets/js/jquery.scrolly.min.js"></script>
|
||||
<script src="http://localhost:4000/assets/js/jquery.scrollex.min.js"></script>
|
||||
<script src="http://localhost:4000/assets/js/skel.min.js"></script>
|
||||
<script src="http://localhost:4000/assets/js/util.js"></script>
|
||||
<!--[if lte IE 8]><script src="http://localhost:4000/assets/js/ie/respond.min.js"></script><![endif]-->
|
||||
<script src="http://localhost:4000/assets/js/main.js"></script>
|
||||
|
||||
<!-- Global site tag (gtag.js) - Google Analytics -->
|
||||
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-74118570-2"></script>
|
||||
<script>
|
||||
window.dataLayer = window.dataLayer || [];
|
||||
|
||||
function gtag() {
|
||||
dataLayer.push(arguments);
|
||||
}
|
||||
gtag('js', new Date());
|
||||
|
||||
gtag('config', 'UA-74118570-2');
|
||||
</script>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
245
_site/random/2019/06/27/Python.html
Normal file
245
_site/random/2019/06/27/Python.html
Normal file
@ -0,0 +1,245 @@
|
||||
<!DOCTYPE html>
|
||||
<!--
|
||||
Forty by HTML5 UP
|
||||
html5up.net | @ajlkn
|
||||
Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
|
||||
-->
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Evan Pratten</title>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
|
||||
<!--[if lte IE 8]><script src="/assets/js/ie/html5shiv.js"></script><![endif]-->
|
||||
<link rel="stylesheet" href="/assets/css/main.css" />
|
||||
<!--[if lte IE 9]><link rel="stylesheet" href="/assets/css/ie9.css" /><![endif]-->
|
||||
<!--[if lte IE 8]><link rel="stylesheet" href="/assets/css/ie8.css" /><![endif]-->
|
||||
|
||||
<!-- Syntax highlight -->
|
||||
<link rel="stylesheet" href="/assets/css/vs.css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<!-- Wrapper -->
|
||||
<div id="wrapper">
|
||||
|
||||
<!-- Header -->
|
||||
<header id="header" >
|
||||
<a href="http://localhost:4000//" class="logo"><strong>Evan Pratten</strong> <span>retrylife</span></a>
|
||||
<nav>
|
||||
<!-- <a href="#menu">Menu</a> -->
|
||||
</nav>
|
||||
</header>
|
||||
|
||||
<!-- Menu -->
|
||||
<!-- <nav id="menu">
|
||||
<ul class="links">
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<li><a href="http://localhost:4000//">Home</a></li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<li><a href="http://localhost:4000/all_posts.html">All posts</a></li>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</ul>
|
||||
<ul class="actions vertical">
|
||||
<li><a href="#" class="button special fit">Get Started</a></li>
|
||||
<li><a href="#" class="button fit">Log In</a></li>
|
||||
</ul>
|
||||
</nav> -->
|
||||
|
||||
|
||||
<!-- Main -->
|
||||
<div id="main" class="alt">
|
||||
|
||||
<!-- One -->
|
||||
<section id="one">
|
||||
<div class="inner">
|
||||
<header class="major">
|
||||
<h1>Hunting snakes with a shotgun</h1>
|
||||
</header>
|
||||
|
||||
<p><p>A rather large number of people know me as “the guy who does weird things with python”. I would object to this title, but it is quite accurate. So, here are some of the things I like playing with in python. None of these are actually breaking the language, just little known facts and syntax. At some point I will share about actually breaking the language. For now, enjoy the weird things I have found over the past 6 years.</p>
|
||||
|
||||
<h2 id="type-hints">Type hints</h2>
|
||||
<p>A little known feature of python is called “type hinting” (PEP 484). This is actually quite common to see in standard libraries, and has it’s own special syntax:</p>
|
||||
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Here is a regular function
|
||||
</span><span class="k">def</span> <span class="nf">meep</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
|
||||
<span class="k">return</span> <span class="n">a</span><span class="o">*</span><span class="n">b</span><span class="o">^</span><span class="mi">2</span>
|
||||
|
||||
<span class="c1"># This function has no real reason to exsist, and is lacking any sort of documentation.
|
||||
# Let's add a docstring to explain what it does
|
||||
</span>
|
||||
<span class="k">def</span> <span class="nf">meep</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
|
||||
<span class="s">""" This function returns the result of a times b squared """</span>
|
||||
<span class="k">return</span> <span class="n">a</span><span class="o">*</span><span class="n">b</span><span class="o">^</span><span class="mi">2</span>
|
||||
|
||||
<span class="c1"># Ok. The docstring explains the function, but is not too helpful
|
||||
# what are a and b? what does this return?
|
||||
# For all we know, a could actually be a string (in which case, this function would return a string)
|
||||
# Let's fix that up with a type hint
|
||||
</span>
|
||||
<span class="k">def</span> <span class="nf">meep</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
|
||||
<span class="s">""" This function returns the result of a times b squared """</span>
|
||||
<span class="k">return</span> <span class="n">a</span><span class="o">*</span><span class="n">b</span><span class="o">^</span><span class="mi">2</span>
|
||||
|
||||
<span class="c1"># Thanks to the :int (called a type hint in case you didn't notice that yet), we now know that this function expects two ints.
|
||||
# Now, to finish this up with a secondary type hint to specify the return type
|
||||
</span><span class="k">def</span> <span class="nf">meep</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
||||
<span class="s">""" This function returns the result of a times b squared """</span>
|
||||
<span class="k">return</span> <span class="n">a</span><span class="o">*</span><span class="n">b</span><span class="o">^</span><span class="mi">2</span>
|
||||
|
||||
<span class="c1"># There. Now we can clearly see that this function takes too ints, and returns one int.
|
||||
# If only this was a requirement in the language. So many headaches could be solved.
|
||||
</span></code></pre></div></div>
|
||||
|
||||
<p>Now, keep in mind that this is called a type <em>hint</em>. The python compiler (yes.. Give me a second for that one) does not actually care if you obey the hint or not. Feel free to send incorrect data into a hinted function and see what you can break. Critical functions should both hint and check the data types being provided.</p>
|
||||
|
||||
<h2 id="type-declarations">Type declarations</h2>
|
||||
<p>Just like type hints for functions, python has hints for variables too.</p>
|
||||
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># A regular variable. Must be declared with an initial value
|
||||
</span><span class="n">my_state</span> <span class="o">=</span> <span class="bp">None</span>
|
||||
|
||||
<span class="c1"># my_state is None, as it has not been set, but needs to exist.
|
||||
# Let's assume that my_state is to be a state:
|
||||
</span><span class="k">class</span> <span class="nc">State</span><span class="p">:</span>
|
||||
<span class="n">status</span> <span class="o">=</span> <span class="bp">False</span>
|
||||
<span class="k">def</span> <span class="nf">toggle</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">status</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">status</span>
|
||||
|
||||
<span class="c1"># Finally, its time to set the state to something useful
|
||||
</span><span class="n">my_state</span> <span class="o">=</span> <span class="n">State</span><span class="p">()</span>
|
||||
<span class="n">my_state</span><span class="o">.</span><span class="n">toggle</span><span class="p">()</span>
|
||||
|
||||
<span class="c1"># Ok.. I hate this. Let's start by using type declarations first
|
||||
# Any variable can be un-initialized and just have a type. Like so:
|
||||
</span><span class="n">my_state</span><span class="p">:</span> <span class="n">State</span>
|
||||
|
||||
<span class="c1"># This works for anything
|
||||
</span><span class="n">is_alive</span><span class="p">:</span> <span class="nb">bool</span>
|
||||
<span class="n">age</span><span class="p">:</span> <span class="nb">int</span>
|
||||
<span class="n">name</span><span class="p">:</span> <span class="nb">str</span>
|
||||
|
||||
<span class="c1"># Now, with this new knowledge, let's rewrite State
|
||||
</span><span class="k">class</span> <span class="nc">State</span><span class="p">:</span>
|
||||
<span class="n">status</span><span class="p">:</span> <span class="nb">bool</span>
|
||||
<span class="k">def</span> <span class="nf">toggle</span><span class="p">(</span><span class="bp">self</span><span class="p">:</span> <span class="n">State</span><span class="p">)</span> <span class="o">-></span> <span class="bp">None</span><span class="p">:</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">status</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">status</span>
|
||||
|
||||
<span class="c1"># And initialize my_state with slightly different syntax
|
||||
</span><span class="n">my_state</span> <span class="o">=</span> <span class="n">State</span><span class="p">(</span><span class="n">status</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
|
||||
</code></pre></div></div>
|
||||
|
||||
<p>I have not found much use for this yet. Hopefully there is something cool to use it for.</p>
|
||||
|
||||
<h2 id="one-line-functions">One-line functions</h2>
|
||||
<p>This is more common knowlage. A function can be declared in one line</p>
|
||||
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Here is an adder function
|
||||
</span><span class="k">def</span> <span class="nf">adder1</span><span class="p">(</span><span class="n">a</span><span class="p">:</span><span class="nb">int</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span><span class="nb">int</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
||||
<span class="k">return</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
|
||||
|
||||
<span class="c1"># Here is a one-line adder function
|
||||
</span><span class="n">adder2</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">a</span><span class="p">,</span><span class="n">b</span> <span class="p">:</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
|
||||
|
||||
<span class="c1"># State from above can be compacted further:
|
||||
</span><span class="k">class</span> <span class="nc">State</span><span class="p">:</span>
|
||||
<span class="n">status</span><span class="p">:</span> <span class="nb">bool</span>
|
||||
<span class="n">toggle</span> <span class="o">=</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">status</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">status</span>
|
||||
</code></pre></div></div>
|
||||
|
||||
<h2 id="ternary-operations">Ternary operations</h2>
|
||||
<p>On the trend of one-line code, We have the one-line if/else, also known as a Ternary in more sensible languages.</p>
|
||||
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Here is an if/else
|
||||
</span><span class="k">if</span> <span class="mi">100</span> <span class="ow">is</span> <span class="mi">5</span><span class="p">:</span>
|
||||
<span class="k">print</span><span class="p">(</span><span class="s">"The world has ended"</span><span class="p">)</span>
|
||||
<span class="k">else</span><span class="p">:</span>
|
||||
<span class="k">print</span><span class="p">(</span><span class="s">"All is good"</span><span class="p">)</span>
|
||||
|
||||
<span class="c1"># Here is a smaller if/else
|
||||
</span><span class="k">print</span><span class="p">(</span><span class="s">"The world has ended"</span> <span class="k">if</span> <span class="mi">100</span> <span class="ow">is</span> <span class="mi">5</span> <span class="k">else</span> <span class="s">"All is good"</span><span class="p">)</span>
|
||||
</code></pre></div></div>
|
||||
|
||||
<h2 id="compiled-python">Compiled python</h2>
|
||||
<p>This one is interesting. Python, like Java, is compiled into bytecode. So yes, it technically is a compiled language. To see said bytecode, take a look at any <code class="highlighter-rouge">.pyc</code> file sitting in your <code class="highlighter-rouge">__pycache__</code></p>
|
||||
|
||||
<h2 id="blog-formatting-experiments">Blog formatting experiments</h2>
|
||||
<p>I am still playing with post formats, and various types of content. This is more random than I usually prefer. Let me know your thoughts on the social media platform of your choosing.</p>
|
||||
</p>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
</div>
|
||||
|
||||
<!-- Footer -->
|
||||
<footer id="footer">
|
||||
<div class="inner">
|
||||
<ul class="icons">
|
||||
|
||||
<li><a href="https://twitter.com/ewpratten" class="icon alt fa-twitter" target="_blank"><span class="label">Twitter</span></a></li>
|
||||
|
||||
<li><a href="https://gitlab.com/u/ewpratten" class="icon alt fa-gitlab" target="_blank"><span class="label">GitLab</span></a></li>
|
||||
|
||||
<li><a href="https://github.com/ewpratten" class="icon alt fa-github" target="_blank"><span class="label">GitHub</span></a></li>
|
||||
|
||||
|
||||
<li><a href="/feed.xml" class="icon alt fa-rss" target="_blank"><span class="label">RSS</span></a></li>
|
||||
</ul>
|
||||
<ul class="copyright">
|
||||
<li>© Evan Pratten retrylife</li>
|
||||
<li>Design: <a href="https://html5up.net" target="_blank">HTML5 UP</a></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
</footer>
|
||||
|
||||
</div>
|
||||
|
||||
<!-- Scripts -->
|
||||
<script src="http://localhost:4000/assets/js/jquery.min.js"></script>
|
||||
<script src="http://localhost:4000/assets/js/jquery.scrolly.min.js"></script>
|
||||
<script src="http://localhost:4000/assets/js/jquery.scrollex.min.js"></script>
|
||||
<script src="http://localhost:4000/assets/js/skel.min.js"></script>
|
||||
<script src="http://localhost:4000/assets/js/util.js"></script>
|
||||
<!--[if lte IE 8]><script src="http://localhost:4000/assets/js/ie/respond.min.js"></script><![endif]-->
|
||||
<script src="http://localhost:4000/assets/js/main.js"></script>
|
||||
|
||||
<!-- Global site tag (gtag.js) - Google Analytics -->
|
||||
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-74118570-2"></script>
|
||||
<script>
|
||||
window.dataLayer = window.dataLayer || [];
|
||||
|
||||
function gtag() {
|
||||
dataLayer.push(arguments);
|
||||
}
|
||||
gtag('js', new Date());
|
||||
|
||||
gtag('config', 'UA-74118570-2');
|
||||
</script>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
Loading…
x
Reference in New Issue
Block a user