Exemple #1
0
def sanitize_packet(file_id):

    data = json.loads(request.data)

    traceFile = TraceFile.query.get_or_404(file_id)

    timestamp = datetime.datetime.now().strftime('%y%m%d-%H%m%S')
    uuid_filename = '.'.join([str(uuid.uuid4()), traceFile.filetype])

    print data['sequential']

    try:
        sanitize(filepath_in=os.path.join(UPLOAD_FOLDER, traceFile.filename),
                 filepath_out=os.path.join(UPLOAD_FOLDER, uuid_filename),
                 sequential=data['sequential'],
                 ipv4_mask=int([0, data['ipv4_mask']
                                ][len(data['ipv4_mask']) > 0]),
                 ipv6_mask=int([0, data['ipv6_mask']
                                ][len(data['ipv6_mask']) > 0]),
                 mac_mask=int([0,
                               data['mac_mask']][len(data['mac_mask']) > 0]),
                 start_ipv4=['10.0.0.1',
                             data['start_ipv4']][len(data['start_ipv4']) > 0],
                 start_ipv6=['2001:aa::1',
                             data['start_ipv6']][len(data['start_ipv6']) > 0],
                 start_mac=['00:aa:00:00:00:00',
                            data['start_mac']][len(data['start_mac']) > 0])
    except Exception as e:
        flash(
            'Sanitizing - %s: %s.%s' %
            (e.message, traceFile.name, traceFile.filetype), 'danger')
        log(
            'error', 'Sanitizing - %s: %s.%s' %
            (e.message, traceFile.name, traceFile.filetype))

    new_file = TraceFile(
        id=str(uuid.uuid4())[:8],
        name=secure_filename(traceFile.name + '_sanitized_' + timestamp),
        user_id=current_user.id,
        filename=uuid_filename,
        filetype=traceFile.filetype,
        filesize=os.path.getsize(os.path.join(UPLOAD_FOLDER, uuid_filename)),
        packet_count=get_capture_count(uuid_filename),
        date_added=datetime.datetime.now())

    db.session.add(new_file)
    db.session.commit()
    db.session.refresh(new_file)

    new_tag = Tag(name='Sanitized', file_id=new_file.id)
    db.session.add(new_tag)
    db.session.commit()

    flash('File sanitized: %s.%s' % (new_file.name, traceFile.filetype),
          'success')
    log(
        'info', 'File sanitized by \'%s\': %s.' %
        (current_user.username, new_file.name))

    return jsonify({'Result': 'Success'}), 200
Exemple #2
0
def api_upload_file(token):
    print('!!!!!!!!!!!!!!!!!!!!!!!')
    try:
        user = User.query.filter_by(token=token).one()
    except NoResultFound:
        return json.dumps({
            "status": 404,
            "exceptions": ["API Token is missing or invalid"]
        }), 404

    if request.method == 'POST':
        traceFile = request.files['file']
        filename = traceFile.filename
        filetype = splitext(filename)[1].strip('.')
        uuid_filename = '.'.join([str(uuid.uuid4()), filetype])
        traceFile.save(os.path.join(UPLOAD_FOLDER, uuid_filename))

    else:
        filename = request.args.get('filename')
        filetype = splitext(filename)[1].strip('.')
        uuid_filename = '.'.join([str(uuid.uuid4()), filetype])
        with open(os.path.join(UPLOAD_FOLDER, uuid_filename), 'w') as f:
            f.write(request.stream.read())

    if allowed_file(filename):

        new_file = TraceFile(id=str(uuid.uuid4())[:8],
                             name=secure_filename(splitext(filename)[0]),
                             user_id=user.id,
                             filename=uuid_filename,
                             filetype=filetype,
                             filesize=os.path.getsize(
                                 os.path.join(UPLOAD_FOLDER, uuid_filename)),
                             packet_count=get_capture_count(uuid_filename),
                             date_added=datetime.datetime.now(),
                             status=1)

        db.session.add(new_file)
        db.session.commit()
        db.session.refresh(new_file)

        #add tags
        if request.form.getlist('additional_tags'):
            for tag in request.form.getlist('additional_tags')[0].split(','):
                if tag.strip(',') != '':
                    new_tag = Tag(name=tag.strip(','), file_id=new_file.id)
                    db.session.add(new_tag)

        db.session.commit()

        log('info', 'File uploaded by \'%s\': %s.' % (user.username, filename))
        return json.dumps({"filename": filename, "id": new_file.id}), 202

    else:
        os.remove(os.path.join(UPLOAD_FOLDER, uuid_filename))
        return json.dumps({
            "status":
            406,
            "exceptions": ["Not a valid file type. (pcap, pcapng, cap)"]
        }), 406
Exemple #3
0
def api_upload_file(token):
    
    try:
        user = User.query.filter_by(token=token).one()
    except NoResultFound:
        return json.dumps({"status":404,"exceptions":["API Token is missing or invalid"]}), 404

    if request.method == 'POST':
        traceFile = request.files['file']
        filename = traceFile.filename
        filetype = splitext(filename)[1].strip('.')
        uuid_filename = '.'.join([str(uuid.uuid4()),filetype])
        traceFile.save(os.path.join(UPLOAD_FOLDER, uuid_filename))

    else:
        filename = request.args.get('filename')
        filetype = splitext(filename)[1].strip('.')
        uuid_filename = '.'.join([str(uuid.uuid4()),filetype])
        with open(os.path.join(UPLOAD_FOLDER, uuid_filename), 'w') as f:
            f.write(request.stream.read())

    if allowed_file(filename):

        new_file = TraceFile(id=str(uuid.uuid4())[:8],
            name=secure_filename(splitext(filename)[0]),
            user_id = user.id,
            filename = uuid_filename,
            filetype = filetype,
            filesize = os.path.getsize(os.path.join(UPLOAD_FOLDER, uuid_filename)),
            packet_count = get_capture_count(uuid_filename),
            date_added = datetime.datetime.now()
            )

        db.session.add(new_file)
        db.session.commit()
        db.session.refresh(new_file)

        #add tags
        if request.form.getlist('additional_tags'):
            for tag in request.form.getlist('additional_tags')[0].split(','):
                if tag.strip(',') != '':
                    new_tag = Tag(name = tag.strip(','), file_id=new_file.id)
                    db.session.add(new_tag)

        db.session.commit()

        log('info','File uploaded by \'%s\': %s.' % (user.username, filename))
        return json.dumps({"filename": filename,"id":new_file.id}), 202

    else:
        os.remove(os.path.join(UPLOAD_FOLDER, uuid_filename))
        return json.dumps({"status":406,"exceptions":["Not a valid file type. (pcap, pcapng, cap)"]}), 406
Exemple #4
0
def capture():
    try:
        token = session['user_token']
        user = User.query.filter_by(token=token).one()
    except NoResultFound:
        return '404'
    netCard = request.form['netCard']
    catchTime = request.form['catchTime']
    pcapName = request.form['pcapName'] + '.pcap'
    if not catchTime:
        return "406"
    cmd = 'tshark -i ' + netCard + ' -a duration:' + catchTime + ' -w ' + os.path.join(
        CATCH_FOLDER, pcapName)
    subprocess.call(cmd, shell=True)
    with open(os.path.join(CATCH_FOLDER, pcapName), 'r') as traceFile:
        (filename, filetype) = tuple(traceFile.name.split('.'))
        uuid_filename = '.'.join([str(uuid.uuid4()), filetype])
        try:
            shutil.copyfile(os.path.join(CATCH_FOLDER, pcapName),
                            os.path.join(UPLOAD_FOLDER, uuid_filename))
        except:
            return '407'

    if allowed_file(pcapName):
        new_file = TraceFile(id=str(uuid.uuid4())[:8],
                             name=secure_filename(splitext(pcapName)[0]),
                             user_id=user.id,
                             filename=uuid_filename,
                             filetype=filetype,
                             filesize=os.path.getsize(
                                 os.path.join(UPLOAD_FOLDER, uuid_filename)),
                             packet_count=get_capture_count(uuid_filename),
                             date_added=datetime.datetime.now())

        db.session.add(new_file)
        db.session.commit()
        db.session.refresh(new_file)

        #add tags
        # if request.form.getlist('additional_tags'):
        #     for tag in request.form.getlist('additional_tags')[0].split(','):
        #         if tag.strip(',') != '':
        #             new_tag = Tag(name = tag.strip(','), file_id=new_file.id)
        #             db.session.add(new_tag)

        db.session.commit()
        log('info', 'File capture by : %s.' % (filename))
    else:
        os.remove(os.path.join(UPLOAD_FOLDER, uuid_filename))
        return '409'

    return '203'
Exemple #5
0
def sanitize_packet(file_id):

    data = json.loads(request.data)
    
    traceFile = TraceFile.query.get_or_404(file_id)

    timestamp = datetime.datetime.now().strftime('%y%m%d-%H%m%S')
    uuid_filename = '.'.join([str(uuid.uuid4()),traceFile.filetype])
    
    print data['sequential']

    try:
        sanitize(filepath_in = os.path.join(UPLOAD_FOLDER, traceFile.filename), 
            filepath_out = os.path.join(UPLOAD_FOLDER, uuid_filename),
            sequential= data['sequential'],
            ipv4_mask= int([0, data['ipv4_mask']][len(data['ipv4_mask']) > 0]),
            ipv6_mask= int([0, data['ipv6_mask']][len(data['ipv6_mask']) > 0]),
            mac_mask=    int([0, data['mac_mask']][len(data['mac_mask']) > 0]),
            start_ipv4= ['10.0.0.1', data['start_ipv4']][len(data['start_ipv4']) > 0],
            start_ipv6= ['2001:aa::1', data['start_ipv6']][len(data['start_ipv6']) > 0],
            start_mac=  ['00:aa:00:00:00:00', data['start_mac']][len(data['start_mac']) > 0]
            )
    except Exception as e:
        flash('Sanitizing - %s: %s.%s' % (e.message, traceFile.name, traceFile.filetype), 'danger')
        log('error', 'Sanitizing - %s: %s.%s' % (e.message, traceFile.name, traceFile.filetype))

    new_file = TraceFile(id=str(uuid.uuid4())[:8],
        name=secure_filename(traceFile.name + '_sanitized_' + timestamp),
        user_id = current_user.id,
        filename = uuid_filename,
        filetype = traceFile.filetype,
        filesize = os.path.getsize(os.path.join(UPLOAD_FOLDER, uuid_filename)),
        packet_count = get_capture_count(uuid_filename),
        date_added = datetime.datetime.now()
        )


    db.session.add(new_file)
    db.session.commit()
    db.session.refresh(new_file)

    new_tag = Tag(name='Sanitized', file_id=new_file.id)
    db.session.add(new_tag)
    db.session.commit()

    flash('File sanitized: %s.%s' % (new_file.name, traceFile.filetype) , 'success')
    log('info','File sanitized by \'%s\': %s.' % (current_user.username, new_file.name))

    return jsonify({'Result': 'Success'}), 200
Exemple #6
0
def detect():
    file_id = request.form['file_id']
    file_name = request.form['file_name']
    oriFile = TraceFile.query.filter_by(id=file_id).one()
    name = oriFile.name
    uuName = oriFile.filename
    afterName = getFingerPrint.isTorExists(name, uuName)
    print afterName
    try:
        token = session['user_token']
        user = User.query.filter_by(token=token).one()
    except NoResultFound:
        return '404'

    with open(os.path.join(CATCH_FOLDER, afterName), 'r') as traceFile:
        (filename, filetype) = tuple(traceFile.name.split('.'))
        uuid_filename = '.'.join([str(uuid.uuid4()), filetype])
        try:
            shutil.copyfile(os.path.join(CATCH_FOLDER, afterName),
                            os.path.join(UPLOAD_FOLDER, uuid_filename))
        except:
            print 'copy fail'
            return '407'

    if allowed_file(afterName):
        new_file = TraceFile(id=str(uuid.uuid4())[:8],
                             name=secure_filename(splitext(afterName)[0]),
                             user_id=user.id,
                             filename=uuid_filename,
                             filetype=filetype,
                             filesize=os.path.getsize(
                                 os.path.join(UPLOAD_FOLDER, uuid_filename)),
                             packet_count=get_capture_count(uuid_filename),
                             date_added=datetime.datetime.now())

        db.session.add(new_file)
        db.session.commit()
        db.session.refresh(new_file)
        db.session.commit()
        log('info', 'File capture by : %s.' % (filename))
        print 'finish'
    else:
        os.remove(os.path.join(UPLOAD_FOLDER, uuid_filename))
        return '409'

    return '204'
Exemple #7
0
def stop_capture():
    global sniffer
    global isRunning

    try:
        params = json.loads(request.form.to_dict()['data'])
        temp_id = params['temp_id']

        if temp_id is None or temp_id == '':
            return json.dumps({
                "status":
                406,
                "message": [{
                    'type': "warning",
                    "message": "Templte ID was not specified."
                }]
            })

        template = sniffer.getTemplate()

        if template is None:
            return json.dumps({
                "status":
                406,
                "message": [{
                    'type': "warning",
                    "message": "Template isn't started yet."
                }]
            })

        if temp_id != template.id:
            return json.dumps({
                "status":
                406,
                "message": [{
                    'type': "warning",
                    "message": template.name + " isn't started yet."
                }]
            })

        template_name = template.name

    except Exception as e:
        return json.dumps({
            "status": 406,
            "message": [{
                'type': "warning",
                "message": e
            }]
        })

    try:
        [filename, temp_id] = sniffer.stop()

        if filename is not None:
            file = TraceFile.query.filter_by(filename=filename,
                                             status=1).first()

            if file is not None:
                file.user_id = current_user.id
                file.filesize = os.path.getsize(
                    os.path.join(UPLOAD_FOLDER, filename))
                file.packet_count = get_capture_count(filename)
                file.date_added = datetime.datetime.now()
                db.session.commit()
            else:
                filetype = splitext(filename)[1].strip('.')
                uuid_filename = '.'.join([str(uuid.uuid4()), filetype])

                new_file = TraceFile(id=str(uuid.uuid4())[:8],
                                     name=secure_filename(
                                         splitext(filename)[0]),
                                     user_id=current_user.id,
                                     filename=filename,
                                     filetype=filetype,
                                     filesize=os.path.getsize(
                                         os.path.join(UPLOAD_FOLDER,
                                                      filename)),
                                     packet_count=get_capture_count(filename),
                                     date_added=datetime.datetime.now(),
                                     status=1)

                db.session.add(new_file)
                db.session.commit()
                db.session.refresh(new_file)

        sniffer.join()

    except Exception as e:
        print(e)
        log('error', 'Exception: %s' % e)
        return render_template('500.html', e=e), 500
    return json.dumps({
        'status':
        200,
        "temp_id":
        temp_id,
        "message": [{
            'type': "success",
            "message": template_name + " was stopped now."
        }]
    })
Exemple #8
0
    def run(self):
        self.frame_no = 0
        self.start_time = datetime.now()
        self.template = Template.query.filter_by(id=self.temp_id).one()
        extra_params = shlex.split(self.template.command)
        isNext = False
        self.filename = None
        params = []

        for param in extra_params:
            if isNext:
                self.filename = param
                isNext = False
                continue

            if param == '-w':
                isNext = True
                continue

            params += [param]

        param_str = ' '.join(params)

        if self.filename is not None:
            self.output_file = SAVE_FOLDER_PATH + self.filename

        capture = pyshark.LiveCapture(extra_params_str=param_str,
                                      output_file=self.output_file)
        capture.set_debug()

        self.processStatus.start()

        isFirst = True

        for p, pid in capture.sniff_continuously():
            if isFirst:
                print('Started capuring')
                try:

                    self.template.process_id = pid
                    self.db.session.commit()
                except Exception as e:
                    print(e)

            self.perPacket(p, pid)
            isFirst = False

        self.processStatus._stopper.set()
        self.processStatus.join()

        print('stopped')

        if self.filename is not None:
            file = TraceFile.query.filter_by(filename=self.filename,
                                             status=1).first()

            if file is not None:
                file.user_id = self.user_id
                file.filesize = os.path.getsize(SAVE_FOLDER_PATH +
                                                self.filename)
                file.packet_count = get_capture_count(self.filename)
                file.date_added = datetime.now()
                self.db.session.commit()
            else:
                filetype = splitext(self.filename)[1].strip('.')
                uuid_filename = '.'.join([str(uuid.uuid4()), filetype])

                new_file = TraceFile(
                    id=str(uuid.uuid4())[:8],
                    name=secure_filename(splitext(self.filename)[0]),
                    user_id=self.user_id,
                    filename=self.filename,
                    filetype=filetype,
                    filesize=os.path.getsize(SAVE_FOLDER_PATH + self.filename),
                    packet_count=get_capture_count(self.filename),
                    date_added=datetime.now(),
                    status=1)

                self.db.session.add(new_file)
                self.db.session.commit()
                self.db.session.refresh(new_file)

        self.template = None
        self.socketio.emit('stopcapture', {
            'data': {
                'temp_id':
                self.temp_id,
                "message": [{
                    'type': 'success',
                    'message': "Device was stopped now."
                }]
            }
        },
                           namespace='/stopcapture')
        #self.socketio.emit('stopcapture', {'data': {'temp_id':self.temp_id, "message":[{'type':'warning', 'message':'Incorrect tshark parameters.'}]}}, namespace='/stopcapture')
        sys.exit()