Changeset 899:733193ef0717
- Timestamp:
- 08/31/08 00:34:17
(4 months ago)
- Author:
- Phil <phil@secdev.org>
- Message:
Improved temp file management
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r892 |
r899 |
|
| 269 | 269 | stats_classic_protocols = [] |
|---|
| 270 | 270 | stats_dot11_protocols = [] |
|---|
| | 271 | temp_files = [] |
|---|
| 271 | 272 | netcache = NetCache() |
|---|
| 272 | 273 | load_layers = ["l2", "inet", "dhcp", "dns", "dot11", "gprs", "hsrp", "ip6", "ir", "isakmp", "l2tp", |
|---|
| r897 |
r899 |
|
| 7 | 7 | from __future__ import generators |
|---|
| 8 | 8 | import os,sys |
|---|
| | 9 | import __builtin__ |
|---|
| 9 | 10 | from error import * |
|---|
| 10 | | import __builtin__ |
|---|
| | 11 | import utils |
|---|
| 11 | 12 | |
|---|
| 12 | 13 | |
|---|
| … | … | |
| 73 | 74 | fname = conf.session |
|---|
| 74 | 75 | if not fname: |
|---|
| 75 | | conf.session = fname = os.tempnam("","scapy") |
|---|
| | 76 | conf.session = fname = utils.get_temp_file(keep=True) |
|---|
| 76 | 77 | log_interactive.info("Use [%s] as session file" % fname) |
|---|
| 77 | 78 | if session is None: |
|---|
| … | … | |
| 121 | 122 | ##### Main ##### |
|---|
| 122 | 123 | ################ |
|---|
| | 124 | |
|---|
| | 125 | def scapy_delete_temp_files(): |
|---|
| | 126 | for f in conf.temp_files: |
|---|
| | 127 | try: |
|---|
| | 128 | os.unlink(f) |
|---|
| | 129 | except: |
|---|
| | 130 | pass |
|---|
| 123 | 131 | |
|---|
| 124 | 132 | def scapy_write_history_file(readline): |
|---|
| … | … | |
| 129 | 137 | try: |
|---|
| 130 | 138 | warning("Could not write history to [%s]\n\t (%s)" % (conf.histfile,e)) |
|---|
| 131 | | tmp = os.tempnam("","scapy") |
|---|
| | 139 | tmp = utils.get_temp_file(keep=True) |
|---|
| 132 | 140 | readline.write_history_file(tmp) |
|---|
| 133 | 141 | warning("Wrote history to [%s]" % tmp) |
|---|
| … | … | |
| 287 | 295 | atexit.register(scapy_write_history_file,readline) |
|---|
| 288 | 296 | |
|---|
| | 297 | atexit.register(scapy_delete_temp_files) |
|---|
| 289 | 298 | sys.ps1 = ColorPrompt() |
|---|
| 290 | 299 | code.interact(banner = the_banner % (conf.version), local=session) |
|---|
| r898 |
r899 |
|
| 5 | 5 | |
|---|
| 6 | 6 | import os,sys,socket,types |
|---|
| 7 | | from config import conf |
|---|
| 8 | | from data import MTU |
|---|
| 9 | 7 | import random,time |
|---|
| 10 | 8 | import gzip,zlib,cPickle |
|---|
| 11 | 9 | import re,struct,array |
|---|
| | 10 | |
|---|
| | 11 | import warnings |
|---|
| | 12 | warnings.filterwarnings("ignore","tempnam",RuntimeWarning, __name__) |
|---|
| | 13 | |
|---|
| | 14 | from config import conf |
|---|
| | 15 | from data import MTU |
|---|
| 12 | 16 | from error import log_runtime,log_loading,log_interactive |
|---|
| 13 | 17 | from base_classes import BasePacketList |
|---|
| … | … | |
| 17 | 21 | ## Tools ## |
|---|
| 18 | 22 | ########### |
|---|
| | 23 | |
|---|
| | 24 | def get_temp_file(keep=False): |
|---|
| | 25 | f = os.tempnam("","scapy") |
|---|
| | 26 | if not keep: |
|---|
| | 27 | conf.temp_files.append(f) |
|---|
| | 28 | return f |
|---|
| 19 | 29 | |
|---|
| 20 | 30 | def sane_color(x): |
|---|
| … | … | |
| 683 | 693 | def wireshark(pktlist): |
|---|
| 684 | 694 | """Run wireshark on a list of packets""" |
|---|
| 685 | | f = os.tempnam("scapy") |
|---|
| | 695 | f = get_temp_file() |
|---|
| 686 | 696 | wrpcap(f, pktlist) |
|---|
| 687 | 697 | os.spawnlp(os.P_NOWAIT, conf.prog.wireshark, conf.prog.wireshark, "-r", f) |
|---|
| … | … | |
| 690 | 700 | def hexedit(x): |
|---|
| 691 | 701 | x = str(x) |
|---|
| 692 | | f = os.tempnam("scapy") |
|---|
| | 702 | f = get_temp_file() |
|---|
| 693 | 703 | open(f,"w").write(x) |
|---|
| 694 | 704 | os.spawnlp(os.P_WAIT, conf.prog.hexedit, conf.prog.hexedit, f) |
|---|