Blog Post

Microsoft Security Community Blog
5 MIN READ

Creating Endpoint DLP Rules using PowerShell - Part 1

pri2agarwalz's avatar
pri2agarwalz
Icon for Microsoft rankMicrosoft
Nov 07, 2024

This blog is Part 1 of our multi-part series on managing Endpoint DLP Rules using PowerShell.

This blog is Part 1 of our multi-part series on managing Endpoint DLP Rules using PowerShell.

In Part 1, we will demonstrate how we can use PowerShell to create Endpoint DLP Rules with AdvancedRule, AlertProperties and EndpointDLPRestrctions Parameter. In Part 2, we will cover the same for EndpointDLPBrowserRestrictions.

Step 1:

Create the text file with complex condition as per the requirements and save it.

Here is a sample for reference: 

{

                                                 "Version": "1.0",

                                                 "Condition": {

                                                   "Operator": "And",

                                                   "SubConditions": [

                                                     {

                                                       "ConditionName":

                                                        "ContentContainsSensitiveInformation",

                                                       "Value": [

                                                         {

                                                           "Groups": [

                                                             {

                                                               "Name": "Default",

                                                               "Operator": "Or",

                                                               "Sensitivetypes": [

                                                                 {

                                                                   "Name": "Credit Card Number",

                                                                   "Mincount": 1,

                                                                   "Maxcount": 5,

                                                                   "Confidencelevel": "Low",

                                                                 },

                                                                 {

                                                                   "Name": "U.S. Bank Account Number",

                                                                   "Mincount": 5,

                                                                   "Confidencelevel": "Medium",

                                                                 }

                                                               ]

                                                             }

                                                           ],

                                                           "Operator": "And"

                                                         }

                                                       ]

                                                     }

                                                   ]

                                                 }

}

In the above example, we are using the condition Content Contains Sensitive Information with SIT’s Credit Card or Bank Account Number. You can choose to add/remove additional SIT’s/conditions as needed along with the desired operator. You can also change the Confidence level to Low/Medium/High as per the requirements and update the Min/Max count.

We have saved it as advancedrule.txt in our example. 

Note: If you do not specify the Min/Max attribute, the value is taken as any by default. In our example we have not specified the Max attribute for the Bank Account Number, hence it would take the default value i.e. Any.

Here is another example:

{

  "Version": "1.0",

  "Condition": {

    "Operator": "And",

    "SubConditions": [

      {

        "ConditionName": "ContentContainsSensitiveInformation",

        "Value": [

          {

            "Groups": [

              {

                "Name": "Default",

                "Operator": "Or",

                "Labels": [

                  {

                    "Name": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",

                    "Id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",

                    "Type": "Sensitivity"

                  }

                ]

              }

            ],

            "Operator": "And"

          }

        ]

      },

      {

        "ConditionName": "ContentFileTypeMatches",

        "Value": [

          "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

        ]

      }

    ]

  }

}

In this example we are using the condition Content Contains Sensitive Level with a specific label and Content matches a specific file type. Please ensure to replace the ID’s with the appropriate values before saving the file.

Step 2:

Define the parameters for endpointDlpRestrictions or create a text file for complex restrictions.

Here is an example for a simple restriction:

$endpointDlpRestrictions =  @(@{"Setting"="Print"; "Value"="Block"},@{"Setting"="RemovableMedia"; "Value"="Warn"})

In this case we are setting the Print action to Block and Copy to removable USB Device to Warn.  We can configure the value to Block/Warn/Audit as per our requirements.

Here is an example to create a text file with complex condition:

[

                                    {

                                        "defaultmessage":  "none",

                                        "setting":  "Print",

                                        "value":  "Block",

                                        "appgroup":  "none",

                                        "networkLocation":  [

                                                                {

                                                                    "priority":  "1",

                                                                    "type":  "vpn",

                                                                    "action":  "Audit"

                                                                }

                                                            ],

                                        "printerGroup":  [

                                                             {

                                                                 "priority":  "1",

                                                                 "id":  "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",

                                                                 "action":  "Audit"

                                                             }

                                                         ]

                                    },

                                    {

                                        "setting":  "RequireBusinessJustification",

                                        "value":  "Required"

                                    },

                                    {

                                        "setting":  "RemovableMedia",

                                        "defaultmessage":  "none",

                                        "value":  "Warn",

                                        "appgroup":  "none"

                                    },

                                    {

                                        "setting":  "CloudEgress",

                                        "defaultmessage":  "none",

                                        "cloudEgressGroup":  [

                                                                 {

                                                                     "priority":  "1",

                                                                     "id":  "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",

                                                                     "action":  "Audit"

                                                                 }

                                                             ],

                                        "value":  "Warn",

                                        "appgroup":  "none"

                                    },

                                    {

                                        "setting":  "PasteToBrowser",

                                        "defaultmessage":  "none",

                                        "pasteSensitiveDomainsGroup":  [

                                                                           {

                                                                               "priority":  "1",

                                                                               "id":  "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",

                                                                               "action":  "Audit"

                                                                           }

                                                                       ],

                                        "value":  "Block",

                                        "appgroup":  "none"

                                    },

                                    {

                                        "setting":  "CopyPaste",

                                        "defaultmessage":  "none",

                                        "value":  "Warn",

                                        "appgroup":  "none",

                                        "networkLocation":  [

                                                                {

                                                                    "priority":  "1",

                                                                    "type":  "corporateNetwork",

                                                                    "action":  "Audit"

                                                                }

                                                            ]

                                    },

]

We are setting the below restrictions in the above example. The Action and restrictions can be modified as per the requirements. We have saved it as endpointdlprestrictions.txt in our example.

Activity

Action

Network Restrictions

Group Restrictions

Print

Block

VPN is set to Audit

A custom Printer Group with Action as Audit

The group ID can be retrieved from the Endpoint DLP Settings using PowerShell. Make sure to update the ID before saving the file.

Copy to Removable USB Device

Warn

 

 

Upload to restricted cloud service domain

Warn

 

A custom Sensitive service domain Group with Action as Audit

The group ID can be retrieved from the Endpoint DLP Settings using PowerShell.

Paste to browser

Block

 

A custom Sensitive service domain Group with Action as Audit

The group ID can be retrieved from the Endpoint DLP Settings using PowerShell.

Copy to clipboard

Warn

CorporateNetwork is set to Audit

 

 

Step 3:

Define the Parameters:

# Define the parameters to read complex condition from the file we created in Step 1

$data = Get-Content -Path "C:\temp\advancedrule.txt" -ReadCount 0

$AdvancedRuleString = $data | Out-string

 

# Define the parameters for the DLP rule with Simple restriction

$ruleName = "Endpoint Rule - Restrict Financial Information Sharing Rule"

$PolicyName = "Endpoint Policy - Restrict Financial Information Sharing"

$endpointDlpRestrictions =  @(@{"Setting"="Print"; "Value"="Block"},@{"Setting"="RemovableMedia"; "Value"="Block"})

$Notifyendpointuser = @{NotificationContent = "default:The sharing is blocked, please contact the helpdesk for more details" ; NotificationTitle = "default:Restricted"}

$alertProperties = @{AggregationType = "SimpleAggregation" ; VolumeThreshold = "5" ; AlertBy = "Tenant"; Threshold = "15"; TimeWindow = "60"}

 

Note: The values in bold for notification content can be changed as per the notification you would like to configure. Similarly, the values in Alert properties can also be changed to meet different requirements.

Step 4

Create the DLP rule:

#Create the DLP rule

New-DlpComplianceRule -Name $ruleName -Policy $PolicyName  -GenerateAlert admin@xxxx.onmicrosoft.com -ReportSeverityLevel "Medium"  -Notifyendpointuser $Notifyendpointuser -EndpointDlpRestrictions $endpointDlpRestrictions -AlertProperties $alertProperties -AdvancedRule $AdvancedRuleString

You can use the below if you want to create a DLP rule with complex EDLP Restriction:

# Define the parameters to read complex condition from a file we created in Step 1

$data = Get-Content -Path "C:\temp\advancedrule.txt" -ReadCount 0

$AdvancedRuleString = $data | Out-string

 

# Define the parameters for the DLP rule with Simple restriction

$ruleName = "Endpoint Rule - Restrict Financial Information Sharing Rule"

$PolicyName = "Endpoint Policy - Restrict Financial Information Sharing"

$Notifyendpointuser = @{NotificationContent = "default:The sharing is blocked, please contact the helpdesk for more details" ; NotificationTitle = "default:Restricted"}

$alertProperties = @{AggregationType = "SimpleAggregation" ; VolumeThreshold = "5" ; AlertBy = "Tenant"; Threshold = "15"; TimeWindow = "60"}

 

# Create the DLP rule using the EndpointDlpRestrictions file we created in Step 2.

New-DlpComplianceRule -Name $ruleName -Policy $PolicyName -GenerateAlert admin@xxxx.onmicrosoft.com -ReportSeverityLevel "Medium" -AlertProperties $alertProperties -Notifyendpointuser $Notifyendpointuser -AdvancedRule $AdvancedRuleString -EndpointDlpRestrictions  (Get-Content -Raw ("C:\temp\endpointdlprestrictions.txt") | ConvertFrom-Json -AsHashtable)

 

Note:  PowerShell 7 is a must for this to work.

Updated Nov 06, 2024
Version 1.0

2 Comments

  • Fiani The policy sync status update depends on factors like device connectivity and configuration. The status won't update if the device is offline, such as when a user has signed out or shut down the machine. Incorrect configurations or missing updates can also cause issues. Ensure the device is properly configured and online for a while before checking the status again. Contact support if the issue persists. 

  • Fiani's avatar
    Fiani
    Copper Contributor

    Hi pri2agarwalz your post are amazing! I just know that we can create Endpoint DLP Policy with PowerShell, but is there any differences in time taken to policy sync / applied to the devices if using the powershell than on portal? Because lately when i created an E-DLP Policy, it takes up 2 days to change the status of Policy Sync from not available to updated.