Exemplo n.º 1
0
def lsbExtract(q):
    post, filename = q.get()
    rgb_data = libstegofile.rgb_data_t()
    png_struct = libstegofile.png_internal_data_t()
    para = libstegofile.lsb_parameter_t()
    para.password = str(post["pw"])
    para.pwlen = len(para.password)
    para.select_mode = 2
    para.use_msb = 0
    stegomsg = libstego.new_charp()
    stegolen = libstego.new_intp()
    retcode = libstegofile.io_png_read(filename, rgb_data, png_struct)
    if retcode == 0:
        pass
    else:
        return -1
    bytes = libstego.new_charpp()
    num_bytes = libstego.new_intp()
    libstego.lsb_convert_png(rgb_data, bytes, num_bytes)

    libstego.lsb_extract_indirect(
        libstego.charpp_value(bytes), libstego.intp_value(num_bytes), stegomsg, stegolen, para
    )
    msglen = libstego.intp_value(stegolen)
    q.put((str(libstego.charp_value(stegomsg)))[0:msglen])
Exemplo n.º 2
0
def embed_or_extract(algorithm, filename, destfilename, message, para, embed):
    """This function gets as parameters
    filename ... the input file name
    destfilename ... the output filen ame
    message ... the message
    para ... the parameter for the chosen algorithm
    embed ... boolean: if True, we embed, else we extract.
    If embed is True it uses libstego to embed the given message
    in the given output file, calling the <algorithm>_embed()
    function in libstego.
    If embed is False, it uses libstego to extract a message
    from the given file, calling the <algorithm>_extract()
    function in libstego.
    This function can therefor be called recursively to check
    if embedding a message worked by calling it with embed=False.
    """
    ending = filename[-3:]
    print ending
    if embed:
        msglen = len(message) + 1
    else:
        message = libstego.new_charp()
        msglen = libstego.new_intp()
    if ending == "gif":
        src_data = libstegofile.palette_data_t()
        stego_data = libstegofile.palette_data_t()
        gif_int = libstegofile.gif_internal_data_t()
        libstegofile.io_gif_read(filename, src_data, gif_int)
        if algorithm == "Sort / Unsort":
            if embed:
                libstego.sortunsort_embed(src_data, stego_data, message, msglen, para)
            else:
                libstego.sortunsort_extract(src_data, message, msglen, para)
        elif algorithm == "GifShuffle":
            if embed:
                if para.method == 1:
                    pw_message = para.password
                    pw_message_len = para.pwlen
                    para.password = ""
                    para.pwlen = 0
                    pwmsg_data = libstegofile.palette_data_t()
                    para.method = 0
                    libstego.gifshuffle_embed(src_data, pwmsg_data, pw_message, pw_message_len, para)
                    para.method = 1
                    libstego.gifshuffle_embed(pwmsg_data, stego_data, message, msglen, para)
                else:
                    print "normal gifshuffle:"
                    print "pw: " + para.password
                    print "pwlen: " + str(para.pwlen)
                    libstego.gifshuffle_embed(src_data, stego_data, message, msglen, para)
            else:
                if para.method == 1:
                    pw_message = para.password
                    pw_message_len = para.pwlen
                    para.password = ""
                    para.pwlen = 0
                    pwmsg_data = libstegofile.palette_data_t()
                    para.method = 0
                    libstego.gifshuffle_embed(src_data, pwmsg_data, pw_message, pw_message_len, para)
                    para.adv_pal = pwmsg_data
                    para.method = 1
                    libstego.gifshuffle_extract(src_data, message, msglen, para)
                else:
                    libstego.gifshuffle_extract(src_data, message, msglen, para)

        elif algorithm == "Frirui":
            if embed:
                libstego.frirui_embed(src_data, stego_data, message, msglen, para)
            else:
                libstego.frirui_extract(src_data, message, msglen, para)
        if embed:
            print "integrate and write ..."
            libstegofile.io_gif_integrate(gif_int, stego_data)
            libstegofile.io_gif_write(destfilename, gif_int)
        # libstegofile.io_gif_cleanup_data(stego_data)
        # libstegofile.io_gif_cleanup_data(src_data)
        # libstegofile.io_gif_cleanup_internal(gif_int)
    elif ending == "png":
        src_data = libstegofile.rgb_data_t()
        stego_data = libstegofile.rgb_data_t()
        png_int = libstegofile.png_internal_data_t()
        libstegofile.io_png_read(filename, src_data, png_int)
        if algorithm == "Battlesteg":
            if embed:
                libstego.battlesteg_embed(src_data, stego_data, message, msglen, para)
            else:
                libstego.battlesteg_extract(src_data, message, msglen, para)

        elif algorithm == "CPT":
            if embed:
                libstego.cpt_embed(src_data, stego_data, message, msglen, para)
            else:
                libstego.cpt_extract(src_data, message, msglen, para)

        elif algorithm == "LSB":
            bytes = libstego.new_charpp()
            num_bytes = libstego.new_intp()
            stego_data = src_data
            libstego.lsb_convert_png(stego_data, bytes, num_bytes)
            if embed:
                ret = libstego.lsb_embed_indirect(
                    libstego.charpp_value(bytes), libstego.intp_value(num_bytes), message, msglen, para
                )
                if ret != 0:
                    print "An error occured. Error code: " + ` libstego.cvar.lstg_errno `
                    return False
            else:
                ret = libstego.lsb_extract_indirect(
                    libstego.charpp_value(bytes), libstego.intp_value(num_bytes), message, msglen, para
                )
                if ret != 0:
                    print "An error occured. Error code: " + ` libstego.cvar.lstg_errno `
                    return False

            libstego.lsb_cleanup_converted_data(libstego.charpp_value(bytes))
            libstego.delete_charpp(bytes)

        if embed:
            libstegofile.io_png_integrate(png_int, stego_data)
            libstegofile.io_png_write(destfilename, png_int)
            libstegofile.io_png_cleanup_data(stego_data)
        # png cleanup ruft segmentation faults hervor...
        # libstegofile.io_png_cleanup_data(src_data)
        # libstegofile.io_png_cleanup_internal(png_int)

    elif ending == "svg":
        src_data = libstegofile.svg_data_t()
        stego_data = libstegofile.svg_data_t()
        svg_int = libstegofile.svg_internal_data_t()
        libstegofile.io_svg_read(filename, src_data, svg_int)
        if algorithm == "SVG Steg":
            if embed:
                ret = libstego.svg_embed(src_data, stego_data, message, msglen, para)

                if ret != 0:
                    print "An error occured. Error code: " + ` libstego.cvar.lstg_errno `
                    return False

            else:
                ret = libstego.svg_extract(src_data, message, msglen, para)

                if ret != 0:
                    print "An error occured. Error code: " + ` libstego.cvar.lstg_errno `
                    return False

        if embed:
            libstegofile.io_svg_integrate(svg_int, stego_data)
            libstegofile.io_svg_write(destfilename, svg_int)
            libstegofile.io_svg_cleanup_data(stego_data)

        libstegofile.io_svg_cleanup_data(src_data)
        libstegofile.io_svg_cleanup_internal(svg_int)

    elif ending == "wav":
        src_data = libstegofile.wav_data_t()
        stego_data = libstegofile.wav_data_t()
        wav_int = libstegofile.wav_internal_data_t()
        libstegofile.io_wav_read(filename, src_data, svg_int)
        if algorithm == "Echo Hiding":
            if embed:
                libstego.echohiding_embed(src_data, stego_data, message, msglen, para)
            else:
                libstego.echohiding_extract(src_data, message, msglen, para)
        elif algorithm == "Phase Coding":
            if embed:
                libstego.phasecoding_embed(src_data, stego_data, message, msglen, para)
            else:
                libstego.phasecoding_extract(src_data, message, msglen, para)
        libstegofile.io_wav_integrate(wav_int, stego_data)
        libstegofile.io_wav_write(destfilename, wav_int)
        libstegofile.io_wav_cleanup_data(stego_data)
        libstegofile.io_wav_cleanup_data(src_data)
        libstegofile.io_wav_cleanup_internal(wav_int)
    elif ending == "jpg" or "peg":
        src_data = libstegofile.jpeg_data_t()
        stego_data = libstegofile.jpeg_data_t()
        jpg_int = libstegofile.jpeg_internal_data_t()
        libstegofile.io_jpeg_read(filename, src_data, jpg_int)
        if algorithm == "F5":
            if embed:
                libstego.f5_embed(src_data, stego_data, message, msglen, para)
            else:
                libstego.f5_extract(src_data, message, msglen, para)
        elif algorithm == "MB2":
            if embed:
                libstego.mb2_embed(src_data, stego_data, message, msglen, para)
            else:
                libstego.mb2_extract(src_data, message, msglen, para)
        libstegofile.io_jpeg_integrate(jpg_int, stego_data)
        libstegofile.io_jpeg_write(destfilename, jpg_int)
        # libstegofile.io_jpeg_cleanup_data(stego_data)
        libstegofile.io_jpeg_cleanup_data(src_data)
        libstegofile.io_jpeg_cleanup_internal(jpg_int)
    # TODO: extract for testing purpose!
    if embed:
        return True
    else:
        new_message = str(libstego.charp_value(message))
        length = libstego.intp_value(msglen)
        print new_message
        return new_message