Skip to content

Commit e3780df

Browse files
committed
Scapy module
1 parent 5333403 commit e3780df

File tree

5,163 files changed

+978373
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

5,163 files changed

+978373
-1
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
3+
my_string = "Hello world, My name is Alex"
4+
5+
new_string = my_string.split(" ")
6+
7+
words = []
8+
9+
for word in new_string:
10+
word.strip(",.?!")
11+
words.append(word)
12+
13+
print(", ".join(words))
14+
15+
16+
print("I love python"[10])
17+
18+
# Using the find method
19+
20+
print(my_string.find("name"))
21+
print(my_string.find("H"))
22+
print(my_string.rfind("16"))
23+
24+
25+
# Negative indeces
26+
python = "Python"
27+
28+
print(python)
29+
30+
print(python[-5:4])
31+
print(python[-5:-2])
32+
33+
# String concantenation
34+
35+
print("Hello" + " World")
36+
print("ha" * 3)
37+
38+
greet = "Hellllo"
39+
40+
print(greet.count("l"))
41+
42+
43+
# Relational operators
44+
45+
print(10<10.5)
46+
47+
# print(3 < "10") Values of different types cannot be compared
48+
49+
print("Dog" > "dog")
50+
51+
print("fun" in "refunded")
52+

Projects/main.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from kivy.app import App
2+
from kivy.uix.boxlayout import BoxLayout
3+
from kivy.uix.label import Label
4+
from kivy.uix.button import Button
5+
from kivy.graphics import Color, Rectangle
6+
import random
7+
8+
# List of quotes
9+
quotes = [
10+
"The only way to do great work is to love what you do. — Steve Jobs",
11+
"Success is not the key to happiness. Happiness is the key to success. — Albert Schweitzer",
12+
"Your time is limited, don’t waste it living someone else’s life. — Steve Jobs",
13+
"The best way to predict the future is to create it. — Peter Drucker",
14+
"It does not matter how slowly you go as long as you do not stop. — Confucius",
15+
"You miss 100% of the shots you don’t take. — Wayne Gretzky",
16+
"Believe you can and you’re halfway there. — Theodore Roosevelt",
17+
"The only limit to our realization of tomorrow will be our doubts of today. — Franklin D. Roosevelt",
18+
"Act as if what you do makes a difference. It does. — William James",
19+
"Success usually comes to those who are too busy to be looking for it. — Henry David Thoreau"
20+
]
21+
22+
class QuoteGeneratorApp(App):
23+
def build(self):
24+
self.title = 'Random Quote Generator'
25+
26+
layout = BoxLayout(orientation='vertical', padding=10, spacing=10)
27+
28+
# Set a background color for the layout
29+
with layout.canvas.before:
30+
Color(1, 0.9, 0.5, 1) # Light yellow background
31+
self.rect = Rectangle(size=layout.size, pos=layout.pos)
32+
33+
layout.bind(size=self.update_rect, pos=self.update_rect)
34+
35+
# Label to display the quote
36+
self.quote_label = Label(text="", size_hint=(1, 0.8), text_size=(400, None),
37+
halign='center', valign='middle', color=(10, 0.5, 0, 1)) # Green text
38+
self.quote_label.bind(size=self.update_text_size) # Bind size to update text size
39+
layout.add_widget(self.quote_label)
40+
41+
# Button to get a new quote
42+
quote_button = Button(text='Get Random Quote', size_hint=(1, 0.2))
43+
quote_button.bind(on_press=self.get_random_quote)
44+
45+
# Set button background color
46+
with quote_button.canvas.before:
47+
Color(1, 0.65, 0, 1) # Orange background
48+
self.btn_rect = Rectangle(size=quote_button.size, pos=quote_button.pos)
49+
50+
quote_button.bind(size=self.update_button_rect, pos=self.update_button_rect)
51+
layout.add_widget(quote_button)
52+
53+
return layout
54+
55+
def update_text_size(self, *args):
56+
# Update the text size to allow for wrapping
57+
self.quote_label.text_size = (self.quote_label.width, None)
58+
59+
def get_random_quote(self, instance):
60+
random_quote = random.choice(quotes)
61+
self.quote_label.text = random_quote
62+
63+
def update_rect(self, instance, value):
64+
self.rect.size = instance.size
65+
self.rect.pos = instance.pos
66+
67+
def update_button_rect(self, instance, value):
68+
self.btn_rect.size = instance.size
69+
self.btn_rect.pos = instance.pos
70+
71+
if __name__ == '__main__':
72+
QuoteGeneratorApp().run()
-347 Bytes
Binary file not shown.

Socket Programming/hack.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from scapy.all import *
2+
3+
def tcp_syn_scan(target, ports):
4+
print(f"Scanning {target} for open TCP ports...")
5+
for port in ports:
6+
src_port = RandShort()
7+
resp = sr1(IP(dst=target)/TCP(sport=src_port, dport=port, flags="S"), timeout=1, verbose=0)
8+
9+
if resp is None:
10+
print(f"Port {port}: Filtered")
11+
elif resp.haslayer(TCP):
12+
if resp.getlayer(TCP).flags == 0x12: # SYN-ACK
13+
# Send RST to gracefully close the connection
14+
sr(IP(dst=target)/TCP(sport=src_port, dport=port, flags="R"), timeout=1, verbose=0)
15+
print(f"Port {port}: Open")
16+
elif resp.getlayer(TCP).flags == 0x14: # RST-ACK
17+
print(f"Port {port}: Closed")
18+
elif resp.haslayer(ICMP):
19+
icmp_layer = resp.getlayer(ICMP)
20+
if int(icmp_layer.type) == 3 and int(icmp_layer.code) in [1, 2, 3, 9, 10, 13]:
21+
print(f"Port {port}: Filtered")
22+
23+
# Example usage
24+
target = "127.0.0.1"
25+
ports = [21, 22, 80, 443, 3389]
26+
tcp_syn_scan(target, ports)

Socket Programming/sockets.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33

44
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
55

6-
76
print(s)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pip
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Copyright (c) 2014
2+
Rackspace
3+
4+
Permission is hereby granted, free of charge, to any person obtaining
5+
a copy of this software and associated documentation files (the
6+
"Software"), to deal in the Software without restriction, including
7+
without limitation the rights to use, copy, modify, merge, publish,
8+
distribute, sublicense, and/or sell copies of the Software, and to
9+
permit persons to whom the Software is furnished to do so, subject to
10+
the following conditions:
11+
12+
The above copyright notice and this permission notice shall be
13+
included in all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
Metadata-Version: 2.1
2+
Name: Automat
3+
Version: 24.8.1
4+
Summary: Self-service finite-state machines for the programmer on the go.
5+
Author-email: Glyph <[email protected]>
6+
License: Copyright (c) 2014
7+
Rackspace
8+
9+
Permission is hereby granted, free of charge, to any person obtaining
10+
a copy of this software and associated documentation files (the
11+
"Software"), to deal in the Software without restriction, including
12+
without limitation the rights to use, copy, modify, merge, publish,
13+
distribute, sublicense, and/or sell copies of the Software, and to
14+
permit persons to whom the Software is furnished to do so, subject to
15+
the following conditions:
16+
17+
The above copyright notice and this permission notice shall be
18+
included in all copies or substantial portions of the Software.
19+
20+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27+
28+
Project-URL: Documentation, https://automat.readthedocs.io/
29+
Project-URL: Source, https://github.com/glyph/automat/
30+
Keywords: fsm,state machine,automata
31+
Classifier: Intended Audience :: Developers
32+
Classifier: License :: OSI Approved :: MIT License
33+
Classifier: Operating System :: OS Independent
34+
Classifier: Programming Language :: Python
35+
Classifier: Programming Language :: Python :: 3
36+
Classifier: Programming Language :: Python :: 3.8
37+
Classifier: Programming Language :: Python :: 3.9
38+
Classifier: Programming Language :: Python :: 3.10
39+
Classifier: Programming Language :: Python :: 3.11
40+
Classifier: Programming Language :: Python :: 3.12
41+
Requires-Python: >=3.8
42+
Description-Content-Type: text/markdown
43+
License-File: LICENSE
44+
Requires-Dist: typing-extensions; python_version < "3.10"
45+
Provides-Extra: visualize
46+
Requires-Dist: graphviz>0.5.1; extra == "visualize"
47+
Requires-Dist: Twisted>=16.1.1; extra == "visualize"
48+
49+
# Automat #
50+
51+
[![Documentation Status](https://readthedocs.org/projects/automat/badge/?version=latest)](http://automat.readthedocs.io/en/latest/)
52+
[![Build Status](https://github.com/glyph/automat/actions/workflows/ci.yml/badge.svg?branch=trunk)](https://github.com/glyph/automat/actions/workflows/ci.yml?query=branch%3Atrunk)
53+
[![Coverage Status](http://codecov.io/github/glyph/automat/coverage.svg?branch=trunk)](http://codecov.io/github/glyph/automat?branch=trunk)
54+
55+
## Self-service finite-state machines for the programmer on the go. ##
56+
57+
Automat is a library for concise, idiomatic Python expression of finite-state
58+
automata (particularly deterministic finite-state transducers).
59+
60+
Read more here, or on [Read the Docs](https://automat.readthedocs.io/), or watch the following videos for an overview and presentation
61+
62+
### Why use state machines? ###
63+
64+
Sometimes you have to create an object whose behavior varies with its state,
65+
but still wishes to present a consistent interface to its callers.
66+
67+
For example, let's say you're writing the software for a coffee machine. It
68+
has a lid that can be opened or closed, a chamber for water, a chamber for
69+
coffee beans, and a button for "brew".
70+
71+
There are a number of possible states for the coffee machine. It might or
72+
might not have water. It might or might not have beans. The lid might be open
73+
or closed. The "brew" button should only actually attempt to brew coffee in
74+
one of these configurations, and the "open lid" button should only work if the
75+
coffee is not, in fact, brewing.
76+
77+
With diligence and attention to detail, you can implement this correctly using
78+
a collection of attributes on an object; `hasWater`, `hasBeans`, `isLidOpen`
79+
and so on. However, you have to keep all these attributes consistent. As the
80+
coffee maker becomes more complex - perhaps you add an additional chamber for
81+
flavorings so you can make hazelnut coffee, for example - you have to keep
82+
adding more and more checks and more and more reasoning about which
83+
combinations of states are allowed.
84+
85+
Rather than adding tedious `if` checks to every single method to make sure that
86+
each of these flags are exactly what you expect, you can use a state machine to
87+
ensure that if your code runs at all, it will be run with all the required
88+
values initialized, because they have to be called in the order you declare
89+
them.
90+
91+
You can read about state machines and their advantages for Python programmers
92+
in more detail [in this excellent article by Jean-Paul
93+
Calderone](https://web.archive.org/web/20160507053658/https://clusterhq.com/2013/12/05/what-is-a-state-machine/).
94+
95+
### What makes Automat different? ###
96+
97+
There are
98+
[dozens of libraries on PyPI implementing state machines](https://pypi.org/search/?q=finite+state+machine).
99+
So it behooves me to say why yet another one would be a good idea.
100+
101+
Automat is designed around this principle: while organizing your code around
102+
state machines is a good idea, your callers don't, and shouldn't have to, care
103+
that you've done so. In Python, the "input" to a stateful system is a method
104+
call; the "output" may be a method call, if you need to invoke a side effect,
105+
or a return value, if you are just performing a computation in memory. Most
106+
other state-machine libraries require you to explicitly create an input object,
107+
provide that object to a generic "input" method, and then receive results,
108+
sometimes in terms of that library's interfaces and sometimes in terms of
109+
classes you define yourself.
110+
111+
For example, a snippet of the coffee-machine example above might be implemented
112+
as follows in naive Python:
113+
114+
```python
115+
class CoffeeMachine(object):
116+
def brewButton(self) -> None:
117+
if self.hasWater and self.hasBeans and not self.isLidOpen:
118+
self.heatTheHeatingElement()
119+
# ...
120+
```
121+
122+
With Automat, you'd begin with a `typing.Protocol` that describes all of your
123+
inputs:
124+
125+
```python
126+
from typing import Protocol
127+
128+
class CoffeeBrewer(Protocol):
129+
def brewButton(self) -> None:
130+
"The user pressed the 'brew' button."
131+
def putInBeans(self) -> None:
132+
"The user put in some beans."
133+
```
134+
135+
We'll then need a concrete class to contain the shared core of state shared
136+
among the different states:
137+
138+
```python
139+
from dataclasses import dataclass
140+
141+
@dataclass
142+
class BrewerCore:
143+
heatingElement: HeatingElement
144+
```
145+
146+
Next, we need to describe our state machine, including all of our states. For
147+
simplicity's sake let's say that the only two states are `noBeans` and
148+
`haveBeans`:
149+
150+
```python
151+
from automat import TypeMachineBuilder
152+
153+
builder = TypeMachineBuilder(CoffeeBrewer, BrewerCore)
154+
noBeans = builder.state("noBeans")
155+
haveBeans = builder.state("haveBeans")
156+
```
157+
158+
Next we can describe a simple transition; when we put in beans, we move to the
159+
`haveBeans` state, with no other behavior.
160+
161+
```python
162+
# When we don't have beans, upon putting in beans, we will then have beans
163+
noBeans.upon(CoffeeBrewer.putInBeans).to(haveBeans).returns(None)
164+
```
165+
166+
And then another transition that we describe with a decorator, one that *does*
167+
have some behavior, that needs to heat up the heating element to brew the
168+
coffee:
169+
170+
```python
171+
@haveBeans.upon(CoffeeBrewer.brewButton).to(noBeans)
172+
def heatUp(inputs: CoffeeBrewer, core: BrewerCore) -> None:
173+
"""
174+
When we have beans, upon pressing the brew button, we will then not have
175+
beans any more (as they have been entered into the brewing chamber) and
176+
our output will be heating the heating element.
177+
"""
178+
print("Brewing the coffee...")
179+
core.heatingElement.turnOn()
180+
```
181+
182+
Then we finalize the state machine by building it, which gives us a callable
183+
that takes a `BrewerCore` and returns a synthetic `CoffeeBrewer`
184+
185+
```python
186+
newCoffeeMachine = builder.build()
187+
```
188+
189+
```python
190+
>>> coffee = newCoffeeMachine(BrewerCore(HeatingElement()))
191+
>>> machine.putInBeans()
192+
>>> machine.brewButton()
193+
Brewing the coffee...
194+
```
195+
196+
All of the *inputs* are provided by calling them like methods, all of the
197+
*output behaviors* are automatically invoked when they are produced according
198+
to the outputs specified to `upon` and all of the states are simply opaque
199+
tokens.

0 commit comments

Comments
 (0)