lupin 1.0.0

A blazing-fast, lightweight steganography tool for concealing secret data within normal files.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// Copyright 2025 Niclas Hedam
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::{
    error::{LupinError, Result},
    SteganographyEngine,
};
use base64::{engine::general_purpose, Engine as _};
use log::debug;

/// PDF steganography engine
///
/// PDFs end with %%EOF, but viewers ignore anything after that.
/// We append a base64-encoded payload after the EOF marker.
pub struct PdfEngine;

impl PdfEngine {
    pub fn new() -> Self {
        Self
    }

    /// Finds where the PDF actually ends (after the last %%EOF marker)
    fn find_eof_end(&self, pdf: &[u8]) -> Option<usize> {
        let eof_marker = b"%%EOF";
        pdf.windows(eof_marker.len())
            .rposition(|window| window == eof_marker)
            .map(|pos| pos + eof_marker.len())
    }
}

impl Default for PdfEngine {
    fn default() -> Self {
        Self::new()
    }
}

impl SteganographyEngine for PdfEngine {
    fn magic_bytes(&self) -> &[u8] {
        b"%PDF"
    }

    fn format_name(&self) -> &str {
        "PDF"
    }

    fn format_ext(&self) -> &str {
        ".pdf"
    }

    fn embed(&self, source_data: &[u8], payload: &[u8]) -> Result<Vec<u8>> {
        let eof_end = self
            .find_eof_end(source_data)
            .ok_or(LupinError::PdfNoEofMarker)?;

        debug!("PDF: Found %%EOF at position {}", eof_end - 5);

        let encoded_payload = general_purpose::STANDARD.encode(payload);

        // Check if there's non-whitespace content after %%EOF (indicating existing hidden data)
        let content_after_eof = &source_data[eof_end..];
        let has_non_whitespace = content_after_eof.iter().any(|&b| !b.is_ascii_whitespace());

        if has_non_whitespace {
            // There's already non-whitespace data after %%EOF - likely hidden data
            return Err(LupinError::EmbedCollision {
                source: std::io::Error::new(
                    std::io::ErrorKind::AlreadyExists,
                    "PDF: Already contains some data after %%EOF",
                ),
            });
        }

        let mut result = Vec::with_capacity(eof_end + encoded_payload.len());
        result.extend_from_slice(&source_data[..eof_end]);
        result.extend_from_slice(encoded_payload.as_bytes());
        Ok(result)
    }

    fn extract(&self, source_data: &[u8]) -> Result<Vec<u8>> {
        let eof_marker = b"%%EOF";
        let eof_pos = source_data
            .windows(eof_marker.len())
            .rposition(|w| w == eof_marker)
            .ok_or(LupinError::PdfNoEofMarker)?;

        debug!("PDF: Found %%EOF at position {}", eof_pos);

        let payload_start = eof_pos + eof_marker.len();
        let payload = &source_data[payload_start..];

        // Skip whitespace after %%EOF
        let payload: Vec<u8> = payload
            .iter()
            .skip_while(|&&b| b.is_ascii_whitespace())
            .copied()
            .collect();

        if payload.is_empty() {
            return Err(LupinError::PdfNoHiddenData);
        }

        general_purpose::STANDARD
            .decode(&payload)
            .map_err(|_| LupinError::PdfCorruptedData)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn create_minimal_pdf() -> Vec<u8> {
        b"%PDF-1.4\n1 0 obj\n<<\n/Type /Catalog\n>>\nendobj\nxref\n0 1\n0000000000 65535 f\ntrailer\n<<\n/Size 1\n/Root 1 0 R\n>>\nstartxref\n73\n%%EOF".to_vec()
    }

    fn create_invalid_pdf_no_eof() -> Vec<u8> {
        b"%PDF-1.4\n1 0 obj\n<<\n/Type /Catalog\n>>\nendobj".to_vec()
    }

    #[test]
    fn test_magic_bytes() {
        // Arrange
        let engine = PdfEngine::new();

        // Act & Assert
        assert_eq!(engine.magic_bytes(), b"%PDF"); // Magic bytes should match PDF file format signature
    }

    #[test]
    fn test_format_name() {
        // Arrange
        let engine = PdfEngine::new();

        // Act & Assert
        assert_eq!(engine.format_name(), "PDF"); // Format name should be human-readable identifier
    }

    #[test]
    fn test_format_ext() {
        // Arrange
        let engine = PdfEngine::new();

        // Act & Assert
        assert_eq!(engine.format_ext(), ".pdf"); // File extension should include the dot
    }

    #[test]
    fn test_find_eof_end() {
        // Arrange
        let engine = PdfEngine::new();
        let pdf = create_minimal_pdf();

        // Act
        let eof_end = engine.find_eof_end(&pdf);

        // Assert
        assert!(eof_end.is_some()); // Should find %%EOF marker in valid PDF
        let pos = eof_end.unwrap();
        assert_eq!(pos, 125); // Position should be immediately after %%EOF in minimal PDF (120 + 5 = 125)
        assert_eq!(&pdf[120..125], b"%%EOF"); // Should find the actual %%EOF marker at position 120
    }

    #[test]
    fn test_find_eof_end_multiple_eof() {
        // Arrange
        let engine = PdfEngine::new();
        let mut pdf = create_minimal_pdf();
        pdf.extend_from_slice(b"\n%%EOF\nfake_data");

        // Act
        let eof_end = engine.find_eof_end(&pdf);

        // Assert
        assert!(eof_end.is_some()); // Should find the last %%EOF marker when multiple exist
        let pos = eof_end.unwrap();
        assert_eq!(pos, 131); // Should point to end of second %%EOF (125 + 6 = 131)
        assert_eq!(&pdf[126..131], b"%%EOF"); // Should find the second %%EOF marker
        assert_eq!(&pdf[131..], b"\nfake_data"); // Content after second %%EOF should remain
    }

    #[test]
    fn test_find_eof_end_no_eof() {
        // Arrange
        let engine = PdfEngine::new();
        let pdf = create_invalid_pdf_no_eof();

        // Act
        let eof_end = engine.find_eof_end(&pdf);

        // Assert
        assert!(eof_end.is_none()); // Should return None when no %%EOF marker is found
    }

    #[test]
    fn test_embed_success() {
        // Arrange
        let engine = PdfEngine::new();
        let pdf = create_minimal_pdf();
        let payload = b"secret message";

        // Act
        let result = engine.embed(&pdf, payload);

        // Assert
        assert!(result.is_ok()); // Embed operation should succeed with valid PDF

        let embedded = result.unwrap();
        assert_eq!(embedded.len(), 145); // Original PDF (125 bytes) + base64 encoded payload (20 bytes) = 145
        assert!(embedded.starts_with(b"%PDF")); // Should preserve PDF magic bytes at start
        assert!(embedded.ends_with(b"c2VjcmV0IG1lc3NhZ2U=")); // Should end with base64 of "secret message"
    }

    #[test]
    fn test_embed_no_eof_marker() {
        // Arrange
        let engine = PdfEngine::new();
        let invalid_pdf = create_invalid_pdf_no_eof();
        let payload = b"secret message";

        // Act
        let result = engine.embed(&invalid_pdf, payload);

        // Assert
        assert!(result.is_err()); // Should fail when PDF has no %%EOF marker

        match result.unwrap_err() {
            LupinError::PdfNoEofMarker => (), // Should return specific error for missing %%EOF
            other => panic!("Expected PdfNoEofMarker, got {:?}", other),
        }
    }

    #[test]
    fn test_embed_empty_payload() {
        // Arrange
        let engine = PdfEngine::new();
        let pdf = create_minimal_pdf();
        let payload = b"";

        // Act
        let result = engine.embed(&pdf, payload);

        // Assert
        assert!(result.is_ok()); // Empty payload should still embed successfully

        let embedded = result.unwrap();
        assert_eq!(embedded.len(), 125); // Original PDF (125 bytes) + base64 of empty string (0 bytes) = 125
    }

    #[test]
    fn test_embed_into_already_embedded_pdf() {
        // Arrange
        let engine = PdfEngine::new();
        let mut pdf = create_minimal_pdf();
        pdf.extend_from_slice(b"c2VjcmV0IG1lc3NhZ2U="); // base64 of "secret message"

        // Act
        let result = engine.embed(&pdf, "more secret".as_bytes());

        // Assert
        assert!(result.is_err()); // Should fail due to embed collision (already embedded PDF)

        match result.unwrap_err() {
            LupinError::EmbedCollision { .. } => (), // Should return specific error for embed collision
            other => panic!("Expected EmbedCollision, got {:?}", other),
        }
    }

    #[test]
    fn test_extract_success() {
        // Arrange
        let engine = PdfEngine::new();
        let mut pdf = create_minimal_pdf();
        pdf.extend_from_slice(b"c2VjcmV0IG1lc3NhZ2U="); // base64 of "secret message"

        // Act
        let result = engine.extract(&pdf);

        // Assert
        assert!(result.is_ok()); // Extract should succeed with valid embedded data

        let extracted_payload = result.unwrap();
        assert_eq!(extracted_payload, b"secret message"); // Should extract exact original payload
    }

    #[test]
    fn test_extract_no_eof_marker() {
        // Arrange
        let engine = PdfEngine::new();
        let invalid_pdf = create_invalid_pdf_no_eof();

        // Act
        let result = engine.extract(&invalid_pdf);

        // Assert
        assert!(result.is_err()); // Should fail when PDF has no %%EOF marker

        match result.unwrap_err() {
            LupinError::PdfNoEofMarker => (), // Should return specific error for missing %%EOF
            other => panic!("Expected PdfNoEofMarker, got {:?}", other),
        }
    }

    #[test]
    fn test_extract_no_hidden_data() {
        // Arrange
        let engine = PdfEngine::new();
        let clean_pdf = create_minimal_pdf();

        // Act
        let result = engine.extract(&clean_pdf);

        // Assert
        assert!(result.is_err()); // Should fail when no data exists after %%EOF

        match result.unwrap_err() {
            LupinError::PdfNoHiddenData => (), // Should return specific error for no hidden data
            other => panic!("Expected PdfNoHiddenData, got {:?}", other),
        }
    }

    #[test]
    fn test_extract_corrupted_data() {
        // Arrange
        let engine = PdfEngine::new();
        let mut pdf = create_minimal_pdf();

        // Add invalid base64 data after %%EOF
        pdf.extend_from_slice(b"invalid@base64!");

        // Act
        let result = engine.extract(&pdf);

        // Assert
        assert!(result.is_err()); // Should fail when base64 data is corrupted

        match result.unwrap_err() {
            LupinError::PdfCorruptedData => (), // Should return specific error for corrupted base64
            other => panic!("Expected PdfCorruptedData, got {:?}", other),
        }
    }

    #[test]
    fn test_extract_with_whitespace() {
        // Arrange
        let engine = PdfEngine::new();
        let pdf = create_minimal_pdf();

        // Create embedded data with whitespace manually
        let mut embedded = Vec::new();
        embedded.extend_from_slice(&pdf);
        embedded.extend_from_slice(b"  \n\t"); // Add whitespace after %%EOF
        embedded.extend_from_slice(b"dGVzdCB3aXRoIHNwYWNlcw=="); // base64 of "test with spaces"

        // Act
        let result = engine.extract(&embedded);

        // Assert
        assert!(result.is_ok()); // Should succeed despite whitespace after %%EOF

        let extracted_payload = result.unwrap();
        assert_eq!(extracted_payload, b"test with spaces"); // Should extract correct payload ignoring whitespace
    }

    #[test]
    fn test_round_trip_with_binary_data() {
        // Arrange
        let engine = PdfEngine::new();
        let pdf = create_minimal_pdf();

        // Act
        let binary_payload = b"\x00\x01\x02\xff";
        let embedded2 = engine.embed(&pdf, binary_payload).unwrap();
        let extracted2 = engine.extract(&embedded2).unwrap();

        // Assert
        assert_eq!(extracted2, b"\x00\x01\x02\xff"); // Binary data should round-trip correctly
    }

    #[test]
    fn test_round_trip_with_unicode_data() {
        // Arrange
        let engine = PdfEngine::new();
        let pdf = create_minimal_pdf();

        // Act
        let unicode_payload = "unicode: 🕵️ αβγ δεζ".as_bytes();
        let embedded3 = engine.embed(&pdf, unicode_payload).unwrap();
        let extracted3 = engine.extract(&embedded3).unwrap();

        // Assert
        assert_eq!(extracted3, "unicode: 🕵️ αβγ δεζ".as_bytes()); // Unicode should round-trip correctly
    }
}