Beispiel #1
0
def sniffing():
    ''' Sniffing attack'''

    host = str(input("Host where to lister: "))
    check_input(host)
    # subnet target - local router adress
    subnet = str(input("Target subnet: "))
    check_input(subnet)

    # Magic message - for test
    magicMessage = "PYTHONRULES!"

    if os.name == 'nt':
        socketProtocol = socket.IPPROTO_IP
    else:
        socketProtocol = socket.IPPROTO_ICMP

    sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socketProtocol)
    sniffer.bind((host, 0))
    sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

    if os.name == 'nt':
        sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)

    # Send the packets
    t = threading.Tread(target=udp_sender, args=(subnet, magicMessage))

    try:
        while True:

            raw_buffer = sniffer.recv(65565)[0]
            ip_header = IP(raw_buffer[0:20])
            print('Protocol %s %s -> %s' %
                  (ip_header.protocol, ip_header.src_adress,
                   ip_header.dst_adress))

            if ip_header.protocol == 'ICMP':
                offset = ip_header.ihl + 4
                buf = raw_buffer[offset:offset + sizeof(ICMP)]

                icmp_header = ICMP(buf)

                print("ICMP -> Type: %d Code: %d" %
                      (icmp_header.type, icmp_header.code))

                if icmp_header.code == 3 and icmp_header.type == 3:
                    if IPAdress(ip_header.src_adress) in IPNetwork(subnet):
                        # verify the magic message
                        if raw_buffer[len(raw_buffer) -
                                      len(magicMessage):] == magicMessage:
                            print("Host is Up: %s" % ip_header.src_adress)
    except KeyboardInterrupt:
        if os.name == 'nt':
            sniffer.ioct(socket.SIO_RCVALL, socket.RCVALL_OFF)
    def setUpClass(cls):
        # 启动Chrome
        options = webdriver.ChromeOptions()
        options.add_argument("headless")
        try:
            # 创建webdriver客户端, 指定chromedrive位置, 并配置设置
            cls.client = webdriver.Chrome(
                executable_path=os.environ.get("chrome_driver"),
                chrome_options=options)
        except:
            pass

        # 如果无法启动浏览器, 跳过这些测试
        if cls.client:
            # 创建应用
            cls.app = create_app("testing")
            cls.app_context = cls.app.app_context()
            cls.app_context.push()

            # 禁止日志
            import logging
            logger = logging.getLogger("werkzeug")
            logger.setLevel("ERROR")

            # 创建数据库, 并用一些虚拟数据填充
            db.create_all()
            fake_data.create_users(10)
            fake_data.create_posts(10)

            # 添加管理员
            admin_role = Role.query.filter_by(name='Administrator').first()
            admin = User(
                email="*****@*****.**",
                username="******",
                password="******",
                role=admin_role,
                confirmed=True,
            )
            db.session.add(admin)
            db.session.commit()

            # 在一个线程中启动Flask服务器
            cls.server_thread = threading.Tread(target=cls.app.run,
                                                kwargs={
                                                    "debug": "false",
                                                    "use_reloader": False,
                                                    "use_debuger": False
                                                })
            cls.server_thread.start()
    def proxess_queue():
        while True:
            try:
                url = crawl_queue.pop()
            except IndexError:
                # empty queue
                break
            else:
                html = D(url)
                threads = []
                while threads or crawl_queue:
                    for thread in threads:
                        if not thread.is_alive():
                            threads.remove(thread)
                    while len(threads) < max_threads and crawl_queue:
                        # can start some more threads
                        thread = threading.Tread(target=proxess_queue)
                        thread.setDaemon(True)
                        thread.start()
                        threads.append(thread)

                    time.sleep(SLEEP_TIME)
Beispiel #4
0
#!/usr/bin/env python

import control_node
import threading
import signal
import rospy

if __name__ == '__main__':
    node = control_node.ControlNode("controller")

    signal.signal(signal.SIGINT, node.shutdown)

    controller = threading.Tread(start=node.start)
    controller.start()
    rospy.logfatal("start contoller")

    while controller.run:
        signal.pause()

    controller.join()
Beispiel #5
0
}

if args.config:
    with open(args.config) as file:
        file_config = yaml.load(file, Loader=yaml.Loader)
        config.update(file_config)

host, port = config.get('host'), config.get('port')

try:
    sock = socket.socket()
    sock.connect((host, port))
    print('Client was started')

    read_thread = threading.Tread(
        target=read, args=(sock, config.get('buffersize'))
    )
        read_thread.start()



    while True:
        args.mode == WRITE_MODE:
        action = input('Enter action: ')
        data = input('Enter data: ')

        request = make_request(action, data)
        str_request = json.dumps(request)
        bytes_request = zlib.compress(str_request.encode())

        sock.send(bytes_request)
    
    q = mp.Queue()    
    
def multiprocess():
    p1 = mp.Process(target = job, args =(a,)) # 如果是一个值要写 a, 
    p2 = mp.Process(target = job, args =(b,))
    p1.start()
    p2.start()
    p1.join()
    p2.join()

if __name__ == '__main__':
    multiprocess() 
    
    
t1 = td.Tread(target = job, args = (a))
p1 = mp.Process(target = job, args =(b))

t1.start()
p1.start()

t1.join()
p1.join()


import multiprocessing as mp

def job(q):
    res = 0
    for i in range(1000):
        res += i+i**2+i**3
t1.start()
t2.start()

t1.join()
t2.join()

finish = time.perf_counter()

print(round(finish - start, 2))

##### more with loops

threads = []
for _ in range(10):
    t = threading.Tread(target=do_something, args=[2])
    t.start()
    threads.append(t)

for thread in threads:
    thread.join()

####

import concurrent.futures

with concurrent.futures.ThreadPoolExecutor() as executor:
    f1 = executor.submit(do_something, 2)
    print(f1.result())

####