Adversarial Attack
Adversarial Attack
Intentional manipulation of inputs to fool an AI model. A security threat.
Adversarial Attack
Intentional manipulation of inputs to fool an AI model. A security threat.
An adversarial attack is a technique that manipulates input data to intentionally deceive an AI model. It applies subtle alterations that are barely noticeable to the human eye but induce completely different results from the AI. For example, attaching a sticker with a specific pattern to an image of a stop sign can cause an autonomous vehicle to misrecognize it as a speed limit sign.
Adversarial attacks are broadly divided into white-box and black-box attacks. A white-box attack calculates the optimal perturbation using gradient information when the model structure and weights are known. Representative examples include FGSM (Fast Gradient Sign Method) and PGD (Projected Gradient Descent). A black-box attack is a method of attacking by observing only inputs and outputs without knowing the model's internals.
Adversarial attacks on LLMs are also being actively researched. Jailbreak attacks manipulate prompts to bypass safety guidelines, and Prompt Injection inserts malicious commands into external data to control the model. Such attacks can pose a serious threat to the security and reliability of AI systems.
Defense techniques include adversarial training, input sanitization, and ensemble defense. However, attack and defense show the pattern of a constant arms race, and perfect defense is still not possible. Robustness testing against adversarial attacks is essential when deploying AI systems.
This is an example demonstrating an FGSM adversarial attack using PyTorch. (For educational purposes)
import torch
import torch.nn.functional as F
from torchvision import models, transforms
from PIL import Image
# FGSM 적대적 공격 구현
def fgsm_attack(image, epsilon, gradient):
"""
image: 원본 이미지 텐서
epsilon: perturbation 크기 (작을수록 탐지 어려움)
gradient: 손실에 대한 이미지의 그래디언트
"""
# 그래디언트 부호 방향으로 perturbation 생성
perturbation = epsilon * gradient.sign()
# 적대적 이미지 생성
adversarial_image = image + perturbation
# 픽셀 값 범위 제한 [0, 1]
adversarial_image = torch.clamp(adversarial_image, 0, 1)
return adversarial_image
# 적대적 예제 생성 함수
def generate_adversarial_example(model, image, target_label, epsilon=0.03):
model.eval()
image.requires_grad = True
# 순전파
output = model(image)
loss = F.cross_entropy(output, target_label)
# 역전파로 그래디언트 계산
model.zero_grad()
loss.backward()
# FGSM 공격 적용
adversarial = fgsm_attack(image, epsilon, image.grad.data)
return adversarial
# 방어: 적대적 학습 (Adversarial Training)
def adversarial_training_step(model, images, labels, epsilon=0.03):
"""적대적 예제를 포함한 학습"""
# 원본 이미지로 손실 계산
outputs = model(images)
loss_clean = F.cross_entropy(outputs, labels)
# 적대적 예제 생성
adv_images = generate_adversarial_example(model, images.clone(), labels, epsilon)
# 적대적 이미지로 손실 계산
outputs_adv = model(adv_images)
loss_adv = F.cross_entropy(outputs_adv, labels)
# 총 손실 (원본 + 적대적)
total_loss = loss_clean + loss_adv
return total_loss
# Robustness 테스트
def test_robustness(model, test_loader, epsilons=[0, 0.01, 0.03, 0.05]):
for eps in epsilons:
correct = 0
total = 0
for images, labels in test_loader:
if eps > 0:
images = generate_adversarial_example(model, images, labels, eps)
outputs = model(images)
_, predicted = outputs.max(1)
correct += (predicted == labels).sum().item()
total += labels.size(0)
print(f"Epsilon={eps:.2f}: Accuracy={100*correct/total:.1f}%")
| Attack Type | Target | Method | Defense Difficulty |
|---|---|---|---|
| FGSM | Image Classification | 1 Gradient Step | Medium |
| PGD | Image Classification | Iterative Optimization | High |
| Patch Attack | Object Detection | Physical Patch Attachment | Very high |
| Jailbreak | LLM | Prompt Manipulation | High |
| Prompt Injection | LLM/RAG | Malicious Command Insertion | Very high |
"We need robustness testing against adversarial attacks before deployment. We should test with FGSM and PGD up to epsilon 0.03 and measure the drop in accuracy."
"There is a risk of prompt injection in the RAG system. We should wrap text retrieved from external documents in separate delimiters and strengthen the system prompt."
"Let's apply adversarial training to increase robustness. Training time will double, but resistance to adversarial attacks will improve significantly."
If you measure only accuracy and deploy, the system is vulnerable to attacks. Run adversarial tests with a variety of epsilon values.
Adversarial examples crafted on one model can also fool other models. Black-box environments are not safe either.
Passing user input directly to an LLM makes it vulnerable to injection. Add input sanitization and validation logic.
Adversarial training can lower accuracy on normal inputs. Find an appropriate balance between security and performance.