<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://book.ghanim.no/index.php?action=history&amp;feed=atom&amp;title=Programming%2FPython_-_Notes_and_Examples</id>
	<title>Programming/Python - Notes and Examples - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://book.ghanim.no/index.php?action=history&amp;feed=atom&amp;title=Programming%2FPython_-_Notes_and_Examples"/>
	<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/Python_-_Notes_and_Examples&amp;action=history"/>
	<updated>2026-04-21T13:03:39Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://book.ghanim.no/index.php?title=Programming/Python_-_Notes_and_Examples&amp;diff=1206&amp;oldid=prev</id>
		<title>imported&gt;Aghanim at 08:22, 14 December 2022</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=Programming/Python_-_Notes_and_Examples&amp;diff=1206&amp;oldid=prev"/>
		<updated>2022-12-14T08:22:18Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[File:2022-12-Python-Symbol.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here are my notes from different courses I&amp;#039;m taking.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Courses and resources =&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://www.udemy.com/course/learn-python-and-ethical-hacking-from-scratch/ https://www.udemy.com/course/learn-python-and-ethical-hacking-from-scratch/]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://tryhackme.com/room/pythonbasics https://tryhackme.com/room/pythonbasics]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://tryhackme.com/room/pythonforcybersecurity https://tryhackme.com/room/pythonforcybersecurity]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://blog.aghanim.net/wp-content/uploads/2023/01/pythonlearn.pdf pythonlearn][https://blog.aghanim.net/wp-content/uploads/2023/01/pythonlearn.pdf Download]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Notes from &amp;#039;[https://www.udemy.com/course/learn-python-and-ethical-hacking-from-scratch/ Learn Python &amp;amp; Ethical Hacking From Scratch]&amp;#039; =&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Lecture 1 - MAC Address Changer ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/subprocess.html https://docs.python.org/3/library/subprocess.html]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Change MAC address using subprocess ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/python3&lt;br /&gt;
&lt;br /&gt;
# Import will import a python module. For example here I&amp;#039;m importing subprocess.&lt;br /&gt;
# Subprocess is used to run system commands on either linux or windows.&lt;br /&gt;
import subprocess&lt;br /&gt;
&lt;br /&gt;
# In pycharm I can use ctrl + d to copy and paste the same line under it.&lt;br /&gt;
# Here Im telling python to take down eth0, change its mac, and start it up again&lt;br /&gt;
subprocess.call(&amp;quot;ifconfig eth0 down&amp;quot;, shell=True)&lt;br /&gt;
subprocess.call(&amp;quot;ifconfig eth0 hw ether 00:11:22:33:44:55&amp;quot;, shell=True)&lt;br /&gt;
subprocess.call(&amp;quot;ifconfig eth0 up&amp;quot;, shell=True)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Script upgrade using variables ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/python3&lt;br /&gt;
&lt;br /&gt;
import subprocess&lt;br /&gt;
&lt;br /&gt;
# Here we are using variables. We are setting interface to eth0 and new_mac to the 00:11...&lt;br /&gt;
# We are also printing a statement to the user, and appending the interface and new mac to its easier to understand what is happening.&lt;br /&gt;
interface = &amp;quot;eth0&amp;quot;&lt;br /&gt;
new_mac = &amp;quot;00:11:22:33:44:77&amp;quot;&lt;br /&gt;
print(&amp;quot;[+] Changing the MAC address for &amp;quot; + interface + &amp;quot; to &amp;quot; + new_mac)&lt;br /&gt;
&lt;br /&gt;
# We replace the hardcoded interface and mac to using the variables instead.&lt;br /&gt;
subprocess.call(&amp;quot;ifconfig &amp;quot; + interface + &amp;quot; down&amp;quot;, shell=True)&lt;br /&gt;
subprocess.call(&amp;quot;ifconfig &amp;quot; + interface + &amp;quot; hw ether &amp;quot; + new_mac, shell=True)&lt;br /&gt;
subprocess.call(&amp;quot;ifconfig &amp;quot; + interface + &amp;quot; up&amp;quot;, shell=True)&lt;br /&gt;
&lt;br /&gt;
# We are printing ifconfig eth0 and grepping the mac to show the user that the mac have actually changed&lt;br /&gt;
print(subprocess.call(&amp;quot;ifconfig &amp;quot; + interface + &amp;quot; |grep ether&amp;quot;, shell=True))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Input from user ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/python3&lt;br /&gt;
&lt;br /&gt;
import subprocess&lt;br /&gt;
&lt;br /&gt;
# Here we are asking a user to input an interface and a new mac. So when executing the script it will look like the pic below.&lt;br /&gt;
interface = input(&amp;quot;Interface &amp;gt; &amp;quot;)&lt;br /&gt;
new_mac = input(&amp;quot;new mac &amp;gt; &amp;quot;)&lt;br /&gt;
print(&amp;quot;[+] Changing the MAC address for &amp;quot; + interface + &amp;quot; to &amp;quot; + new_mac)&lt;br /&gt;
&lt;br /&gt;
subprocess.call(&amp;quot;ifconfig &amp;quot; + interface + &amp;quot; down&amp;quot;, shell=True)&lt;br /&gt;
subprocess.call(&amp;quot;ifconfig &amp;quot; + interface + &amp;quot; hw ether &amp;quot; + new_mac, shell=True)&lt;br /&gt;
subprocess.call(&amp;quot;ifconfig &amp;quot; + interface + &amp;quot; up&amp;quot;, shell=True)&lt;br /&gt;
&lt;br /&gt;
print(subprocess.call(&amp;quot;ifconfig &amp;quot; + interface + &amp;quot; |grep ether&amp;quot;, shell=True))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:2022-12-Pasted-image-20221213151619.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Handling user input ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The above example is not a secure way as any user can input whatever they like in the inputfield.&lt;br /&gt;
For example&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
python3 main.py&lt;br /&gt;
interface &amp;gt; whoami; id ;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The above command will execute &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; command since I&amp;#039;ve added and AND statement.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A more secure way to do it is like this&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/python3&lt;br /&gt;
&lt;br /&gt;
import subprocess&lt;br /&gt;
&lt;br /&gt;
interface = input(&amp;quot;Interface &amp;gt; &amp;quot;)&lt;br /&gt;
new_mac = input(&amp;quot;new mac &amp;gt; &amp;quot;)&lt;br /&gt;
print(&amp;quot;[+] Changing the MAC address for &amp;quot; + interface + &amp;quot; to &amp;quot; + new_mac)&lt;br /&gt;
&lt;br /&gt;
# Here we are providing a list of commands. So the variable interface will be treated as an argument for ifconfig, instead of a seperate command as show above.&lt;br /&gt;
subprocess.call([&amp;quot;ifconfig&amp;quot;, interface, &amp;quot;down&amp;quot;])&lt;br /&gt;
subprocess.call([&amp;quot;ifconfig&amp;quot;, interface, &amp;quot;hw&amp;quot;, &amp;quot;ether&amp;quot;, new_mac])&lt;br /&gt;
subprocess.call([&amp;quot;ifconfig&amp;quot;, interface, &amp;quot;up&amp;quot;])&lt;br /&gt;
&lt;br /&gt;
print(subprocess.call(&amp;quot;ifconfig &amp;quot; + interface + &amp;quot; |grep ether&amp;quot;, shell=True))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An example of training to hijack the script. As you can see that didnt work because &amp;lt;code&amp;gt;ifconfig&amp;lt;/code&amp;gt; does not have an argument named &amp;lt;code&amp;gt;eth0;id&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:2023-01-image.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Command-line arguments ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/python3&lt;br /&gt;
&lt;br /&gt;
import subprocess&lt;br /&gt;
import argparse&lt;br /&gt;
&lt;br /&gt;
# Everything that start with a capital letter is a class. Class is a code or blueprint for the object. It determine&lt;br /&gt;
# what we can do with the parser object.&lt;br /&gt;
parser = argparse.ArgumentParser()&lt;br /&gt;
&lt;br /&gt;
# Here we are saying that when a user specify their argument -i, it will save the output to dest=interface.&lt;br /&gt;
# Same with the -m.&lt;br /&gt;
# And we are telling it to show a help menu if a user types -h or --help.&lt;br /&gt;
parser.add_argument(&amp;quot;-i&amp;quot;, &amp;quot;--interface&amp;quot;, dest=&amp;quot;interface&amp;quot;, help=&amp;quot;Interface to change it&amp;#039;s MAC address&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;-m&amp;quot;, &amp;quot;--mac&amp;quot;, dest=&amp;quot;new_mac&amp;quot;, help=&amp;quot;New MAC address&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Then we parse the input. Allow the object to under what the user have typed and handled it.&lt;br /&gt;
options = parser.parse_args()&lt;br /&gt;
&lt;br /&gt;
# We are then saving the output from the parser to interface. Options contains the value that the user inputs.&lt;br /&gt;
# Then we just type options.interface to get the output from --interface and options.new_mac to get the output from&lt;br /&gt;
# --mac.&lt;br /&gt;
interface = options.interface&lt;br /&gt;
new_mac = options.new_mac&lt;br /&gt;
&lt;br /&gt;
print(&amp;quot;[+] Changing the MAC address for &amp;quot; + interface + &amp;quot; to &amp;quot; + new_mac)&lt;br /&gt;
&lt;br /&gt;
subprocess.call([&amp;quot;ifconfig&amp;quot;, interface, &amp;quot;down&amp;quot;])&lt;br /&gt;
subprocess.call([&amp;quot;ifconfig&amp;quot;, interface, &amp;quot;hw&amp;quot;, &amp;quot;ether&amp;quot;, new_mac])&lt;br /&gt;
subprocess.call([&amp;quot;ifconfig&amp;quot;, interface, &amp;quot;up&amp;quot;])&lt;br /&gt;
&lt;br /&gt;
print(subprocess.call(&amp;quot;ifconfig &amp;quot; + interface&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:2023-01-image-1.png|thumb]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Functions ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/python3&lt;br /&gt;
&lt;br /&gt;
import subprocess&lt;br /&gt;
import argparse&lt;br /&gt;
&lt;br /&gt;
# To define a functions start by typing &amp;quot;def&amp;quot; and a function name. Then specify the input the functions take. In order to change the mac we need an interface and a mac adress, so we will use two variables. Then variable names can be whatever. So if I type X instead of interface, I have to type X in all the following code in the function.&lt;br /&gt;
# Then add the code that the function will run by typing the code under it. See the ident.&lt;br /&gt;
def change_mac(interface, new_mac):&lt;br /&gt;
    print(&amp;quot;[+] Changing the MAC address for &amp;quot; + interface + &amp;quot; to &amp;quot; + new_mac)&lt;br /&gt;
&lt;br /&gt;
    subprocess.call([&amp;quot;ifconfig&amp;quot;, interface, &amp;quot;down&amp;quot;])&lt;br /&gt;
    subprocess.call([&amp;quot;ifconfig&amp;quot;, interface, &amp;quot;hw&amp;quot;, &amp;quot;ether&amp;quot;, new_mac])&lt;br /&gt;
    subprocess.call([&amp;quot;ifconfig&amp;quot;, interface, &amp;quot;up&amp;quot;])&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
parser = argparse.ArgumentParser()&lt;br /&gt;
&lt;br /&gt;
parser.add_argument(&amp;quot;-i&amp;quot;, &amp;quot;--interface&amp;quot;, dest=&amp;quot;interface&amp;quot;, help=&amp;quot;Interface to change it&amp;#039;s MAC address&amp;quot;)&lt;br /&gt;
parser.add_argument(&amp;quot;-m&amp;quot;, &amp;quot;--mac&amp;quot;, dest=&amp;quot;new_mac&amp;quot;, help=&amp;quot;New MAC address&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
options = parser.parse_args()&lt;br /&gt;
&lt;br /&gt;
# If we never call the function, it will not run the code in the function, so we have to call it as shown below.&lt;br /&gt;
# In the function we know that we have to give it an interface name and a new mac. So we will use the argparse options.interface and options.new_mac as we saved them in the parser.&lt;br /&gt;
change_mac(options.interface, options.new_mac)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Programming]]&lt;br /&gt;
[[Category:Python]]&lt;/div&gt;</summary>
		<author><name>imported&gt;Aghanim</name></author>
	</entry>
</feed>