def main(plaintexts, traces): cpa_attacker = Attacker(plaintexts) skey = cpa_attacker.obtain_full_private_key(traces, only_first_byte=False) # Our AES key: # [43, 126, 21, 22, 40, 174, 210, 166, 171, 247, 21, 136, 9, 207, 79, 60] # print(f"First found subkey: {skey[0]}") print(f"Found full key: {skey}")
def start_gpu_thread(part_csv): args['datalist'] = part_csv attacker = Attacker(transform, img2tensor, args) img_pairs = pd.read_csv(args['datalist']) for idx in tqdm(img_pairs.index.values): pair_dict = {'source': img_pairs.loc[idx].source_imgs.split('|'), 'target': img_pairs.loc[idx].target_imgs.split('|')} attacker.attack_method = attacker.FoolBox attacker.attack(pair_dict)
def test_rappor(): rGenerator = secrets.SystemRandom() rappor = Rappor() attacker = Attacker() real_responses = [0] * num_choices noisy_responses = [0] * num_choices real_responses_per = [0] * num_choices estimated_responses_per = [0] * num_choices total_random = 0 random_guesses = 0 guesses_from_ones = 0 for x in range(num_providers): ''' Calculate the real response ''' if use_normal_dist: real_choice = int(round(normalvariate(normal_mean, nomal_dev), 0)) # some times ths gives values out of the range so fix that real_choice = min(num_choices - 1, real_choice) real_choice = max(0, real_choice) else: # real_choice = rGenerator.randrange(num_choices) real_choice = randrange(num_choices) real_responses[real_choice] += 1 ''' Calculate a response use RAPPOR ''' response = rappor.permanent_randomized_response( num_choices, real_choice) for y in range(num_choices): noisy_responses[y] += response[y] ''' Attack! ''' guessed = attacker.select_random(response) if (guessed == real_choice): random_guesses += 1 guessed = attacker.select_from_ones(response) if (guessed == real_choice): guesses_from_ones += 1 estimated_responses = rappor.estimate_responses(noisy_responses, num_providers) print("Percentages of responses") print("Real\t Estimated") for y in range(num_choices): real_responses_per = (real_responses[y] / num_providers) * 100 estimated_responses_per = estimated_responses[y] * 100 print("%.2f \t %.2f" % (real_responses_per, estimated_responses_per)) print("Guesses") print("Random \t From ones \t From weighted") print("%d \t %d \t %d" % (random_guesses, guesses_from_ones, guesses_from_weighted))
def __init__(self, config): self.config = config self.ckpt_dir = config.ckpt_dir if not os.path.exists(self.ckpt_dir): os.makedirs(self.ckpt_dir) self.save_config(config) self.timer = Timer() self.lr = config.lr self.datasets, self.loaders = get_loader(config.batch_size, config.aug, data=config.data) self.epochs = config.epochs self.start_epoch = 0 self.is_ode = 'resnet' not in self.config.model self.model = ModelSelector[config.model](**{ 'channel': config.channel, 'num_blocks': config.nb, 'strides': config.strides, 'solver': config.solver, 'adjoint': config.adj, 'tol': config.tol, 'num_classes': config.nc }) if self.config.use_gpu: self.model = nn.DataParallel(self.model).cuda() self.attacker = Attacker(self.model, self.loaders['test']) self.criterion = nn.CrossEntropyLoss() self.optimizer = optim.SGD(self.model.parameters(), lr=config.lr, momentum=0.9, weight_decay=config.wd) if config.resume: success = self.load(config.resume_path) if success: self.writer = SummaryWriter(log_dir=config.ckpt_dir, purge_step=self.start_epoch) else: self.writer = SummaryWriter(log_dir=config.ckpt_dir) logger.info(self.model) logger.info( f'Number of Total Params: {sum(p.numel() for p in self.model.parameters() if p.requires_grad)}' ) else: self.writer = SummaryWriter(log_dir=config.ckpt_dir) logger.info(self.model) logger.info( f'Number of Total Params: {sum(p.numel() for p in self.model.parameters() if p.requires_grad)}' )
def test_obtain_full_private_key(self): # Import power traces for which we know the used key. key = np.load(self.TEST_DATA_LOC + "key.npy") power_samples = np.load(self.TEST_DATA_LOC + "traces.npy")[:-1] plaintexts = np.load(self.TEST_DATA_LOC + "plain.npy") # Set up the attacker and find the private key as a list of bytes. attacker = Attacker(plaintexts) computed_key_bytes = attacker.obtain_full_private_key(power_samples) # "key.npy" is stored as an array of identical keys, so get key[0] known_key_bytes = key[0] self.assertEqual(list(known_key_bytes), computed_key_bytes)
def attack(self): while True: # breaks after first if not infinite self.pending_sockets = self.create_n_sockets() self.attackers = [] Logger.info('Initializing connections and starting attack job') job = threading.Thread(target=self.attack_job, daemon=True) job.start() try: while len(self.pending_sockets): sock = self.pending_sockets.pop() try: sock.connect((self.host, self.port)) self.attackers.append( Attacker(sock, self.rqgen.payload(), self.options['chunk_size'])) except socket.error as ex: Logger.info( 'Cannot create connection to remote host: ' + str(ex)) self.stats['refused'] += 1 job.join() except KeyboardInterrupt: self.stopped = True Logger.log('Stopping attack...') self.print_stats() exit(0) if not self.options['infinite']: break self.print_stats()
def start_gpu_thread(part_csv, logdir): args['logdir'] = logdir args['datalist'] = part_csv if os.path.exists(args['logdir']): shutil.rmtree(args['logdir']) # exit(1) attacker = Attacker(transform, img2tensor, args) img_pairs = pd.read_csv(args['datalist']) for idx in tqdm(img_pairs.index.values): pair_dict = { 'source': img_pairs.loc[idx].source_imgs.split('|'), 'target': img_pairs.loc[idx].target_imgs.split('|') } attacker.attack_method = attacker.M_DI_2_FGSM attacker.attack(pair_dict)
def spawnEntities(self): for a in range(GLOBALS["attackeramount"]): self.attackers.append(Attacker(self.defender)) for a in self.attackers: self.layout.add_widget(a)
def main(args, save_dir): set_seed(333) # mode: [basetrain / advtrain / attack / generate] if args.mode == "basetrain" or args.mode == "advtrain": net = Trainer(args, save_dir) net.train(args.mode) elif args.mode == "attack": net = Attacker(args) net.run() elif args.mode == "generate": net = Generator(args) net.run() elif args.mode == "convert": net = Converter(args) net.run() elif args.mode == "split": net = Spliter(args) net.run() else: print('#### Please execute with mode: ex) --mode "basetrain"') print( "#### MODE: [basetrain / advtrain / attack / generate / converter / split]" )
def creat_attackers(game_settings, screen, attackers): """生成入侵者军队""" for row in range(3): for num in range(9): attacker = Attacker(game_settings, screen) attacker.rect.x = 55 * num + 10 attacker.rect.y = 50 * row + 50 attackers.add(attacker)
def trainAndAttack(classifier=None, train_X=None, train_y=None, test_X=None, test_y=None): try: model_creator = Model(classifier=classifier) model_creator.fit_data(train_X, train_y) predictions = model_creator.predict(test_X) accuracy = accuracy_score(test_y, predictions) print("Accuracy for " + str(classifier) + " : " + str(accuracy)) attacker = Attacker() attacker.attack(X=test_X, y=test_y, model=model_creator) except Exception as e: print( "Exception in train and attack: ", e, )
def trainAndAttackRaw(classifier=None, train_X=None, train_y=None, audioPath=None): try: print("Imported data " + str(len(train_X)) + " and " + str(len(train_y))) testData = Data() test_X, test_y = testData.process(audioPath, attack=True, peak=True) model_creator = Model(classifier=classifier) print("Imported data " + str(len(train_X)) + " and " + str(len(train_y))) model_creator.fit_data(train_X, train_y) attacker = Attacker() attacker.attack(X=test_X, y=test_y, model=model_creator) except Exception as e: print("Exception in train and attack raw: ", e.__cause__) traceback.print_exc(file=sys.stdout)
def main(): config = ConfigParser.ConfigParser() config.read('setup.config') localIP = config.get('Attacker', 'localIP') localPort = config.get('Attacker', 'localPort') remoteIP = config.get('Attacker', 'remoteIP') remotePort = config.get('Attacker', 'remotePort') protocol = config.get('General', 'protocol') password = config.get('Encryption', 'password') knockList = config.get('General', 'knockList') filePort = config.get('General', 'filePort') ttl = config.get('General', 'ttl') print(localIP, localPort, remoteIP, remotePort, protocol, filePort) attacker = Attacker(localIP, localPort, filePort, remoteIP, remotePort, protocol, password, knockList, ttl) attacker.run()
def get_attacker_lst(num): ''' Function get_attacker_lst Input: an integer Return: a list Does: creat an empty list, and add attacers to the list, the number of attackers based on the input integer. ''' attackers = [] for i in range(num): attackers.append(Attacker()) return attackers
def respawn(self, attacker): explosion = Explosion(attacker.x, attacker.y) self.explosions.append(explosion) self.layout.add_widget(explosion) Clock.schedule_once(lambda x: self.killExplosion(explosion), 0.5) self.attackers.remove(attacker) self.layout.remove_widget(attacker) new = Attacker(self.defender) self.attackers.append(new) self.layout.add_widget(new)
def run_game(): # Initialize game and create screen object. pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Football game") # Create an instance to store game statistics. stats = GameStats(ai_settings) # Make an attacker. attacker = Attacker(ai_settings, screen) # Make the initial ball initial_ball = Ball(ai_settings, screen, attacker) # Make a group of balls and a group of defender. balls = Group() defenders = Group() # Create the defense. gf.create_defense(ai_settings, screen, attacker, defenders) # Start the main loop for the game. while True: gf.check_events(ai_settings, screen, stats, attacker, initial_ball, balls) if stats.game_active: attacker.update() initial_ball.initial_update_ball(attacker) gf.update_balls(ai_settings, screen, stats, attacker, defenders, balls) gf.update_defenders(ai_settings, stats, screen, attacker, defenders, balls) gf.update_screen(ai_settings, screen, attacker, defenders, initial_ball, balls)
def __init__(self, master, data, callback=False, max_iterations_per_sub_problem=50): # The location of the master problem object is stored so constraints can be added directly as they are found. self.master = master # The data and parameters are stored as attributes. self.data = data self.callback = callback self.max_constraints_per_iteration = max_iterations_per_sub_problem self.constraints_added = False # If the sub-problems are run in extended mode then HIGH LOW track all suppressed cells not just the sensitive ones self.extended = False self.HIGH_LOW_cells = [] # A single Attacker object is created. This makes solving a lot more efficient self.attacker = Attacker(data) # Sort the sensitive cells based on the size of their UPL and LPL self.non_increasing_UPL_sensitive_cells = sorted( self.data["sensitive cells"].keys(), key=lambda x: self.data["sensitive cells"][x]["UPL"], reverse=True) self.non_increasing_LPL_sensitive_cells = sorted( self.data["sensitive cells"].keys(), key=lambda x: self.data["sensitive cells"][x]["LPL"], reverse=True) # The HIGH and LOW parameters are initiated to their nominal values. self.HIGH = self.default_nominal_dict() self.LOW = self.default_nominal_dict()
def parse_wars(wars): """Check to see if an Attacker instance exists, update attacks and return dictionary of attackers. Args: wars (dict): dictionary of wars that are to be parsed {date: API Data} Return: dict: Attacker instances {Tag: Attacker Instance}. list: List of results from logged wars [[date, opponent, score]]. """ wars_logged = [] attackers = {} for war in sorted(wars): # print("Parsing War against {}".format(wars[war]["opponent"]["name"])) opponents = wars[war]["opponent"][ "members"] #clan data for the war opponent members = wars[war]["clan"][ "members"] #war data for clan stats being collected for opponent_th_levels = { opponent["tag"]: opponent["townhallLevel"] for opponent in opponents } """Creates a dictionary of opponent ids and townhall level {tag: th level}""" start_number = len( attackers ) #number of Attackers already in the dict to track attackers added. for member in members: """Loop through the members and either adds them to the dictionary or updates that attacks""" if member["tag"] in attackers: attackers[member["tag"]].add_war(member.get("attacks"), opponent_th_levels) else: attackers[member["tag"]] = Attacker(member, opponent_th_levels) # print("War against {} added {} attackers".format(wars[war]["opponent"]["name"], len(attackers)-start_number)) wars_logged.append([ war, wars[war]["opponent"]["name"], "{} - {}".format(wars[war]["clan"]["stars"], wars[war]["opponent"]["stars"]) ]) return attackers, wars_logged
def main(config): cudnn.benchmark = True if config.model_type not in ['LivDet2015']: print('ERROR!! model_type should be selected in LivDet2015') print('Your input for model_type was %s' % config.model_type) return if config.attack_type not in ['DE', 'FGSM', 'DeepFool']: print('ERROR!! model_type should be selected in DE,FGSM,DeepFool') print('Your input for attack_type was %s' % config.attack_type) return # Create directories if not exist if not os.path.exists(config.model_path): os.makedirs(config.model_path) if not os.path.exists(config.result_path): os.makedirs(config.result_path) config.result_path = os.path.join(config.result_path, config.model_type) if not os.path.exists(config.result_path): os.makedirs(config.result_path) ''' lr = random.random()*0.0005 + 0.0000005 augmentation_prob= random.random()*0.7 epoch = 200 decay_ratio = random.random()*0.8 decay_epoch = int(epoch*decay_ratio) config.augmentation_prob = augmentation_prob config.num_epochs = epoch #config.lr = lr config.num_epochs_decay = decay_epoch ''' print(config) train_loader = get_loader(image_path=config.test_path, image_size=config.image_size, batch_size=config.batch_size, num_workers=config.num_workers, mode='train', augmentation_prob=config.augmentation_prob) valid_loader = get_loader(image_path=config.valid_path, image_size=config.image_size, batch_size=config.batch_size, num_workers=config.num_workers, mode='valid', augmentation_prob=0.) test_loader = get_loader(image_path=config.test_path, image_size=config.image_size, batch_size=config.batch_size, num_workers=config.num_workers, mode='test', augmentation_prob=0.) attack_loader = get_loader(image_path=config.test_path, image_size=config.image_size, batch_size=1, num_workers=config.num_workers, mode='test', augmentation_prob=0.) # Train and sample the images if config.attack == 1: attacker = Attacker(config, attack_loader) attacker.train() elif config.attack == 0: solver = Solver(config, train_loader, valid_loader, test_loader) if config.mode == 'train': solver.train() elif config.mode == 'test': solver.test()
if args['username'] == None or args['passlist'] == None: parser.error('Not enough arguments: Missing username or passlist') # validate `args`: values # these are files. We validate them the same way file_args = ['passlist', 'saved_scan_file', 'config'] for key in args: if args[key] == None: continue if key in file_args: f = args[key] if not exists(f): parser.error('Could not open %s: No such file or directory.' % f) elif not isfile(f): parser.error('Could not open %s: Not a file.' % f) attacker = Attacker(args) attacker.start() try: attacker.block() print() except KeyboardInterrupt: print() print(' ==> Exiting... might take a while...') attacker.stop() attacker.block() print()
# In the Userobject, the first string you set is the name of the crawl run and also the name of the created database. # So if you want to keep old runs then just give different names for each crawl # The first of the line below, starts a scan with a logged in user. # Parameter desc: Name of DB - Privilege level: deprecated(Just let it 0) - URL where the login form is stored - login data as dict. The key is the parameter name in the login form that has to be set - # session: reflects the session within a DB. It is deprecated. Just set it to ABC #user = User("WordpressX", 0, "http://localhost:8080/wp-login.php", login_data = {"log": "admin", "pwd": "admin"}, session="ABC") # Crawl without user session. Parameter desc: Name of DB - Privilege level - session user = User("Test", 0, session="ABC") url = "http://localhost/" # Creates the crawler config: URL: start url of the crawler(independent from login) - max_dept: how deep to crawl(link), max_click_depth: how deep to follow events - Crawlspeed: Fast is the best value here crawler_config = CrawlConfig("Some Name, doesn't matter", url, max_depth=1, max_click_depth=2, crawl_speed=CrawlSpeed.Fast) # From here you have nothing to chance. Except you want no attacking, then comment out the lines down logging.info("Crawler started...") database_manager = DatabaseManager(user, dropping=True) crawler = Crawler(crawl_config=crawler_config, database_manager=database_manager)#, proxy="localhost", port=8082) crawler.crawl(user) logging.info("Crawler finished") # If you want no attacking comment out the lines below. logging.info("Start attacking...") attack_config = AttackConfig(url) attacker = Attacker(attack_config, database_manager=database_manager)#, proxy="localhost", port=8082) attacker.attack(user) logging.info("Finish attacking...")
def huawei_run_test(event, context): logging.info('Huawei Serverless.') logging.info(context) args = process_args_dict(event) attacker = Attacker(*args) attacker.attack()
def aliyun_run_test(event, context): logging.info('Aliyun Serverless.') logging.info(context) args = process_args_dict(json.loads(event)) attacker = Attacker(*args) attacker.attack()
parser.add_argument('--topk', type=int, default=3, help='employ the top k shapelets, maxima value is 5') parser.add_argument('--normalize', action='store_true', help='it is unnecessary in our project, we have ' 'normalized the data') parser.add_argument('--e', type=int, default=1499, help='epochs of model') opt = parser.parse_args() print(opt) os.makedirs('result_%s_%d_%s/%s/figures' % (str(opt.magnitude_factor), opt.topk, opt.model, opt.run_tag), exist_ok=True) data_path = 'data/' + opt.run_tag + '/' + opt.run_tag + '_unseen.txt' test_data = np.loadtxt(data_path) size = test_data.shape[0] idx_array = np.arange(size) attacker = Attacker(run_tag=opt.run_tag, top_k=opt.topk, e=opt.e, model_type=opt.model, cuda=opt.cuda, normalize=opt.normalize) # record of the running time start_time = time.time() # count the number of the successful instances, mse,iterations,queries success_cnt = 0 right_cnt = 0 total_mse = 0 total_iterations = 0 total_quries = 0 for idx in idx_array: print('###Start %s : generating adversarial example of the %d sample ###' % (opt.run_tag, idx)) attack_ts, info = attacker.attack(sample_idx=idx, target_class=opt.target_class, factor=opt.magnitude_factor, max_iteration=opt.maxitr, popsize=opt.popsize) # only save the successful adversarial example
# session: reflects the session within a DB. It is deprecated. Just set it to ABC #user = User("WordpressX", 0, "http://localhost:8080/wp-login.php", login_data = {"log": "admin", "pwd": "admin"}, session="ABC") # Crawl without user session. Parameter desc: Name of DB - Privilege level - session user = User("Test", 0, session="ABC") url = "http://localhost/" # Creates the crawler config: URL: start url of the crawler(independent from login) - max_dept: how deep to crawl(link), max_click_depth: how deep to follow events - Crawlspeed: Fast is the best value here crawler_config = CrawlConfig("Some Name, doesn't matter", url, max_depth=1, max_click_depth=2, crawl_speed=CrawlSpeed.Fast) # From here you have nothing to chance. Except you want no attacking, then comment out the lines down logging.info("Crawler started...") database_manager = DatabaseManager(user, dropping=True) crawler = Crawler( crawl_config=crawler_config, database_manager=database_manager) #, proxy="localhost", port=8082) crawler.crawl(user) logging.info("Crawler finished") # If you want no attacking comment out the lines below. logging.info("Start attacking...") attack_config = AttackConfig(url) attacker = Attacker( attack_config, database_manager=database_manager) #, proxy="localhost", port=8082) attacker.attack(user) logging.info("Finish attacking...")
import os import sys import time import threading from constants import Constants from teleporter import Teleporter from healer import Healer from attacker import Attacker from buffer import Buffer from notifier import Notifier from kswarn import KSWarn teleporter = Teleporter() healer = Healer(teleporter) attacker = Attacker(teleporter) buffer = Buffer(teleporter) notifier = Notifier() kswarn = KSWarn(teleporter) threads = [ teleporter, healer, attacker, buffer, notifier, kswarn, ] #import psutil #pid = os.getpid()
glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) # needed so that egi knows where to draw egi.InitWithPyglet(win) # prep the fps display fps_display = clock.ClockDisplay() # register key and mouse event handlers win.push_handlers(on_key_press) win.push_handlers(on_mouse_press) win.push_handlers(on_resize) # create a world for agents world = World(500, 500) # add one agent world.target = PractiseTarget(world) world.attacker = Attacker(world) # unpause the world ready for movement # print("Controls: A to add an agent, O to add an object to the map, C to reset objects on the map, P to pause, and I to show direction info.") world.paused = False while not win.has_exit: win.dispatch_events() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # show nice FPS bottom right (default) delta = clock.tick() world.update(delta) world.render() fps_display.draw() # swap the double buffer win.flip()
pygame.init() # 초기화 pygame.display.set_caption('Hello, world!') # 창 제목 설정 displaysurf = pygame.display.set_mode((width, height), 0, 32) # 메인 디스플레이를 설정한다 clock = pygame.time.Clock() # 시간 설정 #gulimfont = pygame.font.SysFont('굴림', 70) # 서체 설정 #helloworld = gulimfont.render('Hello, world!', 1, black) # .render() 함수에 내용과 안티앨리어싱, 색을 전달하여 글자 이미지 생성 #helloworld = pygame.image.load('attack.png') #helloworld = pygame.transform.scale(helloworld, (50,50)) #hellorect = helloworld.get_rect() # 생성한 이미지의 rect 객체를 가져온다 #hellorect.center = (width / 2, height / 2) # 해당 rect의 중앙을 화면 중앙에 맞춘다 atkr = Attacker() atkr.set_image('attack.png', (50, 50)) atkr.set_pos((width / 2, height / 2)) gaga = Gagamel() gaga.set_image('2659980.png', (50, 50)) gaga.set_pos((width / 2, height / 2)) gaga.set_bound((width, height)) bullets = [] crashpos = (width, height) while True: # 아래의 코드를 무한 반복한다. displaysurf.fill(white) # displaysurf를 하얀색으로 채운다
class Trainer: def __init__(self, config): self.config = config self.ckpt_dir = config.ckpt_dir if not os.path.exists(self.ckpt_dir): os.makedirs(self.ckpt_dir) self.save_config(config) self.timer = Timer() self.lr = config.lr self.datasets, self.loaders = get_loader(config.batch_size, config.aug, data=config.data) self.epochs = config.epochs self.start_epoch = 0 self.is_ode = 'resnet' not in self.config.model self.model = ModelSelector[config.model](**{ 'channel': config.channel, 'num_blocks': config.nb, 'strides': config.strides, 'solver': config.solver, 'adjoint': config.adj, 'tol': config.tol, 'num_classes': config.nc }) if self.config.use_gpu: self.model = nn.DataParallel(self.model).cuda() self.attacker = Attacker(self.model, self.loaders['test']) self.criterion = nn.CrossEntropyLoss() self.optimizer = optim.SGD(self.model.parameters(), lr=config.lr, momentum=0.9, weight_decay=config.wd) if config.resume: success = self.load(config.resume_path) if success: self.writer = SummaryWriter(log_dir=config.ckpt_dir, purge_step=self.start_epoch) else: self.writer = SummaryWriter(log_dir=config.ckpt_dir) logger.info(self.model) logger.info( f'Number of Total Params: {sum(p.numel() for p in self.model.parameters() if p.requires_grad)}' ) else: self.writer = SummaryWriter(log_dir=config.ckpt_dir) logger.info(self.model) logger.info( f'Number of Total Params: {sum(p.numel() for p in self.model.parameters() if p.requires_grad)}' ) def train_and_test(self): for epoch in range(self.start_epoch, self.epochs): logger.info(f"Epoch :[{epoch}/{self.epochs}]") self.train(epoch) test_acc = self.test(epoch) if epoch % 5 == 0: self.save( { 'net': self.model.state_dict(), 'test_acc': test_acc, 'epoch': epoch, 'optim': self.optimizer.state_dict() }, epoch) # self.writer.add_hparams(self.config.__dict__, {'hparam/test_best_acc': test_acc}) self.writer.close() def train(self, epoch): lr = adjust_learning_rate(self.lr, self.optimizer, self.config.lr_d, epoch) self.model.train() losses = AveMeter() f_nfe_meter = AveMeter() b_nfe_meter = AveMeter() correct = 0 for i, (inputs, targets) in enumerate(self.loaders['train']): inputs = inputs + self.config.noise * torch.randn_like(inputs) if self.config.use_gpu: inputs, targets = inputs.cuda(), targets.cuda() self.optimizer.zero_grad() if self.is_ode: self.model.module.nfe = 0 outputs = self.model(inputs) if self.is_ode: nfe_forward = self.model.module.nfe.item() if self.config.loss == 'ce': loss = self.criterion(outputs, targets) elif self.config.loss == 'tr': loss = self.criterion(outputs, targets) + trades_loss( self.model, inputs, self.optimizer, distance=self.config.attack_type) elif self.config.loss == 'ma': loss = madry_loss(self.model, inputs, targets, self.optimizer) if self.is_ode: self.model.module.nfe = 0 loss.backward() self.optimizer.step() if self.is_ode: nfe_backward = self.model.module.nfe.item() self.model.module.nfe = 0 if self.is_ode: f_nfe_meter.update(nfe_forward) b_nfe_meter.update(nfe_backward) losses.update(loss.item(), inputs.size()[0]) correct += outputs.max(1)[1].eq(targets).sum().item() acc = 100 * correct / len(self.datasets['train']) logger.info( f"Train: [{i + 1}/{len(self.loaders['train'])}] | " f"Time: {self.timer.timeSince()} | " f"loss: {losses.avg:.4f} | " f"acc: {acc:.2f}% | NFE-F: {f_nfe_meter.avg:.2f} | NFE-B: {b_nfe_meter.avg:.2f}" ) self.writer.add_scalar('train/lr', lr, epoch) self.writer.add_scalar('train/loss', losses.avg, epoch) self.writer.add_scalar('train/acc', acc, epoch) self.writer.add_scalar('train/nfe-f', f_nfe_meter.avg, epoch) self.writer.add_scalar('train/nfe-b', b_nfe_meter.avg, epoch) for name, param in self.model.named_parameters(): if param.grad is not None: self.writer.add_histogram(name, param.clone().cpu().data.numpy(), epoch) self.writer.add_histogram( name + '/grad', param.grad.clone().cpu().data.numpy(), epoch) def test(self, epoch): self.model.eval() losses = AveMeter() correct = 0 with torch.no_grad(): for i, (inputs, targets) in enumerate(self.loaders['test']): if self.config.use_gpu: inputs, targets = inputs.cuda(), targets.cuda() outputs = self.model(inputs) loss = self.criterion(outputs, targets) losses.update(loss.item(), inputs.size()[0]) correct += outputs.max(1)[1].eq(targets).sum().item() acc = 100 * correct / len(self.datasets['test']) out = f"Test: [{i + 1}/{len(self.loaders['test'])}] | " \ f"Time: {self.timer.timeSince()} | " \ f"loss: {losses.avg:.4f} | " \ f"acc: {acc:.2f}%" if epoch % self.config.save_freq == 0 and epoch != 0: acc_adv, grad_norm = self.attacker.attack( self.config.attack_type, self.config.num_steps, self.config.step_size, self.config.epsilon) out = f"Test: [{i + 1}/{len(self.loaders['test'])}] | " \ f"Time: {self.timer.timeSince()} | " \ f"loss: {losses.avg:.4f} | " \ f"acc: {acc:.2f}% | " \ f"acc_adv_{self.config.attack_type}_{self.config.num_steps}: {acc_adv:.2f}% " \ f"grad_norm: {grad_norm:.4f}" self.writer.add_scalar( f'test/acc_adv_{self.config.attack_type}_{self.config.num_steps}', acc_adv, epoch) logger.info(out) self.writer.add_scalar('test/loss', losses.avg, epoch) self.writer.add_scalar('test/acc', acc, epoch) return acc def save(self, state, epoch): torch.save(state, os.path.join(self.ckpt_dir, f'ckpt_{epoch}.pt')) logger.info('***Saving model***') def load(self, path): assert os.path.exists(path), f"resume {path} not exists!" ckpt_dict = {} for file in os.listdir(path): if file.startswith('ckpt_'): ckpt_dict[file] = int(re.findall('_([0-9]+).pt', file)[0]) if len(ckpt_dict) == 0: logger.info( 'Do not find any checkpoint file, will train from start!') return False else: resume_file = sorted(ckpt_dict.items(), key=lambda x: x[1])[-1][0] state = torch.load( os.path.join(path, resume_file), map_location='cuda' if self.config.use_gpu else 'cpu') self.model.load_state_dict(state['net']) self.optimizer.load_state_dict(state['optim']) self.start_epoch = state['epoch'] + 1 logger.info('******************************************') logger.info(f'Successfully load ckpt from {resume_file}') return True def save_config(self, config): with open(os.path.join(self.ckpt_dir, 'config.json'), 'w+') as f: f.write(json.dumps(config.__dict__, indent=4)) f.close()
class SubProblem: """The sub-problems consist of solving a series of attacker sub-problems. Given the similarity of the attacker problems, we only build a single Attacker object and change the objective function between different solve calls. It is possible to detect that some attacker problems do not need to be solved. This is explained very well in the FS paper. For each sensitive cells a HIGH and a LOW value are tracked, which are originally set to their nominal value. The solutions to attacker problems are used to update these values. If the HIGH exceeds the upper protection limit then it is clearly already feasible It is also not necessary to solve all attacker problems if the suppression pattern is known to be invalid. Hence a parameter is given that limits the maximum number of constraints added per sub-problem iteration, set by default to 50 which is the same as Tau-Argus from what I understand. The subproblem can either be solved during a callback or in a more classical benders decomposition. The functions used to add constraints to the master problem differ slightly as a result so need to be considered explicitly. """ def __init__(self, master, data, callback=False, max_iterations_per_sub_problem=50): # The location of the master problem object is stored so constraints can be added directly as they are found. self.master = master # The data and parameters are stored as attributes. self.data = data self.callback = callback self.max_constraints_per_iteration = max_iterations_per_sub_problem self.constraints_added = False # If the sub-problems are run in extended mode then HIGH LOW track all suppressed cells not just the sensitive ones self.extended = False self.HIGH_LOW_cells = [] # A single Attacker object is created. This makes solving a lot more efficient self.attacker = Attacker(data) # Sort the sensitive cells based on the size of their UPL and LPL self.non_increasing_UPL_sensitive_cells = sorted( self.data["sensitive cells"].keys(), key=lambda x: self.data["sensitive cells"][x]["UPL"], reverse=True) self.non_increasing_LPL_sensitive_cells = sorted( self.data["sensitive cells"].keys(), key=lambda x: self.data["sensitive cells"][x]["LPL"], reverse=True) # The HIGH and LOW parameters are initiated to their nominal values. self.HIGH = self.default_nominal_dict() self.LOW = self.default_nominal_dict() def reset_high_low(self): """Reset the HIGH and LOW parameters to their nominal values. This is done between consecutive solves of the subproblem in the complete solve mode. It does not need to be performed in the diving heuristic""" self.HIGH = self.default_nominal_dict() self.LOW = self.default_nominal_dict() def solve(self, reset_model=False, refresh_bounds=True, extended=False): """ Refine assumes the first callback is to check the trivial seed solution, and the second callback is to check the solution where only primary suppression is completed. """ # The attacker model is reset between subsequent subproblems iterations but not between individual attacker solvers if reset_model: self.attacker.m.reset() # Track how many constraints are added in this subproblem iteration self.constraints_added = 0 # Reset the HIGH and LOW parameters if necessary if refresh_bounds: self.reset_high_low() # Set relevant cells to keep track of HIGH and LOWS. This is typically just the sensitive cells but sometimes all # suppressed cells self.HIGH_LOW_cells = [cell for cell, supp in self.attacker.supp_level.items() if supp > 0.5] if extended else \ self.data["sensitive cells"].keys() # Solve all the UPL in non-increasing order for sensitive_cell in self.non_increasing_UPL_sensitive_cells: if self.constraints_added <= self.max_constraints_per_iteration: self.process_upper_protection_level(sensitive_cell) # Solve all the LPL in non-increasing order for sensitive_cell in self.non_increasing_LPL_sensitive_cells: if self.constraints_added <= self.max_constraints_per_iteration: self.process_lower_protection_level(sensitive_cell) def process_upper_protection_level(self, sensitive_cell): """Process the upper protection level of a specific sensitive cell. Checks if the attacker problem must be solved and if so solves appropriately and either adds a constraint or updates the HIGH LOW parameter""" cell_nominal = self.data["cells"][sensitive_cell]["nominal"] cell_UPL = self.data["sensitive cells"][sensitive_cell]["UPL"] # Checks to see if the limit has not yet been exceeded and if so solves the attacker problem accordingly if self.HIGH[sensitive_cell] < cell_nominal + cell_UPL: y_max = self.attacker.optimise(sensitive_cell, maximise=True) # Either adds a constraint or updates HIGH and LOW if cell_nominal + cell_UPL > y_max: self.add_upper_constraint_to_master(sensitive_cell) self.constraints_added += 1 else: self.update_high_low() def process_lower_protection_level(self, sensitive_cell): """Process the lower protection level of a specific sensitive cell. Checks if the attacker problem must be solved and if so solves appropriately and either adds a constraint or updates the HIGH LOW parameter""" cell_nominal = self.data["cells"][sensitive_cell]["nominal"] cell_LPL = self.data["sensitive cells"][sensitive_cell]["LPL"] # Checks to see if the limit has not yet been exceeded and if so solves the attacker problem accordingly if self.LOW[sensitive_cell] > cell_nominal - cell_LPL: y_min = self.attacker.optimise(sensitive_cell, maximise=False) # Either adds a constraint or updates HIGH and LOW if cell_nominal - cell_LPL < y_min: self.add_lower_constraint_to_master(sensitive_cell) self.constraints_added += 1 else: self.update_high_low() def positive_reduced_cost(self): """Determines the cells that have a positive reduced cost in the current solution of the attacker problem""" for cell in self.data["cells"].keys(): if self.attacker.vars[cell].RC > 0: yield cell, abs(self.attacker.vars[cell].RC) def negative_reduced_cost(self): """Determines the cells that have a negative reduced cost in the current solution of the attacker problem""" for cell in self.data["cells"].keys(): if self.attacker.vars[cell].RC < 0: yield cell, abs(self.attacker.vars[cell].RC) def add_upper_constraint_to_master(self, sensitive_cell): """Adds a constraint due to the violation of the UPL of specific sensitive cell. This is well explained in the FS paper""" protection_limit = self.data["sensitive cells"][sensitive_cell]["UPL"] # The first expression are for cells with positive reduced cost first_expr = LinExpr( (min(value * self.data["cells"][cell]["UB"], protection_limit), self.master.vars[cell]) for cell, value in self.positive_reduced_cost() if self.data["cells"][cell]["nominal"] != 0) # The second expression are for cells with negative reduced cost second_expr = LinExpr( (min(value * self.data["cells"][cell]["LB"], protection_limit), self.master.vars[cell]) for cell, value in self.negative_reduced_cost() if self.data["cells"][cell]["nominal"] != 0) # The functions used to add the constraint are slightly different based on whether its a lazy constraint or not if self.callback: self.master.mdl.cbLazy( first_expr + second_expr >= protection_limit) else: self.master.mdl.addConstr( first_expr + second_expr >= protection_limit, name="lazy_upper_{}".format(sensitive_cell)) def add_lower_constraint_to_master(self, sensitive_cell): """Adds a constraint due to the violation of the LPL of specific sensitive cell. This is well explained in the FS paper""" protect_limit = self.data["sensitive cells"][sensitive_cell]["LPL"] # The first expression are for cells with positive reduced cost first_expr = LinExpr( (min(value * self.data["cells"][cell]["LB"], protect_limit), self.master.vars[cell]) for cell, value in self.positive_reduced_cost()) # The second expression are for cells with negative reduced cost second_expr = LinExpr( (min(value * self.data["cells"][cell]["UB"], protect_limit), self.master.vars[cell]) for cell, value in self.negative_reduced_cost()) # The functions used to add the constraint are slightly different based on whether its a lazy constraint or not if self.callback: self.master.mdl.cbLazy(first_expr + second_expr >= protect_limit) else: self.master.mdl.addConstr( first_expr + second_expr >= protect_limit, name="lazy_lower_{}".format(sensitive_cell)) def update_high_low(self): """Update the HIGH and LOW dictionaries based off allowable solutions to the attacker problem""" for sensitive_cell in self.HIGH_LOW_cells: self.HIGH[sensitive_cell] = max( self.HIGH[sensitive_cell], self.attacker.vars[sensitive_cell].x) self.LOW[sensitive_cell] = min( self.LOW[sensitive_cell], self.attacker.vars[sensitive_cell].x) def default_nominal_dict(self): """A function to initiate a dictionary to the nominal values. This is probably an unnecessary efficieny improvement""" return KeyDependentDict(lambda x: self.data['cells'][x]["nominal"])
def tencent_run_test(event, context): logging.info('Tencent Serverless.') logging.info(context) args = process_args_dict(event) attacker = Attacker(*args) attacker.attack()