+ Reply to Thread
Results 1 to 11 of 11

Thread: Python & Ruby issue     submit to reddit submit to twitter

  1. #1

    Python & Ruby issue

    Working on a project to do some HID encoding, i'm converting the Hak5 ducky encoder from java to either python or ruby. Just doing simple testing right now, but here is what happens.

    The java encoder looks for character strings (like "ESCAPE") and converts it to HID hex value 0x29

    https://code.google.com/p/ducky-decode/wiki/HID_Codes

    when you put that into the inject.bin file it spits out, running hexdump against it obviously gives you 0029. I can mimic this with python but opening up the file and doing a .replace('ESCAPE','\x29') and when I .write to the the output.bin, running hexdump against it shows it as 0029 as well.

    However I can't seem to get it to work in ruby. Ruby interprets the hex to ASCII and writes '41' to the .bin file. If I try to write as a string, it just writes '\x29'.

    Is there anyway to get this to work in ruby? I want to basically write 0x29 to output.bin and have it read as 0029 when run against hexdump -x.

    Here's the original java file i'm trying to convert to python or ruby:

    https://raw.github.com/hak5darren/US...c/Encoder.java

  2. #2

    Solved it.

    f.write('hexvalue'.hex.chr)

  3. #3

    Bumping this as I have another question.


    Is there a way to have Ruby read lines out of a file, put them into an array and then split the array based on what is in [0]?

    So say a line says "STRING Hello World"

    I want ruby to spit that into an array that would be [STRING, Hello, World] then see that STRING is in [0] and split Hello and World into H,e,l,l,o, ,W,o,r,l,d.

    Reason being, I need to replace each letter of the string with its corresponding HID hex values.

  4. #4

    Seems I figured it out somewhat, though slightly messy.

    string = 'Hello'
    array = string.split(//)
    array.each do |x|
    if x == 'check value of element'
    f.write('hex value I need for that'.hex.chr)

  5. #5
    Very Sexy Nerd
    Join Date
    Oct 2005
    Posts
    9,065
    BG Level
    8
    FFXI Server
    Carbuncle

    Not sure I fully grasp what you want.

    Does every line say "STRING stuff", or does it just say STRING at the start of the file?

    The way you're doing it, assuming string = 'Hello' is a single line, you're also doing the hex stuff with the "STRING" part of it.

    Is the output going into a new file, or overwriting the existing?

    I guess all that stuff doesn't matter too much, as long as you can figure that part out.

    Assuming you have a line, called line, you can do:

    Code:
    #line = "STRING test 1 2 3"
    array = line.split(/ /) #split into: ["STRING", "test", "1", "2", "3"]
    if array.first == "STRING" then
        array.shift #get rid of the STRING
        array.join(' ').split(//).each do |x| #get the spaces back, then split it by each character
            f.write(x.hex.chr) #or w/e you're doing here
        end
    end
    If you don't need to get rid of the "STRING" part of it then this obviously becomes a whole lot simpler.

  6. #6
    listen!
    Join Date
    Apr 2011
    Posts
    7,236
    BG Level
    8
    FFXI Server
    Sylph

    Quote Originally Posted by Meresgi View Post
    Seems I figured it out somewhat, though slightly messy.

    string = 'Hello'
    array = string.split(//)
    array.each do |x|
    if x == 'check value of element'
    f.write('hex value I need for that'.hex.chr)
    Why not just do something like

    string = 'Hello'
    for i in (0..string.length-1) do
    f.write(string[i])
    end

    Sorry if it's weird, i've never actually used ruby, but it seems to work.

  7. #7

    I'm not looking to use the actual hex value of the letters or keys, as the HID hex values are different and those are what I need.

    Every line doesn't have STRING on it usually, but a bunch might. I have to account for DELAY, GUI R (windows key + r), RETURN, ESCAPE and all of the shift characters and F keys to make it work properly.

    #line = "STRING test 1 2 3"
    array = line.split(/ /) #split into: ["STRING", "test", "1", "2", "3"]
    if array.first == "STRING" then
    array.shift #get rid of the STRING
    array.join(' ').split(//).each do |x| #get the spaces back, then split it by each character
    f.write(x.hex.chr) #or w/e you're doing here
    end
    end
    Trying to do it like:

    ....each do |x|
    f.write(x.hex.chr) will spit out the wrong value or give an error from what I remember from yesterday, i'll see if it works with the join/split and the shift. That would tidy it up a lot if it functions correctly.

    Thanks for help guys

  8. #8

    I have the thing working now, with just one hitch. The newest java version allows a REPEAT command that repeats the last line before it. Aka:

    STRING Testing
    STRING Hello World
    REPEAT 2
    STRING Test2

    equals:

    Testing
    Hello World
    Hello World
    Test2

    I've started trying to work out a while loop but i'm not too sure how to make it only process the command just before it and not run the entire thing again.

  9. #9

    I can get it to correctly loop the amount of times for Repeat with a while loop...but like I feared it just repeats the entire thing. Trying to figure out what to do.

    Here's what I have. it's sloppy since i'm just fucking around and trying things.

    $f = open('test.bin','wb')

    def repeatfinder
    open('hello.txt').each do |line|
    $input = line.split(" ")
    if $input[0] == ('REPEAT')
    $j = $input[1]

    _______________________________

    def string_found
    while $j > 0
    open('hello.txt').each do |line|
    $input = line.split(" ")
    if $input[0] == ('STRING')
    characters = $input.join(' ')
    hex = characters.unpack('U' * characters.length)
    hex.each do |x|
    if x >= 97 && x <= 122
    x = x -93
    x = x.to_s(16)
    $f.write((x).hex.chr)
    $f.write('00'.hex.chr)
    $j -= 1
    ______________________

    repeatfinder
    puts $j
    string_found
    puts $j

    if input file has:

    STRING hello
    REPEAT 2
    STRING b

    It should spit out:

    hello
    hello
    b

    instead it's spitting out

    hello
    b
    hello
    b

    it will correctly go through x amount of times, but I don't want it to repeat the entire thing.

  10. #10
    Sea Torques
    Join Date
    Jul 2006
    Posts
    500
    BG Level
    5
    FFXI Server
    Odin

    Code:
    $ cat foo.py
    #!/usr/bin/python
    
    def do_string(s):
        for i in range(len(s)):
            print i, s[i],
        print
    
    def parse_line(lines, index):
        # remove line endings and whitespaces at start or end of line
        current=lines[index].strip()
        # split remainder
        line_array=current.split(" ")
    
        op=line_array[0]
    
        # STRING: re-assemble string from remainder array
        if op=="STRING":
            do_string(" ".join(line_array[1:]))
        # REPEAT: WHOO recursion!
        elif op=="REPEAT":
            times = int(line_array[1])
            for _ in range(times):
                parse_line(lines, index-1)
    
    
    file = open("test.txt", "r")
    lines=file.readlines()
    file.close()
    
    for i in range(len(lines)):
         parse_line(lines, i)
    Code:
    $ cat test.txt
    STRING foo
    STRING bar
    REPEAT 2
    STRING baz
    REPEAT 2
    REPEAT 3
    Code:
    $ python foo.py
    0 f 1 o 2 o
    0 b 1 a 2 r
    0 b 1 a 2 r
    0 b 1 a 2 r
    0 b 1 a 2 z
    0 b 1 a 2 z
    0 b 1 a 2 z
    0 b 1 a 2 z
    0 b 1 a 2 z
    0 b 1 a 2 z
    0 b 1 a 2 z
    0 b 1 a 2 z
    0 b 1 a 2 z

  11. #11

    Thanks a ton fussel, just got around to seeing that. Going to modify it a bit and convert it to ruby, but i'm pretty sure that's exactly what I need. Also just putting it a bit into ruby i'm getting ascii values for the letters which is great.