Introduction

Continuing from the previous article, you learned about conditional probability and the Bayes’ Rule. You also learned about how to go about implementing the same in machine learning using C# and initiate the first steps to a malware classifier for the Android platform. In this article, we go about fine tuning the data set and the trying to understand what might be the best ways to improve the detection rates while reducing on the false positives. Furthermore, we also take a good look at spam mail classification and go about a similar process for classifying spam mails.

Android Classification

So far, we have the following options from malware and benign sample sets.

  1. 1260 samples permissions histogram, rank and probabilities from the Malware Genome Project paper.
  2. Permissions count and file sizes based on comparison parameters are taken into account.
  3. Folder structure depth in both sets can be a good factor, which has to be boiled down to a single number.
  4. Finally, the comparison between the manifest permissions and the actually used permissions can give us an excellent idea of the kind of activities the files perform. In addition, the Stoway research data set gives us a pretty comprehensive listing of API’s and their respective permissions required for their execution. This data set can be simply consulted to build a profile in addition to the score ratings from the Bayesian component.
  5. Strings are indeed an important data pool in any malware analysis session and hence a separate set of string-based analysis algorithms can be incorporated. Things like non-standard character sequences, URL strings, high entropy strings sequences, base64 strings, and encryption keyscan also be taken into account.
  6. Opcodes level analysis can also be done using Bayes rule, which would make use of the most probable opcode sequences from a corpus of disassembly. This would entail finding patterns from the opcodes extracted from the instruction arrays referencing each method and finding the statistical significance of the frequency of their occurrence,which would boil down to probability numbers for each opcode sequence which has to be further curated to maximize efficacy.

As of now, we will focus on getting maximum leverage from the permissions and file attributes.

Is the Bayes Rule an exact science? The answer would be no. The performance can only be related to the available data set from which the best parameters have to be selected. Any anomalies or outliers have to be carefully taken into account so that the result is not skewed. On the contrary, this might also involve generalizing things or ignoring certain possibilities to make the model stand on good enough prediction rates without dealing with exceptions. The Bayes rule ultimately relies on the statistical properties of a data set and the accuracy of the data set to begin with. Machine learning takes from both statistics and data mining, which are of course related and hence overlapping approaches, are quite the norm. It is largely based on the observation of collected data and inferring a model based on the statistical properties of the data set. Most definitely not rocket science for our purposes.

Back to the premise, with the existing data sets a C# class is written that encapsulates the probability value of each permission occurring from the top 20 permissions list, which can be easily found out by making a listing of each permission set of a malware and saved in a vector. After which, a simple frequency count is done on the vector list for each of the permissions. Finally, the top 20 counts are listed along with their counts. Dividing that number with the total of corpus and you get the probability values for each of them.

A vector is simply a row where in each parameter (permission) is represented in a separate column. Each row thus represents the total permissions in that particular file. This representation is helpful for data inference and other statistical operations. This representation also lends itself to Matrix maths and things like dimensionality reduction can be done with finesse using matrices. Dimensionality reduction is wherein a number of columns in our current vector format are collapsed so as to minimize the number of dimensions. Herein, 2 or more columns are combined to a single value. This is very helpful when trying to visualise the data. We will also add additional values obtained from the file itself and embellish the vector set.

A problem with the current parameter set would be the inclusion or exclusion of certain permissions that don’t fall in the permissions list. Ideally, the list of all available permissions is mandatory for a perfectly accurate data set. However, such data is not reliably available for many of the android API’s and is also a current research goal. In such cases, the Bayes rule calculation would involve incorporating the probability values of such new or non-data set existent values. In our case, the probability values of such terms are very small statistically or else their values could have been computed simply from the data set itself.

There are quite a few ways to deal with them. One method would involve setting a probability value for all such new values in a general sense, so that is we don’t know what the actual occurrence value is and are just taking a best guess. A very small value between zero and one has to set. Multiplying the probability values with a zero would result in an entire score having the final value zero. So the best we can do with this approach is to set it to something minuscule, such as 0.00001. This would ensure a non-zero value. Again, the data sets analysis has to be re-run to see the effect on the final scores. In our case, this method does no good as the values go towards denormal numbers, which denote very small numbers if the values of such constants are small enough and the permissions list does not account for every single permission, especially the non-top-20 ones. So any such outlier will drastically reduce our existing score and thus register as a zero if the denormal numbers are not taken care of separately.

The other method would be to simply ignore them. This indeed makes sense for our purposes because we also have other corroborating data and are not relying exclusively on manifest data only to make our final decision.

I have a training set of 1824 malwares currently and the obtained vectors for each malware gives a value range like the one shown below –an excerpt taken from 912 malwares from the training set. This is without affecting the prior probability values for the malware and the benign score respectively that is –the prior probability of both scores is taken as 1. Furthermore, each column is sorted individually so as to give a better statistical view of the vector set.

This is an example of supervised machine learning, as we have expected outputs of the training set (i.e. we know what are their parameters and that they are either malware or benign).

In unsupervised learning, there are no expected output samples for a training set and the algorithm is expected to find a pattern to give us a resulting output.

Perm- Count File Size Malware Bayes Score Benign Bayes Score

18

24226645

0.001

0.001

18

8468389

0.001

0.001

17

7467260

0.000977778

0.000890476

17

6415832

0.000977778

0.000890476

17

6039765

0.000977778

0.000890476

16

5974795

0.000977778

0.000890476

16

5777643

0.000977778

0.000890476

16

5457274

0.000977778

0.000890476

16

4818527

0.000977778

0.000890476

16

4497906

0.000977778

0.000890476

16

4496219

0.000977778

0.000890476

16

4006218

0.000977778

0.000890476

16

3987937

0.000914921

0.000645242

15

3773449

0.000914921

0.000387302

15

3601934

0.000914921

0.000306013

15

3502257

0.000793863

0.000306013

15

3499659

0.000742828

0.000221738

15

3493907

0.000742828

0.000221738

15

3485168

0.000742828

0.000221738

15

3455312

0.000742828

0.000221738

15

3444113

0.000742828

0.000221738

15

3387307

0.000742828

0.000221738

15

3342585

0.000742828

0.000221738

14

3183472

0.000672222

0.000154067

14

3144422

0.000672222

0.000118519

14

3025182

0.000672222

0.000118519

14

2896384

0.00061503

0.000118519

14

2808157

0.00061503

8.82E-05

14

2415886

0.00061503

7.02E-05

14

2413657

0.000583807

4.63E-05

14

2345273

0.000583807

4.63E-05

14

2344509

0.000533652

3.84E-05

14

2344509

0.000438889

3.84E-05

14

2344501

0.000438889

3.84E-05

14

2344499

0.000438889

3.42E-05

14

2344370

0.000438889

3.42E-05

14

2327761

0.000438889

3.41E-05

14

2327740

0.000438889

3.41E-05

14

2327731

0.000438889

3.41E-05

14

2327731

0.000438889

3.41E-05

14

2327728

0.000438889

3.41E-05

14

2327723

0.000438889

3.41E-05

14

2326862

0.000438889

3.41E-05

14

2326706

0.000438889

3.41E-05

14

2312662

0.000438889

3.41E-05

14

2312659

0.000438889

3.41E-05

14

2311000

0.000438889

3.41E-05

13

2310879

0.000438889

3.41E-05

13

2298341

0.000438889

3.41E-05

13

2297880

0.000433474

3.41E-05

13

2295666

0.000410675

3.41E-05

13

2250018

0.000405608

3.41E-05

13

2249429

0.000401549

3.41E-05

13

2248127

0.000362338

3.41E-05

13

2246462

0.000333429

3.41E-05

13

2240344

0.000333429

3.41E-05

13

2234780

0.000333429

3.41E-05

13

2231862

0.000333429

3.41E-05

13

2230845

0.00033184

3.41E-05

13

2230794

0.00033184

3.41E-05

13

2229926

0.000329806

3.41E-05

13

2228671

0.000326019

3.41E-05

13

2228652

0.000326019

3.41E-05

13

2228651

0.000326019

3.25E-05

13

2228302

0.000326019

3.25E-05

12

2227522

0.000326019

3.04E-05

12

2227502

0.000326019

2.41E-05

12

2224954

0.000326019

1.79E-05

12

2220170

0.000326019

1.72E-05

12

2219280

0.000326019

1.72E-05

12

2206962

0.000326019

1.64E-05

12

2199999

0.000326019

1.51E-05

12

2184988

0.000326019

1.51E-05

12

2164574

0.000326019

1.19E-05

12

2164452

0.000326019

1.19E-05

12

2162268

0.000326019

1.18E-05

12

2162085

0.000326019

1.18E-05

12

2143573

0.000326019

1.18E-05

12

2140980

0.000326019

1.18E-05

12

2140633

0.000326019

1.18E-05

12

2096814

0.000326019

1.18E-05

12

2077936

0.000326019

1.18E-05

12

2062058

0.000326019

1.18E-05

12

2056288

0.000326019

1.18E-05

12

2047220

0.000326019

1.18E-05

12

2034035

0.000326019

1.18E-05

12

2031469

0.000326019

1.18E-05

12

2028208

0.000326019

1.18E-05

12

2025718

0.000326019

1.18E-05

12

2022822

0.000326019

1.18E-05

12

2011531

0.000326019

1.18E-05

12

2009875

0.000326019

1.18E-05

12

2001244

0.000326019

1.18E-05

12

1965794

0.000326019

1.18E-05

12

1963279

0.000326019

1.18E-05

12

1956846

0.000326019

1.18E-05

12

1954649

0.000326019

1.18E-05

12

1932090

0.000326019

1.18E-05

12

1927973

0.000326019

1.18E-05

12

1916457

0.000326019

1.18E-05

12

1914179

0.000326019

1.18E-05

12

1905009

0.000326019

1.18E-05

12

1902833

0.000326019

1.17E-05

12

1900783

0.000326019

1.17E-05

12

1899228

0.000326019

1.04E-05

12

1897630

0.00031863

1.04E-05

12

1897409

0.00031863

1.04E-05

12

1892519

0.00031863

1.04E-05

12

1892115

0.00031863

1.04E-05

12

1890251

0.00031863

1.04E-05

12

1888421

0.00031863

1.04E-05

12

1888054

0.000272659

1.04E-05

12

1874978

0.00026993

1.04E-05

12

1860822

0.00026993

1.04E-05

12

1859687

0.00026993

9.34E-06

12

1857641

0.00026993

9.13E-06

12

1851677

0.00026993

9.13E-06

12

1849916

0.00026993

9.13E-06

12

1849311

0.00026993

9.13E-06

12

1847296

0.00026993

9.13E-06

12

1844903

0.00026993

8.50E-06

12

1840678

0.00026993

8.50E-06

12

1831141

0.00026993

8.50E-06

12

1825191

0.00026993

8.50E-06

12

1819290

0.00026993

7.74E-06

12

1816244

0.00026993

7.58E-06

12

1813967

0.00026993

7.57E-06

12

1811775

0.00026993

7.57E-06

12

1798959

0.00026993

7.57E-06

11

1789090

0.00026993

7.57E-06

11

1781060

0.00026993

7.57E-06

11

1779459

0.00026993

7.57E-06

11

1779075

0.00026993

7.57E-06

11

1778885

0.00026993

7.57E-06

11

1759935

0.00026993

7.57E-06

11

1753382

0.00026993

7.57E-06

11

1746656

0.00026993

7.57E-06

11

1740083

0.00026993

7.57E-06

11

1738442

0.00026993

7.57E-06

11

1719078

0.00026993

7.57E-06

11

1714852

0.00026993

7.57E-06

11

1689621

0.00026993

7.57E-06

11

1673185

0.00026993

7.57E-06

11

1655169

0.00026993

7.57E-06

11

1653794

0.00026993

7.57E-06

11

1638528

0.00026993

7.57E-06

11

1626369

0.00026993

7.57E-06

11

1624314

0.00026993

7.57E-06

11

1620826

0.00026993

7.57E-06

11

1601949

0.00026993

7.57E-06

11

1582310

0.00026993

7.57E-06

11

1582278

0.00026993

7.57E-06

11

1581549

0.00026993

7.57E-06

11

1579478

0.00026993

7.57E-06

11

1579478

0.00026993

7.57E-06

11

1574782

0.00026993

7.57E-06

11

1574049

0.00026993

7.57E-06

11

1567270

0.00026993

7.57E-06

11

1557201

0.00026993

7.57E-06

11

1555832

0.00026993

7.57E-06

11

1552048

0.00026993

7.57E-06

10

1545015

0.00026993

7.57E-06

10

1540788

0.00026993

7.57E-06

10

1539560

0.00026993

7.57E-06

10

1506249

0.00026993

7.57E-06

10

1498835

0.00026993

7.57E-06

10

1483017

0.00026993

7.57E-06

10

1478844

0.00026993

7.57E-06

10

1478697

0.00026993

7.57E-06

10

1475466

0.000257685

7.57E-06

10

1474115

0.000251959

7.57E-06

10

1472528

0.000250557

7.57E-06

10

1471628

0.000250557

7.57E-06

10

1468020

0.000219157

7.57E-06

10

1459713

0.000219157

7.57E-06

10

1439652

0.000219157

7.57E-06

10

1434699

0.000219157

7.57E-06

10

1433150

0.000219157

7.57E-06

10

1431486

0.000219157

7.57E-06

10

1420318

0.000219157

7.57E-06

10

1410796

0.000219157

7.57E-06

10

1397630

0.000219157

7.57E-06

10

1385207

0.000219157

7.57E-06

10

1377595

0.000219157

7.57E-06

10

1373854

0.000219157

7.57E-06

10

1371828

0.000219157

7.57E-06

10

1367093

0.000219157

7.57E-06

10

1362263

0.000219157

7.57E-06

10

1350946

0.000219157

7.57E-06

10

1331166

0.000219157

7.57E-06

10

1318212

0.000219157

7.57E-06

10

1309760

0.000219157

7.57E-06

10

1280848

0.000219157

7.57E-06

10

1278632

0.000219157

7.57E-06

10

1276012

0.000219157

7.57E-06

10

1274987

0.000219157

7.57E-06

10

1270925

0.000219157

7.57E-06

10

1261166

0.000219157

7.57E-06

10

1256548

0.000219157

7.57E-06

10

1242823

0.000219157

7.57E-06

10

1217251

0.000219157

7.57E-06

10

1212114

0.000219157

7.57E-06

10

1205511

0.000219157

7.57E-06

10

1201799

0.000219157

7.57E-06

10

1197484

0.000219157

7.57E-06

10

1196291

0.000219157

7.57E-06

10

1196163

0.000219157

5.10E-06

10

1195813

0.000219157

4.92E-06

10

1195812

0.000219157

4.60E-06

10

1195805

0.000219157

4.55E-06

10

1195538

0.000219157

4.08E-06

9

1195518

0.000219157

4.04E-06

9

1194417

0.000219157

4.04E-06

9

1192261

0.000219157

4.04E-06

9

1186219

0.000219157

4.04E-06

9

1173777

0.000219157

4.04E-06

9

1170382

0.000219157

4.04E-06

9

1163632

0.000219157

4.04E-06

9

1153963

0.000219157

4.04E-06

9

1137008

0.000219157

4.04E-06

9

1132692

0.000219157

4.04E-06

9

1126089

0.000219157

4.04E-06

Benign training data set excerpt -

Perm- Count File Size Malware Bayes Score Benign Bayes Score

14

28861244

1.12E-06

0.001

9

26175566

0.001

0.001

8

25851136

0.001

0.001

8

25089558

0.001

0.001

8

25082586

0.001

0.001

8

25008889

0.001

0.001

7

24670694

0.001

0.001

7

24602227

0.001

0.001

7

24580718

0.001

0.001

7

24106831

0.001

0.001

7

19844215

0.001

0.001

7

19741531

0.001

0.001

7

18633949

0.001

0.001

7

18327740

0.001

0.001

7

17239268

0.001

0.001

7

17076307

0.001

0.001

6

16534397

0.001

0.001

6

16504297

0.001

0.001

6

16249197

0.001

0.001

6

15814313

0.001

0.001

6

15453474

0.001

0.001

6

14568586

0.001

0.001

6

14390435

0.001

0.001

6

14068884

0.001

0.001

6

14006933

0.001

0.001

6

13582229

0.001

0.001

6

13469927

0.001

0.001

5

13285492

0.001

0.001

5

13051965

0.001

0.001

5

12977404

0.001

0.001

5

12874046

0.001

0.001

5

12766792

0.001

0.001

5

12751810

0.001

0.001

5

12578022

0.001

0.001

5

12442480

0.001

0.001

5

12023098

0.001

0.001

5

12017911

0.001

0.001

5

11946004

0.001

0.001

5

11686860

0.001

0.001

5

11484081

0.001

0.001

5

11483995

0.001

0.001

5

11259678

0.001

0.001

5

11073687

0.001

0.001

5

11019745

0.001

0.001

5

10963547

0.001

0.001

4

10157646

0.001

0.000890476

4

10103706

0.001

0.000890476

4

10087814

0.000977778

0.000890476

4

10068891

0.000977778

0.000890476

4

10056200

0.000977778

0.000890476

4

9948730

0.000977778

0.000890476

4

9686215

0.000977778

0.000890476

4

9379150

0.000977778

0.000890476

4

9323409

0.000977778

0.000890476

4

9245062

0.000977778

0.000890476

4

9142251

0.000977778

0.000890476

4

8977254

0.000977778

0.000890476

4

8746840

0.000977778

0.000890476

4

8730726

0.000977778

0.000890476

4

8683184

0.000977778

0.000890476

4

8575854

0.000977778

0.000890476

4

8454745

0.000977778

0.000890476

4

8444000

0.000977778

0.000890476

4

8426886

0.000977778

0.000890476

4

8410253

0.000977778

0.000890476

4

8267094

0.000977778

0.000890476

4

8222810

0.000977778

0.000890476

4

8001585

0.000977778

0.000890476

4

8001585

0.000977778

0.000890476

4

7959624

0.000977778

0.000890476

4

7927607

0.000977778

0.000890476

4

7914928

0.000977778

0.000890476

4

7862686

0.000977778

0.000890476

4

7800214

0.000977778

0.000890476

4

7665909

0.000977778

0.000890476

4

7639578

0.000977778

0.000890476

4

7588648

0.000977778

0.000890476

4

7533377

0.000977778

0.000890476

4

7522989

0.000977778

0.000890476

3

7471992

0.000977778

0.000890476

3

7439778

0.000977778

0.000645242

3

7438336

0.000977778

0.000645242

3

7412682

0.000914921

0.000645242

3

7409598

0.000914921

0.000645242

3

7350051

0.000914921

0.000645242

3

7276219

0.000914921

0.000645242

3

7225390

0.000914921

0.000645242

3

7042008

0.000914921

0.000645242

3

6977967

0.000793863

0.000387302

3

6847966

0.000793863

0.000387302

3

6749522

0.000793863

0.000387302

3

6550343

0.000793863

0.000387302

3

6435403

0.000793863

0.000387302

3

6231898

0.000793863

0.000387302

3

6157100

0.000793863

0.000344883

3

6136089

0.000793863

0.000344883

3

6071753

0.000759711

0.000344883

3

6007747

0.000759711

0.000344883

3

5996106

0.000759711

0.000344883

3

5876771

0.000742828

0.000344883

3

5766103

0.000742828

0.000306013

3

5707678

0.000742828

0.000306013

3

5690587

0.000742828

0.000306013

3

5608159

0.000672222

0.000306013

3

5592413

0.000672222

0.000306013

3

5441102

0.000672222

0.000306013

3

5342532

0.000672222

0.000249903

3

5335115

0.000672222

0.000249903

3

5313526

0.000672222

0.000249903

3

5307542

0.000657284

0.000249903

3

5292313

0.000657284

0.000249903

3

5283851

0.000657284

0.000249903

3

5274212

0.000657284

0.00024901

3

5238753

0.000657284

0.00024901

3

5156660

0.000657284

0.00024901

3

5148764

0.00061503

0.000227778

3

5144485

0.00061503

0.000227778

3

5094350

0.00061503

0.000227778

3

5003478

0.00061503

0.000227778

3

4976641

0.00061503

0.000227778

3

4948718

0.00061503

0.000227778

3

4931541

0.00061503

0.000227778

3

4889511

0.00061503

0.000227778

3

4863914

0.000583807

0.000227778

3

4846011

0.000533652

0.000227778

3

4841630

0.000533652

0.000227778

3

4766247

0.000533652

0.000227778

3

4663130

0.000533652

0.000227778

3

4657500

0.000533652

0.000227778

3

4606460

0.000533652

0.000227778

3

4565932

0.00041941

0.000227778

3

4544448

0.00041941

0.000227778

3

4514131

0.00041941

0.000227778

3

4475663

0.000392448

0.000227778

3

4456704

0.000383333

0.000227778

3

4452932

0.000383333

0.000227778

3

4451868

0.000383333

0.000227778

3

4417851

0.000383333

0.000227778

3

4393162

0.000383333

0.000227778

3

4359263

0.000383333

0.000227778

3

4295944

0.000383333

0.000227778

3

4272705

0.000383333

0.000227778

3

4265824

0.000383333

0.000227778

3

4250778

0.000383333

0.000227778

3

4235754

0.000383333

0.000227778

3

4220060

0.000383333

0.000227778

3

4164112

0.000383333

0.000227778

3

4136003

0.000383333

0.000227778

3

4120965

0.000383333

0.000221738

3

4114924

0.000383333

0.000221738

3

4088793

0.000383333

0.000221738

3

4085493

0.000383333

0.000221738

3

4010544

0.000383333

0.000202831

3

3983546

0.000383333

0.000202831

3

3940298

0.000383333

0.000202831

2

3929902

0.000383333

0.000202831

2

3852514

0.000383333

0.000202831

2

3839068

0.000383333

0.000202831

2

3826465

0.000383333

0.000202831

2

3791514

0.000383333

0.000202831

2

3780996

0.000383333

0.000202831

2

3717929

0.000383333

0.000202831

2

3663768

0.000383333

0.000202831

2

3644268

0.000383333

0.000202831

2

3644268

0.000383333

0.000202831

Getting the fundamental statistical properties of each column I get:

             
MAL TRAIN SET          
  avg median Max min 3rd Q 4th Q
mal

1.20E-04

4.23E-05

1.00E-03

1.35189E-09

0.000219157

0.001

ben

1.99E-05

7.45E-07

1.00E-03

3.33943E-20

4.04471E-06

0.001

size

699971

174050.5

24226645

11256

6678689

24226645

count

7.094298

7

18

0

9

18

BENIGN TRAIN SET          
  avg median max min 3rd Q 4th Q
mal

4.66E-04

3.75E-04

1.00E-03

1.35E-07

7.94E-04

0.001

ben

0.000313

0.000548

0.001

2.12E-14

0.000387

0.001

size

4978015

6101652

28861244

19420

6384527

28861244

count

2.408108

2

14

0

3

14

The concept of quartiles helps us understand the data value below which a certain percentage of the sample set lies. A quartile is the median value, taken for every 25% of the data set – to illustrate this using a sample set. Taking a regular median value on any data gives us the 50% quartile value/ 2nd value or the value below which 50% of the data set lies. Now take the remaining data set below 50% and recalculate the median value of that set, this would give us the 25% quartile or the first quartile value. Repeat for the upper data set from the 50% median value of the original and complete data set and you get the 3rd quartile or 75% value. The 0 and the 4th gives us the min and max values respectively.

In our case, the values effectively give us an estimate numeric below which half the data set and 75% of the data set lie. Furthermore, the average values also give a good indication of the general value around which the range deviates. Doing a training run on a set of 370 legitimate android apps gives(excerpt of data set below)-

A few things immediately standout:

The permissions count vector summary for malware denotes that the average number is around 7. Whereas the similar count on about 400 games applications are around 2. Zero could indicate both malware and benign and thus would have to be ignored in the final decision, while taking other parameters into account.

While the malware Bayes score in the malware training set and the benign training set are comparable in terms of ranges, the benign Bayes score in the benign training set has a much larger range than that of the malware training set.

This range check could be used in the final threat score.

File sizes are not the best judge of any set affinity, though the average sizes of malwares seem to be much smaller than legitimate apps (though currently the benign set only has the most downloaded games).

Want to learn more?? The InfoSec Institute Ethical Hacking course goes in-depth into the techniques used by malicious, black hat hackers with attention getting lectures and hands-on lab exercises. While these hacking skills can be used for malicious purposes, this class teaches you how to use the same hacking techniques to perform a white-hat, ethical hack, on your organization. You leave with the ability to quantitatively assess and measure threats to information assets; and discover where your organization is most vulnerable to black hat hackers. Some features of this course include:

  • Dual Certification - CEH and CPT
  • 5 days of Intensive Hands-On Labs
  • Expert Instruction
  • CTF exercises in the evening
  • Most up-to-date proprietary courseware available

The 3rd quartile score for the malware set is significantly higher than that of the benign set mal score. The benign set 3rd quartile score is significantly higher than that of the malware set benign score.

Finally, the prior probability can be tuned to values other than benign set prior =0.8 and malware set prior =0.2. This effectively sets the probability of being a malware a chance of 20 out of 100arbitrary samples. This is, again, a statistical inference taken out of a best guess, given that the number of android malware is growing by the day, this value will have to be updated frequently.

Individually, none of these values on their own would indicate anything out of the ordinary but in tandem should give us a better view of the application profile.

An important point to note is that the probability scores by themselves are quite skewed in that the malicious Bayes scores have a tendency to come to a higher value than the benign Bayes score. Hence, a simple value comparison might not work at this stage. In fact, most apps will flag it as malware, which might be playing it too safe. Our focus is, therefore, to get a better detection rate for legitimate apps. Once our classifier can make that decision well enough we are done with the current process.

Threat Rating

Threat rating can be done using a weighted flag based score system. For every such flag being hit, a certain cumulative score is assigned. This can be rule based for now on the data gathered above. Later on, intelligent pattern matching can be used to further embellish this detection rule set.

Rules draft –

Want to learn more?? The InfoSec Institute Ethical Hacking course goes in-depth into the techniques used by malicious, black hat hackers with attention getting lectures and hands-on lab exercises. While these hacking skills can be used for malicious purposes, this class teaches you how to use the same hacking techniques to perform a white-hat, ethical hack, on your organization. You leave with the ability to quantitatively assess and measure threats to information assets; and discover where your organization is most vulnerable to black hat hackers. Some features of this course include:

  • Dual Certification - CEH and CPT
  • 5 days of Intensive Hands-On Labs
  • Expert Instruction
  • CTF exercises in the evening
  • Most up-to-date proprietary courseware available
  1. If permission count is above 4 hit flag.[MALWARE]
  2. If permission count is less than 4 hit flag.[BENIGN]
  3. If benign score >=0.000313. [BENIGN]
  4. If benign score <0.000313and benign score is not less than 2.12E-14.[BENIGN]
  5. If benign score >=3.33943E-20and malware score <=0.000219157[MALWARE]
  6. If malware score <=7.94E-04and benign score <=0.000300 [BENIGN]
  7. If benign score is <=4.04471E-06and malware score<=0.000219157 [MALWARE]
  8. If size >=4978015 [BENIGN]

Recoding the setScore() function in the bayesClassifier.cs class –

publicvoid setScore(List<string> p, int fileSize)
       {

int malIdx = 0; int goodIdx = 0;
foreach (perm20 mal inEnum.GetValues(typeof(perm20)))
           {
foreach (string l in p)
               {
string tmp = getUpper(l);
if (tmp.Contains(mal.ToString()))
                   {
//malScore++; //set existance summation
                       malScore *= (float.Parse(((int)mal).ToString()) / 1260);
//above does the probability summation
                       PermCount++;
                       mar[malIdx] = 1; //hit flagger for viz
                   }

               } malIdx++;
           }

foreach (perm20Benign good inEnum.GetValues(typeof(perm20Benign)))
           {
foreach (string l in p)
               {
if (getUpper(l).Contains(good.ToString()))
                   {
//benignScore++;
                       benignScore *= (float.Parse(((int)good).ToString()) / 1260);
                       ben[goodIdx] = 1;
                   }

               } goodIdx++;
           }

//PRIOR PROBALITIY SET HERE
//benignScore *= 0.80f;
//malScore *= 0.20f;

//THREAT MODELLED RULESET

if (p.Count > 4) { malware_flags++; } else { benign_flags++; }

if (benignScore > 0.000313f) { benign_flags++; }

if (benignScore < 0.000313f && benignScore >= 2.12E-14) { benign_flags++; }

if (benignScore >= 3.33943E-20 && malScore <= 0.000219157) { malware_flags++; }

if (malScore <= 7.94E-04 && benignScore <= 0.000300) { benign_flags++; }

if (benignScore <= 4.04471E-06 && malScore <= 0.000219157) { malware_flags++; }

if (fileSize >= 4978015)
           {
           benign_flags++;
           }

           ScoreRanker();
       }

int malware_flags = 0;
int benign_flags = 0;

privatevoid ScoreRanker()
       {
if (malware_flags > benign_flags) {
               verdict = malware_flags.ToString() + "," + "Malicious";
           }
elseif (malware_flags < benign_flags) {
               verdict = benign_flags.ToString() + "," + "OK";

           }elseif (malware_flags == benign_flags ){
               verdict = malware_flags.ToString() + "," + "Suspicious";
           }

Of course, endless retuning of the rule set can be done until we are satisfied. This is always a work in progress with more data and incorporation of more effective techniques will generally tend to alleviate problem areas.

I wrote a simple console app that counts the keywords in the final result vectors:

privatevoid occur() {

string[] lines = File.ReadAllLines(Environment.CurrentDirectory+"\stats.txt");

int total=lines.Length;
int malCount=0;
int benCount=0;
int susCount = 0;
foreach (string s in lines) {
if (s.Split(',')[5].Contains("Malicious"))
                {

                    malCount++;
                }
elseif (s.Split(',')[5].Contains("Suspicious")) {
                    susCount++;
                }

else { benCount++; }

            }

Console.WriteLine(((malCount * 100) / (total-susCount)).ToString() + " mal percent detection rate");
Console.WriteLine(((benCount * 100) / (total-susCount)).ToString() + " ben percent detection rate");

        }

Let’s see how this classifies our malware training data set.

A sample output of the result vectors:

This is a CSVdelimited file with each vector containing fields –

Permission count, file size (.apk), Bayes malware score, Bayes benign score, Threat Rating, Verdict

8,1860822,1.40374822876765E-05,4.32236291203481E-09,3,Malicious

4,44494,0.000114245114673395,1.1769811862905E-05,3,OK

10,212599,5.02565762872109E-06,1.09017683769252E-08,3,Malicious

6,118882,4.53462416771799E-05,1.13550129299256E-06,3,Malicious

9,1582310,1.99225337382813E-06,2.40371611504031E-11,3,Malicious

8,1574049,5.88169905313407E-06,1.09332454201194E-08,3,Malicious

4,100599,0.000269929820206016,4.04470529247192E-06,3,OK

5,844733,0.000318630220135674,9.13320945983287E-06,2,OK

9,158446,1.26900376926642E-05,1.09017683769252E-08,3,Malicious

7,67828,4.22751363657881E-05,7.5672455750464E-06,2,Suspicious

4,184163,9.05412889551371E-05,2.74950548373454E-06,3,Malicious

4,1753382,0.000269929820206016,4.04470529247192E-06,3,OK

3,200249,0.000155646877828985,1.71241129010014E-06,3,OK

On the malware test set.

On the benign test set.

In this case, the detection rates have to be read in context.

For the above malware training and test sets, the rate of detection we are interested in is the first line – mal percent detection rate, noting that this includes both malware confirmed and suspicious files as well. In both cases, the detection rates are above 60%, which is fair but not exactly production ready. We can surely tune the data set to increase the value. Keep in mind that suspicious files were found quite in number and therefore additional fail safes can be triggered to keep that in very good check.

However, our other high priority task is to greatly minimize the false positive rates. Looking at the benign training set, the classifier has correctly identified more than 80% of genuine android apps. This is also fair in the sense that 14 percent have been flagged as malware/suspicious.Again, taking into account the number of suspicious files for which deeper checking can be done, the percentage can be greatly improved.

The reset benign test run for separate triggering of suspicious apps checking yields:

At this stage, the Bayes classifier is working and a second phase of tuning followed by a failsafe set of checking algorithms will surely bolster the detection rates while keeping false positives to a minimum. I would encourage you to take steps in that direction and share your findings as well.

Conclusion

You have gained an understanding of the application of fundamental statistics and probability and applied the process practically to classify between benign and malware android apps. Much of this and a little more is being implemented in my custom tool SCIENT – an Android Malware Analyser, itself a work in progress.

Building an AV engine is always a challenge, and given the odds, a signature based approach is no longer as effective as it was in the old days. Heuristics and profiling based engines that aim to mimic the human cognitive processes or rely on statistical mechanics have a better chance at survival if enough effort is taken to keep the algorithms tuned and running. Mobile platforms need something resilient and compact and relying on signatures seems to be a losing battle, especially given the current crop and quantity of mobile malware. Similar techniques can be implemented for other platforms as well. Keeping in mind that larger sample sets have a better chance of getting the modelling data, its best to be patient until the data sets do the talking themselves, especially with machine learning-based approaches.

We will take a similar approach and overview towards spam mail classification in the next article.