Ejemplo n.º 1
0
 def task(self,myplugin,exp):
     while self.tq.qsize() > 0:
         target = self.tq.get_nowait() 
         self.pross = self.pross + 1
         self.lock.acquire()
         msg = '[*] %s|%s|%s' % (target,str(self.pross) + "/"+ str(self.maxpross),exp)
         wi = self.console_width -len(msg)
         if wi < 0:
             msg = msg[0:self.console_width]
         sys.stdout.write(msg + ' ' * (self.console_width -len(msg)) + '\r')
         self.lock.release()
         try:
             p = myplugin(target,exp)
             p.verify()
             result = p.result
             if result['status']:
                 self.lock.acquire()
                 msg = "[+] {target} | {file}".format(target=result['target'],file=exp)
                 color.cprint(msg + ' ' * (self.console_width -len(msg)),CYAN)
                 self.lock.release()
                 saveResult(self.filename,result)
         except RequestException,e:
             pass
         except IOError,e:
             pass
Ejemplo n.º 2
0
 def task(self, myplugin, exp):
     while self.tq.qsize() > 0:
         target = self.tq.get_nowait()
         self.pross = self.pross + 1
         self.lock.acquire()
         msg = '[*] %s|%s|%s' % (target, str(self.pross) + "/" +
                                 str(self.maxpross), exp)
         wi = self.console_width - len(msg)
         if wi < 0:
             msg = msg[0:self.console_width]
         sys.stdout.write(msg + ' ' * (self.console_width - len(msg)) +
                          '\r')
         self.lock.release()
         try:
             p = myplugin(target, exp)
             p.verify()
             result = p.result
             if result['status']:
                 self.lock.acquire()
                 msg = "[+] {target} | {file}".format(
                     target=result['target'], file=exp)
                 color.cprint(msg + ' ' * (self.console_width - len(msg)),
                              CYAN)
                 self.lock.release()
                 saveResult(self.filename, result)
         except RequestException, e:
             pass
         except IOError, e:
             pass
Ejemplo n.º 3
0
def predict(testId, testX, model, stander):
    '''
    Do prediction
    :param testId:
    :param testX:
    :param model: The chosen best model
    :return: None
    '''
    ypred = model.predict(testX)
    ypred = stander.inverse_log10_y(ypred)

    # save prediction to csv
    saveResult(testId, ypred)
Ejemplo n.º 4
0
 def works(self):
     while self.que.qsize() > 0:
         exp = self.que.get_nowait()
         m = __import__(exp[:-3])
         myplugin = getattr(m, "Exploit")
         #只剩一个插件,启动线程
         if self.que.qsize() == 0:
             for target in self.targets:
                 self.tq.put(target)
             ts = []
             for i in range(self.tnum):
                 t = threading.Thread(target=self.task,
                                      args=(myplugin, exp))
                 ts.append(t)
                 t.start()
             for t in ts:
                 t.join()
         else:
             for target in self.targets:
                 self.pross = self.pross + 1
                 self.lock.acquire()
                 msg = '[*] %s|%s|%s' % (target, str(self.pross) + "/" +
                                         str(self.maxpross), exp)
                 wi = self.console_width - len(msg)
                 if wi < 0:
                     msg = msg[0:self.console_width]
                 sys.stdout.write(msg + ' ' *
                                  (self.console_width - len(msg)) + '\r')
                 self.lock.release()
                 try:
                     p = myplugin(target, exp)
                     p.verify()
                     result = p.result
                     if result['status']:
                         self.lock.acquire()
                         msg = "[+] {target} | {file}".format(
                             target=result['target'], file=exp)
                         color.cprint(
                             msg + ' ' * (self.console_width - len(msg)),
                             CYAN)
                         self.lock.release()
                         saveResult(self.filename, result)
                 except RequestException, e:
                     pass
                 except IOError, e:
                     pass
                 except Exception, e:
                     if e.message <> "":
                         # print dir(e)
                         print "error:%s|%s|%s" % (e.message, exp, target)
                         pass
Ejemplo n.º 5
0
 def works(self):
     while self.que.qsize() > 0:
         exp = self.que.get_nowait() 
         m = __import__(exp[:-3])
         myplugin = getattr(m, "Exploit")
         #只剩一个插件,启动线程
         if self.que.qsize() == 0:
             for target in self.targets:
                 self.tq.put(target)
             ts = []
             for i in range(self.tnum):
                     t = threading.Thread(target=self.task,args=(myplugin,exp))
                     ts.append(t)
                     t.start()
             for t in ts:
                 t.join()
         else:
             for target in self.targets:
                 self.pross = self.pross + 1
                 self.lock.acquire()
                 msg = '[*] %s|%s|%s' % (target,str(self.pross) + "/"+ str(self.maxpross),exp)
                 wi = self.console_width -len(msg)
                 if wi < 0:
                     msg = msg[0:self.console_width]
                 sys.stdout.write(msg + ' ' * (self.console_width -len(msg)) + '\r')
                 self.lock.release()
                 try:
                     p = myplugin(target,exp)
                     p.verify()
                     result = p.result
                     if result['status']:
                         self.lock.acquire()
                         msg = "[+] {target} | {file}".format(target=result['target'],file=exp)
                         color.cprint(msg + ' ' * (self.console_width -len(msg)),CYAN)
                         self.lock.release()
                         saveResult(self.filename,result)
                 except RequestException,e:
                     pass
                 except IOError,e:
                     pass
                 except Exception,e:
                     if e.message <> "":
                         # print dir(e)
                         print "error:%s|%s|%s" % (e.message,exp,target)
                         pass
Ejemplo n.º 6
0
 def works(self):
     while self.que.qsize() > 0:
         exp = self.que.get()
         m = __import__(exp[:-3])
         myplugin = getattr(m, "Exploit")
         for target in self.targets:
             msg = 'Scaning target:%s' % target
             sys.stdout.write(msg + ' ' * (self.console_width - len(msg)) +
                              '\r')
             try:
                 p = myplugin(target, exp)
                 p.verify()
                 result = p.result
                 if result['status']:
                     self.lock.acquire()
                     color.cprint(
                         "[+] {target} | {file}".format(
                             target=result['target'], file=exp), CYAN)
                     self.lock.release()
                     saveResult(self.filename, result)
             except Exception, e:
                 #print e
                 pass
def evaluate_lenet5(learning_rate=0.1, n_epoches=200, 
	    nkerns=[20, 50], batch_size=500):
	# load data from dataset
	logging.info('... loading data')
	trainData, trainLabel = util.load_total_data()
	testData = util.loadTestData()
	trainData = util.upToInt(trainData)

	train_set_x = theano.shared(np.asarray(trainData,
		dtype = theano.config.floatX),
		borrow = True)

	train_set_y = theano.shared(np.asarray(trainLabel,
		dtype = theano.config.floatX),
		borrow = True)

	test_set_x = theano.shared(np.asarray(testData,
		dtype = theano.config.floatX),
		borrow = True)

	train_set_y = T.cast(train_set_y, 'int32')
	n_train_batches = train_set_x.get_value(borrow=True).shape[0]
	n_valid_batches = train_set_x.get_value(borrow=True).shape[0]
	n_test_batches = test_set_x.get_value(borrow=True).shape[0]

	n_train_batches /= batch_size
	n_valid_batches /= batch_size
	n_test_batches /= batch_size

	rng = np.random.RandomState(23455)

	# allocate symbolic variables for the data
	index = T.lscalar() # index to a [mini]batch

	# start-snippet-1
	x = T.matrix('x')
	y = T.ivector('y')

	logging.info('... building the model')
	layer0_input = x.reshape((batch_size, 1, 28, 28))

	# Construct the first convolutional pooling layer:
    # filtering reduces the image size to (28-5+1 , 28-5+1) = (24, 24)
    # maxpooling reduces this further to (24/2, 24/2) = (12, 12)
    # 4D output tensor is thus of shape (batch_size, nkerns[0], 12, 12)
	layer0 = LeNetConvPoolLayer(
		rng,
		input=layer0_input,
		image_shape=(batch_size, 1, 28, 28),
		filter_shape=(nkerns[0], 1, 5, 5),
		poolsize=(2, 2)
	)

    # Construct the second convolutional pooling layer
    # filtering reduces the image size to (12-5+1, 12-5+1) = (8, 8)
    # maxpooling reduces this further to (8/2, 8/2) = (4, 4)
    # 4D output tensor is thus of shape (batch_size, nkerns[1], 4, 4)
	layer1 = LeNetConvPoolLayer(
		rng,
		input=layer0.output,
		image_shape=(batch_size, nkerns[0], 12, 12),
		filter_shape=(nkerns[1], nkerns[0], 5, 5),
		poolsize=(2, 2)
	)

    # the HiddenLayer being fully-connected, it operates on 2D matrices of
    # shape (batch_size, num_pixels) (i.e matrix of rasterized images).
    # This will generate a matrix of shape (batch_size, nkerns[1] * 4 * 4),
    # or (500, 50 * 4 * 4) = (500, 800) with the default values.
	layer2_input = layer1.output.flatten(2)

    # construct a fully-connected sigmoidal layer
	layer2 = HiddenLayer(
		rng,
		input=layer2_input,
		n_in=nkerns[1] * 4 * 4,
		n_out=500,
		activation=T.tanh
	)

    # classify the values of the fully-connected sigmoidal layer
	layer3 = LogisticRegression(input=layer2.output, n_in=500, n_out=10)

    # the cost we minimize during training is the NLL of the model
	cost = layer3.negative_log_likelihood(y)

    # create a function to compute the mistakes that are made by the model
	validate_model = theano.function(
		[index],
		layer3.errors(y),
		givens={
			x: train_set_x[index * batch_size: (index + 1) * batch_size],
			y: train_set_y[index * batch_size: (index + 1) * batch_size],
		}
	)

	# create a list of all model parameters to be fit by gradient descent
	params = layer3.params + layer2.params + layer1.params + layer0.params

    # create a list of gradients for all model parameters
	grads = T.grad(cost, params)

    # train_model is a function that updates the model parameters by
    # SGD Since this model has many parameters, it would be tedious to
    # manually create an update rule for each model parameter. We thus
    # create the updates list by automatically looping over all
    # (params[i], grads[i]) pairs.
	updates = [
		(param_i, param_i - learning_rate * grad_i)
		for param_i, grad_i in zip(params, grads)
	]

	train_model = theano.function(
		[index],
		cost,
		updates=updates,
		givens={
			x: train_set_x[index * batch_size: (index + 1) * batch_size],
			y: train_set_y[index * batch_size: (index + 1) * batch_size]
		}
	)

	validation_model = theano.function(
		[index],
		layer3.errors(y),
		givens={
			x: train_set_x[index * batch_size: (index + 1) * batch_size],
			y: train_set_y[index * batch_size: (index + 1) * batch_size]
		}
	)

	logging.info('... training')
	patience = 10000
	patience_increase = 2
	improvement_threshold = 0.995

	validation_frequency = min(n_train_batches, patience / 2)

	best_validation_loss = np.inf
	best_iter = 0
	test_score = 0.
	start_time = time.clock()

	epoch = 0
	done_looping = False

	while (epoch < n_epoches) and (not done_looping):
		epoch = epoch + 1
		for minibatch_index in xrange(n_train_batches):
			iter = (epoch - 1) * n_train_batches + minibatch_index

			if iter % 100 == 0:
				logging.info('training @ iter = %d' % (iter))
			cost_ij = train_model(minibatch_index)

			if (iter + 1) % validation_frequency == 0:
				# compute zero-one loss on validation set
				validation_losses = [validation_model(i) for i
						in xrange(n_valid_batches)]
				this_validation_loss = np.mean(validation_losses)
				print('epoch %i, minibatch %i/%i, validation error %f %%' %
					(epoch, minibatch_index + 1, n_train_batches,
						this_validation_loss * 100.))

				if (this_validation_loss * 100.) < 0.001:
					done_looping = True
					break

	end_time = time.clock()
	logging.info('The code for file ' +
						os.path.split(__file__)[1] +
							' ran for %.2fm' % ((end_time - start_time) / 60.))

	# make a prediction and save file
	# make a prediction
	predict_model = theano.function(
		inputs=[index],
		outputs= layer3.predict(),
		givens={
			x: test_set_x[index * batch_size: (index + 1) * batch_size]
		}
	)

	# save the result file
	testLabel = np.array([])
	for test_index in range(n_test_batches):
		tempLabel = predict_model(test_index)
		testLabel = np.hstack((testLabel, tempLabel))
	util.saveResult(testLabel, './result/cnn_result.csv')
def GaussianNBClassify(trainData, trainLabel, testData):
	nbClf = GaussianNB()
	nbClf.fit(trainData, np.ravel(trainLabel))
	testLabel = nbClf.predict(testData)
	util.saveResult(testLabel, './result/gaussion_nb_result.csv')
	return testLabel
Ejemplo n.º 9
0
def knnClassify(trainData, trainLabel, testData):
	knnClf = KNeighborsClassifier()
	knnClf.fit(trainData, np.ravel(trainLabel))
	testLabel = knnClf.predict(testData)
	util.saveResult(testLabel, './result/sklearn_knn_result.csv')
	return testLabel
Ejemplo n.º 10
0
	done_looping = False
	prev_cost = np.inf

	start_time = time.clock()
	while (epoch < n_epochs) and (not done_looping):
		epoch = epoch + 1

		cost = train_model()
		impr = prev_cost - cost
		logging.info('epoch %d, cost: %f, impr: %f' % (epoch+1, cost, impr))
		if impr < improvement_threshold:
			break
		prev_cost = cost

	end_time = time.clock()
	print >> sys.stderr, ('The code for file ' +
			os.path.split(__file__)[1] +
				' ran for %.2fm' % ((end_time - start_time) / 60.))

	# make a prediction
	predict_model = theano.function(
		inputs=[],
		outputs= classifier.predict(),
		givens={
			x: test_set_x
		}
	)

	testLabel = predict_model()
	util.saveResult(testLabel, './result/mlp_result.csv')
Ejemplo n.º 11
0
def treeClassify(trainData, trainLabel, testData):
    treeClf = tree.DecisionTreeClassifier()
    treeClf.fit(trainData, np.ravel(trainLabel))
    testLabel = treeClf.predict(testData)
    util.saveResult(testLabel, "./result/decision_tree_result.csv")
    return testLabel
def rfClassify(trainData, trainLabel, testData):
	rfClf = RandomForestClassifier(n_estimators=100)
	rfClf.fit(trainData, np.ravel(trainLabel))
	testLabel = rfClf.predict(testData)
	util.saveResult(testLabel, './result/random_forest_result.csv')
	return testLabel
Ejemplo n.º 13
0
def svcClassify(trainData, trainLabel, testData):
	svcClf = svm.SVC(C=5.0)
	svcClf.fit(trainData, np.ravel(trainLabel))
	testLabel = svcClf.predict(testData)
	util.saveResult(testLabel, './result/svm_result.csv')
	return testLabel
Ejemplo n.º 14
0
def svmLinearClassify(trainData, trainLabel,testData):
	svcClf = svm.SVC(kernel='linear')
	svcClf.fit(trainData, np.ravel(trainLabel))
	testLabel = svcClf.predict(testData)
	util.saveResult(testLabel, './result/svm_linear_result.csv')
	return testLabel
Ejemplo n.º 15
0
def baggingClassify(trainData, trainLabel, testData):
	baggingCif = BaggingClassifier(KNeighborsClassifier())
	baggingCif.fit(trainData, np.ravel(trainLabel))
	testLabel = baggingCif.predict(testData)
	util.saveResult(testLabel, './result/bagging_result.csv')
	return testLabel
def MultinomialNBClassify(trainData, trainLabel, testData):
	nbClf = MultinomialNB()
	nbClf.fit(trainData, np.ravel(trainLabel))
	testLabel = nbClf.predict(testData)
	util.saveResult(testLabel, './result/multinomial_nb_result.csv')
	return testLabel
def lrClassify(trainData, trainLabel, testData):
	lrClf = linear_model.LogisticRegression(C=1e5)
	lrClf.fit(trainData, np.ravel(trainLabel))
	testLabel = lrClf.predict(testData)
	util.saveResult(testLabel, './result/logistic_regression_result.csv')
	return testLabel
Ejemplo n.º 18
0
def adaboostClassify(trainData, trainLabel, testData):
    abClf = AdaBoostClassifier(n_estimators=500)
    abClf.fit(trainData, np.ravel(trainLabel))
    testLabel = abClf.predict(testData)
    util.saveResult(testLabel, "./result/adaboost_result.csv")
    return testLabel
Ejemplo n.º 19
0
    testData = testData.reshape(testData.shape[0], 1, 28, 28)

    model = Sequential()

    model.add(Convolution2D(32, 1, 3, 3, border_mode='full'))
    model.add(Activation('relu'))
    model.add(Convolution2D(32, 32, 3, 3))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(poolsize=(2, 2)))
    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Dense(32*196, 128))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))

    model.add(Dense(128, nb_classes))
    model.add(Activation('softmax'))

    model.compile(loss='categorical_crossentropy', optimizer='adadelta')

    model.fit(trainData, trainLabel, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True)

    logging.info("Training process finished!")
    logging.info("Predict for testData...")
    y_pred = model.predict(testData,)
    testLabel = np.argmax(y_pred, axis=1)

    logging.info("Save result...")
    util.saveResult(testLabel, './result/keras_cnn_result.csv')