コード例 #1
0
ファイル: oldsafe.py プロジェクト: magthe/keysafe
def get_safe(config=None):
    if not config:
        config = cfg.get_config()
    if not _Safe.instance:
        _Safe.instance = _Safe()
        _load_safe(config=config)
    return _Safe.instance
コード例 #2
0
ファイル: oldsafe.py プロジェクト: magthe/keysafe
def save_safe(config=None):
    s = get_safe()
    if not config:
        config = cfg.get_config()
    try:
        fd = file(os.path.expanduser(config['keyfile']), 'w+')
        pickle.dump(s.get_entries(), fd)
        fd.close()
    except pickle.PickleError, e:
        fd.close()
        raise e
コード例 #3
0
    def set_scan_adapter(self, value):
        """ wireless adapter being used to scan / inject """
        if value:
            value = value.encode('ascii')
        else:
            value = None

        if value and pyw.isinterface(value):
            card = pyw.getcard(value)
            if 'monitor' not in pyw.devmodes(card):
                raise Exception(
                    "Error, adapter: {} cannot perform monitor mode".format(
                        value))
        elif value:
            LOG.error("Invalid Card detected: {}, saving None to config")
            value = None
        config = cfg.get_config()
        config[cfg.FIELD_ADAPTER_SCAN] = value
        cfg.write_config(config)
コード例 #4
0
ファイル: models.py プロジェクト: noahtren/code-glyphs
"""Models, optim, and loss functions based on CFG
"""

import code
from types import MethodType
import gc

import tensorflow as tf

from cfg import get_config
CFG = get_config()
from language import LangEncoder, LangDecoder, \
  sequence_reconstruction_loss, sequence_reconstruction_accuracy
from generator import CNNGenerator, CPPNGenerator
from bit_model import BiT
from adamlrm import AdamLRM
from aug import get_noisy_channel

dense_settings = {
    'kernel_regularizer': tf.keras.regularizers.l2(1e-4),
    'bias_regularizer': tf.keras.regularizers.l2(1e-4),
}


class FunnelDown(tf.keras.Model):
    """Reduce representations of each token.
  """
    def __init__(self):
        super(FunnelDown, self).__init__()
        self.l = []
        num_features = CFG['language_model_size']
コード例 #5
0
ファイル: data.py プロジェクト: noahtren/code-glyphs
"""Train on simple Python functions
"""

import code
import os
import json
import re

import jsonlines
from tqdm import tqdm
import matplotlib.pyplot as plt
import tensorflow as tf

from cfg import get_config; CFG = get_config()
from language import batch_encode, tokenizer

file_dir = os.path.dirname(os.path.abspath(__file__))
code_data_dir = file_dir


def read_code_dataset(subdir='train', read_all=True):
  train_data_dir = os.path.join(code_data_dir, 'code_data', subdir)
  pattern = r'(\"\"\"(.|\n)*\"\"\"\s*)'
  data = []
  for i, train_data_file in tqdm(enumerate(os.listdir(train_data_dir))):
    if i > 0 and not read_all:
      break
    data_file_path = os.path.join(train_data_dir, train_data_file)
    with jsonlines.open(data_file_path) as reader:
      for data_line in reader:
        if len(data_line['code_tokens']) <= CFG['max_len']:
コード例 #6
0
class piClickerState(object):
    # in mem
    channel = StateDescriptor(FIELD_CHANNEL, 0)
    status = StateDescriptor(FIELD_STATUS, {})
    queue_sound = StateDescriptor(FIELD_QUEUE_SOUND, [])
    local_ip = StateDescriptor(FIELD_LOCAL_IP, None)
    bssid = StateDescriptor(FIELD_BSSID, None)
    click_rate = StateDescriptor(FIELD_CLICK_RATE, 0)
    sleep_rate = StateDescriptor(FIELD_SLEEP_RATE, 3)
    wavefile = StateDescriptor(FIELD_WAVEFILE, util.get_say('initializing'))
    dbm = StateDescriptor(FIELD_DBM, 0)
    wordlist = StateDescriptor(FIELD_WORDLIST, cfg.DEFAULT_WORDLIST)
    percentage = StateDescriptor(FIELD_PERCENTAGE, 0)
    scan_adapter = StateDescriptor(
        cfg.FIELD_ADAPTER_SCAN,
        cfg.get_config().get(cfg.FIELD_ADAPTER_SCAN))
    has_interwebs = StateDescriptor(FIELD_HAS_INTERWEBS, False)
    can_crack = StateDescriptor(FIELD_CAN_CRACK, False)

    # persitants
    foxlist = PersistantStorage(FIELD_FOX_LIST, [])

    def __init__(self, inet_adapter, polling_interval, scan_adapter=None):
        # register cleanup routine
        atexit.register(self.__cleanup)

        if cfg.ROOT_ONLY and getpass.getuser() != 'root':
            raise Exception("Error, I need to be run as root")
        self.signal_map = {i.get('dbm'): i.get('percentage') for i in DBM_MAP}
        self.inet_adapter = inet_adapter
        self.polling_interval = polling_interval
        avail_wireless = self.get_available_wireless()
        self.me = modules[__name__]

        # set callbacks
        self.__class__.bssid.add_callback(self, self.set_bssid)
        self.__class__.scan_adapter.add_callback(self, self.set_scan_adapter)

        # if I only have one, go ahead and set it
        if len(avail_wireless) == 1:
            self.scan_adapter = avail_wireless[0]

        # Configure the state machine
        self.machine = Machine(
            model=self,
            states=self.__get_states(),
            initial=MODE_INIT,
        )

        ####################################
        # Now we setup all the transitions
        ####################################
        # init -> config
        self.machine.add_transition(
            trigger=TRIG_CONFIG,
            source=MODE_INIT,
            dest=MODE_CONFIG,
            unless=self.can_scan_mode.__name__,
            after=self.set_config_mode.__name__,
        )
        # config -> scanning
        self.machine.add_transition(
            trigger=TRIG_SCAN,
            source=MODE_CONFIG,
            dest=MODE_SCANNING,
            conditions=self.can_scan_mode.__name__,
            after=self.set_scan_mode.__name__,
        )
        # scanning -> crack
        self.machine.add_transition(
            trigger=TRIG_CRACK,
            source=MODE_SCANNING,
            dest=MODE_CRACKING,
            conditions=self.can_crack_mode.__name__,
            after=self.set_crack_mode.__name__,
        )

    ##############################
    # Custom Callbacks
    ##############################
    def set_bssid(self, value):
        LOG.debug("Setting self.status key value to: {}".format(value))
        self.__class__.status.key = value
        self.status = TEMPLATE_STATUS.copy()

    def set_scan_adapter(self, value):
        """ wireless adapter being used to scan / inject """
        if value:
            value = value.encode('ascii')
        else:
            value = None

        if value and pyw.isinterface(value):
            card = pyw.getcard(value)
            if 'monitor' not in pyw.devmodes(card):
                raise Exception(
                    "Error, adapter: {} cannot perform monitor mode".format(
                        value))
        elif value:
            LOG.error("Invalid Card detected: {}, saving None to config")
            value = None
        config = cfg.get_config()
        config[cfg.FIELD_ADAPTER_SCAN] = value
        cfg.write_config(config)

    ##############################
    # Dunders
    ##############################
    def __cleanup(self):
        """ remove all the state file from the /dev/shm location """
        LOG.info("initializing cleanup")
        for i in glob("{}/*".format(MEM_DIR)):
            LOG.debug("removing file: {}".format(i))
            os.unlink(i)

    def __get_states(self):
        """ get a list of our various modes, to set as machine states """
        return [
            getattr(self.me, i) for i in dir(self.me) if i.startswith('MODE_')
        ]

    ##############################
    # callables
    ##############################
    def to_dict(self):
        """ convert this class state to a dict """
        return {
            FIELD_LOCAL_IP: self.local_ip,
            FIELD_QUEUE_SOUND: self.queue_sound,
            FIELD_BSSID: self.bssid,
            FIELD_STATUS: self.status,
            FIELD_PERCENTAGE: self.percentage,
            FIELD_WORDLIST: self.wordlist,
            FIELD_SLEEP_RATE: self.sleep_rate,
            FIELD_DBM: self.dbm,
            FIELD_WAVEFILE: self.wavefile,
            FIELD_CLICK_RATE: self.click_rate,
            FIELD_FOX_LIST: self.foxlist,
            FIELD_HAS_INTERWEBS: self.has_interwebs,
            cfg.FIELD_ADAPTER_SCAN: self.scan_adapter,
            cfg.FIELD_ADAPTER_INET: self.inet_adapter,
            'timestamp': datetime.datetime.now().strftime('%s'),
            'state': self.get_state(),
        }

    def set_local_ip(self):
        """ if ip is not set, find it and set it, otherwise log an error """
        if not self.local_ip:
            try:
                ip = netifaces.ifaddresses(self.inet_adapter)[2][0].get('addr')
                err = None
            except Exception as e:
                ip = None
                err = "error getting ip from {}, {}".format(
                    self.inet_adapter, e.message)
            if ip:
                self.local_ip = ip
            else:
                LOG.error(err)

    def set_crack_mode(self):
        """ set mode to cracking """
        self.sleep_rate = 0
        self.wavefile = SOUND_ELEVATOR

    def set_scan_mode(self):
        """ set mode to scanning """
        self.sleep_rate = 2
        if self.percentage == 0:
            self.wavefile = SOUND_SONAR
        elif self.percentage > 0:
            self.wavefile = SOUND_CLICK

    def set_config_mode(self):
        """ set mode to configuration """
        self.sleep_rate = 10
        self.set_local_ip()

        if not self.local_ip:
            self.wavefile = util.get_say('no local ip detected')
        elif not self.get_available_wireless():
            self.wavefile = util.get_say(
                'no available scan adapters detected {}'.format(self.local_ip))
        elif not self.scan_adapter:
            self.wavefile = util.get_say('configure scan adapter at {}'.format(
                self.local_ip))
        elif not self.bssid:
            self.wavefile = util.get_say('configure bssid at {}'.format(
                self.local_ip))
        else:
            self.wavefile = util.get_say("I'm in danger")

    def get_available_wireless(self):
        """ get a list of adapters that are no our inet adapter """
        return [i for i in pyw.winterfaces() if i != self.inet_adapter]

    def get_cracked_list(self):
        if os.path.exists(CRACKED_FILE):
            try:
                with open(CRACKED_FILE, 'r') as fh:
                    dat = json.load(fh)
            except Exception:
                LOG.exception("Error, opening: {}".format(CRACKED_FILE))
                dat = []
        else:
            LOG.debug("Cracked file: {} doesnt exist yet".format(CRACKED_FILE))
            dat = []
        for idx, v in enumerate(dat):
            dval = v['date']
            ts = datetime.datetime.fromtimestamp(float(dval))
            dat[idx]['ts'] = ts
            dat[idx]['date_human'] = str(ts)
        return dat

    def can_scan_mode(self):
        """ can haz scan mode? all these need to be true to proceed """
        return all([
            self.local_ip,
            self.scan_adapter,
            self.bssid,
        ])

    def can_crack_mode(self):
        """ can we start crack-a-lackin? this will tell us """
        ok_cnt = self.get_bssid_ok_count() >= CRACK_GO_COUNT
        if not self.can_crack:
            LOG.debug("crack mode not enabled")
            return False
        elif ok_cnt and (not self.wordlist
                         or not os.path.exists(self.wordlist)):
            self.wavefile = util.get_say("need wordlist at {}".format(
                self.local_ip))
            return False
        else:
            return all([
                self.get_bssid_ok_count() >= CRACK_GO_COUNT,
                self.wordlist,
            ])

    def call_trigger(self, mode):
        """ all this with CONSTANTS that begin with MODE_* to switch our status (if conditions allow)"""
        orig_state = self.get_state()
        func = getattr(self, mode)
        func()
        if self.get_state() != orig_state:
            LOG.debug("New State: {}".format(self.get_state()))

    def get_bssid_status(self):
        """ get the status of the bssid we're working on """
        return self.status[TFIELD_CRACK_STATUS]

    def get_bssid_ok_count(self):
        """ get the status of the bssid we're working on """
        return self.status[TFIELD_OK_CNT]

    def get_total_poll_count(self):
        """ get the status of the bssid we're working on """
        return self.status[TFIELD_POLL_COUNT]

    def set_total_poll_count(self, cnt):
        cur = self.status.copy()
        cur[TFIELD_POLL_COUNT] = cnt
        self.status = cur

    def set_bssid_ok_count(self, cnt):
        cur = self.status.copy()
        cur[TFIELD_OK_CNT] = cnt
        self.status = cur

    def set_bssid_status(self, value):
        """ set status of the bssid we're working on """
        cur = self.status.copy()
        cur[TFIELD_CRACK_STATUS] = value
        self.status = cur

    def get_state(self):
        """ get our current state """
        return self.state

    def queue_pop(self):
        """ pop the first value from the sound queue list """
        queue = self.queue_sound
        if not queue:
            return None
        val = queue.pop(0)
        self.queue_sound = queue
        return val

    def queue_add(self, sound):
        """ add a value to the sound queue list """
        queue = list(self.queue_sound)
        queue.append(sound)
        self.queue_sound = queue
コード例 #7
0
import os
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]=""

import code
import yaml

import tensorflow as tf
import streamlit as st
import plotly.express as px

from cfg import get_config, set_config, validate_cfg; local_cfg = get_config(save=False)
from upload import gcs_download_blob_as_string

model_name = st.sidebar.text_input("Model name", value="gestalt_cnn_percept_longtest_w_1_contd_6")
debug = st.checkbox('Debug')
# better validation accuracy: gestalt_cnn_percept_longtest_w_1_contd_6
# dist_percept_long_contd_4

def configure():
  config_path = f"logs/{model_name}/config.yaml"
  config_str = gcs_download_blob_as_string(config_path)
  model_cfg = yaml.safe_load(config_str)
  validate_cfg(model_cfg)
  model_cfg['batch_size'] = 1
  set_config(model_cfg)
  return model_cfg


model_cfg = configure()
from language import tokenizer
コード例 #8
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import state
import cfg

config = cfg.get_config()
STATE = state.piClickerState(
    config.get(cfg.FIELD_ADAPTER_INET),
    config.get(cfg.FIELD_POLLING_INTERVAL),
    config.get(cfg.FIELD_ADAPTER_SCAN),
)
コード例 #9
0
 def __init__(self, ip, port, sn_q, sto_q, action_q, sen_q, img_q, sock):
     print('Loading Thermal camera library')
     dll_path = opjoin('dll', 'CG_ThermalCamDll_2018.dll')
     try:
         self.dll = windll.LoadLibrary(dll_path)
     except:
         print('Could not load Thermal camera DLL.')
         quit()
     print('Completed')
     self.img_q = img_q
     self.dll.GetCorrectedTemp.restype = c_float
     self.init_cam_vari(ip, port)
     self.fid = open(NMAP_FILE, "r+")
     self.map = mmap.mmap(self.fid.fileno(), 0)
     self.width = 1080
     self.height = 810
     self.crop_width = 1320
     self.crop_height = 990
     self.load_app_settings()
     self.cfg_data = cfg.get_config()
     # buf = np.empty((480, 640, 3), dtype=uint8)
     self.hour_dir = ''
     self.record_counter = 0
     self.sound_q = sn_q
     self.storage_q = sto_q
     # self.logo = cv2.imread('logo.png')
     self.setting_logo = cv2.imread('Setting-icon.png')
     self.split_logo = cv2.imread('quit-logo.png')
     self.scr_buff = np.empty(
         (SCR_HEIGHT, SCR_WIDTH, 3),
         dtype=uint8)  # preallocate this array to speed up
     self.action_q = action_q
     self.sen_q = sen_q
     # self.src_rgb = np.zeros((SCR_HEIGHT,SCR_WIDTH//2,3),dtype=uint8)
     self.ref_pair = rp.reference_pair()
     self.ir_full = False
     try:
         self.mask_bw = np.load('mask.npy')
         # print(self.mask_bw)
     except:
         self.mask_bw = 255 * np.ones((32, 40), dtype=uint8)
         np.save('mask', self.mask_bw)
     self.mask_color = np.ones(
         (self.mask_bw.shape[0], self.mask_bw.shape[1], 3), dtype=uint8)
     self.mask_color[:, :, 0] = (np.invert(self.mask_bw) / 255) * 0
     self.mask_color[:, :, 1] = (np.invert(self.mask_bw) / 255) * 69
     self.mask_color[:, :, 2] = (np.invert(self.mask_bw) / 255) * 255
     self.show_mask = False
     self.cursor = (50, 50)
     self.cursor_textpos = (50, 50)
     self.cursor_reading = 0
     self.sock = sock
     self.exit_all = False
     self.rgb_pop_up = False
     self.disp_q = queue.Queue(3)
     self.imshow_thread = threading.Thread(target=self.imshow_loop,
                                           args=(self.disp_q,
                                                 self.storage_q,
                                                 self.scr_buff))
     self.imshow_thread.daemon = True
     self.imshow_thread.start()
     self.alert_timer = time.time()
     self.alert_counter = 0
     self.no_mask_counter = 0
     self.reconnected = True
     self.on_setting = False
     # import setting
     # self.setting_proc=mp.Process(target=setting.func1,args=(self.action_q,))
     # self.setting_proc.daemon=True
     FORMAT = '%(asctime)s %(levelname)s: %(message)s'
     path = opjoin('log', 'main.log')
     logging.basicConfig(level=logging.DEBUG,
                         format=FORMAT,
                         datefmt='%Y-%m-%d %H:%M',
                         handlers=[
                             RotatingFileHandler(path,
                                                 maxBytes=2 * 1024 * 1024,
                                                 backupCount=9)
                         ])