### Setup ThreatExchange Access Token Source: https://github.com/facebook/threatexchange/blob/main/api-reference-examples/python-notebook/Getting Started with Sharing.ipynb This Python snippet shows how to set up an access token for ThreatExchange using the pytx library. It demonstrates calling the access_token function, which handles token retrieval. ```python from pytx.access_token import access_token # Specify the location of your token via one of several ways: # https://pytx.readthedocs.org/en/latest/pytx.access_token.html access_token() ``` -------------------------------- ### Install golang-threatexchange Library Source: https://github.com/facebook/threatexchange/blob/main/api-reference-examples/go/README.md Installs the golang-threatexchange library using the go get command. This is the standard way to fetch Go packages. ```bash go get github.com/Facebook/ThreatExchange/api-reference-examples/go/threatexchange ``` -------------------------------- ### Enable Debug Logging Source: https://github.com/facebook/threatexchange/blob/main/api-reference-examples/python-notebook/Getting Started with Sharing.ipynb This Python code snippet demonstrates how to enable debug level logging for the pytx library. It uses the setup_logger function, which can be configured to write logs to a file. ```python from pytx.logger import setup_logger # Uncomment this, if you want debug logging enabled # setup_logger(log_file="pytx.log") ``` -------------------------------- ### Install pytx using pip Source: https://github.com/facebook/threatexchange/blob/main/api-reference-examples/python/pytx/docs/installation.rst This command installs the pytx library using the pip package installer. It is the recommended method for setting up the library. ```bash $ pip install pytx ``` -------------------------------- ### Setup pytx Logger Source: https://github.com/facebook/threatexchange/blob/main/api-reference-examples/python/pytx/docs/quickstart.rst Configures the pytx logger to write debug information to a specified log file. If no file path is provided, logging will not occur. This is crucial for troubleshooting pytx operations. ```python from pytx import setup_logger setup_logger('/path/to/my/log/file.log') ``` -------------------------------- ### Direct Broker Usage for ThreatExchange API Source: https://github.com/facebook/threatexchange/blob/main/api-reference-examples/python/pytx/docs/quickstart.rst Demonstrates how to use the pytx Broker directly to make GET requests to the ThreatExchange API. This approach bypasses the convenience classes and allows for manual URL and parameter construction. ```python from pytx.request import Broker from pytx.vocabulary import ThreatExchange as te b = Broker() url = te.URL + te.THREAT_DESCRIPTORS params = {te.TEXT: "www.facebook.com"} response = b.get(url, params) ``` -------------------------------- ### Confirm Data Shared to ThreatExchange Source: https://github.com/facebook/threatexchange/blob/main/api-reference-examples/python-notebook/Getting Started with Sharing.ipynb This Python snippet demonstrates how to confirm data has been shared to ThreatExchange by performing a search. It imports necessary libraries for datetime, time, pandas, and pytx. ```python from datetime import datetime, timedelta from time import strftime import pandas as pd from pytx import ThreatDescriptor from pytx.vocabulary import ThreatExchange as te ``` -------------------------------- ### Placeholder for Sharing Malicious Data Source: https://github.com/facebook/threatexchange/blob/main/api-reference-examples/python-notebook/Getting Started with Sharing.ipynb This is a placeholder for writing Python code to share lists of malicious URLs or domain names with other members or privacy groups within the ThreatExchange platform. ```python # Put your Python code here! ``` -------------------------------- ### Configure SSL Context for ThreatExchange RTUListener Source: https://github.com/facebook/threatexchange/blob/main/api-reference-examples/python/pytx/docs/quickstart.rst This example shows how to create a custom SSLContext using Python's 'ssl' module to enable HTTPS for the RTUListener, ensuring secure communication. It requires certificate and key files. ```python import ssl ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)ssl_context.load_cert_chain(certfile=' ``` -------------------------------- ### Install vPDQ Dev Tools with Pip Source: https://github.com/facebook/threatexchange/blob/main/vpdq/CONTRIBUTING.md Installs clang-format and other necessary development tools for the vPDQ Python binding using pip with the 'dev' optional dependency. ```sh python -m pip install -e .[dev] ``` -------------------------------- ### Start ThreatExchange Wiki Local Editor Source: https://github.com/facebook/threatexchange/wiki/README Runs the Docker container to start the local wiki editor. It maps the host's current directory to the container's wiki volume and exposes the editor on port 9999. ```shell ThreatExchange.wiki$ docker run -p 9999:80 -v`pwd`:/var/wiki threatexchange.wiki:latest ``` -------------------------------- ### Install libav* Dependencies (Ubuntu) Source: https://github.com/facebook/threatexchange/blob/main/vpdq/README.md Installs essential development libraries for libav* on Ubuntu systems using apt-get. These are required for building the vpdq C++ library. ```sh sudo apt-get install -y libavdevice-dev libavfilter-dev libavformat-dev libavcodec-dev libswresample-dev libswscale-dev libavutil-dev ``` -------------------------------- ### Submit Example (Ruby) Source: https://github.com/facebook/threatexchange/blob/main/api-reference-examples/ruby/te-tag-query/README.md Demonstrates submitting data to the ThreatExchange API using a Ruby script. This script likely handles authentication and data formatting for submission. ```ruby ruby api-example-submit.rb ``` -------------------------------- ### Install vPDQ Python Bindings (Editable Mode) Source: https://github.com/facebook/threatexchange/blob/main/vpdq/CONTRIBUTING.md Installs the vPDQ Python bindings in editable mode from the vpdq/ directory. This allows for development and testing of the Python components. ```sh # vpdq/ python -m pip install --editable . ``` -------------------------------- ### CLI Command Example: Fetching Datasets Source: https://github.com/facebook/threatexchange/blob/main/python-threatexchange/README.md Demonstrates how to use the `threatexchange` CLI to fetch datasets, specifically mentioning the `-P` flag which is part of the public interface. ```shell threatexchange dataset -P ``` -------------------------------- ### vstr File Format Example Source: https://github.com/facebook/threatexchange/blob/main/tmk/README-more.md Example of the binary structure of a .vstr file, which stores decoded video stream data. The output shows the initial header containing magic numbers and dimensions, followed by the RGB frame-raster data. ```bash # The header includes the dimensions of the data $ hex ~/wideband/tmk/vstr/misc-shelf-sd.vstr | head 00000000: 54 4d 4b 31 56 53 54 52 90 01 00 00 e0 00 00 00 |TMK1VSTR........| 00000010: 0f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000020: 3d 47 61 3d 47 61 3d 47 61 3d 47 61 3d 47 61 3d |=Ga=Ga=Ga=Ga=Ga=| 00000030: 47 61 3d 47 61 3d 47 61 3d 47 61 3d 47 61 3d 47 |Ga=Ga=Ga=Ga=Ga=G| 00000040: 61 3d 47 61 3d 47 61 3d 47 61 3d 47 61 3d 47 61 |a=Ga=Ga=Ga=Ga=Ga| 00000050: 3e 48 62 3e 48 62 3d 47 61 3d 47 61 3d 47 61 3b |>Hb>Hb=Ga=Ga=Ga;| 00000060: 45 5f 3a 44 5e 3a 44 5e 38 42 5c 38 42 5c 37 41 |E_:D^:D^8B\8B\7A| 00000070: 5b 36 40 5a 36 40 5a 34 3e 58 34 3e 58 34 3e 58 |[6@Z6@Z4>X4>X4>X| 00000080: 33 3d 57 33 3d 57 33 3d 57 33 3d 57 33 3d 57 33 |3=W3=W3=W3=W3=W3| 00000090: 3d 57 32 3c 56 32 3c 56 30 3a 54 30 3a 54 2f 39 |=W2, proxies=, verify=) ``` -------------------------------- ### Get Tagged Object IDs by Tag ID Source: https://github.com/facebook/threatexchange/blob/main/api-reference-examples/curl/te-tag-query/README.md Fetches a list of object IDs associated with a specific tag ID. This query can be paginated if more results are available. ```shell curl -s 'https://graph.facebook.com/v4.0/1283621968426798/tagged_objects/'\ "?access_token=$TX_ACCESS_TOKEN"\ '&limit=100' ``` -------------------------------- ### Build and Run PDQ Benchmark (CPP) Source: https://github.com/facebook/threatexchange/blob/main/pdq/cpp/index/README.md Instructions to build the benchmark executable using make and then run it. This is the primary command for executing the performance tests. ```bash cd pdq/cpp make bin/benchmark-query ./benchmark-query ``` -------------------------------- ### Get Tag ID by Text Source: https://github.com/facebook/threatexchange/blob/main/api-reference-examples/curl/te-tag-query/README.md Retrieves the ThreatExchange tag ID for a given text string using a prefix match. This is the first step in querying for tagged objects. ```shell curl -s 'https://graph.facebook.com/v4.0/threat_tags/'\ "?access_token=$TX_ACCESS_TOKEN"\ '&text=pwny&limit=10' ``` -------------------------------- ### Initialize ThreatExchange Client (Go) Source: https://github.com/facebook/threatexchange/blob/main/api-reference-examples/go/README.md Demonstrates how to create a new ThreatExchange client instance. Requires an appID, appSecret, and a logger. ```go client, err = threatexchange.New( appID, appSecert, log.New(logrus.StandardLogger().Writer(), "ThreatExchange: ", log.Lshortfile), ) ``` -------------------------------- ### Build and run PDQ hashing benchmark Source: https://github.com/facebook/threatexchange/blob/main/pdq/cpp/hashing/README.md This snippet shows how to navigate to the C++ benchmark directory, compile the photo hasher benchmark, and then execute it. It's the primary way to initiate the performance testing. ```bash cd pdq/cpp make bin/benchmark-photo-hasher ./benchmark-photo-hasher ``` -------------------------------- ### Retrieve Full ThreatDescriptor Details Source: https://github.com/facebook/threatexchange/blob/main/api-reference-examples/python/pytx/docs/quickstart.rst Explains how to fetch the complete details for a ThreatDescriptor object after obtaining a summary. This requires an additional API call using the `.details()` method, followed by converting the object to a dictionary. ```python from pytx import ThreatDescriptor results = ThreatDescriptor.objects(text='www.facebook.com') for result in results: # Make another API call to get all of the fields result.details() print result.to_dict() ``` -------------------------------- ### Python Logs: Sample API Fetching Simulation Source: https://github.com/facebook/threatexchange/blob/main/hasher-matcher-actioner/docs/user-interface.md Demonstrates the logging output when the 'Setup Sample API' is used, showing the process of fetching simulated signals for the SEED_SAMPLE exchange. This helps in understanding the data fetching mechanism in a development environment. ```python INFO in fetcher: SEED_SAMPLE[sample] Fetching signals for SEED_SAMPLE from sample INFO in fetcher: SEED_SAMPLE[sample] No checkpoint, should be the first fetch. DEBUG in fetcher: SEED_SAMPLE[sample] fetch_iter() with 18 new records INFO in fetcher: SEED_SAMPLE[sample] Fetched all data! Up to date! ``` -------------------------------- ### PDQ hashing benchmark help command Source: https://github.com/facebook/threatexchange/blob/main/pdq/cpp/hashing/README.md Displays the usage and available options for the `benchmark-photo-hasher` executable. This includes flags for verbose output, specifying the number of hashes, and enabling dihedral hash computation. ```bash $ ./benchmark-query -h Usage: ./benchmark-photo-hasher [options] folder_path Options: -v Verbose output -n N Total number of hashes to generate, can be more or less than the number of images in the folder (default: 0, meaning generate one hash for each image in the folder) --dihedral Compute dihedral versions of the hashes (default: false) ``` -------------------------------- ### Build .NET Test Project Source: https://github.com/facebook/threatexchange/blob/main/pdq/wasm/README.md Compiles the .NET project that contains the Selenium WebDriver tests. This command creates executables in the bin/Debug/net6.0 directory. ```bash dotnet build ``` -------------------------------- ### FFmpeg Installation Check Source: https://github.com/facebook/threatexchange/blob/main/vpdq/README.md This command verifies if FFmpeg is installed on the system by displaying its version information. If FFmpeg is not installed or not in the system's PATH, it will output a 'command not found' error. ```sh $ ffmpeg ffmpeg version 4.4.2 Copyright (c) 2000-2023 the FFmpeg developers ... ``` -------------------------------- ### Manually Run Flask Startup Script (Bash) Source: https://github.com/facebook/threatexchange/blob/main/hasher-matcher-actioner/CONTRIBUTING.md Manually executes the startup script for the Flask server, which is useful if automatic startup fails due to syntax errors. This script is also run automatically when the Dev Container starts. ```bash cd /workspace .devcontainer/startup.sh ``` -------------------------------- ### Install vpdq Locally Source: https://github.com/facebook/threatexchange/blob/main/vpdq/python/README.md Installs the vpdq Python package locally from the root vpdq folder. This is useful for development or testing modifications. ```sh python -m pip install -e . ``` -------------------------------- ### Get Threat Exchange Members Source: https://github.com/facebook/threatexchange/blob/main/api-reference-examples/js/node/README.md Retrieves a list of threat exchange members. This function makes a GET request to the /threat_exchange_members endpoint. ```javascript api.getThreatExchangeMembers(function(err,data) { // process data from response here }); ``` -------------------------------- ### Building PHP 7 with GD Support Source: https://github.com/facebook/threatexchange/blob/main/pdq/php/README.md Commands and configuration steps to compile PHP 7 from source, ensuring the GD library is enabled and correctly linked with JPEG and PNG support. ```bash $ tar zvxf php-7.1.11.tar.gz $ cd php-7.1.11 $ ./configure \ --prefix=/usr/local/php7 \ --with-gd \ --with-jpeg-dir=/usr/local/opt/jpeg \ --with-png-dir=/usr/local/opt/libpng \ --with-zlib-dir=/usr $ make $ make install ``` -------------------------------- ### Install vpdq Python Binding Source: https://github.com/facebook/threatexchange/blob/main/vpdq/README.md Installs the vpdq Python binding using pip. Requires all C++ implementation dependencies to be met. ```sh pip install vpdq ``` -------------------------------- ### Generate documentation locally Source: https://github.com/facebook/threatexchange/blob/main/api-reference-examples/python/pytx/CONTRIBUTING.md Builds the project documentation locally using Sphinx and reStructuredText (rst) format. The generated documentation will be in the 'build/docs' directory. ```bash make docs ``` -------------------------------- ### PHP PDQ Hashing Usage Example Source: https://github.com/facebook/threatexchange/blob/main/pdq/php/README.md Demonstrates how to execute the PDQ hashing tool from the command line using a PHP script, showing the expected output for both pure-PHP and Zend extension hashing. ```bash $ cat php.ini memory_limit = -1; include_path = "."; extension=ext/pdq/modules/pdq.so; $ php -c . pdqhashertest.php b.jpg d8f8f0cec0f4a84f0637022a278f67f0b36e2ed596621e1d33e6339c4e9c9b22,100,purephp,b.jpg d8f8f0cec0f4a84f0637022a278f67f0b36e2ed596621e1d33e6339c4e9c9b22,100,extnphp,b.jpg ``` -------------------------------- ### Get Threat Indicator Object by ID Source: https://github.com/facebook/threatexchange/blob/main/api-reference-examples/js/node/README.md Fetches details for a specific threat indicator using its ID. Allows for optional fields to customize the GET request. ```javascript var fields = {}; // optional fields here as shown in Facebook documentation var id = 12345; // unique ID to threat indicator object api.getThreatIndicatorObject(id,fields,function(err,data) { // process data here }); ``` -------------------------------- ### Get Malware Object by ID Source: https://github.com/facebook/threatexchange/blob/main/api-reference-examples/js/node/README.md Retrieves detailed information about a specific malware object using its unique ID. Supports optional fields for the GET request. ```javascript var fields = {}; // optional fields here as shown in Facebook documentation var id = 12345; // unique ID to malware object api.getMalwareObject(id,fields,function(err,data) { // process data here }); ``` -------------------------------- ### Get Threat Indicators Source: https://github.com/facebook/threatexchange/blob/main/api-reference-examples/js/node/README.md Retrieves threat indicators. This function supports optional parameters for filtering or specifying data, making a GET request to the /threat_indicators endpoint. ```javascript var options = {}; // optional values here as shown in Facebook documentation api.getThreatIndicators(options,function(err,data) { // process data from response here }); ``` -------------------------------- ### Get Malware Analyses Source: https://github.com/facebook/threatexchange/blob/main/api-reference-examples/js/node/README.md Fetches malware analyses from the Threat Exchange. Allows for optional parameters to filter or specify the data to retrieve via a GET request. ```javascript var options = {}; // optional values as shown in Facebook documentation api.getMalwareAnalyses(options,function(err,data) { // process data from response here }); ``` -------------------------------- ### Build and Verify with Maven Source: https://github.com/facebook/threatexchange/blob/main/pdq/java/README.md Compiles, tests, and installs the PDQ hashing library using Maven. This process creates a JAR file in the target directory. It's an alternative to using the Makefile for building. ```shell mvn clean test install cp target/pdq-0.0.1-SNAPSHOT.jar pdq.jar ``` -------------------------------- ### Upgrade Database Schema using Flask CLI Source: https://github.com/facebook/threatexchange/blob/main/hasher-matcher-actioner/CONTRIBUTING.md Applies generated migration files to the local database. Run this command from within the 'src/OpenMediaMatch' folder. ```bash flask db upgrade ``` -------------------------------- ### Install PDQ Hashing Dependencies Source: https://github.com/facebook/threatexchange/blob/main/pdq/python/README.md Installs the necessary Python libraries, PIL (Python Image Library) and Pillow, required for the PDQ hashing implementation. ```shell sudo pip3 install Image sudo pip3 install pillow ``` -------------------------------- ### Threatexchange CLI for Extension Management Source: https://github.com/facebook/threatexchange/blob/main/python-threatexchange/threatexchange/extensions/README.md Shows how to use the `threatexchange` CLI to manage project extensions. This includes adding new extensions, listing installed extensions, removing extensions, and performing a factory reset of the configuration. ```bash # Provide the path to the module with TX_MANIFEST $ threatexchange config extensions add threatexchangecontributions.myproject Added my.module.name: Signals: foo - FooSignal bar - BarSignal Contents: baz - BazContent APIs: zed - ZedAPI # What do I have? $ threatexchange config extensions list threatexchangecontributions.myproject # I don't want this anymore! $ threatexchange config extensions remove threatexchangecontributions.myproject # Oh no I broke everything and am filled with sadness and regret! $ rm ~/.threatexchange # factory reset ``` -------------------------------- ### Compile Parallel TMK Code (C++) Source: https://github.com/facebook/threatexchange/blob/main/tmk/README.md Compiles parallelized variants of TMK scoring binaries. Requires OpenMP runtime library and FAISS. On macOS, install with 'brew install libomp faiss'. ```bash make parallel ``` -------------------------------- ### Run PDQ hashing benchmark with different hash counts Source: https://github.com/facebook/threatexchange/blob/main/pdq/cpp/hashing/README.md These examples demonstrate running the `benchmark-photo-hasher` with varying numbers of hashes to generate, showcasing the impact on performance metrics like hashing speed and read speed. ```bash $ ./benchmark-photo-hasher ../data/reg-test-input/dih/ PHOTO COUNT: 8 ERROR COUNT: 0 TIME SPENT HASHING PHOTOS (SECONDS): 0.015171 PHOTOS HASHED PER SECOND: 527.324158 TIME SPENT READING PHOTOS (SECONDS): 0.299410 PHOTOS READ PER SECOND: 26.719229 ``` ```bash $ ./benchmark-photo-hasher -n 10 ../data/reg-test-input/dih/ PHOTO COUNT: 10 ERROR COUNT: 0 TIME SPENT HASHING PHOTOS (SECONDS): 0.018181 PHOTOS HASHED PER SECOND: 550.028442 TIME SPENT READING PHOTOS (SECONDS): 0.334852 PHOTOS READ PER SECOND: 29.863914 ``` ```bash $ ./benchmark-photo-hasher -n 100 ../data/reg-test-input/dih/ PHOTO COUNT: 100 ERROR COUNT: 0 TIME SPENT HASHING PHOTOS (SECONDS): 0.182545 PHOTOS HASHED PER SECOND: 547.810364 TIME SPENT READING PHOTOS (SECONDS): 2.841218 PHOTOS READ PER SECOND: 35.196167 ``` ```bash $ ./benchmark-photo-hasher -n 1000 ../data/reg-test-input/dih/ PHOTO COUNT: 1000 ERROR COUNT: 0 TIME SPENT HASHING PHOTOS (SECONDS): 1.767847 PHOTOS HASHED PER SECOND: 565.659729 TIME SPENT READING PHOTOS (SECONDS): 27.135609 PHOTOS READ PER SECOND: 36.851948 ```