TL;DR

  • Extended Attributes (xattrs) are stored within the file system as key-value pairs (on supported file systems).
  • There is no predefined size limit for xattrs.
  • Xattrs are often overlooked, particularly when files are transferred between different file systems.
  • The ._ files you see when transferring files between macOS and Windows are due to differences in the file systems. Modifying these ._ files on Windows can have an impact when the files are moved back to macOS (Try it out!).

What is xattr

Overview

Technical details

  • Each xattr has a key-value pair: a unique key and a value of any type and length.
  • For more information, search “forked file systems”, “resource forks”, and “data forks”.

CLI Examples

You will utilize /usr/bin/xattr to interact with extended attributes.

Basic Examples

  • Print: -p

    KEY='com.apple.metadata:_kMDItemUserTags'; xattr -p $KEY $FILE
    
  • Write: -w

    xattr -w $KEY $VALUE $FILE
    
  • Delete: -d

    xattr -d $KEY $VALUE $FILE
    
  • -x: attr_value is represented as a hex string for input and output

Advanced Examples

Storing a binary data in xattr

TARGET='/bin/ls'
shasum $TARGET
value="$(xxd -c 0 -p $TARGET)"
FILE=benign.txt; touch $FILE
KEY="rand0m.key.$RANDOM"; echo $KEY
xattr -x -w "$KEY" "$value" $FILE
/bin/ls -alt@ $FILE
xattr -x -p $KEY $FILE|xxd -r -p|file -
xattr -x -p $KEY $FILE|xxd -r -p|shasum -
file $FILE; xxd $FILE

result image
No one suspects that this 0 byte file 'contains' an executable file 😁

Code reading