Skip to content

Commit

Permalink
Adding basic logging
Browse files Browse the repository at this point in the history
  • Loading branch information
phretor committed Oct 18, 2016
1 parent 45ff152 commit 690b5c4
Show file tree
Hide file tree
Showing 10 changed files with 312 additions and 260 deletions.
13 changes: 12 additions & 1 deletion build.gradle
Expand Up @@ -7,7 +7,6 @@ buildscript {
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'
apply plugin: 'com.github.johnrengelman.shadow'
Expand All @@ -32,6 +31,13 @@ compileJava {
options.compilerArgs << "-Xlint:unchecked"
}

eclipse {
classpath {
downloadJavadoc = true
downloadSources = true
}
}

dependencies {
compile files('lib/androidmarketapi-0.6.jar')
compile files('lib/axml-2.0.jar')
Expand All @@ -40,6 +46,7 @@ dependencies {
compile files('lib/soot-infoflow.jar')
compile files('lib/soot-infoflow-android.jar')

compile 'org.slf4j:slf4j-parent:1.7.21'
compile 'org.apache.commons:commons-compress:1.12'
compile 'jnetpcap:jnetpcap:1.4.r1425-1f'
compile 'net.sourceforge.jregex:jregex:1.2_01'
Expand All @@ -58,6 +65,10 @@ dependencies {
compile 'org.languagetool:language-all:3.5'
}

task showMeCache << {
configurations.compile.each { println it }
}

sourceSets {
main {
java {
Expand Down
5 changes: 3 additions & 2 deletions src/java/it/polimi/elet/necst/heldroid/goodware/Main.java
Expand Up @@ -375,8 +375,9 @@ private static Double checkAnalysisDetectionRatio(File apkFile) {
int detectionCount = 0;
int totalScans = 0;

for (Iterator<String> iterator = scans.keys(); iterator.hasNext(); ) {
String key = iterator.next();
Iterator<?> iterator = scans.keys();
while (iterator.hasNext()) {
String key = (String) iterator.next();
Object field = scans.get(key);

if (field instanceof JSONObject) {
Expand Down
Expand Up @@ -321,8 +321,8 @@ private class HashHandler extends BaseHandler implements HttpHandler {

@Override
public void handle(HttpExchange exchange) throws IOException {
Map<String, String> params = (Map<String, String>)exchange.getAttribute("parameters");
String hash = params.get("hash");
Map<?, ?> params = (Map<?, ?>)exchange.getAttribute("parameters");
String hash = (String) params.get("hash");
String response = MainServer.this.fetchResponseByHash(hash);

if (response == null) {
Expand Down
279 changes: 151 additions & 128 deletions src/java/it/polimi/elet/necst/heldroid/ransomware/Factory.java
Expand Up @@ -9,6 +9,9 @@

import javax.xml.parsers.ParserConfigurationException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import it.polimi.elet.necst.heldroid.ransomware.device_admin.DeviceAdminDetector;
import it.polimi.elet.necst.heldroid.ransomware.emulation.TrafficScanner;
import it.polimi.elet.necst.heldroid.ransomware.encryption.EncryptionFlowDetector;
Expand Down Expand Up @@ -39,169 +42,189 @@
import opennlp.tools.stemmer.snowball.SnowballStemmer;

public class Factory {
public static EncryptionFlowDetector createEncryptionFlowDetector(File confDir)
private final static Logger logger = LoggerFactory.getLogger(Factory.class);

public static EncryptionFlowDetector createEncryptionFlowDetector(File confDir)
throws ParserConfigurationException {

EncryptionFlowDetector encryptionFlowDetector = new EncryptionFlowDetector(confDir);
EncryptionFlowDetector encryptionFlowDetector = new EncryptionFlowDetector(confDir);

encryptionFlowDetector.setAndroidPlatformsDir(Globals.ANDROID_PLATFORMS_DIRECTORY);
return encryptionFlowDetector;
}
encryptionFlowDetector.setAndroidPlatformsDir(Globals.ANDROID_PLATFORMS_DIRECTORY);
return encryptionFlowDetector;
}

public static DeviceAdminDetector createDeviceAdminDetector()
throws ParserConfigurationException {
DeviceAdminDetector deviceAdminDetector = new DeviceAdminDetector();

return deviceAdminDetector;
}

public static PhotoDetector createPhotoAdminDetector() {
return new PhotoDetector();
}
public static DeviceAdminDetector createDeviceAdminDetector() throws ParserConfigurationException {
DeviceAdminDetector deviceAdminDetector = new DeviceAdminDetector();

public static TrafficScanner createTrafficScanner() {
HtmlScanner htmlScanner = new HtmlScanner(createClassifierCollection());
htmlScanner.setAcceptanceStrategy(createAcceptanceStrategy());
return new TrafficScanner(htmlScanner);
}
return deviceAdminDetector;
}

public static PhotoDetector createPhotoAdminDetector() {
return new PhotoDetector();
}

public static MultiLockingStrategy createLockingStrategy() throws ParserConfigurationException {
MultiLockingStrategy allLockingStratgies = new MultiLockingStrategy();
public static TrafficScanner createTrafficScanner() {
HtmlScanner htmlScanner = new HtmlScanner(createClassifierCollection());
htmlScanner.setAcceptanceStrategy(createAcceptanceStrategy());
return new TrafficScanner(htmlScanner);
}

allLockingStratgies.add(new AdminLockingStrategy());
allLockingStratgies.add(new DrawOverLockingStrategy());
allLockingStratgies.add(new DialogLockingStrategy());
public static MultiLockingStrategy createLockingStrategy() throws ParserConfigurationException {
MultiLockingStrategy allLockingStratgies = new MultiLockingStrategy();

return allLockingStratgies;
}
allLockingStratgies.add(new AdminLockingStrategy());
allLockingStratgies.add(new DrawOverLockingStrategy());
allLockingStratgies.add(new DialogLockingStrategy());

public static ImageScanner createImageScanner() {
TextClassifierCollection textClassifierCollection = createClassifierCollection();
ImageScanner imageScanner = new ImageScanner(textClassifierCollection);

imageScanner.setAcceptanceStrategy(createAcceptanceStrategy());
return imageScanner;
}
return allLockingStratgies;
}

public static MultiResourceScanner createResourceScanner() throws ParserConfigurationException {
TextClassifierCollection textClassifierCollection = createClassifierCollection();
MultiResourceScanner multiResourceScanner = new MultiResourceScanner(textClassifierCollection);
public static ImageScanner createImageScanner() {
logger.info("Creating ImageScanner");

multiResourceScanner.add(new XmlLayoutScanner(textClassifierCollection));
multiResourceScanner.add(new XmlValuesScanner(textClassifierCollection));
multiResourceScanner.add(new HtmlScanner(textClassifierCollection));
multiResourceScanner.setAcceptanceStrategy(createAcceptanceStrategy());
TextClassifierCollection textClassifierCollection = createClassifierCollection();

return multiResourceScanner;
}
logger.info("Instantiating ImageScanner class");

public static AcceptanceStrategy createAcceptanceStrategy() {
return new AcceptanceStrategy() {
@Override
public Result accepts(TextClassification textClassification) {
List<SentenceClassification> accuses = textClassification.findAllSentences(Globals.MIN_LIKELIHOOD_THRESHOLD, "threat", "porn", "law", "copyright");
List<SentenceClassification> moneypaks = textClassification.findAllSentences(Globals.MIN_LIKELIHOOD_THRESHOLD, "moneypak");

double accuseScore = weightNumerosity(accuses);
double moneypakScore = weightNumerosity(moneypaks);

Result result = new Result();

result.setAccepted((moneypakScore >= Globals.MIN_LIKELIHOOD_THRESHOLD) && (accuseScore >= Globals.MIN_LIKELIHOOD_THRESHOLD));
result.setScore(accuseScore);
result.setComment(
String.format("Threat: %f, Porn: %f, Law: %f, Copyright: %f, Moneypak: %f",
textClassification.maxLikelihood("threat"),
textClassification.maxLikelihood("porn"),
textClassification.maxLikelihood("law"),
textClassification.maxLikelihood("copyright"),
textClassification.maxLikelihood("moneypak")));

result.setFileClassification(textClassification.getFileClassification());
return result;
}
};
}
ImageScanner imageScanner = new ImageScanner(textClassifierCollection);

private static double weightNumerosity(List<SentenceClassification> sentences) {
double max = 0;
double sum = 0;
logger.info("Setting acceptance strategy");
imageScanner.setAcceptanceStrategy(createAcceptanceStrategy());

for (SentenceClassification s : sentences) {
double t = computeThreshold(s);
return imageScanner;
}

if (s.getLikelihood() >= t) {
sum += (s.getLikelihood() - t);
if (s.getLikelihood() > max)
max = s.getLikelihood();
}
}
public static MultiResourceScanner createResourceScanner() throws ParserConfigurationException {
logger.info("Creating resource scanner...");

if (max < Globals.MIN_LIKELIHOOD_THRESHOLD)
return 0;
TextClassifierCollection textClassifierCollection = createClassifierCollection();
MultiResourceScanner multiResourceScanner = new MultiResourceScanner(textClassifierCollection);

return max + (1 - max) * (1 - Math.exp(-sum));
}
multiResourceScanner.add(new XmlLayoutScanner(textClassifierCollection));
multiResourceScanner.add(new XmlValuesScanner(textClassifierCollection));
multiResourceScanner.add(new HtmlScanner(textClassifierCollection));
multiResourceScanner.setAcceptanceStrategy(createAcceptanceStrategy());

private static double computeThreshold(SentenceClassification s) {
double stemCoefficient = (s.getProducedStemsCount() - Globals.MIN_PRODUCED_STEMS) / (Globals.MAX_PRODUCED_STEMS - Globals.MIN_PRODUCED_STEMS);
return multiResourceScanner;
}

stemCoefficient = Math.max(0, Math.min(1, stemCoefficient));
public static AcceptanceStrategy createAcceptanceStrategy() {
return new AcceptanceStrategy() {
@Override
public Result accepts(TextClassification textClassification) {
List<SentenceClassification> accuses = textClassification
.findAllSentences(Globals.MIN_LIKELIHOOD_THRESHOLD, "threat", "porn", "law", "copyright");
List<SentenceClassification> moneypaks = textClassification
.findAllSentences(Globals.MIN_LIKELIHOOD_THRESHOLD, "moneypak");

return Globals.MAX_LIKELIHOOD_THRESHOLD - stemCoefficient * (Globals.MAX_LIKELIHOOD_THRESHOLD - Globals.MIN_LIKELIHOOD_THRESHOLD);
}
double accuseScore = weightNumerosity(accuses);
double moneypakScore = weightNumerosity(moneypaks);

Result result = new Result();

result.setAccepted((moneypakScore >= Globals.MIN_LIKELIHOOD_THRESHOLD)
&& (accuseScore >= Globals.MIN_LIKELIHOOD_THRESHOLD));
result.setScore(accuseScore);
result.setComment(String.format("Threat: %f, Porn: %f, Law: %f, Copyright: %f, Moneypak: %f",
textClassification.maxLikelihood("threat"), textClassification.maxLikelihood("porn"),
textClassification.maxLikelihood("law"), textClassification.maxLikelihood("copyright"),
textClassification.maxLikelihood("moneypak")));

result.setFileClassification(textClassification.getFileClassification());
return result;
}
};
}

public static TextClassifierCollection createClassifierCollection() {
TextClassifier englishClassifier = createClassifier(SupportedLanguage.ENGLISH);
TextClassifier russianClassifier = createClassifier(SupportedLanguage.RUSSIAN);
TextClassifier spanishClassifier = createClassifier(SupportedLanguage.SPANISH);
TextClassifierCollection textClassifierCollection = new TextClassifierCollection();
private static double weightNumerosity(List<SentenceClassification> sentences) {
double max = 0;
double sum = 0;

textClassifierCollection.add(SupportedLanguage.ENGLISH, englishClassifier);
textClassifierCollection.add(SupportedLanguage.RUSSIAN, russianClassifier);
textClassifierCollection.add(SupportedLanguage.SPANISH, spanishClassifier);
for (SentenceClassification s : sentences) {
double t = computeThreshold(s);

return textClassifierCollection;
if (s.getLikelihood() >= t) {
sum += (s.getLikelihood() - t);
if (s.getLikelihood() > max)
max = s.getLikelihood();
}
}

private static TextClassifier createClassifier(SupportedLanguage language) {
StopWordList swc = StopWordList.fromFile(new File(Globals.STOP_WORDS_DIRECTORY, language.getName() + ".txt"));
Stemmer stm = new SnowballStemmer(language.getStemmerAlgorithm());
if (max < Globals.MIN_LIKELIHOOD_THRESHOLD)
return 0;

try {
InputStream modelStream = new FileInputStream(new File(Globals.MODELS_DIRECTORY, language.getCode() + "-sent.bin"));
SentenceModel model = new SentenceModel(modelStream);
SentenceDetector sd = new SentenceDetectorME(model);
Segmenter segmenter = new Segmenter(swc, stm, sd);
GenericTextClassifier classifier = new GenericTextClassifier(segmenter);
return max + (1 - max) * (1 - Math.exp(-sum));
}

File trainingData = new File(Globals.TRAINING_DATA_DIRECTORY, language.getCode() + "-ransom.csv");
BufferedReader reader = new BufferedReader(new FileReader(trainingData));
String line;
private static double computeThreshold(SentenceClassification s) {
double stemCoefficient = (s.getProducedStemsCount() - Globals.MIN_PRODUCED_STEMS)
/ (Globals.MAX_PRODUCED_STEMS - Globals.MIN_PRODUCED_STEMS);

while ((line = reader.readLine()) != null) {
if (line.equals(""))
continue;
stemCoefficient = Math.max(0, Math.min(1, stemCoefficient));

int commaIndex = line.indexOf(',');
String category = sanitize(line.substring(0, commaIndex - 1));
String text = sanitize(line.substring(commaIndex + 1));
return Globals.MAX_LIKELIHOOD_THRESHOLD
- stemCoefficient * (Globals.MAX_LIKELIHOOD_THRESHOLD - Globals.MIN_LIKELIHOOD_THRESHOLD);
}

classifier.teach(category, text);
}
public static TextClassifierCollection createClassifierCollection() {
logger.info("Creating classifier collection...");

reader.close();
TextClassifier englishClassifier = createClassifier(SupportedLanguage.ENGLISH);
TextClassifier russianClassifier = createClassifier(SupportedLanguage.RUSSIAN);
TextClassifier spanishClassifier = createClassifier(SupportedLanguage.SPANISH);

return classifier;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
TextClassifierCollection textClassifierCollection = new TextClassifierCollection();

textClassifierCollection.add(SupportedLanguage.ENGLISH, englishClassifier);
textClassifierCollection.add(SupportedLanguage.RUSSIAN, russianClassifier);
textClassifierCollection.add(SupportedLanguage.SPANISH, spanishClassifier);

return textClassifierCollection;
}

private static TextClassifier createClassifier(SupportedLanguage language) {
logger.info("Creating classifier for " + language);

StopWordList swc = StopWordList.fromFile(new File(Globals.STOP_WORDS_DIRECTORY, language.getName() + ".txt"));
Stemmer stm = new SnowballStemmer(language.getStemmerAlgorithm());

try {
InputStream modelStream = new FileInputStream(
new File(Globals.MODELS_DIRECTORY, language.getCode() + "-sent.bin"));
SentenceModel model = new SentenceModel(modelStream);
SentenceDetector sd = new SentenceDetectorME(model);
Segmenter segmenter = new Segmenter(swc, stm, sd);
GenericTextClassifier classifier = new GenericTextClassifier(segmenter);

File trainingData = new File(Globals.TRAINING_DATA_DIRECTORY, language.getCode() + "-ransom.csv");
BufferedReader reader = new BufferedReader(new FileReader(trainingData));
String line;

private static String sanitize(String str) {
return str.trim().replace("\"", "");
while ((line = reader.readLine()) != null) {
if (line.equals(""))
continue;

int commaIndex = line.indexOf(',');
String category = sanitize(line.substring(0, commaIndex - 1));
String text = sanitize(line.substring(commaIndex + 1));

classifier.teach(category, text);
}

reader.close();

logger.info("Classifier for " + language + " is ready!");

return classifier;
} catch (Exception e) {

logger.error("Cannot create classifier for " + language + " because: " + e);
e.printStackTrace();
return null;
}
}

private static String sanitize(String str) {
return str.trim().replace("\"", "");
}
}

0 comments on commit 690b5c4

Please sign in to comment.