Board Logo
« Hiding (adding) info to a bitmap »

Welcome Guest. Please Login or Register.
Jan 16th, 2018, 3:41pm


Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Please use the forums Search feature before asking.
Please post code using the code box described in Format Your Messages.
This will keep indentation, separate it better form the message and prevent gibberish.
If the code is too long for one post or additional files are needed, upload a ZIP archive to the Just BASIC Files Archive Site.

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Hiding (adding) info to a bitmap  (Read 130 times)
tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3635
xx Hiding (adding) info to a bitmap
« Thread started on: Nov 25th, 2017, 1:27pm »

I've got an idea how to add data inside a BMP file (without breaking it).
BMP has 'offset' field, from where actual pixels are stored
so we just add stuff after this offset
and fix offset so it still point to pixel data.
Resulted images read OK by Windows
But with JB results are varied.
Anyway, this sample image
https://yadi.sk/i/YjJv36n13Q3QtC
with message added at position 66.
reads OK with JB too.
sample output
Code:
Message of length 13
is padded to 16
file
   C:\WRK\JB\readBMP\Yrose_32bit.bmp
is copied to test.bmp
Message
-----------------------
>*Hello there*   <
-----------------------
of length 16
is added to it at offset 66 

Code:
Code:
'add to BMP
'by tsh73
'BMP has 'offset' field, from where actual pixels are stored
'so we just add stuff after this offset
'and fix offset so it still point to pixel data.
'   it seems message should be divizable by 4, though (to JB to read it back)

msg$="*Hello there*"

offsetPos=10

filedialog "Open BMP file", "*.bmp", fname$
if fname$ ="" then
    print "Aborted by user"
    end
end if

'pad to 4x
if len(msg$) mod 4 <>0 then
    print "Message of length ";len(msg$)
    msg$=msg$+space$(4 - len(msg$) mod 4)
    print "is padded to ";len(msg$)
end if

open fname$ for binary as #1

    seek #1, offsetPos
    offset = readLong()

    seek #1, 0
    head$=input$(#1,offset)
    tail$=input$(#1,lof(#1)-offset)

close #1

newFname$="test.bmp"
open newFname$ for output as #2
    #2 left$(head$, offsetPos);
    newOffset=offset+len(msg$)
    #2 writeLong$(newOffset);
    #2 mid$(head$, offsetPos+4+1);
    #2 msg$;
    #2 tail$;
close #2

Print "file"
print "   ";fname$
print "is copied to ";newFname$
print "Message "
print "-----------------------"
print ">";msg$;"<"
print "-----------------------"
print "of length ";len(msg$)
print "is added to it at offset ";offset
end

' some functions ********************************************8
function readByte()
    readByte = asc(input$(#1, 1))
end function

function readLong()
    readLong = byte2Num(input$(#1, 4))
end function

function read2Bytes()
    read2Bytes = byte2Num(input$(#1, 2))
end function

function readStr$(n)
     readStr$ = input$(#1, n)
end function

function byte2Num(c$)
    res = 0
    for i = len(c$) to 1 step -1    'lower byte first
        res = res * 256+asc(mid$(c$,i,1))
    next i
    byte2Num = res
end function

function writeLong$(n)
    for i =1 to 4
        c=n mod 256
        n=int(n/256)
        writeLong$=writeLong$+chr$(c)
    next
end function

function stripPath$(fname$)
    found = 0
    for i = len(fname$) to 1 step -1
        c$=mid$(fname$,i,1)
        'print i; c$,
        if c$="\" then
            found = 1
            exit for
        end if
    next i
    if found then
         stripPath$ = mid$(fname$,i+1)
    else
        stripPath$ = fname$
    end if
end function

function inlist(aVal, list$)
    list$ = ","+list$+","
    inlist = (instr(list$, ","+str$(aVal)+",")<>0)
end function

Function hex$(n)
    h$ = ""
    do
        h$ = mid$("0123456789ABCDEF", (n mod 16)+1, 1) + h$
        n = int(n/16)
    loop while n > 0
    hex$ = h$
End Function
 
User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
tenochtitlanuk ( John F)
Full Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 430
xx Re: Hiding (adding) info to a bitmap
« Reply #1 on: Nov 25th, 2017, 4:05pm »

I've played quite a lot with steganography ( hiding information 'in plain sight') and obfuscation ( misleading hackers by making them believe something which is actually not true) See the LB forum and my website..
The following is a shorter variant which simply sticks the message$ on the end of the file. The file still behaves normally as an image.
Note it uses the LB afterlast$() function- don't have my machine with JB on to check if that is in the latest JB. Easily defined as a function anyway.
Code:
nomainwin

msg$ ="Hello there Anatoly from John."

filedialog "Open BMP file", "*.bmp", fname$
if fname$ ="" then print "Aborted by user": end
fname$ =afterlast$( fname$, "\")

open fname$ for binary as #fIn
    origFile$ =input$( #fIn, lof( #fIn))
close #fIn

open "test" +fname$ for output as #fOut
    #fOut origFile$;
    #fOut msg$
close #fOut

end
 

The tail end of the file viewed in a hex editor follows. Of course as soon as a hacker realises you are simply stuffing in plaintext they are home and dry. I'd probably eor all the msg with a prearranged byte ( or byte string, or seeded PRNG) so it is no longer plaintext.)
User Image
And here is the result.. not one of my best paintings!
User Image
User IP Logged

Visit my LB/JB pages at http://www.diga.me.uk/index.html
I use XP and Ubuntu Linux on old machines!
tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3635
xx Re: Hiding (adding) info to a bitmap
« Reply #2 on: Nov 26th, 2017, 02:53am »

Ok here's another idea
32-bit BMP has data in RR GG BB 00 format
that is, every other 4th byte is not used (00!)

So this program writes message there
and / or reads message from there, until first 00-byte.

Here's how "every fourth 00" looks like
(with a message added too)
EDIT and it is not-so-obvious ;)
User Image
Code:
'add to BMP-2
'by tsh73 Nov 2017
'BMP has 'offset' field, from where actual pixels are stored
'also JB saves BMP as 32-bit
'so all data are in RR GG BB 00 format
'that is every other 4th byte is not used (00!)
'Let's write to there

offsetPos=10
BBPpos=28

filedialog "Open BMP file", "*.bmp", fname$
if fname$ ="" then
    print "Aborted by user"
    end
end if

open fname$ for binary as #1

    seek #1, offsetPos
    offset = readLong()
    seek #1, BBPpos
    bpp = read2Bytes()
    if  bpp<>32 then
        print "Only 32-bit BMP (like, saved from JB with BMPSAVE) is supported"
        print "*Aborted*"
        close #1
        end
    end if
close #1

print "File "
print "  ";fname$
print "is 32-bit BMP."
Input "Do you want to add message (1) or read message?"; a$
if val(a$)=1 then 'add message
    print "Please enter a message:"
    input ">";msg$
    'to a copy

    open fname$ for input as #1
        all$=input$(#1,lof(#1))
    close #1

    newFname$="test2.bmp"
    open newFname$ for output as #2
        #2 all$;
    close #2

    open newFname$ for binary as #3
        for i = 1 to len(msg$)
            c$=mid$(msg$,i,1)
            seek #3, offset + i*4-1
            print #3, c$;
        next
    close #3

    Print "file"
    print "   ";fname$
    print "is copied to ";newFname$
    print "Message "
    print "-----------------------"
    print ">";msg$;"<"
    print "-----------------------"
    print "of length ";len(msg$)
    print "is added to it at offset ";offset+3
    print "every fourth byte"
else    'read a message
    msg$=""
    i = 1
    pos = offset + (i-1)*4-1
    open fname$ for binary as #3
        while pos < LOF(#3)
            seek #3, offset + i*4-1
            byte$ = input$(#3,1)
            if byte$=chr$(0) then exit while
            msg$=msg$+byte$
            i=i+1
        wend
    close #3
    if msg$="" then
        print "It seems there weren't any message"
    else
        print "Message was:"
        print "-----------------------"
        print ">";msg$;"<"
        print "-----------------------"
    end if
end if
end

' some functions ********************************************8
function readByte()
    readByte = asc(input$(#1, 1))
end function

function readLong()
    readLong = byte2Num(input$(#1, 4))
end function

function read2Bytes()
    read2Bytes = byte2Num(input$(#1, 2))
end function

function readStr$(n)
     readStr$ = input$(#1, n)
end function

function byte2Num(c$)
    res = 0
    for i = len(c$) to 1 step -1    'lower byte first
        res = res * 256+asc(mid$(c$,i,1))
    next i
    byte2Num = res
end function

function writeLong$(n)
    for i =1 to 4
        c=n mod 256
        n=int(n/256)
        writeLong$=writeLong$+chr$(c)
    next
end function

function stripPath$(fname$)
    found = 0
    for i = len(fname$) to 1 step -1
        c$=mid$(fname$,i,1)
        'print i; c$,
        if c$="\" then
            found = 1
            exit for
        end if
    next i
    if found then
         stripPath$ = mid$(fname$,i+1)
    else
        stripPath$ = fname$
    end if
end function

function inlist(aVal, list$)
    list$ = ","+list$+","
    inlist = (instr(list$, ","+str$(aVal)+",")<>0)
end function

Function hex$(n)
    h$ = ""
    do
        h$ = mid$("0123456789ABCDEF", (n mod 16)+1, 1) + h$
        n = int(n/16)
    loop while n > 0
    hex$ = h$
End Function
 
« Last Edit: Nov 26th, 2017, 02:56am by tsh73 » User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
rtr
Member in Training
ImageImage


member is offline

Avatar




PM


Posts: 42
xx Re: Hiding (adding) info to a bitmap
« Reply #3 on: Nov 26th, 2017, 08:24am »

on Nov 26th, 2017, 02:53am, tsh73 wrote:
Ok here's another idea
32-bit BMP has data in RR GG BB 00 format

That won't always work, because the fourth byte may be interpreted as 'alpha' (transparency/opacity). For example when SDL reads a 32-bpp BMP file it checks whether all the 'alpha' bytes are the same, and if so ignores them. But if they aren't all the same it assumes they are alpha values; your 'modified' BMP would display incorrectly in that case.

Historically, 32-bpp BMP files were not even valid, and many applications refused to read them at all (including some versions of Gimp). Over the years they have become more widely accepted, but a 24-bpp BMP is more 'standard' (and significantly smaller, of course).

Richard.
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls