{'fraction_non_alphanumeric': 0.025151012045639198, 'fraction_numerical': 0.007806704581581829, 'mean_word_length': 4.816724881857407, 'pattern_counts': {'":': 0, '<': 1, '<?xml version=': 0, '>': 0, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Adversarial collision attacks on image hashing functions

Hashing images with a perceptual algorithm is a common approach to solving duplicate image detection problems. However, perceptual image hashing algorithms are differentiable, and are thus vulnerable to gradient-based adversarial attacks. We demonstrate that not only is it possible to modify an image to produce an unrelated hash, but an exact image hash collision between a source and target image can be produced via minuscule adversarial perturbations. In a white box setting, these collisions can be replicated across nearly every image pair and hash type (including both deep and non-learned hashes). Furthermore, by attacking points other than the output of a hashing function, an attacker can avoid having to know the details of a particular algorithm, resulting in collisions that transfer across different hash sizes or model architectures. Using these techniques, an adversary can poison the image lookup table of a duplicate image detection service, resulting in undefined or unwanted behavior. Finally, we offer several potential mitigations to gradient-based image hash attacks.


Introduction
Adversarial attacks on machine learning algorithms have received widespread recent attention, as some of the studied attacks could potentially have dire real-world consequences. In particular, computer vision models have been shown to be susceptible to imperceptible changes to an image. These changes are generally known as adversarial perturbations, and depending on whether or not an attacker has full or limited access to a model, they seem to "fool" nearly all models that make predictions over raw pixels [28,37].
Image hashing algorithms are used widely in critical production systems, and are used to detect anything from copyright violations to misinformation [10]. These algorithms allow fast lookup and retrieval of duplicate or near-duplicate images, and thus provide a cheap way to find similar images among a haystack of billions [2]. While many hashes are computed with deep networks (and thus are potentially susceptible to adversarial perturbations), for better scaling, other systems utilize cheaper non-learned algorithms [1]. The choice of which algorithm to use depends on the application, but in general, once an algorithm is chosen it re-mains fixed to avoid unnecessary re-computation of the image hash database, or because access to the original images is no longer possible.
However, image hashes, like other hashes, are vulnerable to collisions, where two different images map to the same hash. In fact, because of some inherent properties of image hashing functions, these algorithms are particularly susceptible to a collision attack (formally known in cryptography as a preimage attack), where an adversary deliberately modifies an input image so that it maps to the same hash as some other unrelated image. The applications of this type of attack can include evasion, where an adversary changes the hash of an undesirable image to exactly match that of a benign image, or a poisoning attack via collision, where the hash is changed but with the intention of polluting some hash lookup table, thus causing a duplicate image detection system to behave in an unknown or even harmful manner.
While previous research has examined the efficacy of targeted attacks designed to collide classes in a single dataset [19], we instead focus on the much stronger criteria of creating exact image hash collisions which are datasetagnostic. Furthermore, we examine various attack vectors from a holistic standpoint. An end-to-end image hashing algorithm contains multiple steps, each of which are specific to the the task of generating an image hash, but each of which provide an exploitable weak point. In addition, creating a collision at an early stage in the hashing pipeline will cause that collision to cascade to the final hash, potentially increasing the effectiveness of gray-or black-box transfer attacks. As many algorithms consist of similar early steps (such as resizing the image to a fixed size, or utilizing similar low-level learned filters in deep networks), attacking an earlier step allows an adversary to avoid having to know any details about the end stages of the hashing pipeline. For example, if a collision is created within a deep network that produces embeddings used to create a hash, the specific hashing algorithm parameters become irrelevant.
We examine the susceptibility of image hashing algorithms to gradient-based adversarial attacks, but with several important novel contributions. (1) We consider the entire hashing system, including the image resizing step, which allows perturbations of full-resolution and noncropped images; (2) we examine interior surjective attacks in the regime of deep networks, where we seek to create collisions not only at the output of the network, but at the intermediate output level; (3) we demonstrate that gradientbased adversarial attacks are not only effective against deep hashes, but also effective against the type of "shallow", nonlearned hashes that are often used in large-scale detection systems; (4) we show that all of these attacks are effective not only in a white-box setting, but can transfer between a variety of models and methods; (5) we use these results to determine the effectiveness of several mitigation strategies.

Background
Duplicate image detection is a well-studied task [5,6,15], with many varying solutions and applications. One may wish to find image duplicates to protect copyright [40], to remove unwanted or illegal images from a platform [1,10], or for forensic analysis [24,35]. One practical solution to this problem is to check a probe image against a bank of known images and subsequently performing an action such as removal or flagging if a match is found. There are a variety of methods that can be used to perform duplicate detection, but for many large-scale systems, binary perceptual hashes are usually employed [20]. Depending on the hashing function used, binary hashes are cheap to compute and store and robust to small perturbations such as color or resolution changes [39]. Furthermore, performing distance calculations for a nearest-neighbors search is more computationally efficient when using small hashes [22].
An adversarial image attack is an attack that "perturbs" an image in such a way that some target classifier or detector fails when given the image as input [8]. These attacks occur in either a white box setting, where an attacker has full access to a model [29], or a gray-or black-box setting, where the adversary has varying levels of access to the target model, either in terms of query access or some general knowledge of the target model's architecture [13]. The perturbations themselves are computed via stochastic gradient descent that treats the input image as a variable to optimize.
The desired failure mode can either be evasion [4], where an attacker seeks to perturb an image by as little as possible so that a classifier classifies the image as anything except its true class. A more difficult form of attack is a targeted attack, where the image is perturbed so that it is classified as the desired target class. A still stronger outcome is a targeted collision attack [33], where an image is perturbed so that from the point of view of a model, the adversarial image is identical to another target image.
In this work, we target image hashing methods in general. Image hashes are typically computed in one of two ways -either via a deep hashing algorithm [23,44], where the embeddings from the penultimate layer of a deep network are encoded and binarized, or with what we denote as a "shallow" hashing function, which uses deterministic image operations to produce a fixed-length hash [16,26,27]. Previous work has focused on targeted or evasion attacks for deep hashes [19,43]. The targeted attacks in [3,19] explore poisoning attacks for deep hashing systems, but optimize for a general target class and not the stronger criterion of an exact collision. Furthermore, for large scale production systems shallow hashes are often used as they contain far fewer parameters than a deep network and can be quickly computed, even on a CPU. In addition, they are extremely precise and are particularly well-suited for duplicate image detection. For greater applicability, we experiment with both shallow and deep hashes, and show that shallow hashes are just as vulnerable as deep hashes to gradient-based adversarial attacks.
Collision attacks have been well studied in the field of cryptography [21,36,41]. The specific attack family we explore are preimage attacks [32], where an adversary seeks to create a message that has a specific hash value. Ideally, a hash function with k bits requires an O(2 k ) amount of work to craft a collision, which is infeasible for typical hash lengths on the order of hundreds of bits. Technically, in this work we focus on second preimage attacks, where we seek to perturb an image x with noise r such that it has the same hash as a target image y, i.e. h(x + r) = h(y). By design, image hashes are robust under small perturbations, and do not exhibit the avalanche effect [7] of cryptographicallysecure hashes, and thus they are particularly vulnerable to second preimage attacks. In addition, an attacker can exploit the fact that image hashing algorithms are (mostly) differentiable, so a gradient-based method such as [37] gives an efficient way to find a collision.
Finally, the specific attack scenario we simulate is a poisoning attack [11] where a bank of image hashes is unknowingly polluted with an image designed to disrupt the duplicate image detection service. For instance, a bank may contain images that are not allowed to be posted by a third party, and any uploaded image is checked against this bank and flagged if there is a match. An adversary can imperceptibly perturb an image that would normally be placed in this bank (either automatically or via human intervention), but has a hash that matches a benign target image. After poisoning, all live instances of the benign image will then be either incorrectly removed or flagged. We explore the poisoning susceptibility of shallow and deep hashing systems in under various levels of model access.

Attack methods
To perform a hash collision, one can modify a source image x i so that it has exactly the same hash as a target image y i , according to some hash function where each individual f i is a distinct step in the hashing process. Given the shorthand defi- , the set of image pairs that will have identical hashes is defined as: Thus given a target image y, our task is to find an attack image x such that (x, y) ∈ C. As it is intractable to search this set in a brute-force manner, we instead can instead approximate x via gradient descent. We adopt a modified form of the box-constrained optimization method of [37], and for a given source and target image, we seek to derive a minimally perturbed image x + r that minimizes a hash distance by optimizing Eq. 2.
Although Eq. 2 admits optimizing for a general distance threshold d, for most experiments we set d = 0 to encourage exact collisions. The hyperparameter c controls the rate of change between the magnitude of the noise and the hash difference. The L 1 norm is used as a hash distance metric as we experiment exclusively with binary hashes.

Interior surjective attacks
A key observation regarding image hashing systems is that, if at any point in the hashing pipeline there exists a collision between two intermediate outputs produced by two different images, then all successive steps will also produce collisions. Eq. 1 does not restrict attacks to only colliding the final output, but also permits optimizing earlier steps in the hashing pipeline. For instance, in a deep hash, colliding one set of intermediate feature maps will result in a collision at the output of the deep network (as all operations after that point are identical). Attacking earlier stages in a hashing function may lead to better transfer attacks, a hypothesis we examine in the Experiments section. A variant of this method was used in [25] (although not within the hashing domain), where the interior feature maps of a deep network were used as attack objectives.
Secondly, hashing functions by design are surjective (also known as onto). An image of arbitrary size consisting of m pixel values is mapped to a binary hash of fixed length k by the hashing function h : R m → {0, 1} k . By the pigeonhole principle, many inputs are mapped to the same hash, and it may be the case that semantically dissimilar images have identical hashes. Additionally, because a hash is small relative to the size of the input space, the magnitude of the perturbation r required for a hash collision may be smaller than that needed for a different adversarial task. Because the overall hashing function is surjective, then there must exist an interior function f i that is also surjective, and sweeping over the interior functions of a hashing algorithm may uncover a particularly susceptible weak point. Finally, there are typically more informative gradients before the final output of the hashing algorithm, so optimization is easier if an interior function is targeted. Thus, a generalization of the optimization method of [37] is given in Eq. 3, where an additional minimization term is added to minimize the distance of one or a set of interior functions I. minimize r i∈I Since binarization is common to most image hashing techniques, we can design a special-case surjective attack that enables small perturbations to have a large effect on the output hash. The last function f n in a hashing algorithm is usually implemented as a step function over each bit j: The gradient of the step function f n is zero everywhere except zero, therefore optimization is difficult. However, h j (x) must only be perturbed by the minimum amount needed to push its output from either than zero to greater than or equal to zero (or vice-versa), so we can utilize a hinge loss to reflect this fact. As shown in Eq. 5, we can also replace the step function itself with a sigmoid that acts like a soft step (similar to [43], which uses a tanh function) and contains useful gradients, especially around zero.
In Eq 5, δ < 0.5 is a hyperparameter that controls the loss margin -smaller values of δ will push bits farther from the binarization boundary (potentially increasing transferability of the attack), while larger values will minimize the required perturbation of the original image. In our experiments, δ was fixed to 0.45 (w.l.o.g.).

Hashing algorithms analyzed
We examine not only the typical deep hashing methods already covered in some prior work, but a set of "shallow" hashes. We define shallow hashes as those that do not use deep networks and do not contain learned components. Rather, they are entirely deterministic algorithms that typically consist of resampling an image to a fixed size and then running some other form of decimation to reduce the image further into a binary hash.
Furthermore, we include image pre-processing steps as part of the overall hashing function and include them in the gradient-based adversarial attack optimization. Most adversarial attacks in the literature operate on fixed-size images, so the resizing algorithm is ignored in the optimization. However, image hashes are sensitive to the resizing algorithm used, and some previous work has examined the behavior of general adversarial attacks under different forms of resizing or compression [9,30,31,34]. Accordingly, we directly target the resize step as a potential attack point. Furthermore, this allows for general attacks using any image as an adversarial or target image.
The specific shallow hashes we examined were variants of the methods described in [17], specifically aHash, dHash, and pHash. The methods are available as an open source Python package ImageHash 1 . All three methods first converts the image to a single luma channel and then uses Lanczos resampling to resize an image to a fixed size. aHash computes the average over the resized image (either 8x8, 12x12, or 16x16, depending on the hash size) and uses this as a threshold to binarize each pixel into 64, 144, or 256 bits. dHash is similar, but resizes to a fixed size with an extra column of pixels, computes the horizontal image gradient of the resized image, and sets bits based on whether each gradient value is positive or negative. Finally, pHash performs a DCT on the resized image (which is set to twice the size of an aHash image of the same size), discards the high frequency components, and produces a bit based on whether or not the low-frequency DCT component exceeds or is below the median component value. For each hash, the resize function is similar to the other hashes, and provides a potential weak point to attack.
For deep hashes, we examined a variety of architectures developed over the past decade. These architectures were AlexNet [18], ResNet [12], and EfficientNet [38]. AlexNet uses standard convolutional operations in a feedforward fashion; ResNet adds skip connections that propagate information forward to avoid vanishing gradients; EfficientNet currently provides the best performance on the ImageNet benchmark 2 and uses fewer parameters than an equivalent ResNet, which makes it well suited for mobile or production environments. Each was trained on the ImageNet dataset. For each deep network, the hash computation step was performed with PCA followed by binarization. PCA was performed on all embeddings in the ImageNet training set, and a set of components designed to project embeddings to 64, 128, and 256 dimensions were computed. Then, the median of each vector element of the projected embeddings was computed and saved. Finally when computing the binary hash at inference time, the inferred embedding is projected and binarized according to whether or not a value at a given element was greater than or equal to the stored median value.

Experiments
We performed a variety of white box, gray box, and transfer attacks attacks against the hashes listed in Sec. 3.2. In the white box setting, we consider only an exact hash collision as an attack "success." Although duplicate image detection systems usually use a small distance threshold for finding "exact" matches, exact collisions allow an attacker to avoid having to determine the match threshold through repeated attacks. The white box setting is designed to simulate how successful an attacker might be in poisoning a detection system if they knew the algorithm used to produce the hash bank used in that system (for instance, if the system used an open source hash). For gray and black box settings, because these hash systems often use a detection threshold, we provide additional analysis on the attack success rates when using a precision-tuned distance threshold greater than zero. Only 256 bit hash results are shown here, but results for 64 and 128 bit hashes are given in the supplementary material.
For each hash type and architecture, we pre-defined a set of "split points" where a particular interior function was used as an optimization objective as shown in Eq. 3. For the shallow hashes, these split points were after every step in the hashing process, and so the total number varied depending on the algorithm used. For the deep hashes, 9 split points were chosen at roughly equal depths across each architecture, and each step in the PCA hashing function was also chosen as a split point, giving an overall total of 12 split points per deep network. For AlexNet, the split points were placed after ReLU layers; for the ResNet, after residual blocks; for EfficientNet, after equally-spaced mobile invertable convolutional blocks.
A final consideration is the choice of source images when attacking a given target image. It may be the case that it is easier to perturb semantically-related images, especially in the case where deep hases are used, as deep networks are usually trained for a particular classification task and then co-opted for use in image hashing. To this end, for each model, split point, and hash length combination, we tested 1,000 random image pairs from different ImageNet classes and 1,000 image pairs taken from the same Ima-geNet class in order to determine if there is an advantage in using similar source and target images. Also note that all visualized images in this paper were not sampled from Im-ageNet, which provides qualitative evidence that this attack framework is agnostic to specific training distributions.

Baselines
Prior to measuring our adversarial hash attack success rates, we computed a set of baselines over each hash to give a rough idea of the performance of each under light noise and perturbations. To compute this, we used a perturbed version of the ImageNet validation set as probes, and performed a nearest-neighbors lookup into the unperturbed validation set using FAISS [14]. Furthermore, we used the original training images as a set of distractors. The top-k accuracy of each hash is shown in Table 1. Additionally, to verify that our attacks are not measuring spurious successes, we computed a baseline collision rate which measures how likely it is for a random image to share the exact hash as another image. For brevity, only 256-bit hashes are included in Table 1, but full results are available in the supplemental material. The maximum incidental collision rate was 0.039 for the 64-bit version of aHash; most hashes besides the simplistic aHash had a baseline collision rate less than 0.001.

White box attacks
In the white box setting, an attacker has full access to a model or algorithm and any parameters to that algorithm. To perform this set of white box attacks, we divided the ImageNet validation set into two partitions of 500 classes each, the first of which was used for hyperparameter tuning, and the second for measuring attack success. The validation set was used as it contains class labels that can be used to measure if semantically-similar images are easier to collide. For each of the 1,000 random pairs and 1,000 class pairs, we saved the output white box adversarial images for testing our approach's gray and black box performance. For each model and hash type, we optimized Eq. 3 using the Adam optimization method (with betas 0.1, 0.1). The learning rates were determined via hyper-parameter tuning (5.0 for shallow hashes, and 0.005 for deep hashes), and the content loss weight c was fixed at 0.001 for all experiments in order to maximize the chance of achieving a hash collision. Fig. 2 demonstrates the white box capabilities of optimizing with the hinge loss in Eq. 5. Each row shows a set of adversarial images, each of which has the exact same hash as the target image, generated by optimizing over a particular deep or shallow hash. Clearly if the parameters  and algorithm of a hashing system are known, then white box attacks are extremely effective. Fig. 3 demonstrates that when using a source image that is semantically related to the target image, the attacks are slightly more successful and with less perturbation required.
In addition, the figure shows that for deep networks, there is a clear inflection point as we attack later points in the network. In fact as shown in Fig. 7, images produced by attacking earlier layers in deep networks tend to be mostly semantically transformed into the target image itself (thus defeating the purpose of the attack). However, especially around model split points 7 and beyond, the attack is much more effective at preserving the original source image while still producing an exact hash collision. The most likely explanation is that it is at this point in the network where lower-level features are combined into hierarchical semantic concepts. Specifically for the ResNet architecture, the skip connections propagate the original image deep into the network, providing a good defense against this attack. A corollary of this result showing inflection points is that this may be the optimal point to fine tune a vision model for transfer learning.

Gray box attacks
In the gray box setting, an attacker knows some information about the hashing algorithm used. For instance, the adversary may know that a pre-trained ResNet is used to extract primary feature embeddings, but they do not know the algorithm used to produce the final binary hash. To measure the efficacy of our approach, we grouped the shallow hashes together, as well as the deep models from the same architectural family, and measured every gray box pair. As seen in Fig. 3, for some early split points it is difficult to achieve exact hash collisions in a white box setting, so instead we measure the success rate at a pre-computed "exact match" distance threshold. These thresholds were derived for each hash by using the augmented images described in Sec. 4.1, and finding the distance such that the nearest neighbor in the given hash space was an exact match with precision 0.99 (these threshold values are given in the supplementary material). Then for any adversarial image/target image pair, if their distance is less than this threshold, the attack is marked as a success. Within each cell in Figs. 4 and 5, the success rate is plotted, with higher values in each cell corresponding to an earlier attack point for the adversarial hash. As is evident in Fig. 4, our attack transfers well between different hash functions that use similar architectures, and transfers especially well if the same base architecture is used, regardless of the hashing function appended to that architecture. However, transferability decreases as deeper split points are used.
For shallow hashes, all three share similar resizing functions, so it is a particularly suitable interior surjective attack point for gray box attacks. Fig. 5 demonstrates that if an adversary were to only know the algorithm used to perform a resize, and were able to produce a collided resized image, the downstream hashing method does not matter.

Transfer attacks
Finally, we measured the ability of an adversary to use an unrelated hash to create collisions -specifically if a different deep architecture was used, or a collision was optimized for a shallow hash and then applied to a deep hash (and viceversa). In the supplementary material, a combined version of Figs. 4, 5 are provided. The transfer attack success rate was very low, indicating that shallow and deep hashes differ significantly and that a shallow/deep hash is "safe" from an attack targeting a deep/shallow hash. Note that while Ima-geNet was solely used as it is fairly representative of the dis- tribution of "all images," different classification tasks may produce different low-level convolutional filters. Additionally we observed diminishing returns across different deep architecture types that were trained on the same dataset, so we surmise that using networks trained on a secret dataset or keeping the architecture secret provides an appropriate defense to hash collision attacks.

Mitigations and conclusions
Based on our comprehensive analysis, several conclusions can be drawn. First, all attacks that have white box access to a model are able to produce exact hash collisions with minimal perturbations of the source image. Consequently, it is necessary that the algorithm used in a produc-tion system is not made public if one wishes to guarantee the security of said system -otherwise the system is completely vulnerable to poisoning attacks. Second, we demonstrate that our attack method is successful even in a gray box setting, so one must withhold even general details about the algorithm such as the training set or base model. A third approach is more practical than the above methods which rely on "security by obscurity." Transfer attacks between shallow and deep hashes were much less successful, indicating that the image operations in both hashing algorithms are fundamentally different. Thus using a combination shallow/deep hash will provide more security as an adversarial attack must perturb an image to fool both models that operate in an essentially orthogonal manner.