Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Thursday, September 1, 2016

How to trace python daemon program

Well some of you might think whats there to trace in a python program simply use pdb.
Oh if you dont know pdb debugging you ought to read whole full post, if you know about pdb I will describe other method which worked for demonized + threads code from fourth paragraph.

    So I was helping a friend to debug a code to find data flow. The code was running as demon making it difficult to debug. The module consisted multiple files and main file being around 2000 lines of code. So I first went to my favorite option pdb.

 import pdb
 pdb.set_trace()

Normally if you put these two line in any normal python code then you will get a interactive shell and execution will stop at that line and wait for your commands. Then you can just enter command like 'n' to execute n(ext) line, or s(tep) to step into functions. You can use command  w(here) to print a stack trace to check where is the current execution line and nearby code. If you are interested in details of the pdb basics checkout the article at https://pymotw.com/2/pdb/ or https://docs.python.org/2/library/pdb.html Since code was running as daemon and running threads it was difficult to interact with pdb. The pdb shell was just getting printed somewhere on the command line and was not possible to interact with it. So I went ahead to check other options how trace the function calls of these program.

    I found a other option of  sys.settrace() to see which function of the program are being called in which order. The sys.settrace() needs a function to be written to handle the callback. So here is a function code which I got from internet and updated to my needs:

def trace_calls(frame, event, arg):
    if event != 'call':
        return

    co = frame.f_code
    if co is not None:
        func_name = co.co_name
        if func_name in ['write',' __getattr__']:
            # Ignore write() calls from print statements
            return
        line_no = frame.f_lineno
        filename = co.co_filename
        #only output function names from module dir
        if 'mymodule' not in filename:
            return

        str1 = 'Call to %s on line %s of %s \n Args:%s\n' % (func_name, line_no, filename,str(arg))
        print (str1)

        if func_name in TRACE_INTO:
            # Trace into this function
            return trace_lines
            
        return

I will explain somethings from this code that I changed/added to make it more simple for me. First of all There were multiple calls to multiple imported libraries. So easily the file size went to 28-30 MB. Then I put this if condition:

if 'mymodule' not in filename:


So now only function calls from 'mymodule' will be printed. Now the printed statements became less. The code prints line number from which function name was called and the file path to which the function belongs. The sys.settrace() function needs to be called and this function takes parameter function name (in our case trace_calls) which will be called when a function call happens.

sys.settrace(trace_calls)

The trace_calls function will print the information about function call as describe before.
So then I went through the multiple function calls and got the whole data flow and where to make changes to alter the data.

You might have notice one more function call trace_lines in the trace_calls function.

TRACE_INTO=['dataReceived']
def trace_lines(frame, event, arg):
    if event != 'line':
        return

    co = frame.f_code
    if co is not None:
        func_name = co.co_name
        line_no = frame.f_lineno
        filename = co.co_filename
        str1 = '  %s line %s' % (func_name, line_no)
        print (str1)

This trace line function can be called if you want to trace the execution line by line inside any function. Here a list TRACE_INTO is created and only functions in side this list will be traced as line by line. So if you feel any function is important and need to check execution you can use this functionality.

Wednesday, April 8, 2015

UPDATED python script to download files via google search V2.2

I was using my old script to download some pdf files automatically via searching in google but it seems that google has changed its format of giving search URL. So again modified the script and currently working script as of 09-April-2015

You can download the new updated script HERE

Saturday, January 14, 2012

python script to download files via google search

Just now some one was asking to me on chat if there is some script which can download the files if given a google search query.

When thinking I suddenly remember that I had coded such a script some time ago using xgoogle libray of python. So I just searched for my script and here it is.

As you know I am lazy, I have used xgoogle and not directly handle google via httplib or urllib etc etc. My script used getopt library to parse the options given to the script. (again I am lazy)
(xgoogle library can be downloaded at http://www.catonmat.net/blog/python-library-for-google-search/)

The general syntax of this script is

python gsrchDwn.py --query "query_text" [--ftype file_extension] [--cnt contine_result_number] [--dir download_dir]

usage: python gsrchDwn.py --query maths made easy --ftype pdf

IMP Notes
1)It proper results are not got try the query in " (double quotes)
2) This file need xgoogle library found at http://www.catonmat.net/blog/python-library-for-google-search/

If --dir is not given it will download files into current directory.
If the script is stopped inbetween you can continue from the last result number by using --cnt result_number

This time I am have become a good boy and also added a status printing which shows how much percentage of current file is downlaoded.

grsrchDwn.py can be downloaded HERE

Let me know any comments.

Friday, May 22, 2009

python script to find cisco routers with default password

While I was doing my work (No options...have to do some to earn) in recent days
I was busy auding lots of cisco routers. As hacker mind I just went ahead hacking my way in to nearly all routers getting full access. But when I had completed my work I suddenly remember that some of the routers used default passwords and others I had extracted password from config of already accessed routers. But I just didnt remember for which routers I found using default password. As usual the programmer in me wake up (My colleague said he would check out manualy in 1 hour) I said I would better use half hour to write a script to find out those. This script will be use full in finding out routers with default password in future also.
So I did write a python script (you must be knowing by now python is my fav language) to check out routers with default passwords.
This script is still in its early stages. So looking forward for some good or bad feedbacks.

You can find script at
http://neo1981.googlepages.com/ciscoPassChk.py