

module DetectionByPermissions;
create OUT : ANO from IN : ASEC;


--Check list for Malicious UsesPermissions:
helper def:all_uses_permissions:Sequence(OclAny)=
	Sequence{'android.permission.INTERNET',
	         'android.permission.WRITE_SMS',
			 'android.permission.RECEIVE_BOOT_COMPLETED',
			 'android.permission.READ_SMS',
	         'android.permission.RECEIVE_SMS',
			 'android.permission.PROCESS_OUTGOING_CALLS',
			 'android.permission.MOUNT_UNMOUNT_FILESYSTEMS',
			 'android.permission.READ_HISTORY_BOOKMARKS',
			 'android.permission.WRITE_HISTORY_BOOKMARKS',
			 'android.permission.READ_LOGS',
			 'android.permission.INSTALL_PACKAGES',
			 'android.permission.MODIFY_PHONE_STATE',
			 'android.permission.WRITE_EXTERNAL_STORAGE',
			 'android.permission.SEND_SMS',
			 'android.permission.READ_PHONE_STATE'
			 
	};


	
--get all instance of Uses permissions
helper context ASEC!UsesPermission def:USES_PERMISSIONS: Sequence(ASEC!UsesPermission)=
	ASEC!UsesPermission.allInstances();


helper context ASEC!UsesPermission def:isMalicious: Boolean=
	--let UsesPermissionTOcheck : Sequence(String) =
		
		if thisModule.all_uses_permissions.includes(self.name)  then
			true
		   else
		    false
	   endif;
	
	
--for detection MaliciousPermissions:
helper context ASEC!UsesPermission def:getMaliciousPermissions :Sequence(String) =
	
	let UsesPermissions : Sequence(ASEC!UsesPermission) = self.refImmediateComposite().applicationlevelPermission in
	UsesPermissions->iterate(n ; output : Sequence(String) = Sequence{} | 
	

		if thisModule.all_uses_permissions.includes(self.name) then
		--self.name='android.permission.INTERNET' then
		    output->including(': '+ self.name)
			 else
			output->reject(c | c.name=self.name)
		endif
													
		)->asSet()->asSequence();	
			
			

		
	
	
--for transfer UsesPermission to MaliciousPermissions
rule UsesPermission2MaliciousPermissions{
	
	from
	    s:ASEC!UsesPermission (s.USES_PERMISSIONS.size()>10 and s.isMalicious)
	to
	   t:ANO!MaliciousPermission (
	   	
	   description <- s.getMaliciousPermissions->iterate(up; output: String = '' | output->concat(up)+'\n')
	       
	   
	   )
	   do {
	t.trace<-Sequence{s};
}
}

