diff --git a/pykd-0.3-2010.sln b/pykd-0.3-2010.sln index 2086949..3455214 100644 --- a/pykd-0.3-2010.sln +++ b/pykd-0.3-2010.sln @@ -52,6 +52,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "snippets", "snippets", "{AA snippets\cr4.py = snippets\cr4.py snippets\ctlcode.py = snippets\ctlcode.py snippets\export.py = snippets\export.py + snippets\gdt.py = snippets\gdt.py snippets\iat.py = snippets\iat.py EndProjectSection EndProject diff --git a/snippets/cr0.py b/snippets/cr0.py index 82d6f1b..511c8f0 100644 --- a/snippets/cr0.py +++ b/snippets/cr0.py @@ -8,7 +8,7 @@ from pykd import * def cr0( value = 0 ): if value == 0: - value = reg( "cr0" ) & 0xFFFFFFFF + value = reg( "cr0" ) dprintln( "CR0: %x (" % value + "".join( [ ( value & ( 1 << ( 31 - i ) ) ) and "1" or "0" for i in range(0,32) ] ) + ")" ) diff --git a/snippets/cr4.py b/snippets/cr4.py index 5f66727..fa46b9d 100644 --- a/snippets/cr4.py +++ b/snippets/cr4.py @@ -8,7 +8,7 @@ from pykd import * def cr4( value = 0 ): if value == 0: - value = reg( "cr4" ) & 0xFFFFFFFF + value = reg( "cr4" ) dprintln( "CR4: %x (" % value + "".join( [ ( value & ( 1 << ( 31 - i ) ) ) and "1" or "0" for i in range(0,32) ] ) + ")" ) diff --git a/snippets/gdt.py b/snippets/gdt.py new file mode 100644 index 0000000..3a0ab50 --- /dev/null +++ b/snippets/gdt.py @@ -0,0 +1,69 @@ +# +# +# + +import sys +from pykd import * + + +def printGdtEntry( addr ): + + dprintln( "GDT Entry: %x" % addr ) + + attr = ptrByte( addr + 5 ) + ( ( ptrByte( addr + 6 ) & 0xF0 ) << 4 ) + + limit = ptrWord( addr ) + ( ( ptrByte( addr + 6 ) & 0xF ) << 16 ) + + base = ptrWord( addr + 2 ) + ( ptrByte( addr + 4) << 16 ) + ( ptrByte( addr + 7 ) << 24 ) + + + if attr & 0x10: + pass + else: + if is64bitSystem(): + base = ( ptrDWord( addr + 8 ) << 32 ) + base + + if attr & 0x800: + limit = ( limit << 12 ) | 0xFFF + + + dprint( "attr: %x ( " % attr + "".join( [ ( attr & ( 1 << ( 11 - i ) ) ) and "1" or "0" for i in range(0,12) ] ) + " )" ) + dprint( " base: %x" % base ) + dprintln( " limit: %x" % limit ) + + +def printGdt( gdtr ): + for s in ( "cs", "es", "ds", "ss", "gs", "fs", "tr" ): + dprintln( s + " (%x):" % reg(s) ) + printGdtEntry( gdtr + ( reg( s ) & 0xFFF8 ) ) + dprintln("") + + + +def printGdtHelp(): + + dprintln( "Usage:" ) + dprintln( "!py gdt help - Print out this message" ) + dprintln( "!py gdt x - Print out gdt entry. The gdt entry's base is got from gdtr" ) + dprintln( "!py gdt x y - Print out gdt entry. The gdt entry's base is x, y - offset" ) + + +if __name__ == "__main__": + + if not isWindbgExt(): + print "script is launch out of windbg" + quit( 0 ) + + gdtr = reg("gdtr") + + if len( sys.argv)==1: + printGdt( gdtr ) + + elif sys.argv[1] == "help": + printGdtHelp() + + elif len( sys.argv )==2: + printGdtEntry( gdtr + ( int( sys.argv[1], 16 ) & 0xFFF8 ) ) + + else: + printGdtEntry( int( sys.argv[1], 16 ) + ( int( sys.argv[2], 16 ) & 0xFFF8 ) ) diff --git a/snippets/help.py b/snippets/help.py new file mode 100644 index 0000000..9df5129 --- /dev/null +++ b/snippets/help.py @@ -0,0 +1,61 @@ +# +# +# + +import sys +import pykd + +from pykd import dprintln + + +def getFuncs(): + + funcs = sorted( [ item for item in pykd.__dict__.values() if type(item).__name__ == "function" ], + key=lambda x: x.__name__ ) + + return funcs + +def getClasses(): + + classes = sorted( [ item for item in pykd.__dict__.values() if type(item).__name__ == "class" ], + key=lambda x: x.__name__ ) + + return classes + + + +def printContent(): + + dprintln( "\nPYKD API Help\n") + + + funcs = getFuncs() + + dprintln( "\nFUNCTIONS\n", True ) + + for f in funcs: + dprintln( "%s" %( f.__name__, f.__name__ ), True ) + + classes = getClasses() + + dprintln( "\nCLASSES\n", True ) + + for c in classes: + dprintln( "%s" %( c.__name__, c.__name__ ), True ) + + +def printDetail( name ): + + if name in pykd.__dict__: + help( "pykd.%s" % name ) + + dprintln( "\n\nView content", True ) + + + +if __name__ == "__main__": + if len(sys.argv)<=1: + printContent() + if len(sys.argv)==2: + printDetail(sys.argv[1]) + diff --git a/snippets/pytowiki.py b/snippets/pytowiki.py new file mode 100644 index 0000000..78fbd05 --- /dev/null +++ b/snippets/pytowiki.py @@ -0,0 +1,133 @@ +# +# +# + +import sys + + +def usage(): + print "python pytowiki.py module_name output_file" + + +class CodeplexFormatter: + + def endl( self ): + return "\n" + + def header1( self, s ): + return "! " + s + self.endl() + + def header2( self, s ): + return "!! " + s + self.endl() + + def header3( self, s ): + return "!!! " + s + self.endl() + + def header4( self, s ): + return "!!!! " + s + self.endl() + + def bulletItem( self, s ): + return "* " + s + self.endl() + + def escapeMarkup( self, s ): + return "{\"" + s + "\"}" + + def link( self, text, link ): + return "[" + text + "|#" + link + "]" + + def anchor( self, link ): + return "{anchor:" + link + "}" + self.endl() + + + +class ModuleInfo: + + def __init__ (self, module): + self.funcs = sorted( [ item for item in module.__dict__.values() if type(item).__name__ == "function" ], key=lambda x: x.__name__ ) + self.classes = sorted( [ item for item in module.__dict__.values() if type(item).__name__ == "class" ], key=lambda x: x.__name__ ) + + for cls in self.classes: + cls.methods = sorted( [ item for item in cls.__dict__.values() if type(item).__name__ == "function" ], key=lambda x: x.__name__ ) + cls.properties = sorted( [ item for item in cls.__dict__.items() if type(item[1]).__name__ == "property" ], key=lambda x: x[0] ) + + +def buildDoc( ioStream, formatter, apiInfo ): + + ioStream.write( formatter.header2( "Functions" ) ) + + for func in apiInfo.funcs: + ioStream.write( formatter.bulletItem( formatter.link( func.__name__, func.__name__ ) ) ) + + ioStream.write( formatter.header2( "Classes" ) ) + + for cls in apiInfo.classes: + ioStream.write( formatter.bulletItem( formatter.link( cls.__name__, cls.__name__ ) ) ) + + + for func in apiInfo.funcs: + ioStream.write( formatter.anchor( func.__name__ ) ) + ioStream.write( formatter.header3( "Function " + func.__name__ ) ) + if func.__doc__ != None: + ioStream.write( formatter.escapeMarkup( func.__doc__) + formatter.endl() ) + + + for cls in apiInfo.classes: + ioStream.write( formatter.anchor( cls.__name__ ) ) + ioStream.write( formatter.header3( "Class " + cls.__name__ ) ) + if cls.__doc__ != None: + ioStream.write( formatter.escapeMarkup( cls.__doc__) + formatter.endl() ) + + if cls.properties: + ioStream.write( formatter.header4( "Properties:") ) + for p in cls.properties: + ioStream.write( formatter.bulletItem( formatter.link( p[0], cls.__name__ + "." + p[0]) ) ) + + if cls.methods: + ioStream.write( formatter.header4( "Methods:") ) + for m in cls.methods: + if m.__doc__ != None: + ioStream.write( formatter.bulletItem( formatter.link( m.__name__, cls.__name__ + "." + m.__name__) ) ) + + if cls.properties: + for p in cls.properties: + if p[1].__doc__ != None: + ioStream.write( formatter.anchor( cls.__name__ + "." + p[0] ) ) + ioStream.write( formatter.header4( formatter.escapeMarkup( "Property " + cls.__name__ + "." + p[0] ) ) ) + ioStream.write( formatter.escapeMarkup( p[1].__doc__ ) + formatter.endl() ) + + if cls.methods: + for m in cls.methods: + if m.__doc__ != None: + ioStream.write( formatter.anchor( cls.__name__ + "." + m.__name__ ) ) + ioStream.write( formatter.header4( formatter.escapeMarkup( "Method " + cls.__name__ + "." + m.__name__ ) ) ) + ioStream.write( formatter.escapeMarkup( m.__doc__ ) + formatter.endl() ) + + +def main(): + + if len(sys.argv) < 3: + usage() + return + + moduleName = sys.argv[1] + fileName = sys.argv[2] + + try: + + module = __import__( moduleName ) + + with file( fileName, "w" ) as wikiIo: + + apiInfo = ModuleInfo( module ) + + formatter = CodeplexFormatter() + + buildDoc( wikiIo, formatter, apiInfo ) + + except ImportWarning: + + print "failed to import module " + moduleName + + +if __name__ == "__main__": + main()