473,757 Members | 9,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

binutils "strings" like functionality?

cjl
Hey all:

I am working on a little script that needs to pull the strings out of a
binary file, and then manipulate them with python.

The command line utility "strings" (part of binutils) has exactly the
functionality I need, but I was thinking about trying to implement this
in pure python.

I did some reading on opening and reading binary files, etc., and was
just wondering if people think this is possible, or worth my time (as a
learning exercise), or if something like this already exists.

-cjl

Jul 18 '05 #1
9 3295
I don't know anything about binutils strings, other than that a quick
google reveals that you can search binary files for printable strings
with it.

In python the following code will read a binary file into a single
string :

long_string = open(filename, 'rb').read()

You can then slice long_string, iterate over it... or do whatever you
like with it.

Now you *ought* to be creating a proepr file handle and closing it. You
probably don't want to read the whole file into memory at once, etc....
But implementing these thigns are trivial.

What particular aspect of 'strings' did you want to implement ?

Regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml

Jul 18 '05 #2
Take a look at python's struct module in the standard library.
Many people use it to manipulate binary objects at the byte
level with great success.

Larry Bates
cjl wrote:
Hey all:

I am working on a little script that needs to pull the strings out of a
binary file, and then manipulate them with python.

The command line utility "strings" (part of binutils) has exactly the
functionality I need, but I was thinking about trying to implement this
in pure python.

I did some reading on opening and reading binary files, etc., and was
just wondering if people think this is possible, or worth my time (as a
learning exercise), or if something like this already exists.

-cjl

Jul 18 '05 #3
cjl wrote:
Hey all:

I am working on a little script that needs to pull the strings out of a
binary file, and then manipulate them with python.

The command line utility "strings" (part of binutils) has exactly the
functionality I need, but I was thinking about trying to implement this
in pure python.

I did some reading on opening and reading binary files, etc., and was
just wondering if people think this is possible, or worth my time (as a
learning exercise), or if something like this already exists.


I think a bare-bones version is pretty simple:
- read the file
- use re.split() to split on non-printable characters (string.printab le could be handy here)
- print anything in the resulting list that is at least 4 characters

Since you mention this as a learning exercise I'll refrain from posting code but it's only a handful
of lines...of course implementing all of 'strings' is a bit more work.

Kent
Jul 18 '05 #4
Kent Johnson wrote:
cjl wrote:
Hey all:

I am working on a little script that needs to pull the strings out of a
binary file, and then manipulate them with python.

The command line utility "strings" (part of binutils) has exactly the
functionality I need, but I was thinking about trying to implement this
in pure python.

I did some reading on opening and reading binary files, etc., and was
just wondering if people think this is possible, or worth my time (as a
learning exercise), or if something like this already exists.

I think a bare-bones version is pretty simple:
- read the file
- use re.split() to split on non-printable characters (string.printab le
could be handy here)
- print anything in the resulting list that is at least 4 characters

The binutils version may well use a zero terminator character to
differentiate between random chunks of printable characters and those
explicitly genreated as strings from C. Or it may not ...
Since you mention this as a learning exercise I'll refrain from posting
code but it's only a handful of lines...of course implementing all of
'strings' is a bit more work.

regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.pycon.org/
Steve Holden http://www.holdenweb.com/
Jul 18 '05 #5
"cjl" wrote:
I am working on a little script that needs to pull the strings out of a
binary file, and then manipulate them with python.

The command line utility "strings" (part of binutils) has exactly the
functionality I need, but I was thinking about trying to implement this
in pure python.


something like this could work:

import re

text = open(file, "rb").read( )

for m in re.finditer("([\x20-\x7f]{4,})[\n\0]", text):
print m.start(), repr(m.group(1) )

you may wish to modify the "[\x20-\x7f]" part to match your definition of
"printable characters". "[-,.!?\w ]" is a reasonable choice in many cases...

if the files can be huge, use the mmap module to map the file into memory,
and run the RE on the mapped view.

</F>

Jul 18 '05 #6
cjl
Fredrik Lundh wrote:
something like this could work:

import re

text = open(file, "rb").read( )

for m in re.finditer("([\x20-\x7f]{4,})[\n\0]", text):
print m.start(), repr(m.group(1) )

Hey...that worked. I actually modified:

for m in re.finditer("([\x20-\x7f]{4,})[\n\0]", text):

to

for m in re.finditer("([\x20-\x7f]{4,})", text):

and now the output is nearly identical to 'strings'. One problem
exists, in that if the binary file contains a string
"monkey/chicken/dog/cat" it is printed as "mokey//chicken//dog//cat",
and I don't know enough to figure out where the extra "/" is coming
from.

Help?

-CJL

Jul 18 '05 #7
"cjl" <cj****@gmail.c om> writes:
Fredrik Lundh wrote:
something like this could work:

import re

text = open(file, "rb").read( )

for m in re.finditer("([\x20-\x7f]{4,})[\n\0]", text):
print m.start(), repr(m.group(1) )


Hey...that worked. I actually modified:

for m in re.finditer("([\x20-\x7f]{4,})[\n\0]", text):

to

for m in re.finditer("([\x20-\x7f]{4,})", text):

and now the output is nearly identical to 'strings'. One problem
exists, in that if the binary file contains a string
"monkey/chicken/dog/cat" it is printed as "mokey//chicken//dog//cat",
and I don't know enough to figure out where the extra "/" is coming
from.


Are you sure it's monkey/chicken/dog/cat, and not
monkey\chicken\ dog\cat? The later one will print monkey\\chicken ...
because of the repr() call.

Also, you probably want it as [\x20-\x7e] (the DEL character \x7f
isn't printable). You're also missing tabs (\t).

The GNU binutils string utility looks for \t or [\x20-\x7e].

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)phy sics(dot)mcmast er(dot)ca
Jul 18 '05 #8
cjl
David M. Cooke wrote:
Are you sure it's monkey/chicken/dog/cat, and not
monkey\chicken\ dog\cat? The later one will print monkey\\chicken ...
because of the repr() call.

Also, you probably want it as [\x20-\x7e] (the DEL character \x7f
isn't printable). You're also missing tabs (\t).

The GNU binutils string utility looks for \t or [\x20-\x7e].


Yeah, it is "monkey\chicken \dog\cat", thank you for pointing that out.

I started snooping throught the sourcecode for 'strings' in binutils to
see what it matches against, but I don't really know how to program, so
you just saved me even more time. Thanks again!

-CJL

Jul 18 '05 #9
In <11************ **********@l41g 2000cwc.googleg roups.com>, cjl wrote:
I am working on a little script that needs to pull the strings out of a
binary file, and then manipulate them with python.

The command line utility "strings" (part of binutils) has exactly the
functionality I need, but I was thinking about trying to implement this
in pure python.

I did some reading on opening and reading binary files, etc., and was
just wondering if people think this is possible, or worth my time (as a
learning exercise), or if something like this already exists.


If you find it interesting and challenging go ahead and try it. It is
possible and with the help of the 're' and 'mmap' modules it should be
quite easy to filter strings out of files without the need to load them
into memory at once.

Ciao,
Marc 'BlackJack' Rintsch
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3104
by: Bill Janssen | last post by:
I've encountered an issue dealing with strings read from files. I read a line from a file, then try to print it out as an ASCII string: line = fp.readline() print line.encode('US-ASCII', 'replace') and of course I get an error like: Traceback (most recent call last): File "<stdin>", line 1, in ?
3
1371
by: Petr Prikryl | last post by:
Hi all, My question is: How do you tackle with mixing Unicode and non-Unicode parts of your application? Context: ======== The PEP 3000 says "Make all strings be Unicode, and have a separate bytes() type."
7
911
by: SunRise | last post by:
Hi I am creating a C Program , to extract only-Printable-characters from a file ( any type of file) and display them. OS: Windows-XP Ple help me to fix the Errors & Warnings and explain how to use Command-Line Arguments inside C program.
2
1493
by: ÕÔÁ¢ÈÊ | last post by:
I have a large number of Console.WriteLine() function to display debug information debug mode. Now,I want to save them to a text or display them in a text box.How Can I do it?? thank you for your replay!
3
1449
by: Curtis Justus | last post by:
Hi, We are porting an application that contains a list of strings. We would like to use these strings within code such as a switch statement. What would be the best way to create a list of items that could be used within a switch statement (or something like that)? Thanks in advance, cj
8
3618
by: Ulysse | last post by:
Hello, I need to clean the string like this : string = """ bonne mentalit&eacute; mec!:) \n <br>bon pour info moi je suis un serial posteur arceleur dictateur ^^* \n <br>mais pour avoir des resultats probant il faut pas faire les mariolles, comme le &quot;fondateur&quot; de bvs
0
9298
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7286
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5172
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.