Preventing Homoglyph attacks with OCR.
Tags: python, security, machinelearning, azure
Published: 2019-11-12 20:06:40

---
title: Preventing Homoglyph attacks with OCR.
published: true
date: 2019-04-29 05:34:11 UTC
tags: python, security, machinelearning, azure
canonical_url: https://towardsdatascience.com/homoglyph-attack-prevention-with-ocr-a6741ee7c9cd
---

![](https://cdn-images-1.medium.com/max/1024/1*ESX_hzipSuoUOExCrZt7wQ.png)

TLDR; This post describes what [homoglyph attacks](https://en.wikipedia.org/wiki/Homoglyph) are and how to prevent them with Cognitive Services.

### Getting Started

Code for this story can be found on github.

[aribornstein/HomoglyphAttackPreventionService](https://github.com/aribornstein/HomoglyphAttackPreventionService)

One click deployment instructions on Azure can be found below.

### What is a Homoglyph attack?

![](https://cdn-images-1.medium.com/max/233/1*szoIMc-zPaSVMO7-Jrvh8g.png)

In [orthography](https://en.wikipedia.org/wiki/Orthography) and [typography](https://en.wikipedia.org/wiki/Typography), a **homoglyph** is one of two or more [characters](https://en.wikipedia.org/wiki/Character_(computing)) with shapes that appear identical or very similar. In layman's terms, a homo-glyph is any character that looks similar to another character such the **S** and **$** in the image above **.**

Language models are often vulnerable to obfuscation attacks using homo-glyphs, due to the way they [encode text](https://www.w3.org/International/questions/qa-what-is-encoding). In Unicode and Ascii for example, the same character codes look different in different fonts, and a model will struggle to learn their similarities.

[Character encodings for beginners](https://www.w3.org/International/questions/qa-what-is-encoding)

To drive this point home, let’s take a look at the phrase below:

> I got a $hitty result from my awesome cloud sentiment analysis model.

The phrase above clearly demonstrates **negative** sentiment. The word _$hitty_ is a homoglyphic obfuscation of the profane word _shitty_.

Let’s see how the four most popular cloud sentiment analysis services on the internet, handle this attack.

![](https://cdn-images-1.medium.com/max/1024/1*aceQ4O6h8nJyszjz6wqveg.png)<figcaption>Clockwise, Azure Text Analytics and GCP Natural Language correctly classify the original sentiment but fail on the obfuscated text, IBM Watson fails to correctly classify sentiment of either text and AWS Comprehend does not provide a demo with out an AWS account but also fails on the example sentence.</figcaption>

As we can see Azure Text Analytics and GCP Natural Language correctly classify the original sentiment but both fail on the obfuscated text. IBM Watson, fails to correctly classify sentiment of either text. AWS Comprehend, does not provide a demo without an AWS subscription and also fails on the example sentence.

While the $ and S example above may seem arbitrary, the Latin and Cyrillic letters below in wikipedia demonstrate how such an attack can be effective and hard to detect.

![](https://cdn-images-1.medium.com/max/359/1*-richb0paKHkx0y1vRZDVQ.png)

This presents all sorts of problems for use cases, where such attacks can exploit or cause harm to applications, such as bots trying to hide from fake news detectors.

### How to Prevent Homoglyph attacks?

![](https://cdn-images-1.medium.com/max/1024/1*_yJhZlgkGTf5FpZR7uAG_A.png)<figcaption>Azure Computer Vision Correctly Reveals the Homoglyph when the text is represented as an image.</figcaption>

After talking with my friend Amit Moryossef from the BIU NLP lab, we realized we might be able to prevent homoglyph attacks using OCR systems.

Using Azure Computer Vision service, I tested this theory, with the sentence above and it correctly used the image domain context to extract the word **Shitty** from the homoglyph **$hitty**.

Using this capability I’ve written the following open source container service that will:

1. Take a given text as input

2. Convert the text to an image

3. Process the image using OCR

4. Return the correct text with homoglyphs removed.

The Docker service is cloud agnostic. I provide a one click deployment option to Azure for convenience.

If you have an existing Azure subscription, you can get started by clicking the button below to auto deploy the service.

![](https://cdn-images-1.medium.com/proxy/0*4t7SGtozSL1f1SrR.png)<figcaption><a href="https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2Faribornstein%2FHomoglyphAttackPreventionService%2Fmaster%2Fazuredeploy.json">Click here to get started!</a></figcaption>

Otherwise you can get a [free Azure Account here](https://azure.microsoft.com/offers/ms-azr-0044p/?WT.mc_id=medium-blog-abornst) and then click the deploy button above.

[Create your Azure free account today | Microsoft Azure](https://azure.microsoft.com/offers/ms-azr-0044p/?WT.mc_id=medium-blog-abornst)

If you have any questions, comments, or topics you would like me to discuss, feel free to follow me on [Twitter](https://twitter.com/pythiccoder). Thanks again to Amit Moryossef and the BIU NLP lab for the amazing inspiration and Iddan Sachar with his help debugging ARM for one click deployment.

### Using the Service

To use the service just send a URL encoded query string of up to 200 characters to the service perfect for validating tweets. Below is an example call using curl be sure to use your own service endpoint.

![](https://cdn-images-1.medium.com/max/694/1*pyYdiHRBelu5YCBRGS2UrQ.png)

### Next Steps

While the service works very well at removing homoglyphs there are still a few cases it fails on.

![](https://cdn-images-1.medium.com/max/801/1*bvIl0H4yBri9ze9JlCF5rw.png)<figcaption>Example Failure Case</figcaption>

Future work will explore using a more custom approach to solving this problem but this approach works very well for very minimal effort.

### Additional Resources

- [Image Processing with the Computer Vision API | Microsoft Azure](https://azure.microsoft.com/en-us/services/cognitive-services/computer-vision/#text?WT.mc_id=blog-medium-abornst)
- [App Service - Web App for Containers | Microsoft Azure](https://azure.microsoft.com/en-us/services/app-service/containers/?WT.mc_id=blog-medium-abornst)
- [Text Analytics API | Microsoft Azure](https://azure.microsoft.com/en-us/services/cognitive-services/text-analytics/?WT.mc_id=blog-medium-abornst)

### About the Author

[**Aaron (Ari) Bornstein**](https://www.linkedin.com/in/aaron-ari-bornstein-22aa7a77/) is an avid AI enthusiast with a passion for history, engaging with new technologies and computational medicine. As an Open Source Engineer at Microsoft’s Cloud Developer Advocacy team, he collaborates with Israeli Hi-Tech Community, to solve real world problems with game changing technologies that are then documented, open sourced, and shared with the rest of the world.

* * *