Process Injection - CreatRemoteThread

Creating SQL view from Mordor Process Injection dataset

Create Spark session

from pyspark.sql import SparkSession

spark = SparkSession \
    .builder \
    .appName("Spark_Data_Analysis") \
    .config("spark.sql.caseSensitive","True") \
    .getOrCreate()

Unzip Mordor Dataset

! unzip -o ../datasets/empire_psinject.zip -d ../datasets/
Archive:  ../datasets/empire_psinject.zip
  inflating: ../datasets/empire_psinject_2020-08-07143205.json  

Expose the dataframe as a SQL view

processInjectionJson = '../datasets/empire_psinject_2020-08-07143205.json'

processInjectionDf = spark.read.json(processInjectionJson)

processInjectionDf.createOrReplaceTempView('processInjection')

Filtering & Summarizing data

Get most frecuent Access Flags (Bitmask) of Processes accessing other Processes

  • Create dataframe

processAccess = spark.sql(
'''
SELECT GrantedAccess, count(*) as Count
FROM processInjection
WHERE lower(Channel) LIKE '%sysmon%'
    AND EventID = 10
GROUP BY GrantedAccess
ORDER BY Count DESC
''')

print('This dataframe has {} records!!'.format(processAccess.count()))
processAccess.show()
This dataframe has 10 records!!
+-------------+-----+
|GrantedAccess|Count|
+-------------+-----+
|       0x1000|  463|
|       0x3000|   83|
|         0x40|    4|
|     0x1fffff|    2|
|       0x1400|    2|
|       0x1410|    2|
|       0x1478|    2|
|     0x1f3fff|    1|
|     0x100000|    1|
|     0x101541|    1|
+-------------+-----+

Transforming data

Filter events that requested “Creation of Thread” rights

  • Filter PROCESS_CREATE_THREAD (0x0002): Required to create a thread.

createThread = spark.sql(
'''
SELECT GrantedAccess, SourceImage, TargetImage
FROM processInjection
WHERE lower(Channel) LIKE '%sysmon%'
    AND EventID = 10
    AND array_contains(getAccessRights(GrantedAccess),'PROCESS_CREATE_THREAD')
''')

print('This dataframe has {} records!!'.format(createThread.count()))
createThread.show(truncate = 80)
This dataframe has 3 records!!
+-------------+---------------------------------------------------------+-------------------------------------+
|GrantedAccess|                                              SourceImage|                          TargetImage|
+-------------+---------------------------------------------------------+-------------------------------------+
|     0x1f3fff|C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe|      C:\windows\system32\notepad.exe|
|     0x1fffff|                          C:\windows\system32\svchost.exe|C:\windows\system32\wbem\wmiprvse.exe|
|     0x1fffff|                            C:\windows\system32\csrss.exe|C:\windows\system32\wbem\wmiprvse.exe|
+-------------+---------------------------------------------------------+-------------------------------------+

Correlating data

Find Source Processes that used CreateRemoteThread APIs

networkConnection = spark.sql(
'''
SELECT b. SourceImage, b.TargetImage, a.NewThreadId
FROM processInjection b
INNER JOIN(
    SELECT SourceProcessGuid, NewThreadId
    FROM processInjection
    WHERE lower(Channel) LIKE '%sysmon%'
        AND EventID = 8
)a
ON b.SourceProcessGUID = a.SourceProcessGuid
WHERE lower(Channel) LIKE '%sysmon%'
    AND b.EventID = 10
    AND array_contains(getAccessRights(GrantedAccess),'PROCESS_CREATE_THREAD')
''')

print('This dataframe has {} records!!'.format(networkConnection.count()))
networkConnection.show(truncate = 40)
This dataframe has 88 records!!
+----------------------------------------+-------------------------------+-----------+
|                             SourceImage|                    TargetImage|NewThreadId|
+----------------------------------------+-------------------------------+-----------+
|C:\windows\System32\WindowsPowerShell...|C:\windows\system32\notepad.exe|       3004|
|C:\windows\System32\WindowsPowerShell...|C:\windows\system32\notepad.exe|       3756|
|C:\windows\System32\WindowsPowerShell...|C:\windows\system32\notepad.exe|       2836|
|C:\windows\System32\WindowsPowerShell...|C:\windows\system32\notepad.exe|       5764|
|C:\windows\System32\WindowsPowerShell...|C:\windows\system32\notepad.exe|       8044|
|C:\windows\System32\WindowsPowerShell...|C:\windows\system32\notepad.exe|       6168|
|C:\windows\System32\WindowsPowerShell...|C:\windows\system32\notepad.exe|       8292|
|C:\windows\System32\WindowsPowerShell...|C:\windows\system32\notepad.exe|       2976|
|C:\windows\System32\WindowsPowerShell...|C:\windows\system32\notepad.exe|       1820|
|C:\windows\System32\WindowsPowerShell...|C:\windows\system32\notepad.exe|       8252|
|C:\windows\System32\WindowsPowerShell...|C:\windows\system32\notepad.exe|       4952|
|C:\windows\System32\WindowsPowerShell...|C:\windows\system32\notepad.exe|       5436|
|C:\windows\System32\WindowsPowerShell...|C:\windows\system32\notepad.exe|       9036|
|C:\windows\System32\WindowsPowerShell...|C:\windows\system32\notepad.exe|       6556|
|C:\windows\System32\WindowsPowerShell...|C:\windows\system32\notepad.exe|       8468|
|C:\windows\System32\WindowsPowerShell...|C:\windows\system32\notepad.exe|       8592|
|C:\windows\System32\WindowsPowerShell...|C:\windows\system32\notepad.exe|       6628|
|C:\windows\System32\WindowsPowerShell...|C:\windows\system32\notepad.exe|       2272|
|C:\windows\System32\WindowsPowerShell...|C:\windows\system32\notepad.exe|        904|
|C:\windows\System32\WindowsPowerShell...|C:\windows\system32\notepad.exe|       8816|
+----------------------------------------+-------------------------------+-----------+
only showing top 20 rows

Find Target Processes that made Network Connections

networkConnection = spark.sql(
'''
SELECT b.TargetImage, a.SourceIp, a.DestinationIp
FROM processInjection b
INNER JOIN(
    SELECT ProcessGuid, SourceIp, DestinationIp
    FROM processInjection
    WHERE lower(Channel) LIKE '%sysmon%'
        AND EventID = 3
)a
ON b.TargetProcessGUID = a.ProcessGuid
WHERE lower(Channel) LIKE '%sysmon%'
    AND b.EventID = 10
    AND array_contains(getAccessRights(GrantedAccess),'PROCESS_CREATE_THREAD')
''')

print('This dataframe has {} records!!'.format(networkConnection.count()))
networkConnection.show(truncate = 40)
This dataframe has 16 records!!
+-------------------------------+-----------+-------------+
|                    TargetImage|   SourceIp|DestinationIp|
+-------------------------------+-----------+-------------+
|C:\windows\system32\notepad.exe|172.18.39.5|   10.10.10.5|
|C:\windows\system32\notepad.exe|172.18.39.5|   10.10.10.5|
|C:\windows\system32\notepad.exe|172.18.39.5|   10.10.10.5|
|C:\windows\system32\notepad.exe|172.18.39.5|   10.10.10.5|
|C:\windows\system32\notepad.exe|172.18.39.5|   10.10.10.5|
|C:\windows\system32\notepad.exe|172.18.39.5|   10.10.10.5|
|C:\windows\system32\notepad.exe|172.18.39.5|   10.10.10.5|
|C:\windows\system32\notepad.exe|172.18.39.5|   10.10.10.5|
|C:\windows\system32\notepad.exe|172.18.39.5|   10.10.10.5|
|C:\windows\system32\notepad.exe|172.18.39.5|   10.10.10.5|
|C:\windows\system32\notepad.exe|172.18.39.5|   10.10.10.5|
|C:\windows\system32\notepad.exe|172.18.39.5|   10.10.10.5|
|C:\windows\system32\notepad.exe|172.18.39.5|   10.10.10.5|
|C:\windows\system32\notepad.exe|172.18.39.5|   10.10.10.5|
|C:\windows\system32\notepad.exe|172.18.39.5|   10.10.10.5|
|C:\windows\system32\notepad.exe|172.18.39.5|   10.10.10.5|
+-------------------------------+-----------+-------------+

Thank you! I hope you enjoyed it!